$(document).ready(function(){ 

	$("#basketItemsWrap tr:first").hide();
	
	$(".productPriceWrapRight form input.add").click(function() {
		var productIDValSplitter 	= (this.id).split("_");
		var productIDVal 			= productIDValSplitter[1];
		var aantall 				= $("#aantal" + productIDVal).val();
		
		$("#basketleeg").css("display", "none");
	
		$("#notificationsLoader").html('<img src="img/basket/loader.gif">');
	
		$.ajax({  
			type: "POST",  
			url: "class/Basket.php",  
			data: { productID: productIDVal, action: "addToBasket", aantal: aantall},  
			success: function(theResponse) {
				
				if( $("#productID_" + productIDVal).length > 0){
					$("#productID_" + productIDVal).animate({ opacity: 0 }, 250, function() {
						$("#productID_" + productIDVal).hide("normal",  function() {$(this).remove();});
						$("#lijn_" + productIDVal).hide("normal",  function() {$(this).remove();});
						$("#lijn_" + productIDVal).remove();
						$("#productID_" + productIDVal).before(theResponse).remove();
						$("#productID_" + productIDVal).css("display", "block");
					});				
					$("#productID_" + productIDVal).animate({ opacity: 0 }, 250);
					$("#productID_" + productIDVal).animate({ opacity: 1 }, 250);
					$("#notificationsLoader").empty();			
				} else {
					$("#basketItemsWrap").css("display", "block");
					$("#basketItemsWrap tr:first").before(theResponse);
					$("#basketItemsWrap tr:first").css("display", "block");
					$("#basketItemsWrap tr:first").show("normal");
					$("#notificationsLoader").empty();			
				}
				$.berekenTotaal();
			}  
		});
		
	});
	
	
	
	$("#basketItemsWrap td img").live("click", function(event) { 
														
		var productIDValSplitter 	= (this.id).split("_");
		var productIDVal 			= productIDValSplitter[1];	
	
		$("#notificationsLoader").html('<img src="img/basket/loader.gif">');
	
		$.ajax({  
			type: "POST",  
			url: "class/Basket.php",  
			data: { productID: productIDVal, action: "deleteFromBasket"},  
			success: function(theResponse) {
				
				$("#productID_" + productIDVal).hide("normal",  function() {$(this).remove();});
				$("#lijn_" + productIDVal).hide("normal",  function() {$(this).remove();});
				$("#notificationsLoader").empty();
				$.berekenTotaal();
			}  
		});
		
		
	});
	
	$.extend({
		berekenTotaal : function(){
			$.ajax({  
				type: "POST",  
				url: "class/Basket.php",  
				data: {action: "berekenTotaal"},  
				success: function(theResponse) {
					$("#shoplist-totaal-tr").hide("normal",  function() {$(this).remove();});
					$("#shoplist-totaal-tr").before(theResponse).remove();
					$("#shoplist-totaal-tr").animate({ opacity: 0 }, 250);
					$("#shoplist-totaal-tr").animate({ opacity: 1 }, 250);
				}  
			});
		  }
		});

});

