function showPopupCart(pId) {
	showPopupCart(pId, '');
}

function showPopupCart(pId, siteName) {
	var quantity = checkQuantity($F('prod'+pId));
	if (quantity > 0) {
		showPopup();
		var dest = 'addProductToCart.jspr?id_product=' + pId + '&quantity=' + quantity + '&siteName=' + siteName +'&state=popup';
		new Ajax.Updater('popupContent', dest);
	}
}

function checkQuantity(quantity) {
	if (isNaN(quantity)){
        alert("Entrer un nombre entier s'il vous plait.");
        return 0;
     }else{
        //then it must be an integer
        quantity = parseInt(quantity);
        if (quantity<1) {
        	alert("La quantité doit être supérieure à 0");
        	return 0;
        }
     }
     return quantity;
}

//TODO: à supprimer quand tous les liens d'ajout de produits dans le panier seront gérés en ajax 
function interceptProductCartClick(event){
    var elem = Event.element(event);
    if (elem.tagName=="IMG"){
    	elem = elem.parentNode;
    }
    Event.stop(event);
    var href = elem.href;
    var elem_id = elem.id;
    var id = elem_id.substr(1);
    var q_id = 'prod' + id;
    var quantity = $(q_id).value;
    //first check we have a number
    if (isNaN(quantity)){
        alert("Entrer un nombre entier s'il vous plait.");
        $(q_id).value = 1;
     }else{
        //then it must be an integer
        quantity = parseInt(quantity);
        if (quantity<1) quantity = 1;
        href = href.replace(/quantity=\d+/,"quantity="+quantity);
        document.location = href;
        //document.location = "addProductToCart.jspr?id_product="+id+"&quantity="+quantity;
     }   
}
function interceptVariationCartClick(event){
    var elem = Event.element(event);
    if (elem.tagName=="IMG"){
    	elem = elem.parentNode;
    }
    Event.stop(event);
    var href = elem.href;
    var elem_id = elem.id;
    var id = elem_id.substr(1);
    var q_id = 'var' + id;
    var quantity = $(q_id).value;
    //first check we have a number
    if (isNaN(quantity)){
        alert("Entrer un nombre entier s'il vous plait.");
        $(q_id).value = 1;
     }else{
        //then it must be an integer
        quantity = parseInt(quantity);
        if (quantity<1) quantity = 1;
        href = href.replace(/quantity=\d+/,"quantity="+quantity);
        document.location = href;
        //document.location = "addVariationToCart.jspr?id_variation="+id+"&quantity="+quantity;
     }   
}

//TODO: supprimer les 3 premiers quand tous les liens d'ajout de produits dans le panier seront gérés en ajax 
function addObserverToAllCartLink(){  
   $$('.LienAjout').each(function(a){
        Event.observe(a,'click',interceptProductCartClick);
   });
   $$('.Ajout').each(function(a){
        Event.observe(a,'click',interceptProductCartClick);
   });
   $$('.Ajout2').each(function(a){
        Event.observe(a,'click',interceptProductCartClick);
   });
   $$('.variationCartLink').each(function(a){
        Event.observe(a,'click',interceptVariationCartClick);
   });
}
Event.observe(window,'load',addObserverToAllCartLink);

function updateCartInfos(UTDHeaderInfo, UTDBlockInfo) {
	// updates cart section of the header
	var element1 = document.getElementById('infoCartBandeau');
	if (element1 && UTDHeaderInfo) {
		element1.innerHTML = UTDHeaderInfo;
	}
	// updates cart section of the right column
	var element2 = document.getElementById('infoCartBlock');
	if (element2 && UTDBlockInfo) {
		element2.innerHTML = UTDBlockInfo;
	}
	
	var dest = 'addProductToCart.jspr?updateBandeau=true';
	new Ajax.Updater('bandeauCart', dest);
}

/*****************************************************/
/*     	ELEMENTS DES SCROLLERS DE LA POPUP		     */
/*****************************************************/
/* ********** PRODUITS ASSOCIES ********** */
// variables
var scrollerShortcutPopup = null;
var maxClicsLeftShortcutPopup = 0; // nombre de clics sur la flèche de gauche autorisés
var availableClicsLeftShortcutPopup = 0; // nombre du clics sur suivants possibles
var sizeShortcutPopup = 0;
var firstShortcutPopup = true;

function initShortcutPopup() {
	scrollerShortcutPopup = '#scrollerShortcutsPopup';
	maxClicsLeftShortcutPopup = document.getElementById('nbShortcutsPopup').value - 3;
	sizeShortcutPopup = maxClicsLeftShortcutPopup*127;
	if (firstShortcutPopup) {
		firstShortcutPopup = false;
		availableClicsLeftShortcutPopup = maxClicsLeftShortcutPopup;
	}
}

function leftShortcutPopup() {
	initShortcutPopup();
	
	if (availableClicsLeftShortcutPopup > 0) {
		availableClicsLeftShortcutPopup--;
		jQuery(scrollerShortcutPopup).animate({marginLeft:'-=127px'}, 1000);
	} else {
		availableClicsLeftShortcutPopup = maxClicsLeftShortcutPopup;
		jQuery(scrollerShortcutPopup).animate({marginLeft:'0px'}, 1000);
	}
}
 
function rightShortcutPopup() {
	initShortcutPopup();
	
	if (availableClicsLeftShortcutPopup < maxClicsLeftShortcutPopup) {
		availableClicsLeftShortcutPopup++;
		jQuery(scrollerShortcutPopup).animate({marginLeft:'+=127px'}, 1000);
	} else {
		availableClicsLeftShortcutPopup = 0;
		jQuery(scrollerShortcutPopup).animate({marginLeft:-sizeShortcutPopup}, 1000);
	}
}

/* ********** PRODUITS EGALEMENT ACHETES ********** */
var scrollerAssociationPopup = null;
var maxClicsLeftAssocPopup = 0; // nombre de clics sur la flèche de gauche autorisés
var availableClicsLeftAssocPopup = 0; // nombre du clics sur suivants possibles
var sizeAssocPopup = 0;
var firstAssocPopup = true;

function initAssociationPopup() {
	scrollerAssociationPopup = '#scrollerAssociationsPopup';
	maxClicsLeftAssocPopup = document.getElementById('nbAssociationsPopup').value - 3;
	sizeAssocPopup = maxClicsLeftAssocPopup*127;
	if (firstAssocPopup) {
		firstAssocPopup = false;
		availableClicsLeftAssocPopup = maxClicsLeftAssocPopup;
	}
}

function leftAssociationPopup() {
	initAssociationPopup();
	
	if (availableClicsLeftAssocPopup > 0) {
	 	availableClicsLeftAssocPopup--;
	 	jQuery(scrollerAssociationPopup).animate({marginLeft:'-=127px'}, 1000);
	} else {
		availableClicsLeftAssocPopup = maxClicsLeftAssocPopup;
		jQuery(scrollerAssociationPopup).animate({marginLeft:'0px'}, 1000);
	}
}

function rightAssociationPopup() {
	initAssociationPopup();
	
	if (availableClicsLeftAssocPopup < maxClicsLeftAssocPopup) {
		availableClicsLeftAssocPopup++;
		jQuery(scrollerAssociationPopup).animate({marginLeft:'+=127px'}, 1000);
	} else {
		availableClicsLeftAssocPopup = 0;
		jQuery(scrollerAssociationPopup).animate({marginLeft:-sizeAssocPopup}, 1000);
	}
}