// JavaScript Document
//fonction de création de popup en calque div
// string url : url de la page a ouvrir en popup
// int width : largeur du contenu de la popup
// int height : hauteur du contenu de la popup
// string css : feuille de style de l'exterieur de la popup

//v2.02clem
//Necessite lib JS : autoload/ prototype / kTrace

var zIndexKmzPopupParam=5000;
var nNbOfPopupParam=0;
var nNbOfPopupOpenParam=0;
function kmzPopupSimple(aParam){
	ongletGammeClose('close');
	//setParam
	
	this._sId=aParam['id'];	
	this._sUrl=aParam['url'];
	this._sUrl_distante=aParam['url_distante'];
	this._sParam=aParam['param'];
	this._nWidth=aParam['width'];
	this._nHeight=aParam['height'];		
	this._nTop=aParam['top'];
	this._nLeft=aParam['left'];
	this._TitleColor=aParam['titleColor'];
	this._bgTitleColor=aParam['bgTitleColor'];
	this._Title=aParam['title'];
	this._padding=aParam['padding'];
	this._nTailleOmbre=aParam['tailleOmbre'];
	this._bAllowDuplicate=aParam['allowDuplicate'];
	this._sHtml=aParam['HTML'];
	this._bLock=aParam['lock'];
	this._sParent=aParam['parent'];
	this._sIcone=aParam['icone'];
	this._onLoad=aParam['onLoad'];
	 //valeur par defaut 
	if(!this._sId)this._sId='_Popup_';	 
	if(!this._sUrl && !this._sHtml)this._sHtml=CONF_aMessage["chargementPopup"];
	if(!this._TitleColor)this._TitleColor='#ffffff';
	if(!this._bgTitleColor)this._bgTitleColor='#1b3f69';
	if(!this._Title)this._Title='Fen&ecirc;tre';
	if(!this._padding)this._padding=15;
	if(!this._nTailleOmbre)this._nTailleOmbre=16;
	if(!this._nHeight)this._nHeight="auto";
	if(this._nHeight=="auto"){this._nTailleOmbre=16};
	
	//Bloque la popup
	if(!this._bLock){this._bLock = true;}
	
	nNbOfPopupOpenParam++;
	
	//duplication
	if(this._bAllowDuplicate){
		nNbOfPopupParam++;
		this._sId=this._sId+nNbOfPopupParam;
	}
	
	//construction HTML modifiable
	var sHTMLBtnClose='<img src="img/popinfo/interf_20.gif" width="17" height="17"  onclick="kmzClosePopup({id:\''+this._sId+'\'});" style="cursor:pointer;">';
	var sStyleDrag="";
	var sHTMLBtnReduce='';
	if(!this._bLock){
		var sHTMLBtnReduce='<img src="img/reduce.gif" width="17" height="17" onclick="kmzReducePopup({id:\''+this._sId+'\'});" style="cursor:pointer;" id="'+this._sId+'KmzbtnReduce">&nbsp;&nbsp;';
		sStyleDrag="cursor:pointer";
	}
	
	var sHTMLHeader="";
	sHTMLHeader='<div style="background-color:#FFFFFF; position:absolute; height:28px; width:28px; top:18px; right:20px; "></div><div style="position:absolute;margin-top:5px;width:'+(this._nWidth+15)+'px;"><img src="img/popinfo/interf_20.gif" width="17" height="17"    onclick="kmzClosePopup({id:\''+this._sId+'\'});" style="cursor:pointer;margin-left:'+(this._nWidth+8)+'px;"></div>';
	var sHtmlHeight='';
	if(this._nHeight=="auto"){
		sHtmlHeight=";";		
		//sHtmlHeight=";overflow:auto;height:'+this._nHeight+'px;";
	}else{
		sHtmlHeight=';overflow:auto;height:'+this._nHeight+'px;';
	}
	var sHTMLBody='<div id="'+this._sId+'DivContent" ><table cellpadding="0" cellspacing="0" border="0" style="background-image:url(\'img/popinfo/fd.jpg\'); background-repeat:repeat-x; background-color:#ffffff'+sHtmlHeight+'border:10px solid #ffffff;" width="'+(this._nWidth+(this._padding*2)+2)+'"><tr><td width="'+this._padding+'">&nbsp;</td><td  width="'+(this._nWidth)+'" valign="top"><div style="display:block'+(sHtmlHeight)+'width:'+(this._nWidth+this._padding-10)+';" id="'+this._sId+'PopupContentHtml" ><br /><br /><br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'+CONF_aMessage["chargementPopup"]+'<br /><br /><br /><br /><br /><br /></div></td></tr></table></div>';
	
	var eBody=document.getElementsByTagName('body')[0];
	if(!document.getElementById('rootKMZPopup')){
		var eParentRoot = document.createElement("div");
		eParentRoot.id='rootKMZPopup';	
		eBody.appendChild(eParentRoot);
	}
	
	if(this._sParent){
		var eParent=$(this._sParent+"DivDragNDrop");
	}else{	
		var eParent=document.getElementById('rootKMZPopup');
	}
	
	if(!document.getElementById(this._sId+"DivParent")){
		new Insertion.After(eParent,"<div id=\""+this._sId+"DivParent\" style=\"\">&nbsp;</div>");
	}else{
		nNbOfPopupOpenParam--;
	}
	
	var sPopup='<span id="'+this._sId+'DivDragNDrop" style="'+sStyleDrag+';marign-top:0px;">'+sHTMLHeader+'</span>'+sHTMLBody;
	
	//on applique l'effet d'ombre
		//var nHauteurOmbre = this._nHeight+20;
		//<img  id="'+this._sId+'imgOmbreLeft" src="img/shadowLeftPopup.png" height="'+(nHauteurOmbre)+'" width="'+this._nTailleOmbre+'" class="png"/>
		//<img id="'+this._sId+'imgOmbreRight" src="img/shadowRightPopup.png" height="'+(nHauteurOmbre)+'" width="'+this._nTailleOmbre+'" class="png"/>
	$(this._sId+"DivParent").innerHTML='<div><table cellpadding="0" border="0" cellspacing="0" >'
		+'<tr><td><img src="img/shadowTopPopupAngleLeft.png" width="'+this._nTailleOmbre+'" height="'+this._nTailleOmbre+'" class="png"/></td>'
			+'<td><img src="img/shadowTopPopup.png" width="'+(this._nWidth+(this._padding*2)+2)+'" height="'+this._nTailleOmbre+'" class="png"/></td>'
			+'<td><img src="img/shadowTopPopupAngleRight.png" width="'+this._nTailleOmbre+'" height="'+this._nTailleOmbre+'" class="png"/></td></tr>'
		+'<tr><td style="background-image:url(img/shadowLeftPopup.png); background-repeat:repeat-y;"></td>'
		+'<td style="vertical-align:top">'
		+sPopup
		+'</td><td style="background-image:url(img/shadowRightPopup.png); background-repeat:repeat-y;"></td></tr>'
		+'<tr><td ><img src="img/shadowBottomPopupAngleLeft.png" width="'+this._nTailleOmbre+'" height="'+this._nTailleOmbre+'" class="png"/></td>'
			+'<td ><img src="img/shadowBottomPopup.png" width="'+(this._nWidth+(this._padding*2)+2)+'" height="'+this._nTailleOmbre+'" class="png"/></td>'
			+'<td style="height:'+this._nTailleOmbre+'px;"><img src="img/shadowBottomPopupAngleRight.png" width="'+this._nTailleOmbre+'" height="'+this._nTailleOmbre+'" class="png"/></td></tr></table></div>';
	
	if(!this._bLock){
		new Draggable(
			$(this._sId+"DivParent"),
			{
				handle:this._sId+"DivDragNDrop",
				snap:[30,15],
				starteffect:function(element){
					zIndexKmzPopupParam++;
					element.style.zIndex=zIndexKmzPopupParam;
				},
				endeffect:function(element){
					zIndexKmzPopupParam++;
					element.style.zIndex=zIndexKmzPopupParam;
					var sTop=element.style.top;
					var nTop=parseInt(sTop.replace('px',''));
					if(nTop<10){
						element.style.top="10px";
					}
					//var sLeft=element.style.left;var nLeft=parseInt(sLeft.replace('px',''));	
					//if(nLeft<10){element.style.left="10px";}
				}
			}
		);
	}
	
	var aPos=Position.realOffset(document.body);
	zIndexKmzPopupParam++;
	$(this._sId+"DivParent").style.display='block';
	//$(this._sId+"DivParent").appear({duration:0.5});
	$(this._sId+"DivParent").style.zIndex=zIndexKmzPopupParam;
	$(this._sId+"DivParent").style.position='absolute';
	if (window.innerWidth || window.innerHeight){ 
		docwidth = window.innerWidth; 
		docheight = window.innerHeight; 
	} 
	//IE Mozilla 
	if (document.body.clientWidth || document.body.clientHeight){ 
		docwidth = document.body.clientWidth; 
		docheight = document.body.clientHeight; 
	} 

	if(!this._sParent){		
		//$(this._sId+"DivParent").style.top=(aPos[1]+(25*nNbOfPopupOpenParam))+'px';
		//$(this._sId+"DivParent").style.left=(aPos[0]+((docwidth/2)-(this._nWidth/2))+25*nNbOfPopupOpenParam)+'px';
		$(this._sId+"DivParent").style.top=(aPos[1]+(60))+'px';
		$(this._sId+"DivParent").style.left=(aPos[0]+((docwidth/2)-(this._nWidth/2))-25)+'px';
	}
	
	if(this._nLeft){
		$(this._sId+"DivParent").style.left=(this._nLeft)+'px';		
	}
	if(this._nTop){
		$(this._sId+"DivParent").style.top=(this._nTop)+'px';
	}
	
	$(this._sId+"DivParent").style.verticalAlign='top';
	
	//google analytics
	var statUrl = this._sUrl.replace('aja/index.php?','');
	statUrl = this._sUrl.replace('&amp;','&');
	var aParamUrl=statUrl.split('&');
	var sPage = "";
	var sIdParam = "";
	var page="";
	for(var k=0;k<aParamUrl.length;k++){		
		if(aParamUrl[k].indexOf("page=")>-1){
			aPageParam=aParamUrl[k].split("=");
			sPage = aPageParam[1];
		}
		if(aParamUrl[k].indexOf("id=")>-1){
			aPageParam=aParamUrl[k].split("=");
			sIdParam = aPageParam[1];
		}
	}
	if(sPage){
		page="popup/"+sPage;
		if(sIdParam){
			page+='/';
			if(CONF_aProduits[sIdParam]){page+=CONF_aProduits[sIdParam];}
			page+='-'+sIdParam;
		}
		//alert('google : '+page);
		try {pageTracker._trackPageview(page);} catch(err){}
	}
	//--
	
	if(this._sUrl){		
		var ajax=new Ajax.Request(
			this._sUrl,
			{ 	
				asynchronous:true ,
				method:'get',
				parameters:this._sParam,
				onSuccess:	function(t){	
					var sChaine=t.responseText;
					$(this._sId+'PopupContentHtml').innerHTML=sChaine+"";
					//$(this._sId+'PopupContentHtml').style.overflow='visible';
					autoLoad({id:this._sId+'PopupContentHtml'});
					if(typeof this._onLoad == 'function'){
						var func=this._onLoad;
						func();
					}
					
				}

			}								
		);
	}
	if(this._sHtml){
		$(this._sId+'PopupContentHtml').innerHTML=this._sHtml;
	}
	if(	this._sUrl_distante){
		$(this._sId+'PopupContentHtml').innerHTML='<iframe frameborder="0" src="'+this._sUrl_distante+'" width="'+this._nWidth+'" height="'+this._nHeight+'"  />';
	}
}


// JavaScript Document
//fonction de fermeture de popup en calque div
// string id : id de la popup

var GkmzPopupTimerClose;
function kmzClosePopup(aParam){	
	//setParam
	this._sId=aParam['id'];	
	$(this._sId+"DivParent").remove();
	//$(this._sId+"DivParent").fade({duration:0.5});
	//GkmzPopupTimerClose=setTimeout("kmzRemovePopup('"+this._sId+"DivParent')",500);
	nNbOfPopupOpenParam--;
	
}

function kmzRemovePopup(aParam){	
	$(this._sId+"DivParent").remove();
}

function kmzReducePopup(aParam){
	//setParam
	this._sId=aParam['id'];
		
	if($(this._sId+"DivContent").style.display=='none'){
		$(this._sId+"DivContent").style.display='block';
		$(this._sId+'KmzbtnReduce').src='img/reduce.gif';
		//$(this._sId+"DivContent").blindDown({duration:0.5});
		var sHauteur=$(this._sId+'PopupContentHtml').style.height;
		nHeight=parseInt(sHauteur.replace('px',''));		
		$(this._sId+"imgOmbreLeft").height=nHeight+30+15+1;
		$(this._sId+"imgOmbreRight").height=nHeight+30+15+1;
	}else{
		//$(this._sId+"DivContent").blindUp({duration:0.5});
		$(this._sId+'KmzbtnReduce').src='img/restore.gif';
		$(this._sId+"DivContent").style.display='none';
		$(this._sId+"imgOmbreLeft").height=30;
		$(this._sId+"imgOmbreRight").height=30;
	}
}

/*
--------------------------------------------------------------------------------------------------------------------------
*/
getOpacity = function(element){
	if(element){
		var opacity=null;  
		//Get the opacity based on the current browser used
		var sUserAgent = navigator.userAgent;
		if(sUserAgent.indexOf("MSIE") != -1){
			filter = element.style.filter;
			if(filter){
				alpha = filter.split("alpha(opacity=");
				opacity = alpha[1].substr(0,(alpha[1].length-1))/100;
			}
		}else{
			opacity = element.style.opacity;
		}
	}else{
		var opacity=0;
	}
	return opacity;
}

setOpacity = function(eID, opacityLevel) {
	if(document.getElementById(eID)){
		var eStyle = document.getElementById(eID).style;
		eStyle.opacity = opacityLevel / 100;
		eStyle.filter = 'alpha(opacity='+opacityLevel+')';
	}
}



var aSimpleLoading = new Array();
function getMaskSimpleLoad(sId,width,height){
	var sReturn='<div id="'+sId+'LoadcontentHtml" style="height:100%;"><img src="img/loading-anim.gif" alt="" style="position:absolute; left:50%; top:50%;" /></div>';
	var aReturn = {sBody:sReturn};
	return aReturn;
}

function showSimpleLoading(aParam){
	var sId = aParam["id"];
	
	var div=document.createElement('div');
	div.setAttribute("id",sId+"-grise_loading");
	
	$(sId).appendChild(div);
	$(sId).insertBefore(div, $(sId).firstChild);
	
	var nW = parseInt($(sId).clientWidth);
	var nH = parseInt($(sId).clientHeight);
	
	var nTop = 0;
	//nTop = $(sId).style.top;
	//if(this.aPopup['param'][aParam["id"]]['offsetTop-gris']){
		//nTop = this.aPopup['param'][aParam["id"]]['offsetTop-gris'];
	//}
	
	var aReturned = getMaskSimpleLoad(sId,nW,nH);
	var sBody = aReturned['sBody'];
	
	$(sId+'-grise_loading').innerHTML             = sBody;
	//$(sId+'-grise_loading').style.top             = nTop+"px";
	$(sId+'-grise_loading').style.display         = 'none';
	$(sId+'-grise_loading').style.backgroundColor = '#000000';
	$(sId+'-grise_loading').style.position        = 'absolute';
	$(sId+'-grise_loading').style.width           = nW+"px";
	$(sId+'-grise_loading').style.height          = nH+"px";
	
	//$(aParam["id"]+'-grise_loading').style.zIndex          = this.zIndex + this.aPopup['pop'].length;
	
	var element = $(sId+'-grise_loading');
	
	var nOpacity=parseFloat(getOpacity(element));
	if(!nOpacity){
		setOpacity(element.id, 60);		
		nOpacity=0;
	}
	if(element.style.display=='none'){element.style.display='block';}
	
	if(!aSimpleLoading[sId]){aSimpleLoading[sId]=new Array();}
	if(aSimpleLoading[sId]['load-fading']){aSimpleLoading[sId]['load-fading'].cancel();}
	
	aSimpleLoading[sId]['load-appearing'] = new Effect.Opacity(
		element.id, 
		{ 
			duration: 0.5,
			transition: Effect.Transitions.linear,
			from: nOpacity, 
			to: 0.6
		}
	);
}
//  --
function hideSimpleLoading(aParam){
	var sId = aParam["id"];
	
	if(document.getElementById(sId+'-grise_loading')){
		var element = $(sId+'-grise_loading');
		
		var nOpacity=parseFloat(getOpacity(element));
		if(!nOpacity ){
			setOpacity(element.id, 100);
			nOpacity=0.6;
		}
		if(element.style.display=='none'){element.style.display='block';} 
		if(aSimpleLoading[sId]['load-appearing']){aSimpleLoading[sId]['load-appearing'].cancel();}
		aSimpleLoading[sId]['load-fading'] = new Effect.Opacity(
			element.id,
			{ 
				duration : 0.5,
				transition : Effect.Transitions.linear,
				from : nOpacity, 
				to : 0,
				afterFinish : function(){ $(sId).removeChild($(sId+'-grise_loading')); }
			}
		);
	}
}