$(function(){
	
	// si le panier est vide on le masque
	if(document.getElementById('paniervide')){
		$('#panier').hide();
		$('#paniercontent').hide();
	}
	// sinon on le montre
	else{
		$('#panier').show();
		
		$('#paniercontent').show();	
	}
	
	// pour ouvrir fermer le panier
	var $paniertoggle = $('a.paniertoggle');
	$paniertoggle.toggle(
		function(){
			$('#paniercontent').slideUp('fast');
		//	$('#totalpanier').hide();
		},
		function(){
			$('#paniercontent').slideDown('fast',function(){
		//		$('#totalpanier').fadeIn('fast');			
			});			
		});
		


	$('a.paniercommander').click(function(){
		$('#panier').hide();

	});	


	// pour ajouter un produit au panier
	$('.produits a').click(function(){	
		
		// on bloc l'ajout au panier tant que l'animation n'est pas finie
		if ($(".produits img").is(":animated")) { 
            return; 
        }
		
		var productIDValSplitter 	= (this.id).split("_");
		var productIDVal 			= productIDValSplitter[1];
		var productAttrVal = $("select#"+productIDVal+" option:selected").val();  
			
		$('#panier').show();
		$('#totalpanier').fadeIn('slow');
		
		posleft = $('#panier').offset().left;
		postop = $('#panier').offset().top;	
		posprodleft = $(this).parent('.produits').offset().left;
		posprodtop = $(this).parent('.produits').offset().top;
		var dirx = posleft - posprodleft;
		var diry = postop - posprodtop;
		
		$('#paniercontent').slideDown('fast',function(){
			
			$("#productImageWrapID_" + productIDVal + " img")
			.clone()
			.prependTo("#productImageWrapID_" + productIDVal)
			.css({'position' : 'absolute'})
			.css({'z-index' : '1'})
			.css({'border':'1px solid #ccc'})
			.animate({left:dirx ,top:diry,opacity: 0.1,width: 50}, 1000, function() {
			
				$(this).remove();
				$("#notificationsLoader").html('<img src="http://www.cmonsite.fr/images/new/loader.gif">');
			
				$.ajax({  
					type: "POST",  
					url: "design/fonctions_panier.php",  
					data: {productID: productIDVal, productAttr: productAttrVal, action: "addToBasket"},  
					success: function(theResponse) {
					
							$("#totalpanier").remove();
							
						if( $("tr#produit_" + productIDVal +"_"+productAttrVal).length > 0){
							$("#totalpanier").remove();
							$("tr#produit_" + productIDVal +"_"+productAttrVal).animate({ opacity: 0 }, 100);
							
							$("tr#produit_" + productIDVal +"_"+productAttrVal).before(theResponse).remove();
							$("tr#produit_" + productIDVal +"_"+productAttrVal).animate({ opacity: 0 }, 100);
							$("tr#produit_" + productIDVal +"_"+productAttrVal).animate({ opacity: 1 }, 100);
							$("#notificationsLoader").empty();
						}
						
						else{
							$("#totalpanier").hide();
							$('#paniervide').remove();
							$("table#paniertable tr:last").after(theResponse);
							$("tr#produit_" + productIDVal +"_"+productAttrVal).animate({ opacity: 0 }, 100);
							$("tr#produit_" + productIDVal +"_"+productAttrVal).animate({ opacity: 1 }, 100);
							$("#notificationsLoader").empty();
						}
					
					
							
						
						
					}  
				});  
					
			});

		});

	});
	
	
	// SUPPRESSION DANS LE PANIER FLOTTANT
	$("td.suppr span.panier").live("click", function(event) { 
		var productIDValSplitter 	= (this.id).split("_");
		var productIDVal 			= productIDValSplitter[1];	
		var productAttrVal 			= productIDValSplitter[2];	
		
		$("#notificationsLoader").html('<img src="http://www.cmonsite.fr/images/new/loader.gif">');
		
		$.ajax({  
			type: "POST",  
			url: "design/fonctions_panier.php", 
			data: { productID: productIDVal, productAttr: productAttrVal, action: "deleteFromBasket"},  
			success: function(theResponse) {
					
					
					
					$("#totalpanier").before(theResponse).remove();
			
									
					$("tr#produit_" + productIDVal +"_"+productAttrVal).hide("fast",  function() {$(this).remove();});
					$("#notificationsLoader").empty();
					
					
					
		
				
				}
		}); 
	
	});
	
	
	
	
	
	
	
	
	// SUPPRESSION DANS LE PANIER FINAL 
	$("td.suppr span.final").live("click", function(event) { 
		var productIDValSplitter 	= (this.id).split("_");
		var productIDVal 			= productIDValSplitter[1];	
		var productAttrVal 			= productIDValSplitter[2];	
		
		$("#notificationsLoader").html('<img src="http://www.cmonsite.fr/images/new/loader.gif">');
		
		$.ajax({  
			type: "POST",  
			url: "design/fonctions_panier.php", 
			data: { productID: productIDVal, productAttr: productAttrVal, action: "deleteFromBasketFinal"},  
			success: function(theResponse) {
					
					$("#totalbloc").before(theResponse).remove();
					
									
					$("tr#produit_" + productIDVal +"_"+productAttrVal).hide("fast",  function() {$(this).remove();});
					$("#notificationsLoader").empty();
					
					//$('#panier', window.parent.document).load('panier.php');
				
					
					
					
								
				}
		}); 
	
	});
	
	$("span.downqte").live("click", function(event) { 
		var productIDValSplitter 	= (this.id).split("_");
		var productIDVal 			= productIDValSplitter[1];
		var productAttrVal 			= productIDValSplitter[2];	
		
		$("#notificationsLoader").html('<img src="http://www.cmonsite.fr/images/new/loader.gif">');
		
		$.ajax({  
			type: "POST",  
			url: "design/fonctions_panier.php", 
			data: { productID: productIDVal, productAttr: productAttrVal, action: "addToBasket2"},  
			success: function(theResponse) {
					
					$("#totalpanier").remove();
							$("tr#produit_" + productIDVal +"_"+productAttrVal).animate({ opacity: 0 }, 100);
							
							$("tr#produit_" + productIDVal +"_"+productAttrVal).before(theResponse).remove();
							$("tr#produit_" + productIDVal +"_"+productAttrVal).animate({ opacity: 0 }, 100);
							$("tr#produit_" + productIDVal +"_"+productAttrVal).animate({ opacity: 1 }, 100);
					$("#notificationsLoader").empty();
				
				}
		}); 
	
	});
	
	$("span.upqte").live("click", function(event) { 
		var productIDValSplitter 	= (this.id).split("_");
		var productIDVal 			= productIDValSplitter[1];
		var productAttrVal 			= productIDValSplitter[2];	
$("#notificationsLoader").html('<img src="http://www.cmonsite.fr/images/new/loader.gif">');		
	
		$.ajax({  
			type: "POST",  
			url: "design/fonctions_panier.php", 
			data: { productID: productIDVal, productAttr: productAttrVal, action: "addToBasket"},  
			success: function(theResponse) {
					
					$("#totalpanier").remove();
							$("tr#produit_" + productIDVal +"_"+productAttrVal).animate({ opacity: 0 }, 100);
							
							$("tr#produit_" + productIDVal +"_"+productAttrVal).before(theResponse).remove();
							$("tr#produit_" + productIDVal +"_"+productAttrVal).animate({ opacity: 0 }, 100);
							$("tr#produit_" + productIDVal +"_"+productAttrVal).animate({ opacity: 1 }, 100);
					$("#notificationsLoader").empty();
						
					
					
				
				}
		}); 
	
	});
	


	
});


