﻿/*
var globalSifrStyles = function () {
	sIFR.replaceElement("#nav li", named({sFlashSrc: BaseUrl+"/Includes/swf/GoudyBold.swf", sColor: "#A9A4A4", sHoverColor:"#b8b2b2", sCase: "upper", sWmode: "transparent"}));
	sIFR.replaceElement("#right .title span" , named({sFlashSrc: BaseUrl+"/Includes/swf/GoudyBold.swf", sColor: "#ffffff", sCase: "upper", sWmode: "transparent"}));
	sIFR.replaceElement("#right .boxRightCommon .title" , named({sFlashSrc: BaseUrl+"/Includes/swf/GoudyBold.swf", sColor: "#9c8175", sCase: "upper", sWmode: "transparent"}));
	sIFR.replaceElement("#right .btns span" , named({sFlashSrc: BaseUrl+"/Includes/swf/GoudyBold.swf", sColor: "#454545", sHoverColor:"#ae6e60", sCase: "", sWmode: "transparent"}));
	sIFR.replaceElement("#right .boxRightCommon .tel" , named({sFlashSrc: BaseUrl+"/Includes/swf/GoudyBold.swf", sColor: "#785b4f", sCase: "", sWmode: "transparent"}));
	sIFR.replaceElement("#footer li", named({sFlashSrc: BaseUrl+"/Includes/swf/GoudyBold.swf", sColor: "#ffffff", sCase: "upper", sWmode: "transparent"}));
}
*/

var subNavRoll = function () {
	var subNavNodes = $$("#subNav td");
	for (var i=0;i<subNavNodes.length;i++) {
		if (subNavNodes[i].className=="") {
			subNavNodes[i].onmouseover = function () { this.className = "on"; }
			subNavNodes[i].onmouseout = function () { this.className = ""; }
		}
	}
}

var onLoadGlobal = new EventsContainer();
onLoadGlobal.add("subNavRoll");		
//onLoadGlobal.add("globalSifrStyles");  


/*
	* ZOOM PRODUITS
*/

function zoomImg (url) {   
	var str = "";
	oPop = window.open('','popImg','width=600,height=600,toolbar=no,scrollbars=no,resizable=no');
	str += "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'><html xmlns='http://www.w3.org/1999/xhtml'><head><title>Tectona</title>"; 
	str += "<script type=\"text/javascript\">removeSpace = (navigator.userAgent.indexOf('Safari')!=-1)?70 :0; function resizePop () { if (document.images[0].complete || navigator.userAgent.indexOf('Safari')!=-1 && document.images[0].width>0) { window.resizeTo(document.images[0].width+50,document.images[0].height+100-removeSpace);} else { setTimeout('resizePop()',200) } }</"+"script></head>";
	str += "<body onload='resizePop();'><table width='100%' height='100%' summary=''><tr><td align='center' valign='middle'><a href='javascript:close();'><img src='" + url + "' border='0' alt='' /></a></td></tr></table></body></html>";
	oPop.document.write(str);
	oPop.document.close();
}

/*
	* FIN ZOOM PRODUITS
*/


/*
	* COORDONNEES
*/

function showMyInfos () {
	var tmp = $$("#boxCoordonnes .text")[0];
	tmp.style.display = (tmp.style.display=="block") ?"none" :"block";
}

/*
	* FIN COORDONNEES
*/

/*
	* PANIER
*/

function basketAddProductWithAjax (qte, productId, rootUrl) {
	var data = $H({  'CMD': 'ADD'  ,'QTE': qte  , 'PID' : productId }).toQueryString();
	var path = rootUrl + '/Includes/Ajax/AjaxBasket.aspx';	
	var DoAjax = new Ajax.Updater( 'AjaxBasketReponse' , path, { postBody: data , evalScripts:true } );		
	var data2 = $H({  'CMD': 'POPUP_ADD' ,'QTE': qte , 'PID' : productId }).toQueryString();
	DoAjax = new Ajax.Updater( 'AjaxDescriptionProduitAjoute' , path, { postBody: data2 , evalScripts:true } );	
	$$("#basketPopAdd .leftBrdArrows a")[0].onclick = $("basketPopAddClose").onclick = function () { $("basketPopAdd").className = "basketPopAddOff"; }
	$("basketPopAdd").className = "basketPopAddOn";
}

var productContent = "";

function basketRefreshTotal (total,textArticle) {
	var basket = $("boxBasket");
	$("totalDatas").innerHTML = '<strong><span>'+total.quantity+'</span> ' + textArticle + ' </strong> <strong class="price">'+total.price+'</strong>';	
}

function basketAddProduct (rootUrl, product) {
	
	var basket = $("boxBasket");
	var productList = $("productsList");
	
	if (basket.className=="boxBasketEmpty") {
		Element.removeClassName(basket, "boxBasketEmpty");
		Element.addClassName(basket, "boxBasketOn");
	}
	
	if (basket.className=="boxBasketOn") {
		var findProductId = -1;
		var productListTextNodes = $$("#productsList li .text");
		for (var i=0;i<productListTextNodes.length&&findProductId==-1;i++) if (product.id == parseInt(productListTextNodes[i].id)) findProductId = i;
	}
	
	if (findProductId==-1) {
		var newNode = document.createElement("li");
		productList.appendChild(newNode);
	}
	
	var productListNodes = $$("#productsList li");
	var targNode = (findProductId==-1) ? (productListNodes.length>0) ?productListNodes[productListNodes.length-1]:productListNodes[0] :productListNodes[findProductId];
	targNode.innerHTML = '<div class="text" id="'+product.id+'productBasket"><strong>' + product.label + '</strong><br />' + product.description + '</div> <strong class="price"><span>' + product.quantity + ' x </span><br/>' + product.price + '</strong><a href="javascript:basketDeleteProduct(' + product.id + ',\'' + rootUrl + '\');" class="delete"><img src="' + rootUrl + '/Includes/images/Pictos/poubelle.gif" alt="" /></a></li>';
	Element.addClassName(targNode, "last");
	if (productListNodes.length>1) Element.removeClassName(productListNodes[productListNodes.length-2], "last");
	
}

function basketDeleteProduct(productID,rootUrl) {
	
	var data = $H({  'CMD': 'DELETE' , 'PID' : productID }).toQueryString();
	var path = rootUrl + '/Includes/Ajax/AjaxBasket.aspx';	
	var DoAjax = new Ajax.Request( path, { postBody: data } );
	DoAjax = new Ajax.Updater( 'AjaxDelete' , path, { postBody: data , evalScripts:true } );	
	
	var findProductId = -1;
	var productListTextNodes = $$("#productsList li .text");
	for (var i=0;i<productListTextNodes.length&&findProductId==-1;i++) if (productID == parseInt(productListTextNodes[i].id)) findProductId = i;
	
	Element.remove($$("#productsList li")[findProductId]);
	
	var productListNodes = $$("#productsList li");
	if (productListNodes.length>0) Element.addClassName(productListNodes[productListNodes.length-1], "last");
	else {
		Element.removeClassName($("boxBasket"), "boxBasketOn");
		Element.addClassName($("boxBasket"), "boxBasketEmpty");
	}
	
}

/*
	* FIN PANIER
*/

/*
	* FICHE PRODUIT
*/

function productCommandeTools (quantiteInput,btnAdd,btnRemove) {
	this.quantiteInput = $(quantiteInput);
	Event.observe(btnAdd, "click", this.quantityAdd.bind(this));
	Event.observe(btnRemove, "click", this.quantityRemove.bind(this));
}
productCommandeTools.prototype.quantityAdd = function () {
	this.quantiteInput.value = Number(this.quantiteInput.value) + 1;
}
productCommandeTools.prototype.quantityRemove = function () {
	if (this.quantiteInput.value>0) this.quantiteInput.value = Number(this.quantiteInput.value) - 1;
}

/*
	* FIN FICHE PRODUIT
*/

/*
	* COMMANDE
*/

function orderStep1 () {
	
	this.subTotal = $("subTotal");
	this.typeLivraison1 = $("typeLivraison1");
	this.typeLivraison2 = $("typeLivraison2");
	this.typeLivraison3 = $("typeLivraison3");
	this.livraisonTotal = $("livraisonTotal");
	this.total = $("total");
	
	this.products = [];
	this.basketNodes = $$("#basketSummary .productItem");
	
	for (var i=0;i<this.basketNodes.length;i++) {
		
		this.basketNodes[i].id = i+"productItem";
		var oProduct = this.basketNodes[i];
		var oInput, oPrice = oQuantity = oTotal = null;
		var oProductRef = null;
		this.itemNodes = this.basketNodes[i].getElementsByTagName("td");
		
		for (var j=0;j<this.itemNodes.length;j++) {
			var tmp = this.itemNodes[j].className;
			if (tmp=="productPrice") {
			    oPrice = this.itemNodes[j];
			    oProductRef = this.itemNodes[j].id;
			}
			else if (tmp=="quantity") oQuantity = this.itemNodes[j];
			else if (tmp=="productTotal") oTotal = this.itemNodes[j];
		}
		
		if (oQuantity) oInput = oQuantity.getElementsByTagName("input")[0];
				
		this.productAdd(oInput,oInput.value,oProduct,oPrice,oQuantity,oTotal,oProductRef);		
	}
	this.initProductEvents();
	this.initLivraisonEvents();
}

orderStep1.prototype.productAdd = function (oInput,oInputValue,oProduct,oPrice,oQuantity,oTotal,oProductRef) {
	this.products[this.products.length] = {oInput:oInput,oInputValue:oInputValue,oProduct:oProduct,oPrice:oPrice,oQuantity:oQuantity,oTotal:oTotal,oProductRef:oProductRef};
}

orderStep1.prototype.initProductEvents = function () {
	var o = this;
	for (var i=0;i<this.products.length;i++) {
		
		if (this.products[i].oQuantity) {
			
			this.products[i].oInput.onchange = function () { o.productCheckValue(this); }
			
			this.quantityBtnsNodes = this.products[i].oQuantity.getElementsByTagName("a");						
			for (var j=0;j<this.quantityBtnsNodes.length;j++) {						    			
				var tmp = this.quantityBtnsNodes[j];								
				tmp.id = i + "btn" + j;				
				if (tmp.className=="quantityAdd") tmp.onclick = function () { o.productAddItem(this.id.charAt(0)); };
				else if (tmp.className=="quantityRemove") tmp.onclick = function () { o.productRemoveItem(this.id.charAt(0)); };
				else if (tmp.className=="quantityRemoveAll") tmp.onclick = function () { o.productRemoveAllItems(this.id.charAt(0)); };									
			}

			this.quantityBtnsNodes = this.products[i].oQuantity.getElementsByTagName("input");
			for (var j=0;j<this.quantityBtnsNodes.length;j++) {						    			
				var tmp = this.quantityBtnsNodes[j];
				tmp.id = i + "btn" + j;																												
			    if (tmp.className=="quantityChange") tmp.onchange = function () { o.productChangeItem(this.id.charAt(0)); };			
			}			
		}		
	}
}

orderStep1.prototype.productCheckValue = function (oInput) {
	var productId = oInput.parentNode.parentNode.id.charAt(0);
	if (isNaN(oInput.value)) oInput.value = this.products[productId].oInputValue;
	this.products[productId].oInputValue = oInput.value;
}

orderStep1.prototype.productAddItem = function (productId) {
	with (this.products[productId]) oInput.value = oInputValue =  parseInt(oInput.value) + 1;
	    /* Ajax update => productId item has been added */
		var data = $H({  'CMD': 'COMMAND_UPDATE'  , 'ITEM' : productId,'QTE': parseInt(this.products[productId].oInput.value)  , 'PID' : this.products[productId].oProductRef, 'LIV1' : this.getLivraisonParams()[0], 'ETAGE' : this.getLivraisonParams()[1], 'LIV2' : this.getLivraisonParams()[2], 'LIV3' : this.getLivraisonParams()[3]  }).toQueryString();
		var path = BaseUrl + '/Includes/Ajax/AjaxBasket.aspx';
		var DoAjax = new Ajax.Updater( 'AjaxCommandBasketReponse' , path, { postBody: data , evalScripts:true } );
}

orderStep1.prototype.productRemoveItem = function (productId) {
	with (this.products[productId]) {
		if (parseInt(oInput.value)>0) oInput.value = oInputValue =  parseInt(oInput.value) - 1;
		/* Ajax update => productId item has been removed */;
		var data = $H({  'CMD': 'COMMAND_UPDATE'  , 'ITEM' : productId,'QTE': parseInt(this.products[productId].oInput.value)  , 'PID' : this.products[productId].oProductRef, 'LIV1' : this.getLivraisonParams()[0], 'ETAGE' : this.getLivraisonParams()[1], 'LIV2' : this.getLivraisonParams()[2], 'LIV3' : this.getLivraisonParams()[3]  }).toQueryString();
		var path = BaseUrl + '/Includes/Ajax/AjaxBasket.aspx';
		var DoAjax = new Ajax.Updater( 'AjaxCommandBasketReponse' , path, { postBody: data , evalScripts:true } );
	}
}

orderStep1.prototype.productChangeItem = function (productId) {            
    with (this.products[productId]) {
        if (parseInt(oInput.value)>0) oInput.value = oInputValue =  parseInt(oInput.value)
		/* Ajax update => productId item has been removed */;
		var data = $H({  'CMD': 'COMMAND_UPDATE'  , 'ITEM' : productId,'QTE': parseInt(oInput.value)  , 'PID' : this.products[productId].oProductRef, 'LIV1' : this.getLivraisonParams()[0], 'ETAGE' : this.getLivraisonParams()[1], 'LIV2' : this.getLivraisonParams()[2], 'LIV3' : this.getLivraisonParams()[3]  }).toQueryString();
		var path = BaseUrl + '/Includes/Ajax/AjaxBasket.aspx';
		var DoAjax = new Ajax.Updater( 'AjaxCommandBasketReponse' , path, { postBody: data , evalScripts:true } );
	}
}

orderStep1.prototype.productRemoveAllItems = function (productId) {
	this.products[productId].oProduct.style.display = "none";
	var quantite = "-1";
	//with (this.products[productId]) oInput.value = oInputValue = 0;
	
	var data = $H({  'CMD': 'COMMAND_UPDATE'  , 'ITEM' : productId,'QTE': parseInt(quantite)  , 'PID' : this.products[productId].oProductRef, 'LIV1' : this.getLivraisonParams()[0], 'ETAGE' : this.getLivraisonParams()[1], 'LIV2' : this.getLivraisonParams()[2], 'LIV3' : this.getLivraisonParams()[3]  }).toQueryString();
	var path = BaseUrl + '/Includes/Ajax/AjaxBasket.aspx';
	var DoAjax = new Ajax.Updater( 'AjaxCommandBasketReponse' , path, { postBody: data , evalScripts:true } );
}

orderStep1.prototype.productTotalRefresh = function (productId,price) {
	this.products[productId].oTotal.innerHTML = price;
}

orderStep1.prototype.subTotalRefresh = function (price, sousTotal, livraisonConfortPrixMini) {
    //this.checkLivraisonState(sousTotal,livraisonConfortPrixMini);    	
	this.subTotal.innerHTML = price;	
	sousTotalMem = sousTotal;	
}

orderStep1.prototype.totalRefresh = function (price) {
	this.total.innerHTML = price;			
}

var sousTotalMem = '';
function ValiderEtape1(nextURL, MessError)
{
    if(('0,0' == sousTotalMem)||('0.0' == sousTotalMem))
        alert(MessError);        
    else
        document.location.href = nextURL;        
}

orderStep1.prototype.initLivraisonEvents = function () {
	Event.observe("livraison1","click",this.livraisonCheck.bindAsEventListener(this));
	Event.observe("livraison2","click",this.livraisonCheck.bindAsEventListener(this));
	Event.observe("livraison3","click",this.livraisonCheck.bindAsEventListener(this));
	Event.observe("etages","click",this.livraisonCheck.bindAsEventListener(this));
	sousTotalMem = $("subTotal").innerHTML.substring(0,3);	
}

orderStep1.prototype.livraisonCheck = function () {
                            
	if (!$("livraison1").checked)
	{
		$("etages").checked = false;
		$("typeLivraisonEtages").style.display = "none";
	}
	else
	    $("typeLivraisonEtages").style.display = "block";
	    
	    	    
    if ($("typeLivraisonEtages").className == 'invisible')
        $("typeLivraisonEtages").style.display = "none";
	    	    	    	   	    	   	    
	/* Ajax update => Livraison has been changed */		
	var data = $H({  'CMD': 'COMMAND_DELIVERY'  , 'LIV1' : this.getLivraisonParams()[0], 'ETAGE' : this.getLivraisonParams()[1], 'LIV2' : this.getLivraisonParams()[2], 'LIV3' : this.getLivraisonParams()[3]  }).toQueryString();
	var path = BaseUrl + '/Includes/Ajax/AjaxBasket.aspx';
	var DoAjax = new Ajax.Updater( 'AjaxCommandBasketReponse' , path, { postBody: data , evalScripts:true } );
}

orderStep1.prototype.getLivraisonParams = function () {
	return [    ($("livraison1"))?$("livraison1").checked:false,
	            ($("etages"))?$("etages").checked:false,
	            ($("livraison2"))?$("livraison2").checked:false,
	            ($("livraison3"))?$("livraison3").checked:false];	
}

orderStep1.prototype.checkLivraisonState = function (priceTotal,priceLivraison2Min) {
	    //if (parseFloat(priceTotal)<parseFloat(priceLivraison2Min)) $("livraison2").disabled = true; 
	    //else $("livraison2").disabled = false;
}

orderStep1.prototype.livraisonRefresh = function (type1,option1,type2,type3,total) {
	this.typeLivraison1.innerHTML = type1;
	this.typeLivraison1.innerHTML += option1;
	this.typeLivraison2.innerHTML = type2;
	this.typeLivraison3.innerHTML = type3;
	this.livraisonTotal.innerHTML = total;
}

/*
	* FIN COMMANDE
*/

function init () { onLoadGlobal.init(); }
window.onload = init;

/*
	* POPUP
*/

function popup (page,name,w,h,sc) {
	window.open(page,name,'width='+w+',height='+h+',left=' + ((screen.width/2) - (w/2)) + ',top=' + ((screen.height/2) - (h/2)) + ',scrollbars='+sc+'');
}
/*
	* FIN POPUP
*/




function listCollection(familleId,sFamilleId,hightlightFamilleId,rewriteFamilleName,rewriteSFamilleName)
{    
    var data = $H({ 'CMD'                   : 'LIST_SFAMILLE_ET_COLLECTION',
                    'FAMILLE_ID'            : familleId,
                    'SFAMILLE_ID'           : sFamilleId,
                    'HIGHTLIGHTFAMILLEID'   : hightlightFamilleId,
                    'REWRITEFAMILLENAME'    : rewriteFamilleName,
                    'REWRITESFAMILLENAME'   : rewriteSFamilleName }).toQueryString();	          	                
      	                   
    var path = BaseUrl + '/Includes/Ajax/Ajax.aspx';
    var DoAjax = new Ajax.Updater( 'AjaxEnvoiReponse' , path, { postBody: data , evalScripts:true } );     
}

function setNavTri () {
	if ($('navTri')) {
		
		var show = $$('#AjaxEnvoiReponse .invisible');
		show = show[1] ?show[1] :show[0];
		
		var intervalClose = setInterval(function(){
			clearInterval(intervalClose);
		},100);
		
		Event.observe(show,'mouseover',function(){
			Element.setStyle(show,{ display:'block' });
			clearInterval(intervalClose);
		});
		
		Event.observe(show,'mouseout',function(){
			intervalClose = setInterval(function(){
				Element.setStyle(show,{ display:'none' });
				clearInterval(intervalClose);
			},100);
		});
		
		$$('#navTri li').each(function(o){
			
			Event.observe(o,'mouseover',function(){
				$$('#AjaxEnvoiReponse .invisible #navMarqueAll a').each(function(oi){
					
					var s = o.down().href.split('/');
					var folder = s[s.length-1].substring(0,s[s.length-1].length-5);					
					var ois = oi.href.split('/');
					
					if(ois[ois.length - 2] == folder) {
						Element.setStyle(oi.up(),{ display:'block' });
					}
					else {
						Element.setStyle(oi.up(),{ display:'none' });
					}
					
				});
				
				Element.setStyle(show,{ display:'block' });
				clearInterval(intervalClose);
			});
			
			Event.observe(o,'mouseout',function(){
				intervalClose = setInterval(function(){
					Element.setStyle(show,{ display:'none' });
					clearInterval(intervalClose);
				},100);
			});
			
		});
	}
}

Event.observe(window,'load',function(){
	setNavTri();
});