var centreOnBlock = false;

function requestIdentif() {
	form_identif.submit();
}

function showSelectedPart(linkClicked) {
	centreOnBlock = linkClicked;
	var bindArgs = {
		    url: "ProductLinks.do",
		    useCache: false,
			preventCache: true,
			cacheContent: false,
			handler: fillContent,
			formNode: dojo.byId("form_product")
		};
	dojo.io.bind(bindArgs);
}

// Remplit le bloc en fonction de la selection
function fillContent(type, data, event){
	if(type == "error"){
		alert("fillContent : " + data);
	} else {
		dojo.byId("div_linksContent").innerHTML = data;
		if (centreOnBlock) {
			document.location.href = "#a_showblock";
		}
	}
}

function askForCreateComment() {
	centreOnBlock = true;
	var bindArgs = {
			url: "ProductComments.do",
			handler: fillContent,
			useCache: false,
			preventCache: true,
			cacheContent: false,
			formNode: dojo.byId("form_comment")
		};
	dojo.io.bind(bindArgs);
}



function valideComment() {
	var bindArgs = {
		    url: "ProductCommentsCreate.do",
		    useCache: false,
			preventCache: true,
			cacheContent: false,
			handler: fillContent,
			formNode: dojo.byId("form_comment")
		};
	dojo.io.bind(bindArgs);
}


function valideCheaper() {
	var bindArgs = {
		    url: "ProductCheaper.do",
		    useCache: false,
			preventCache: true,
			cacheContent: false,
			handler: fillContent,
			formNode: dojo.byId("form_cheaper")
		};
	dojo.io.bind(bindArgs);
}


/***** Gestion des images *****/
function updatePicture(newPicturePath, newLargePicturePath) {
	var picture = document.getElementById("id_large_img");
	picture.src	= newPicturePath;
	var link = document.getElementById("a_linkLargeView");
	link.href = "javascript: showPictureDialog('" + newLargePicturePath + "');";
	if(newLargePicturePath == "") { // Cache le lien s'il n'y a pas d'image
		link.style.visibility = "hidden";
	} else {
		link.style.visibility = "visible";
	}
}
/***** Image grand format *****/
var pictureDialog;
var objImg;

function initPictureDialog(e) {
	pictureDialog = dojo.widget.byId("div_pictureDialog");
	document.getElementById("div_pictureDialog").style.display = "none";
}
dojo.addOnLoad(initPictureDialog);

function showPictureDialog(picturePath) {
	document.getElementById("div_pictureDialog").style.display = "block";
	if ((objImg == null) || (objImg.src != picturePath)) { 
		// si nouvelle image ou differente precedente -> la charger et afficher
		loadObjectImage(picturePath);
	}
	else {
		// l'image a deja ete chargee -> afficher sans recharger
		displayPicture(false);
		pictureDialog.show();
	}
	pictureDialog.setCloseControl(document.getElementById("a_pictureDialogEnd")); //img_largePicture
}

// Initialisation objet Image
function loadObjectImage(picturePath) {
	objImg = new Image();
	objImg.onload = function() {
		objImg.maxWidth 	= this.width;
		objImg.maxHeight 	= this.height;
		displayPicture(false);
		pictureDialog.show();
	}
	objImg.src = picturePath;
}

/** Calcule la taille fenetre en fct de la taille actuelle disponible sur l'ecran et positionne l'image dedans */
function displayPicture(realSize) {
	var divPictureDialog = document.getElementById("div_pictureDialog");
	var divContent = document.getElementById('div_pictureDialogContent');

	// Au depart on donne les dimensions max a la photo
	objImg.width =  objImg.maxWidth;
	objImg.height = objImg.maxHeight;
	
	if (realSize) { // pas besoin de donner une taille a la fenetre on conserve celle precedemment affichee
		divContent.style.overflow = 'scroll';
	}
	else {
		divContent.style.overflow = 'hidden';
		// Marge autour de la fenetre * 2
		var margin = 20;
		// Taille bordure fenetre * 2
		var border = 2;
		// Taille ascenceur eventuel
		var scrollBar = 0;
		// Taille des barres Titre + pied
		var headerAndFooter = 58;
		// Taille fenêtre navigateur
		var viewportSize = dojo.html.getViewport();
		// Enregistrement des variables sur la fenetre
		var windowMaxWidth 	= viewportSize.width - margin - border;
		var windowMaxHeight = viewportSize.height - margin - border;
		
		// Enregistrement des variables sur l'image
		var pictureMaxWidth = windowMaxWidth - scrollBar;
		var pictureMaxHeight = windowMaxHeight - scrollBar - headerAndFooter;
				
		// Nouvelles valeurs pour la taille recalculee
		var pictureNewWidth = 0;
		var pictureNewHeight = 0;
		
		if ((objImg.height > pictureMaxHeight) || (objImg.width > pictureMaxWidth)) {
			// L'image ne peut pas être affichee en taille reelle
			var ratioHeight = objImg.height / pictureMaxHeight;
			var ratioWidth = objImg.width / pictureMaxWidth;
			
			if (ratioHeight > ratioWidth) {
				pictureNewHeight = pictureMaxHeight;
				pictureNewWidth = objImg.width / ratioHeight;
			}
			else {
				pictureNewWidth =  pictureMaxWidth;
				pictureNewHeight = objImg.height / ratioWidth;
			}
	      	
	      	objImg.width = pictureNewWidth;
	      	objImg.height = pictureNewHeight;
	      	
	      	// L'image n'etant pas affichee taille reelle -> les liens sont affiches 
	      	document.getElementById("a_pictureDialogFitInWindow").style.visibility = 'visible';
			document.getElementById("a_pictureDialogRealSize").style.visibility = 'visible';
		}
		else {
			// L'image est affichee taille reelle -> les liens ne sont pas affiches
			document.getElementById("a_pictureDialogFitInWindow").style.visibility = 'hidden';
			document.getElementById("a_pictureDialogRealSize").style.visibility = 'hidden';
		}

		divPictureDialog.style.width = objImg.width;
		divPictureDialog.style.height = objImg.height + headerAndFooter;
		
		// Taille maxi du content
		divContent.style.maxWidth = objImg.width;
		divContent.style.maxHeight = objImg.height;
	}
	
	// On ajout l'image au conteneur
	while (divContent.childNodes.length>0) {
  		divContent.removeChild(divContent.firstChild);
	}
  	divContent.appendChild(objImg);
}

function hidePictureDialog() {
	if (pictureDialog != null) {
		pictureDialog.hide();
		document.getElementById("div_pictureDialog").style.display = "none";
	}
}
/**** Controle saisie commentaire (limite taille) ***/
function controlComment(nbCaracsMax) {
	var textZone = document.getElementById("comment");
	var caracsZone = document.getElementById("nbCaracs");
	
	if (textZone.value.length > nbCaracsMax) {
		textZone.value = textZone.value.substr(0,nbCaracsMax);
	}
	caracsZone.value = nbCaracsMax - textZone.value.length;
}
/*******************************/
/* Envoyer cette page a un ami */
var adviceDialog;
function initAdviceDialog(e) {
	adviceDialog = dojo.widget.byId("div_adviceDialog");
	var advice = document.getElementById("div_adviceDialog");
	advice.style.display = "block";
	advice.style.visibility = "hidden";
}
dojo.addOnLoad(initAdviceDialog);
function adviceProductRequested(productId) {
	var bindArgs = {
		    url: "AdviceProductInit.do?productId=" + productId,
		    useCache: false,
			preventCache: true,
			cacheContent: false,
		    handler: fillAdviceDialog
		};
	dojo.io.bind(bindArgs);
	adviceDialog.show();
}
function adviceProduct() {
	var bindArgs = {
		    url: "AdviceProduct.do",
		    useCache: false,
			preventCache: true,
			cacheContent: false,
		    handler: fillAdviceDialog,
		    formNode: dojo.byId("form_advice")
		};
	dojo.io.bind(bindArgs);
	adviceDialog.show();
}
function fillAdviceDialog(type, data, event){
	if(type == "error"){
		alert("fillAdviceDialog : " + data);
	} else {
		// Visibilite
		document.getElementById("div_adviceDialog").style.visibility = "visible";
		// contenu de la popup
		dojo.byId("div_adviceDialog").innerHTML = data;
		// defini le lien de fermeture de la popup
		var closeButton = document.getElementById("a_dojoAdviceDialogContinue");
		adviceDialog.setCloseControl(closeButton);
	}
}
/* Envoyer cette page a un ami */
/*******************************/

/*******************************/
/* Ajout produit a la wishList */
var wishListDialog;
function initWishListDialog(e) {
	wishListDialog = dojo.widget.byId("div_wishListDialog");
	var wishList = document.getElementById("div_wishListDialog");
	wishList.style.display = "block";
	wishList.style.visibility = "hidden";
}
dojo.addOnLoad(initWishListDialog);
function addWishListRequested(userId,productId) {
	var bindArgs = {
		    url: "AddWishList.do?productId=" + productId + "&userId=" + userId,
		    useCache: false,
			preventCache: true,
			cacheContent: false,
		    handler: fillWishListDialog
		};
	dojo.io.bind(bindArgs);
	wishListDialog.show();
}
function fillWishListDialog(type, data, event){
	if(type == "error"){
		alert("fillWishListDialog : " + data);
	} else {
		// Visibilite
		document.getElementById("div_wishListDialog").style.visibility = "visible";
		// contenu de la popup
		dojo.byId("div_wishListDialog").innerHTML = data;
		// defini le lien de fermeture de la popup
		var closeButton = document.getElementById("a_dojoWishListDialogContinue");
		wishListDialog.setCloseControl(closeButton);
	}
}

/****************************************************/
/* Ajout d'une alerte baisse de prix sur un produit */
var productAlertDialog;
function initProductAlertDialog(e) {
	productAlertDialog = dojo.widget.byId("div_productAlertDialog");
	var prodAlert = document.getElementById("div_productAlertDialog");
	prodAlert.style.display = "block";
	prodAlert.style.visibility = "hidden";
}
dojo.addOnLoad(initProductAlertDialog);
function addProductAlertRequested(userId,productId) {
	var bindArgs = {
		    url: "AddProductAlert.do?productId=" + productId + "&userId=" + userId,
		    useCache: false,
			preventCache: true,
			cacheContent: false,
		    handler: fillProductAlertDialog
		};
	dojo.io.bind(bindArgs);
	productAlertDialog.show();
}
function fillProductAlertDialog(type, data, event){
	if(type == "error"){
		alert("fillProductAlertDialog : " + data);
	} else {
		// Visibilite
		document.getElementById("div_productAlertDialog").style.visibility = "visible";
		// contenu de la popup
		dojo.byId("div_productAlertDialog").innerHTML = data;
		// defini le lien de fermeture de la popup
		var closeButton = document.getElementById("a_dojoProductAlertDialogContinue");
		productAlertDialog.setCloseControl(closeButton);
	}
}