function nowtime()
{
	now = new Date().getTime();
	return now;
}
function shopcartInit() {
	// dane z koszyka
	if ($('#shopcart_mini').length == 1) {
		loadXML('shop.php?'+nowtime(), 'shopRefresh', 'shopcart_mini');
		var wys = $('#body_contenter').height();
		$("#rightColumn").height(wys);
		$("div.shopcart_nonindex").css('margin-top', '10px');
		$("div.shopcart_nonindex").stickyfloat( {
			duration : 400
		})
	}
	// dodawanie i odejmowanie liczby sztuk
	$("img[id^=plus_]").click(function() {
		var plus = $(this).attr('id').substring(5);
		$("#il_" + plus).val(parseInt($("#il_" + plus).val()) + 1);
	});
	$("img[id^=minus_]").click(function() {
		var minus = $(this).attr('id').substring(6);
		var newVar = parseInt($("#il_" + minus).val());
		if (newVar > 1) {
			$("#il_" + minus).val((newVar - 1));
		}
	});
	$('img[id^="buy"]').click(function() {
		var prod_id = $(this).attr('id').substring(4);
		var amount = $('#il_' + prod_id).val();
		var responseUrl = 'shop.php?prod=' + prod_id + '&amount=' + amount;
		loadXML(responseUrl, 'shopRefresh', 'shopcart_mini');
	});
	$('#clear_shopcart').click(function() {
		var responseUrl = 'shop.php?reset';
		loadXML(responseUrl, 'shopRefresh', 'shopcart_mini');
	});
	$('img[id^="rem"]').live('click', function() {
		var prod_id = $(this).attr('id').substring(4);
		var responseUrl = 'shop.php?remove&id=' + prod_id;
		loadXML(responseUrl, 'shopRefresh', 'shopcart_mini');
	});
	$('img[id^="dec"]').live('click', function() {
		var prod_id = $(this).attr('id').substring(4);
		var responseUrl = 'shop.php?dec&id=' + prod_id;
		loadXML(responseUrl, 'shopRefresh', 'shopcart_mini');
	});
	$('img[id^="inc"]').live('click', function() {
		var prod_id = $(this).attr('id').substring(4);
		var responseUrl = 'shop.php?inc&id=' + prod_id;
		loadXML(responseUrl, 'shopRefresh', 'shopcart_mini');
	});

	// czas dostawy
	$('div.delivery_button').click(function() {
		var hours;
		$('#choosed_hour').removeAttr('id');
		if ($('#active_delivery_button').length > 0) {
			$('#active_delivery_button').removeAttr('id');
		}
		$(this).attr('id', 'active_delivery_button');
		checkDeliveryTime();
		hours = $(this).attr('title').substring(6);
		// wybieranie godziny sugerowanej
			$('div.suggested_hours').css('display', 'none');
			$('#h_' + hours).css('display', 'block');
		});
	$('div.suggested_hour').click(function() {
		$('#choosed_hour').removeAttr('id');
		$(this).attr('id', 'choosed_hour');
		checkDeliveryTime();
	});

	/* wczytywanie rejestracji, jesli istnieje kontener */
	if ($("#registration_content").length > 0) {
		registration_loadContent();
		/* wysylanie formularza */
		$('#button_next').live('click', function() {
			// wyjatek dla miejsca dostawy
			loadPost('shop_register.php', 'registration_content', 'f_reg');
		});
	}

	
	/* wczytywanie finalizacji zamowienia, jesli istnieje kontener */
	if ($("#finalization_content").length > 0) {
		finalization_loadContent();
		/* wysylanie formularza */
		$('#button_next').live('click', function() {
			if($('#deliveryTime').length && $('#deliveryTime').val() == '')
			{
				jAlert('musisz wybrać godzinę dostawy', 'Delikatesy Free')
				return false;
			}
			loadPost('shop_finalize.php', 'finalization_content', 'f_reg');
		});
	}
	// uzupelnienie danych do dostawy
	$('select[name=delivery_address]').livequery(
			'change',
			function() {
				var newVal = $(this).val();
				if(newVal == "")
				{
					$("#f_reg input[type=text]").val('');
					$(this).val('');
					return false;
				}
				loadXML('shop_finalize.php?deliveryPlace=' + $(this).val(),
						'loadDeliveryAddress', 'forms_content');
			});

	// ukrywanie i pokazywanie elementow za pomoca checkboxa
	$('input[id^=hidc_]').live('click', function() {
		var target = $(this).attr('id').substring(5);
		if ($(this).is(":checked")) {
			$('.' + target).each(function() {
				$(this).css('display', 'table-row');
			});
		} else {
			$('.' + target).each(function() {
				$(this).css('display', 'none');
			});
		}
	});
}
function shopRefresh(xml) {
	$("#total_price").text($("#total_price", xml).text());
	$("#total_products").text($("#total_products", xml).text());
	$("#left_to_minimal_price").text($("#left_to_minimal_price", xml).text());
	if(parseFloat($("#left_to_minimal_price", xml).text()) > 0)
	{
		$("#left_to").css('display', 'block');
	}
	else
	{
		$("#left_to").css('display', 'none');
	}
	if ($('#shopcart_mini_details').length != 0) {
		// update listy produktow z koszyka
		$('#shopcart_mini_details_list').text('');
		$(xml)
				.find('product')
				.each(function() {
					// alert($(this).text());

						// tworzenie nowego elementu listy
						var newListItem = '<li>';
						newListItem += '<div><span class="px_125">' + $(this)
								.text() + '</span>';
						newListItem += '<span class="to_r"><img src="images.php?picture=' + $(
								this).attr('imgsrc') + '&width=40&height=45&normal" /></span></div>';
						newListItem += '<span style="margin: 4px" class="full_w"><img class="imgl" id="rem_'
								+ $(this).attr('product_id')
								+ '" src="images/delete.png" alt="usun" />&nbsp;&nbsp;Ilość: '
								+ $(this).attr('amount')
								+ ' szt&nbsp;&nbsp;&nbsp;<img id="dec_'
								+ $(this).attr('product_id')
								+ '" src="images/minus.png" class="imgl" alt="minus" />&nbsp;&nbsp;<img id="inc_'
								+ $(this).attr('product_id')
								+ '" src="images/plus.png" class="imgl" alt="plus" />&nbsp;&nbsp;&nbsp;Cena:&nbsp;'
								+ $(this).attr('price') + '&nbsp;zł</span>';
						newListItem += '</li>';
						$('#shopcart_mini_details_list').append(newListItem);
					});
	}
	if ($('#message', xml).length != 0) {
		jAlert($('#message', xml).text(), 'Delikatesy Free');
	}
}
function parseXml(xml) {
	if (jQuery.browser.msie) {
		var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.loadXML(xml);
		xml = xmlDoc;
	}
	return xml;
}
function loadXML(url, callback, obj) {
	if (obj != undefined) {
		$("#" + obj).mask("");
	}
	var response;
	var content = $.ajax( {
		url : url,
		type : "GET",
		dataType : ($.browser.msie) ? "text" : "xml",
		error : function() {
			// alert('blad');
		$("#" + obj).unmask();
	},
	success : function(response) {
		if (obj != undefined) {
			$("#" + obj).unmask("loading...");
		}
		response = parseXml(response);
		eval(callback + "(response)");
	}
	});
	// alert (content);
}
function loadGET(url, obj) {
	$("#" + obj).mask("loading...");
	var responseText;
	responseText = $.ajax( {
		url : url,
		type : "GET",
		dataType : "html",
		cache : false,
		error : function() {
			$("#" + obj).text('wystapil blad');
			$("#" + obj).unmask();
		},
		success : function(html) {
			$("#" + obj).html(html);
			$("#" + obj).unmask();
		}
	});
}
function loadPost(url, obj, frm) {
	var postData = $('#' + frm).serialize();
	$("#" + obj).mask("loading...");
	var responseText;
	responseText = $.ajax( {
		url : url,
		type : "POST",
		cache : false,
		dataType : "html",
		data : postData,
		error : function() {
			$("#" + obj).text('wystapil blad');
			alert('error');
			$("#" + obj).unmask();
		},
		success : function(html) {
			$("#" + obj).html(html);
			$("#" + obj).unmask();
		}
	});
}
function readMessages() {
	var msgs = $('div.alert_msg');
	if (msgs.length > 0) {
		var msgText = '';
		msgs.each(function() {
			msgText += msgs.text();
		});
		if (msgText != '')
			jAlert(msgText, 'Delikatesy Free');
	}
}
function saveShopcartList() {
	jPrompt('Zapisz listę', '', 'Nowa lista zakupowa', function(r) {
		if (r)
			location.href = 'shopcart_details.html&savelist=' + r;
	});
}
function loadDeliveryAddress(xml) {
	$(xml).find('delivery field').each(
			function() {
				if($(this).attr('textarea') == 'yes')
				{
				$('textarea[name=' + $(this).attr('name') + ']').text(
						$(this).attr('value'));
				}
				else
				{
					$('input[name=' + $(this).attr('name') + ']').val(
							$(this).attr('value'));
				}
			});
	var deliveryTime = $(xml).find('delivery #deliveryTime').text();
	$('input[name=deliveryTime]').val(deliveryTime);
}
// zmiana ceny w detalach koszyka
function changeAmountUrl(id, option) {
	var reqUrl;
	var opt = 'inc';
	switch (option) {
	case 'inc':
		opt = 'inc';
		break;
	case 'dec':
		opt = 'dec';
		break;
	default:
		break;
	}
	reqUrl = 'shop.php?'+opt+'&id='+id;
	return(reqUrl);
}
function changeAmountEffect(xml)
{
var prodParams = $('#editedprod', xml);
var pid = prodParams.attr('pid');
var left_to = parseFloat($('#left_to_minimal_price', xml).text());
$('#total_overall').text($("#total_price", xml).text());
$('#full_price_'+pid).text(prodParams.attr('fullprice'));
$('#amount_'+pid).text(prodParams.attr('amount'));
if(parseInt(left_to) > 0)
	{
		$('#left_to').text('do złożenia dostawy brakuje: '+left_to+' zł').css('display', 'inline-block');
		$('#finalize_button').css('display', 'none');
	}
else
	{
		$('#left_to').css('display', 'none');
		$('#finalize_button').css('display', 'inline-block');
	}
if ($('#message', xml).length != 0) {
	jAlert($('#message', xml).text(), 'Delikatesy Free');
}
}
