/**
 * @author Nurun
 */


if (this.jQuery !== undefined) {
	// $ == jQuery inside this closure
	// do not use prototype here
	(function($) {
		
		var W = this, D = this.document;
		
		if (!D.getElementById || !D.getElementsByTagName || !D.createElement) {
			return;
		}
		
		var html = D.documentElement || $('html')[0];
		html.id = 'js';
		
		var giftPrice, basketTotalField, basketTotalDisplay;
		
		/**
		 * Get the current basket total price
		 * Accessible outside this closure as global variable
		 * @return {Number} price
		 */
		var getBasketTotal = function() {
			return parseInt(basketTotalField.val(), 10);
		};
		W.getBasketTotal = getBasketTotal;
	
		/**
		 * Set the basket total price
		 * Accessible outside this closure as global variable
		 * @param {Number} value: the new price
		 */
		var setBasketTotal = function(value) {
			basketTotalField.val(value);
			basketTotalDisplay.text(basketTotalDisplay.text().replace(/\d+/, value));
		};
		W.setBasketTotal = setBasketTotal;
		
		/**
		 * Get the current product total price
		 * Accessible outside this closure as global variable
		 * @param {String} productID: the ID attribute of the element to target
		 * @return {Number} price
		 */
		var getProductPrice = function(productID) {
			return parseInt($(productID).text(), 10);
		};
		W.getProductPrice = getProductPrice;
		
		/**
		 * Set the current product total price
		 * Accessible outside this closure as global variable
		 * @param {String} productID: the ID attribute of the element to target
		 * @param {Number} value: the new price
		 */
		var setProductPrice = function(productID, nbProducts) {
			var newTotal = getBasketTotal() - getProductPrice(productID) + nbProducts * unitPrice[productID];
			$(productID).text($(productID).text().replace(/\d+/, nbProducts * unitPrice[productID]));
			setBasketTotal(newTotal);
		};
		W.setProductPrice = setProductPrice;
		
		/**
		 * Initialize form fields enhancement
		 * Accessible outside this closure as global variable
		 * @param {String} context: the ID attribute of the element to target (use for example in Ajax callbacks)
		 */
		var initFauxFields = function(context) {
			context = context || '';
			
			W.fauxRadio = FauxFields.Factory({
				'context': context,
				'field': 'radio'
			});
			
			W.fauxCheckbox = FauxFields.Factory({
				'context': context,
				'field': 'checkbox'
			});
			
			W.fauxSelect = FauxFields.Factory({
				//'context': context,
				'field': 'select',
				'maxHeight': 175
			});
		};
		W.initFauxFields = initFauxFields;
		
		var unitPrice = {};
		
		$(D).ready(function() {
			W.giftPrice = giftPrice = parseInt($('#prixCadeau').text(), 10);
			basketTotalField = $('#total');
			basketTotalDisplay = $('#totalPanier');
			if($('#panierListe').size()){
			$('#panierListe select').each(function(i, select) {
				unitPrice['#prix' + (i + 1)] = getProductPrice('#prix' + (i + 1)) / parseInt(select.value, 10);
			});
			}
		});
		
	})(jQuery);
}



//*** ECOUTEURS ***//
var initTunnel = function() {
	
	var hauteurPage = document.documentElement.clientHeight;
	
	if ($('panier')) {
		//Event.observe('pictoMail', 'click', popinMessageClose, false);
	}	
	if ($('produitDescription')) {
		Event.observe('pictoMail', 'click', popinEnvoiAmi, false);
		Event.observe('popinEnvoiAmiClose', 'click', popinEnvoiAmiClose, false);
	}
	if ($('popinErreursFormulairesClose')) {
		Event.observe('saveFollow', 'click', popinErreursFormulaires, false);
		Event.observe('popinErreursFormulairesClose', 'click', popinErreursFormulairesClose, false);
	}
	if ($('lienwishlist')) {
		if (!$$('.liendeconnexion')[0]) {
			Event.observe('lienwishlist', 'click', popinFullWishlist, false);
			//Event.observe('lienwishlist', 'click', popinWishlist, false);
			Event.observe('lienwishlist', 'click', function(){
				if(hauteurPage < 440){
					$$('body')[0].className = 'lowDef';
					$$('html')[0].style.overflow = 'hidden';
				}
				else{
					$$('body')[0].className = '';
					}
				popinWishlist();
			}, false);
		}
	}
	
	if(!$$('liendeconnexion')[0] && $('panierDispo')){
		
		Event.observe($$('#panierDispo a')[1], 'click', function(){		
			//console.log('GO');
			popinWishlist();
			addScrollToContainer($$('.scrollForm')[0],230);
			if ($('popinWishlist') && ($$('#popinWishlist .fauxSelect').length == 0)) {
				FauxFields.Factory({
					context: 'popinWishlist',
					field: 'select',
					maxHeight: 130				
					});
				}	
		},
		 false);
	}
	
	if ($('ajoutWL')) {
		Event.observe('ajoutWL', 'click', popinFullWishlist, false);
		Event.observe('popinFullWishlistClose', 'click', popinFullWishlistClose, false);
	}
	if ($('rappelPanier')) {
		if ($('lienpanier')) {
			Event.observe('lienpanier', 'click', displaySummary, false);
		}
	    Event.observe('lienwishlist', 'click', displaySummary, false);
	}
	/*if ($('lienpanier')) {
		Event.observe('lienpanier', 'click', popinFullBasket, false);
		Event.observe('popinFullBasketClose', 'click', popinFullBasketClose, false);
	}*/
	if ($('lienConditionsdEntretien')) {
		Event.observe('lienConditionsdEntretien', 'click', popinConditionsdEntretien, false);
		Event.observe('popinConditionsdEntretienClose', 'click', popinConditionsdEntretienClose, false);
	}
	if($('passeOubli')){
		Event.observe('passeOubli', 'click', popinMdpOublie, false);
		Event.observe('popinMdpOublieClose', 'click', popinMdpOublieClose, false);
		//Event.observe('popinMdpOublieEnvoi', 'click', popinMdpOublieClose, false);
	}
	if($('liencontenuassoc')){
		Event.observe('liencontenuassoc', 'click', popinContenuAssocie, false);
		Event.observe('popinContenuAssocieClose', 'click', popinContenuAssocieClose, false);
	}

};

Event.observe(window, 'load', initTunnel, false);

//Event.observe(window, 'resize', scrollingPage, false);

var lastsummary = 0;
function displaySummary() {
    var summary = $(this.parentNode.getElementsByTagName("div")[0]);
    if ((lastsummary != 0)&&(!(lastsummary == summary))) {
        lastsummary.removeClassName("show");
        lastsummary = 0;
    }
    if (summary.hasClassName("show")){
        summary.removeClassName("show");
        lastsummary = 0;
    }
    else{
        summary.addClassName("show");
        lastsummary = summary;
    }
}
//*** Fin Gestion des wishlist/basket summaries ***//

function getViewPortDimensions() {
    var de = this.document.documentElement;
    return {
        'width': this.innerWidth || (de && de.clientWidth) || this.document.body.clientWidth,
        'height': this.innerHeight || (de && de.clientHeight) || this.document.body.clientHeight
    };
};

maskMngr = {
	show:function(){
		$('popinMask').style.left="0";
		$('popinMask').style.height = document.documentElement.scrollHeight + 'px';
		$('popinMask').style.width = document.documentElement.scrollWidth + 'px';
	},
	hide:function(){
		$('popinMask').style.left="-999999px";
	}
}

//*** POP-IN : MESSAGE ***//
function popinMessage(){
	 //$('popinMask').style.display="block";	
	 maskMngr.show();
	 $('popinMessage').style.display="block";
}
function popinMessageClose(){
	 //$('popinMask').style.display="none";
	 maskMngr.hide();
	 $('popinMessage').style.display="none";
}


//*** POP-IN : INSCRIPTION ***//
function popinInscription(){
	 //$('popinMask').style.display="block";
	 maskMngr.show();
	 $('popinInscription').style.left="50%";
	 if(!$$('.scrollContent')[1]){
	 addScrollToContainer($$('.scrollForm')[0],230);
	if ($('popinInscription') && ($$('#popinInscription .fauxSelect').length == 0)) {
		FauxFields.Factory({
			context: 'popinInscription',
			field: 'select',
			maxHeight: 130
		
		});
	}	
	}
}
function popinInscriptionClose(){
	 //$('popinMask').style.display="none";
	 maskMngr.hide();
	 $('popinInscription').style.left="-9999px";
}

//*** POP-IN : CONTENU ASSOCIE***//
function popinContenuAssocie(){
	 //$('popinMask').style.display="block";
	 maskMngr.show();	 
	 $('popinContenuAssocie').style.display="block";
}
function popinContenuAssocieClose(){
	 //$('popinMask').style.display="none";
	 maskMngr.hide();
	 $('popinContenuAssocie').style.display="none";
}

//*** POP-IN : ENVOYER A UN AMI ***//
function popinEnvoiAmi(){
	 //$('popinMask').style.display="block";
	 maskMngr.show();
	 $('popinEnvoiAmi').style.display="block";

	 //$('popinMask').style.visibility="visible";
	 //$('popinEnvoiAmi').style.visibility="visible";
}
function popinEnvoiAmiClose(){
	 //$('popinMask').style.display="none";
	 maskMngr.hide();
	 $('popinEnvoiAmi').style.display="none";


	 //$('popinMask').style.visibility="hidden";
	 //$('popinEnvoiAmi').style.visibility="hidden";
}


//*** POP-IN : AFFICHAGE DES ERREURS ***//
function popinErreursFormulaires(){
	 //$('popinMask').style.display="block";
	 maskMngr.show();
	 $('popinErreursFormulaires').style.display="block";
}
function popinErreursFormulairesClose(){
	 //$('popinMask').style.display="none";
	 maskMngr.hide();
	 $('popinErreursFormulaires').style.display="none";
}

//*** POP-IN : INSCRIPTION WISHLIST ***//
function popinWishlist(){
	//console.log('go Through popinWishlist()');
	if(document.documentElement.clientHeight < 440){
		//console.log('go through popinWishlist')
		document.body.style.overflow = 'visible';
	}
	maskMngr.show();
	 //$('popinMask').style.display="block";
	 $('popinWishlist').style.left="50%";

	 //$('popinMask').style.visibility="visible";
	 //$('popinWishlist').style.visibility="visible";
}
function popinWishlistClose(){
	 //$('popinMask').style.display="none";
	 maskMngr.hide();
	 $('popinWishlist').style.left="-9999px";
}

//*** POP-IN : WISHLIST PLEINE ***//
function popinFullWishlist(){
	 //$('popinMask').style.display="block";
	 maskMngr.show();
	 $('popinFullWishlist').style.display="block";
}
function popinFullWishlistClose(){
	 //$('popinMask').style.display="none";
	 maskMngr.hide();
	 $('popinFullWishlist').style.display="none";
}

//*** POP-IN : PANIER PLEIN ***//
function popinFullBasket(){
	 //$('popinMask').style.display="block";
	 maskMngr.show();
	 $('popinFullBasket').style.display="block";
}
function popinFullBasketClose(){
	 //$('popinMask').style.display="none";
	 maskMngr.hide();
	 $('popinFullBasket').style.display="none";
}

//*** POP-IN : CONDITIONS D'ENTRETIEN ***//
function popinConditionsdEntretien(){	
	 //$('popinMask').style.display="block";
	 maskMngr.show();
	 $('popinConditionsdEntretien').style.display="block";
}
function popinConditionsdEntretienClose(){
	 //$('popinMask').style.display="none";
	 maskMngr.hide();
	 $('popinConditionsdEntretien').style.display="none";
}

//*** POP-IN : MOT DE PASSE OUBLIE ***//
function popinMdpOublie(){
	//$('popinMask').style.display="block";
	maskMngr.show();
	$('popinMdpOublie').style.display="block";
	if ($('popinWishlist')) {
		$('popinWishlist').style.left="-9999px";
	}
	if ($('popinInscription')) {
		$('popinInscription').style.left="-9999px";
	}
}

function popinMdpOublieClose(){
	//$('popinMask').style.display="none";
	maskMngr.hide();
	$('popinMdpOublie').style.display="none";
}


//*** SUPPRIME PRODUIT PANIER ***//
function supprimerPanier(arg,arg1,arg2){
	 champ1 = "quantite"+arg1;
	 valeur1 = Number($(champ1).value);
	 valeur=valeur1*arg2;
	 
	 $(arg).innerHTML="";	
	 
	 // Met � jour TOTAL
	 majTOTAL('d',valeur);	
}

function majTOTAL(act,arg){
	 oValeur = Number($('total').value);
	 arg = Number(arg);
	 
	 if (act == 'a') {nValeur=oValeur+arg;}
	 else {nValeur=oValeur-arg;}
	 
	 $('total').value = nValeur;
	 $('totalPanier').innerHTML = nValeur+" &euro;";
}

/** rollover sur pastille de couleur**/
function rollOver(ImgNme)
		{
			obj=(document.images)? document.images[ImgNme]:0; 
			ClicNme=(document.clicImg)? document.clicImg.name:"";
			
			if (!obj && document.layers) {
			
				for (i=0; i<document.layers.length; i++) { ObjLyr=document.layers[i].document;
			
					if (ObjLyr.images && ObjLyr.images[ImgNme]) 
			
						obj=ObjLyr.images[ImgNme];}}
						//obj.width = '13';
						//obj.height = '13';
							//if (ClicNme != ImgNme) {
								//obj.src=(obj.src.lastIndexOf('-on')!=-1)? obj.src.replace(/-on/gi,"-off"):obj.src.replace(/-off/gi,"-on");
								
								//obj.width=(obj.width('15')!=-1)? obj.width.replace("15","13"):obj.width.replace("13","15");
																
								if (obj.width == '15' && obj.height == '15') {
									obj.width = 13;
									obj.height = 13;
									obj.style.width = '13px';
									obj.style.height = '13px';
								}
								else 
									if (obj.width == '13' && obj.height == '13') {
										/*obj.width = 15;
										obj.height = 15;*/
										obj.style.width = '15px';
										obj.style.height = '15px';
									}
								//obj.height=(obj.height('15')!=-1)? obj.height.replace("15","13"):obj.height.replace("13","15");
								
								document.OnImg = obj;
							//}
		}
		
function Clic(ImgNme)
			{
			 obj=(document.images)? document.images[ImgNme]:0;
			 ClicNme=(document.clicImg)? document.clicImg.name:"";
			 
			 if (!obj && document.layers) {
				for (i=0; i<document.layers.length; i++) { ObjLyr=document.layers[i].document;
					if (ObjLyr.images && ObjLyr.images[ImgNme]) obj=ObjLyr.images[ImgNme];}}
					
					//if (document.clicImg) document.clicImg.src=document.clicImg.src.replace(/_on/gi,"_off");
					if (document.clicImg) //document.clicImg.width=document.clicImg.width && document.clicImg.height=document.clicImg.height;
						//obj.src=obj.src.replace(/_off/gi,"_on");
						{obj.width = '15';obj.height = '15'}
						document.clicImg=obj;
						//return false;
}

movePopin2 = {
	init: function(){
		
		if (!$('panier')) return;
		var closeBtn = $('popinMessageClose');
		if(!closeBtn) return;
		closeBtn.onclick = function(){
			movePopin2.hideMask();
			movePopin2.hide(this);
		}
		var wish = document.getElementById("panierCadeau");	
		var openBtn = wish.getElementsByTagName('a')[0];	
		//console.log(openBtn);
		var wish_popin = document.getElementById('popinMessage');	
		//$('popinMessage').remove();	
					
		
		var page = document.getElementById("page");		
		var parentDiv = page.parentNode;			
		// insère le nouvel élément dans le DOM avant l'id page
		var insertWishPopin =  parentDiv.insertBefore(wish_popin, page);
		var wish_popin = document.getElementById('popinMessage');	
		
		
		openBtn.onclick = function(){
			//console.log('openBtn.onclick')
			movePopin2.showPopinMsg(insertWishPopin);
			/*console.log('click => '+insertWishPopin.innerHTML);
			insertWishPopin.style.display = 'block';*/
			return false;
		}		
		},
	hideMask: function(){
		var mask = $('popinMask');
		mask.style.display = 'none';
	},	
	hide: function(a){
		a.parentNode.style.display = 'none'
	},
	showPopinMsg: function(popin){
		//console.log(popin);
		popin.style.display = 'block'
	}	
}	
Event.observe(window, 'load', movePopin2.init, false);