$(document).ready(function(){ 

	$("#basketItemsWrap li:first").hide();
	
	$(".productPriceWrapRight a img").click(function() {
		var productIDValSplitter 	= (this.id).split("_");
		var productIDVal 			= productIDValSplitter[1];
	
		$("#notificationsLoader").html('<img src="images/loader.gif">');
	
		$.ajax({  
		type: "POST",  
		url: "inc/functions.php",  
		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();	
				
				$("#cartTotal").hide();
				
				$.ajax({
				type: "post",
				url: "inc/functions.php",  
				data: { productID: productIDVal, action: "getBasketTotal"}, 
				// beforeSend: function(){$("#loading").show("fast");}, //show loading just when link is clicked
				// complete: function(){ $("#loading").hide("fast");}, //stop showing loading when the process is complete
				success: function(html){ //so, if data is retrieved, store it in html
				$("#cartTotal").html(html); //show the html inside .content div
				
				$("#cartTotal").show("slow"); //animation
	 			}
	 			}); //close $.ajax(
	 
				
				// $("#cartTotal").html('All new content');
				
				// $("#cartTotal").show("slow"); 
			} else {
				$("#basketItemsWrap li:first").before(theResponse);
				$("#basketItemsWrap li:first").hide();
				$("#basketItemsWrap li:first").show("slow");  
				$("#notificationsLoader").empty();	
				
				$("#cartTotal").hide();
				
				$.ajax({
				type: "post",
				url: "inc/functions.php",  
				data: { productID: productIDVal, action: "getBasketTotal"}, 
				// beforeSend: function(){$("#loading").show("fast");}, //show loading just when link is clicked
				// complete: function(){ $("#loading").hide("fast");}, //stop showing loading when the process is complete
				success: function(html){ //so, if data is retrieved, store it in html
				$("#cartTotal").html(html); //show the html inside .content div
				
				$("#cartTotal").show("slow"); //animation
				}
	 			}); //close $.ajax(
			}
			
		}  
		});  
		
	});
	
	
	
	$("#basketItemsWrap li img").live("click", function(event) { 
														
		var productIDValSplitter 	= (this.id).split("_");
		var productIDVal 			= productIDValSplitter[1];	
	
		$("#notificationsLoader").html('<img src="images/loader.gif">');
	
		$.ajax({  
		type: "POST",  
		url: "inc/functions.php",  
		data: { productID: productIDVal, action: "deleteFromBasket"},  
		success: function(theResponse) {
			
			$("#productID_" + productIDVal).hide("slow",  function() {$(this).remove();});
			$("#notificationsLoader").empty();
		
		}  
		});  
		
	});
	
	
	
	
	//select all the a tag with name equal to modal
	$('a[name=modal]').click(function(e) {
		//Cancel the link behavior
		e.preventDefault();
		
		//Get the A tag
		var id = $(this).attr('href');
	
		//Get the screen height and width
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
	
		//Set heigth and width to mask to fill up the whole screen
		$('#mask').css({'width':maskWidth,'height':maskHeight});
		
		//transition effect		
		$('#mask').fadeIn(1000);	
		$('#mask').fadeTo("slow",0.8);	
	
		//Get the window height and width
		var winH = $(window).height();
		var winW = $(window).width();
              
		//Set the popup window to center
		$(id).css('top',  winH/2-$(id).height()/2);
		$(id).css('left', winW/2-$(id).width()/2);
	
		//transition effect
		$(id).fadeIn(2000); 
	
	});
	
	//if close button is clicked
	$('.window .close').click(function (e) {
		//Cancel the link behavior
		e.preventDefault();
		
		$('#mask').hide();
		$('.window').hide();
	});		
	
	//if mask is clicked
	$('#mask').click(function () {
		$(this).hide();
		$('.window').hide();
	});		

});

