/**
 * ajout panier en AJAX
 * @author JBA, BVE, jdacosta
*/

var dialog = false;
var mySimpleDialog;
var urlRetour;
var urlPanier;
var windowOpener = false;

function SimpleDialogInit() {
	YAHOO.widget.SimpleDialog.prototype.configModal = function(type,args,obj) {
		var modal = args[0];

		if (modal) {
			this.buildMask();

			if (typeof this.maskOpacity=='undefined') {
				this.mask.style.visibility = "hidden";
				this.mask.style.display = "block";
				this.maskOpacity = YAHOO.util.Dom.getStyle(this.mask,"opacity");
				this.mask.style.display = "none";
				this.mask.style.visibility = "visible";
			}

			if (!YAHOO.util.Config.alreadySubscribed(this.beforeShowEvent,this.showMask,this)) {
				this.beforeShowEvent.subscribe(this.showMask,this,true);
			}
			if (!YAHOO.util.Config.alreadySubscribed(this.hideEvent,this.hideMask,this)) {
				this.hideEvent.subscribe(this.hideMask,this,true);
			}
			if (!YAHOO.util.Config.alreadySubscribed(YAHOO.widget.Overlay.windowResizeEvent,this.sizeMask,this)) {
				YAHOO.widget.Overlay.windowResizeEvent.subscribe(this.sizeMask,this,true);
			}
			if (!YAHOO.util.Config.alreadySubscribed(this.destroyEvent,this.removeMask,this)) {
				this.destroyEvent.subscribe(this.removeMask,this,true);
			}
			this.cfg.refireEvent("zIndex");
		} else {
			this.beforeShowEvent.unsubscribe(this.showMask,this);
			this.beforeHideEvent.unsubscribe(this.hideMask,this);
			YAHOO.widget.Overlay.windowResizeEvent.unsubscribe(this.sizeMask);
		}
	};

	YAHOO.widget.SimpleDialog.prototype.showMask = function() {
		if (this.cfg.getProperty("modal")&&this.mask) {
			YAHOO.util.Dom.addClass(document.body,"masked");
			this.sizeMask();

			var o = this.maskOpacity;

			if (!this.maskAnimIn) {
				this.maskAnimIn = new YAHOO.util.Anim(this.mask,{
					        opacity : {
						        to : o
					        }
				        },0.5)
				YAHOO.util.Dom.setStyle(this.mask,"opacity",0);
			}

			if (!this.maskAnimOut) {
				this.maskAnimOut = new YAHOO.util.Anim(this.mask,{
					        opacity : {
						        to : 0
					        }
				        },0.5)
				this.maskAnimOut.onComplete.subscribe(function() {
					        this.mask.tabIndex = -1;
					        this.mask.style.display = "none";
					        this.hideMaskEvent.fire();
					        YAHOO.util.Dom.removeClass(document.body,"masked");
				        },this,true);

			}
			this.mask.style.display = "block";
			this.maskAnimIn.animate();
			this.mask.tabIndex = 0;
			this.showMaskEvent.fire();
		}
	};

	// Overrides the showMask function to allow for fade-out animation
	YAHOO.widget.SimpleDialog.prototype.hideMask = function() {
		if (this.cfg.getProperty("modal")&&this.mask) {
			this.maskAnimOut.animate();
		}
	};

	mySimpleDialog = new YAHOO.widget.SimpleDialog("simpledialog2",{
		        effect : {
			        effect : YAHOO.widget.ContainerEffect.FADE,
			        duration : 0.5
		        },
		        fixedcenter : true,
		        visible : false,
		        draggable : false,
		        close : true,
		        modal : true,
		        constraintoviewport : true
	        });
}

var handleRetour = function() {
	dialog = false;
	this.hide();
	/*if (urlRetour!="catalogue") {
		document.location.href = urlRetour;
	}*/
};

var handleYes = function() {
	dialog = false;
	this.hide();
};

var handleNo = function() {
	dialog = false;
	this.hide();
	if (windowOpener) {
		window.opener.location.href = urlPanier;
		window.close();
	} else {
		document.location.href = urlPanier;
	}
};

var AddToCartSuccess = function(o) {

	SimpleDialogInit();

	var message = o.responseXML.documentElement.getElementsByTagName("message")[0].firstChild.nodeValue;
	var nb_elem_panier = o.responseXML.documentElement.getElementsByTagName("nb_elem_panier")[0].firstChild.nodeValue;

	//on récupère les messages
	var header_confirmation = o.responseXML.documentElement.getElementsByTagName("confirm")[0].firstChild.nodeValue;
	var message_confirmation = o.responseXML.documentElement.getElementsByTagName("ajout_devis")[0].firstChild.nodeValue;
	var header_avert = o.responseXML.documentElement.getElementsByTagName("avertissement")[0].firstChild.nodeValue;
	var message_avert = o.responseXML.documentElement.getElementsByTagName("produit_trouve")[0].firstChild.nodeValue;
	var bouton_continuer = o.responseXML.documentElement.getElementsByTagName("bouton_continuer")[0].firstChild.nodeValue;
	var bouton_acceder = o.responseXML.documentElement.getElementsByTagName("bouton_acceder")[0].firstChild.nodeValue;

	if (message=="1") {
		mySimpleDialog.setHeader(header_confirmation);
		mySimpleDialog.setBody(message_confirmation+"<br/>");
		var myButtons = [{
			        text : bouton_continuer,
			        handler : handleRetour,
			        isDefault : true
		        },{
			        text : bouton_acceder,
			        handler : handleNo
		        }];
		mySimpleDialog.cfg.queueProperty("buttons",myButtons);

		if (document.getElementById('boite_panier_contenu_conteur')) {
			document.getElementById('boite_panier_contenu_conteur').innerHTML = nb_elem_panier;
		} else {
			window.opener.document.getElementById('boite_panier_contenu_conteur').innerHTML = nb_elem_panier;
		}

	} else {
		mySimpleDialog.setHeader(header_avert);
		mySimpleDialog.setBody(message_avert+"<br/>");
		var myButtons = [{
			        text : "OK",
			        handler : handleYes,
			        isDefault : true
		        }];
		mySimpleDialog.cfg.queueProperty("buttons",myButtons);
	}

	mySimpleDialog.render(document.body);

	mySimpleDialog.show();
}

var AddToCartFailure = function(o) {
}
var callbackAddToCart = {
	success : AddToCartSuccess,
	failure : AddToCartFailure
}

var handleConfirm = function() {
	//var leform = document.getElementById("form_demande_devis");
	dialog = false;
	this.hide();
	//leform.submit();
};

function confirmation_devis(lang) {
	var message;
	var titre;

	switch (lang) {
		case "fr" :
			message = "Votre demande de devis a été prise en compte<br/>";
			titre = "Confirmation";
			break;

		case "en" :
			message = "Your price request has been registered<br/>";
			titre = "Confirmation";
			break;

		case "es" :
			message = "Su peticion de presupuesto esta siendo atendida<br/>";
			titre = "Confirmación";
			break;

		case "de" :
			message = "Ihre Preisanfrage ist registriert worden<br/>";
			titre = "Bestätigung";
			break;

		case "it" :
			message = "la sua richiesta di preventivo è stata inoltrata<br/>";
			titre = "Conferma";
			break;

	}

	SimpleDialogInit();

	mySimpleDialog.setHeader(titre);
	mySimpleDialog.setBody(message);
	var myButtons = [{
		        text : "OK",
		        handler : handleConfirm,
		        isDefault : true
	        }];
	mySimpleDialog.cfg.queueProperty("buttons",myButtons);

	mySimpleDialog.render(document.body);

	mySimpleDialog.show();
}

/**
 * fonction d'ajout au panier
 * @param type : le type de refernce ajouté (page ou produit_selection)
 * @param reference : un page_id ou une reference_id de produit_selection
 * @param url_retour : voir plus haut handleRetour
 */
function panier_ajouter(type,reference,url_retour,couleurs,marquages,matieres,tailles) {
	urlRetour = url_retour;
	windowOpener = false;
	urlPanier = 'index.php?todo=panier_lister';
	if (dialog==false) {
		dialog = true;
		// connexion AJAX
		var url = 'index.php?todo=panier_ajouter&type='+type+'&reference='+reference;
		if (couleurs)
			url = url+'&couleurs='+couleurs;
		if (marquages)
			url = url+'&marquages='+marquages;
		if (matieres)
			url = url+'&matieres='+matieres;
		if (tailles)
			url = url+'&tailles='+tailles;
		YAHOO.util.Connect.asyncRequest('GET',url,callbackAddToCart,null);
	}
}

function panier_ajouter_message(type,reference,url_retour,window_opener) {
	urlRetour = url_retour;
	windowOpener = window_opener;
	urlPanier = 'index.php?todo=panier_lister';
	if (dialog==false) {
		dialog = true;
		// connexion AJAX
		YAHOO.util.Connect.asyncRequest('GET','index.php?todo=panier_ajouter&type='+type+'&reference='+reference,callbackAddToCart,null);
	}
}

