/* Bloc a remplir */
var currentBlock;
var currentBlockId;

/* Permet de naviguer vers la page de la categorie selectionnee */
function goToCategory(id){
	var form				= document.getElementById("form_category");
	form.categoryId.value	= id;
	form.submit();
}

/* Timer apparition du menu */
var menuTimerShow;

var idReceived;
function askShowMenu(id) {
	idReceived = id;
	abortMenuDisappearing();
	menuTimerShow = setTimeout("showMenu()", 250);
}

/* Affiche le menu des sous-categories de la categorie dont identifiant est donne en parametre */
function showMenu(){
	/*abortMenuDisappearing();*/	
	/* Categorie demandee */ 
	var blockId	= "div_category" + idReceived;
	var block	= dojo.byId(blockId);
	document.getElementById(blockId).style.display	= "block";
	
	if (block != currentBlock) {
		var oldBlock	= document.getElementById(currentBlockId);
		if (oldBlock) {
			oldBlock.style.display	= "none";
		}
		currentBlockId	= blockId;
		currentBlock	= block;
	}
	var form = dojo.byId("subCategoryForm");
	form.categoryId.value = idReceived;
	var bindArgs = {
		    url: "DisplaySubCategories.do",
		    useCache: false,
			preventCache: true,
			cacheContent: false,
			handler: fillMenu,			    
		    formNode: form
		};
	dojo.io.bind(bindArgs);
	document.getElementById("select_category").style.display	= "none";
}


/*

// Affiche le menu des sous-categories de la categorie dont identifiant est donne en parametre
function showMenu(id){
	abortMenuDisappearing();	
	// Categorie demandee 
	var blockId	= "div_category" + id;
	var block	= dojo.byId(blockId);
	document.getElementById(blockId).style.display	= "block";
	
	if (block != currentBlock) {
		var oldBlock	= document.getElementById(currentBlockId);
		if (oldBlock) {
			oldBlock.style.display	= "none";
		}
		currentBlockId	= blockId;
		currentBlock	= block;
	}
	var form = dojo.byId("subCategoryForm");
	form.categoryId.value = id;
	var bindArgs = {
		    url: "DisplaySubCategories.do",
		    useCache: false,
			preventCache: true,
			cacheContent: false,
			handler: fillMenu,			    
		    formNode: form
		};
	dojo.io.bind(bindArgs);
	//document.getElementById("div_subCategories").style.display = 'block';
	document.getElementById("select_category").style.display	= "none";
}
*/
/* Remplit le div du menu avec les donnees issu de la requete */
function fillMenu(type, data, event){
	if (type == "error") {
		/*alert("Erreur de remplissage du menu", data);*/
	} else {
		/*dojo.byId("div_subCategories").innerHTML = data;*/
		currentBlock.innerHTML									= data;
		document.getElementById(currentBlockId).style.display	= "block";
	}
}

/* Cache le menu */
function hideMenu(){
	/*document.getElementById("div_subCategories").style.display = 'none';*/
	try{
		document.getElementById(currentBlockId).style.display	= "none";
	}catch(err){
	}finally{
		document.getElementById("select_category").style.display	= "block";
	}
}

/* Timer de disparition du menu */
var menuTimer;

/* Demande de disparition du menu */
function requestMenuDisappearing() {
	menuTimer = setTimeout("hideMenu()", 1000);
	if (menuTimerShow) { /* stop demande affichage eventuelle */
		clearTimeout(menuTimerShow);
	}
}

/* Annulation du timer de disparition */
function abortMenuDisappearing() {
	if (menuTimer) {
		clearTimeout(menuTimer);
	}
}

/* /////////////////////////
// Gestion des tooltips //
////////////////////////// */
/* Variables necessaires */
var currentTooltipId = '';
var tooltipTimer;
/* Annule la disparition programmee du tooltip */
function abortTooltipDisappearing() {
	if (tooltipTimer) {
		clearTimeout(tooltipTimer);
	}
}
/* Affiche le tooltip avec identifiant tooltipId */
function showTooltip(tooltipId) {
	/* Annulation de l eventuelle disparition programmee */
	abortTooltipDisappearing();
	/* Recuperation du tooltip a afficher */
	var tooltip = document.getElementById(tooltipId);
	/* Affichage du tooltip */
	tooltip.style.display = "block";
	/* Archivage */
	if (tooltipId != currentTooltipId) {
		/* Le tooltip n est pas le dernier affiche */
		var oldTooltip = document.getElementById(currentTooltipId);
		if (oldTooltip) { /* Verification necessaire au premier appel */
			oldTooltip.style.display = "none";
		}
		currentTooltipId = tooltipId;
	}
}
/*Cache le tooltip courant*/
function hideTooltip() {
	document.getElementById(currentTooltipId).style.display = "none";
}
/* Demande la disparition du tooltip courant */
function requestTooltipDisappearing() {
	tooltipTimer = setTimeout("hideTooltip()", 1000);
}


/* Mise a jour zone panier en haut */
function updateCart(){
	var bindArgs = {
		    url: "_cart.jsp",
			useCache:false,
			preventCache: true,
			cacheContent: false,
			handler: fillCart
		};
	dojo.io.bind(bindArgs);
}

/* Remplit le contenu de la zone panier */
function fillCart(type, data, event){
	if(type == "error"){
		alert(data);
	} else {
		dojo.byId("div_cart").innerHTML = data;
	}
}


/*Affiche le resume du panier*/
function showCartSummary(){
	abortCartSummaryDisappearing();
	document.getElementById("div_cartSummaryTooltip").style.display	= "block";
}

/* Cache le resume du panier */
function hideCartSummary(){
	document.getElementById("div_cartSummaryTooltip").style.display	= "none";
}

/* Timer de disparition du resume panier */
var cartSummaryTimer;

/* Demande de disparition du resume panier */
function requestCartSummaryDisappearing() {
	cartSummaryTimer = setTimeout("hideCartSummary()", 1000);
}

/* Annulation du timer de disparition resume panier */
function abortCartSummaryDisappearing() {
	if (cartSummaryTimer) {
		clearTimeout(cartSummaryTimer);
	}
}




/* Aller sur la page d un produit */
function goToProduct(productId) {
	var form = document.getElementById("form_headerProduct");
	form.productId.value = productId;
	form.submit();
}


/*
// Fonctions pour l ajout de produits au panier //
////////////////////////////////////////////////// */
var cartDialog;
var initCartDialogOk = false;

function initCartDialog(e) {
	cartDialog = dojo.widget.byId("div_addToCart");
	document.getElementById("div_addToCart").style.display = "none";
	initCartDialogOk = true;
}

function addToCart(productId) {
	if (initCartDialogOk) {
		document.getElementById("form_headerProduct").productId.value=productId;
		var bindArgs = {
			    url: "AddCartProduct.do",
			    useCache: false,
				preventCache: true,
				cacheContent: false,
			    handler: fillPopup,
				formNode: dojo.byId("form_headerProduct")
			};
		dojo.io.bind(bindArgs);
	}
}

function addPackToCart(productId) {
	if (initCartDialogOk) {
		document.getElementById("form_headerProduct").productId.value=productId;
		var bindArgs = {
			    url: "AddCartPack.do",
			    useCache: false,
				preventCache: true,
				cacheContent: false,
			    handler: fillPopup,
				formNode: dojo.byId("form_headerProduct")
			};
		dojo.io.bind(bindArgs);
	}
}

function addToCartFromWishList(productId,wishlistId) {
	if (initCartDialogOk) {
		document.getElementById("form_headerProduct").productId.value=productId;
		document.getElementById("form_headerProduct").wishlistId.value=wishlistId;
		var bindArgs = {
			    url: "AddCartProductWishlist.do",
			    useCache: false,
				preventCache: true,
				cacheContent: false,
			    handler: fillPopup,
				formNode: dojo.byId("form_headerProduct")
			};
		dojo.io.bind(bindArgs);
	}
}

function fillPopup(type, data, event){
	if(type == "error"){
		alert("fillContent : " + data);
	} else {
		/* contenu de la popup */
		document.getElementById("div_addToCart").style.display = "block";
		dojo.byId("div_addToCart").innerHTML = data;
		/* defini le lien de fermeture de la popup */
		var btn = document.getElementById("a_dialogCartContinue");
		cartDialog.setCloseControl(btn);
		updateCart();
		cartDialog.show();
	}
}

dojo.addOnLoad(initCartDialog);

/* ////////////////////////////////////////////////// */

String.prototype.trim = function() {
	return this.replace(/(^\s*)|(\s*$)/g,'');
}


