var listeCookieName = 'myShoppingCookie';
var firstPanier = true; 
var firstListe = true;

function supprimerDuPanier ( $asin ) 
{
	pageTracker._trackEvent('panier', 'supprimer', ''+$asin+'');
	jQuery.post("/ajax.php", { action: 'supprimer_panier', ASIN: $asin }, function ($data) { retourPanier($data, $asin) } );
}

function ajouterAuPanier ( $asin, $title )
{
	pageTracker._trackEvent('panier', 'ajouter', ''+$asin+'',''+$title+'');
	jQuery.post("/ajax.php", { action: 'ajouter_panier', ASIN: $asin }, function ($data) { retourPanier($data, $asin) } );
} 

function retourPanier ( data, $uid ) 
{
	jQuery("#monPanier > tbody > tr").remove();
	jQuery("#monPanier > tbody").append(data);
	
	if ( firstPanier != true ) {
		jQuery(".ajouterPanier-"+$uid).css('background-position','0 -102px');
	}
	firstPanier = false;
}

function retourCommanderPanier (data ) 
{
	window.location.replace(data);
	
}

function commanderPanier ( ) 
{
	pageTracker._trackEvent( 'panier', 'commander' );
	jQuery.post("/ajax.php", { action: 'commander_panier'}, retourCommanderPanier );
}

function rafraichirLePanier ( ) 
{
	jQuery.post("/ajax.php", { action: 'rafraichir_panier'}, retourPanier );
}

function ajouterALaListe ( $post_ID, $uid, $title )
{
	var myCart;
	myCart = jQuery.cookie(listeCookieName);
	
	if ( myCart == null ){
		myCart = $post_ID;
	} else {
	 	var cartArray = myCart.split(',');
		var isExist = false;
		
		if ( cartArray[0] == '' ) 
			cartArray = cartArray.splice(0,1);
		
		for ( var i = 0 ; i < cartArray.length ; i ++ ) 
		{
	
			if ( cartArray[i] == $post_ID ) 
				isExist=true;
		}
		
		if ( isExist == false ) 
			myCart = myCart+','+$post_ID;	
		
	}
	pageTracker._trackEvent('liste', 'ajouter', ''+$uid+'',''+$title+'');
	jQuery.cookie( listeCookieName, myCart, { expires: 25, path: '/', domain: 'www.le-pere-noel.fr' });
	 
	rafraichirLaListe($uid);
}
 
function supprimerDeLaListe ( $post_ID )
{
	var myCart;
	myCart = jQuery.cookie(listeCookieName);
	
	var cartArray = myCart.split(',');
	if ( cartArray[0] == '' ) cartArray.splice(0,1);
	
	for ( var i = 0 ; i < cartArray.length ; i ++ ) 
	{
		if ( cartArray[i] == $post_ID ) 
			cartArray.splice(i,1);
	}
	pageTracker._trackEvent('liste', 'supprimer' );
	jQuery.cookie(listeCookieName,cartArray.join(','), { expires: 25, path: '/', domain: 'www.le-pere-noel.fr' });
	 
	rafraichirLaListe();
}
 
function rafraichirLaListe($uid ) 
{
	var cart = jQuery.cookie(listeCookieName);
	jQuery.post("/ajax.php", { action: 'get_cart', ids: cart }, function (data) { afficherMaListe(data,$uid) } );
	//jQuery.cookie(listeCookieName,'', { expires: 25, path: '/', domain: 'www.le-pere-noel.fr' });
}
 
function afficherMaListe ( data, $uid )
{

	jQuery("#maListe > tbody > tr").remove();
	jQuery("#maListe > tbody").append(data);

	if ( firstListe != true && $uid != null ) {
		jQuery(".ajouterListe-"+$uid).css('background-position','0 -102px');
	}
	firstListe = false;
}

function recupererPrix ( $asin ) 
{
	jQuery.post("/ajax.php", { action: 'recuperer_prix', ASIN: $asin}, function (data) { afficherPrix(data,$asin) } );
}

function afficherPrix ( data , $asin ) 
{
	jQuery('#prix-'+$asin).html(data);
}
 
jQuery(document).ready(
	
	function()
	{
		rafraichirLaListe();
		rafraichirLePanier();
		jQuery("a[rel^='prettyPopin']:eq(0)").prettyPopin({height: 420, width: 620,followScroll:false});
	}
);

