$(document).ready(function(){ 

	$("#basketItemsWrap li:first").hide();
	
	$("li .add2cart").click(function() {
		var productIDValSplitter 	= (this.id).split("_");
		var productIDVal 			= productIDValSplitter[1];
	
		$.ajax({  
			type: "POST",  
			url: "/jbasket/functions/",  
			data: { productID: productIDVal, action: "addToBasket"},  
			success: function(theResponse) {
				/*
				if( $("#productID_" + productIDVal).length > 0){
					$("#productID_" + productIDVal).animate({ opacity: 0 }, 500, function() {
						$("#productID_" + productIDVal).before(theResponse).remove();
					});				
					$("#productID_" + productIDVal).animate({ opacity: 0 }, 500);
					$("#productID_" + productIDVal).animate({ opacity: 1 }, 500);
					$("#notificationsLoader").empty();			
				} else {
					$("#basketItemsWrap li:first").before(theResponse);
					$("#basketItemsWrap li:first").hide();
					$("#basketItemsWrap li:first").show("slow");  
					$("#notificationsLoader").empty();			
				}

				$.ajax({
					type: "POST",
					url: "/jbasket/functions/",
					data: {productID: productIDVal, action: "totalprice"},
					success: function(theResponse1) {
							//$("#totalprice").html(theResponse1 + "Р");
					}
				});

				$.ajax({
					type: "POST",
					url: "/jbasket/functions/",
					data: {productID: productIDVal, action: "deliverycost"},
					success: function(theResponse2) {
							//$("#deliverycost").html(theResponse2);
					}
				});
				*/
				
				$.ajax({
					type: "POST",
					url: "/jbasket/functions/",
					data: {productID: productIDVal, action: "getbasket", filter: rest_filter},
					success: function(theResponse3) {
							$("#your-order").animate({ opacity: "hide" }, 10);
							$("#your-order").empty();
							$("#your-order").html(theResponse3);
							$("#your-order").animate({ opacity: "show" }, "slow");
					}
				});
			}
		});
	});	
	
	$("li .add2cart_supp").click(function() {
		var productIDValSplitter 	= (this.id).split("_");
		var productIDVal 			= productIDValSplitter[1];
		var forIDVal 				= productIDValSplitter[2];
	
		$.ajax({  
			type: "POST",  
			url: "/jbasket/functions/",  
			data: { productID: productIDVal, action: "addToBasketSupp", forID: forIDVal},  
			success: function(theResponse) {
				/*
				if( $("#productID_" + productIDVal).length > 0){
					$("#productID_" + productIDVal).animate({ opacity: 0 }, 500, function() {
						$("#productID_" + productIDVal).before(theResponse).remove();
					});				
					$("#productID_" + productIDVal).animate({ opacity: 0 }, 500);
					$("#productID_" + productIDVal).animate({ opacity: 1 }, 500);
					$("#notificationsLoader").empty();			
				} else {
					$("#basketItemsWrap li:first").before(theResponse);
					$("#basketItemsWrap li:first").hide();
					$("#basketItemsWrap li:first").show("slow");  
					$("#notificationsLoader").empty();			
				}
				
				$.ajax({
					type: "POST",
					url: "/jbasket/functions/",
					data: {productID: productIDVal, action: "totalprice"},
					success: function(theResponse1) {
							//$("#totalprice").html(theResponse1);
					}
				});

				$.ajax({
					type: "POST",
					url: "/jbasket/functions/",
					data: {productID: productIDVal, action: "deliverycost"},
					success: function(theResponse2) {
							//$("#deliverycost").html(theResponse2);
					}
				});
				*/
				
				$.ajax({
					type: "POST",
					url: "/jbasket/functions/",
					data: {productID: productIDVal, action: "getbasket", filter: rest_filter},
					success: function(theResponse3) {
							$("#your-order").animate({ opacity: "hide" }, 10);
							$("#your-order").empty();
							$("#your-order").html(theResponse3);
							$("#your-order").animate({ opacity: "show" }, "slow");
					}
				});
			}  
		});
	});	
	
	$("#basketItemsWrap li img").live("click", function(event) { 
														
		var productIDValSplitter 	= (this.id).split("_");
		var productIDVal 			= productIDValSplitter[1];	
	
		/*$("#notificationsLoader").html('<img src="/i/loader.gif">');*/
	
		$.ajax({  
			type: "POST",  
			url: "/jbasket/functions/",  
			data: { productID: productIDVal, action: "deleteFromBasket"},  
			success: function(theResponse) {
				/*
				//$("#productID_" + productIDVal).hide("slow",  function() {$(this).remove();});
				$("#notificationsLoader").empty();
			
				$.ajax({
					type: "POST",
					url: "/jbasket/functions/",
					data: {productID: productIDVal, action: "totalprice"},
					success: function(theResponse1) {
							//$("#totalprice").html(theResponse1 + "Р");
					}
				});

				$.ajax({
					type: "POST",
					url: "/jbasket/functions/",
					data: {productID: productIDVal, action: "deliverycost"},
					success: function(theResponse2) {
							//$("#deliverycost").html(theResponse2);
					}
				});
				*/
				
				$.ajax({
					type: "POST",
					url: "/jbasket/functions/",
					data: {productID: productIDVal, action: "getbasket", filter: rest_filter},
					success: function(theResponse3) {
							$("#your-order").animate({ opacity: "hide" }, 10);
							$("#your-order").empty();
							$("#your-order").html(theResponse3);
							$("#your-order").animate({ opacity: "show" }, "slow");
					}
				});
			}  
		});  
	});
	/*
	$(".pane .delete").live("click", function(event) { 
														
		var productIDValSplitter 	= (this.id).split("_");
		var productIDVal 			= productIDValSplitter[1];	
	
		$("#notificationsLoader").html('<img src="/i/loader.gif">');
		$(this).parents(".pane").animate({ opacity: "hide" }, "slow");
	
		$.ajax({  
			type: "POST",  
			url: "/jbasket/functions/",  
			data: { productID: productIDVal, action: "deleteFromBasket"},  
			success: function(theResponse) {
				$("#notificationsLoader").empty();

				if ($('.orderitem:visible').length > 2)
				{
					$.ajax({
					type: "POST",
					url: "/jbasket/functions/",
					data: {productID: productIDVal, action: "deliverycost"},
					success: function(theResponse1) {
						$("#deliverycost").html(theResponse1);

						$.ajax({
							type: "POST",
							url: "/jbasket/functions/",
							data: {productID: productIDVal, action: "totalprice"},
							success: function(theResponse2) {
									$("#totalprice").html("Итого: " + (parseInt(theResponse1) + parseInt(theResponse2)) + "<span class=\"rursign\">Р</span>");
							}
						});
					}
					});                        
				}
				else
				{
					$('#delivery').html('Ваша корзина пуста. <a href="/" style="color:#000;">Заказать еду >></a>');
					$('#price_all,.price_all,#orderform,.centered').animate({ opacity: "hide" }, "slow");
					$("#totalprice").html("");
				}
			}  
		});		
	});
	*/
	$("a.delete").live("click", function(event) { 
														
		var productIDValSplitter 	= (this.id).split("_");
		var productIDVal 			= productIDValSplitter[1];	
	
		$("#notificationsLoader").html('<img src="/i/loader.gif">');
		$("#productID_"+productIDVal).animate({ opacity: "hide" }, "slow");
	
		$.ajax({  
			type: "POST",  
			url: "/jbasket/functions/",  
			data: { productID: productIDVal, action: "deleteFromBasket"},  
			success: function(theResponse) {
				$("#notificationsLoader").empty();

				if ($('.orderitem:visible').length > 2)
				{
					$.ajax({
					type: "POST",
					url: "/jbasket/functions/",
					data: {productID: productIDVal, action: "deliverycost"},
					success: function(theResponse1) {
						$("#deliverycost").html(theResponse1);

						$.ajax({
							type: "POST",
							url: "/jbasket/functions/",
							data: {productID: productIDVal, action: "totalprice"},
							success: function(theResponse2) {
									$("#totalprice").html("Итого: " + (parseInt(theResponse1) + parseInt(theResponse2)) + "<span class=\"rursign\">Р</span>");
							}
						});
					}
					});                        
				}
				else
				{
					$('#delivery').html('Ваша корзина пуста. <a href="/" style="color:#000;">Заказать еду >></a>');
					$('#price_all,.price_all,#orderform,.centered').animate({ opacity: "hide" }, "slow");
					$("#totalprice").html("");
				}
			}  
		});		
	});
});

