$(function() {
	$("#cart tr .remove input").click(function() {
		var orderCode = $(this).val();
		$.ajax({
			type: "GET",
			url: "cart_action.php",
			data: "remove[]=" + orderCode,
			success: function() {
				$("#cart tr .remove input[value=" + orderCode + "]").parent().parent().fadeOut(500, function() {
					$(this).remove();
					calcPrice();
				});
			},
			error: function() {
				window.location("cart_action.php?remove[]="+orderCode);
			}
		});
	});
	
	$("#cart tr .quantity input").change(function() {
		var orderCode = $(this).attr("name").slice(9, -1);
		var quantity = $(this).val();
		$.ajax({
			type: "GET",
			url: "cart_action.php",
			data: "quantity[" + orderCode + "]=" + quantity,
			success: function() {
				var startColor = $("#cart tr .quantity input[name*=" + orderCode + "]").parent().parent().hasClass("odd") ? "#eee" : "#fff";
				$("#cart tr .quantity input[name*=" + orderCode + "]").parent().parent().find("td").animate({ backgroundColor: "#ff8" }, 100).animate({ backgroundColor: startColor }, 800);
				calcPrice();
			},
			error: function() {
				window.location("cart_action.php?quantity[" + orderCode + "]=" + quantity);
			}
		});
	});
});

function calcPrice() {
	var totalPrice = 0;
	$("#cart tr .quantity").parent().each(function() {
		var quantity = $(".quantity input", this).val();
		var unitPrice = $(".unit_price", this).text().slice(1);
		var extendedPrice = quantity*unitPrice;
		totalPrice += extendedPrice;
		
		$(".extended_price", this).html(extendedPrice+" руб.");
		$("#total_price").html(totalPrice+" руб.");
	});
	if ( totalPrice == 0 ) {
		$("#cart").parent().replaceWith("<p class='center'>Ваша корзина пуста.</p>");
	}
}


function showPicDiv(imgPath) {
varPicDivImg = document.getElementById('picDivImg');
varPicDivImg.src = imgPath;
varPicDiv = document.getElementById('picDiv');
varPicDiv.style.display = "block";
}

function hidePicDiv() {
varPicDiv = document.getElementById('picDiv');
varPicDivImg.src = '/images/dot.gif';
varPicDiv.style.display = "none";
}

function updatePos(e) {
var xw1,yw1;
if (self.innerHeight) {
xw1 = self.innerWidth;
yw1 = self.innerHeight;
} else if (document.documentElement && document.documentElement.clientHeight) {
xw1 = document.documentElement.clientWidth;
yw1 = document.documentElement.clientHeight;
} else if (document.body) {
xw1 = document.body.clientWidth;
yw1 = document.body.clientHeight;
} 		
	
varPicDiv = document.getElementById('picDiv');
x1 = (document.all) ? window.event.x1  : e.pageX;
y1 = (document.all) ? window.event.y1  : e.pageY;
/* iw = document.getElementById('picDivImg').width; */
if (varPicDiv != null) {
	if ( x1 < (xw1 - 450) ) {
				 varPicDiv.style.left = (x1 + 20) + "px";
				 varPicDiv.style.top  = (y1 + 10) + "px";
			}
			else { 
/*				 varPicDiv.style.left = (x1 - iw + 1) + "px";
				 varPicDiv.style.top  = (y1 + 10) + "px"; */
				 }
}
}

document.onmousemove = updatePos;


