var mfe={};
mfe.nav={};

mfe.nH=[27,48,68]; /*hauteurs pour les 3 niveaux de nav*/

mfe.resetNav=function(){

	var iniSel=$c("li","selected",$i("nav"));
	
	if(iniSel.length==0){
		iniSel=[$t("li",$i("nav"))[0]];
	}
	
	var Lvl = 1;
	if(iniSel[0]==mfe.nav.actuSel[0]){
		Lvl=2;
		if(iniSel[1]==mfe.nav.actuSel[1]){
			Lvl=3;
		};
	};

	CEDjs.Timer.displace($i("navL1"),"height",$i("navL1").offsetHeight,-5,Lvl==1?mfe.nH[0]:Lvl==2?mfe.nH[1]:mfe.nH[2],function(){
		for(var i=0;i<mfe.nav.L3.length;i++)mfe.nav.L3[i].style.display="none";
		for(var i=0;i<mfe.nav.L2.length;i++)mfe.nav.L2[i].style.display="none";
		for(var i=0;i<iniSel.length;i++){
			var ulLcl=$t1L("ul",iniSel[i])[0];
			if(ulLcl)ulLcl.style.display="block";
		}
		
		var trgHeight;
		if (iniSel.length == 1) {
			trgHeight = mfe.nH[$t1L("ul",iniSel[0])[0]?1:0];
		}else if (iniSel.length == 2) {
			trgHeight = mfe.nH[$t1L("ul",iniSel[1])[0]?2:1];
		}else if (iniSel.length == 3) {
			trgHeight = mfe.nH[2];
		}
		
		CEDjs.Timer.displace($i("navL1"),"height",$i("navL1").offsetHeight,5,trgHeight);

		if(trgHeight==mfe.nH[0]){
			CEDjs.Timer.displace($i("nav"),"paddingBottom",parseInt($i("nav").style.paddingBottom),3,11);
		}else{
			CEDjs.Timer.displace($i("nav"),"paddingBottom",parseInt($i("nav").style.paddingBottom),-3,0);
		}
	});

	for(var i=0;i<mfe.nav.a.length;i++){
		mfe.nav.a[i].style.opacity="1";
		mfe.nav.a[i].style.filter="Alpha(opacity=100)";
	}
	
	mfe.navL1ActuOver = null;
	mfe.nav.actuSel=$c("li","selected",$i("nav"));

};

mfe.expandNav=function(toLevel,o){
	if(mfe.navL1ActuOver != o){
		mfe.navL1ActuOver = o;
		CEDjs.Timer.displace($i("navL1"),"height",$i("navL1").offsetHeight,-5,toLevel==1?mfe.nH[0]:toLevel==2?mfe.nH[1]:mfe.nH[2],function(){
			if($t("ul",o)[0]){
				
				var ulLst=[];
				if(toLevel<3) for(var i=0;i<mfe.nav.L3.length;i++)mfe.nav.L3[i].style.display="none";
				if(toLevel<2) for(var i=0;i<mfe.nav.L2.length;i++)mfe.nav.L2[i].style.display="none";
				
				$t("ul",o)[0].style.display="block";
				CEDjs.Timer.displace($i("nav"),"paddingBottom",parseInt($i("nav").style.paddingBottom),-3,0);
				CEDjs.Timer.displace($i("navL1"),"height",$i("navL1").offsetHeight,5,toLevel==1?mfe.nH[1]:mfe.nH[2]);
			}else if(toLevel==1){
				CEDjs.Timer.displace($i("nav"),"paddingBottom",parseInt($i("nav").style.paddingBottom),3,11);
			};
		});
	};
};

mfe.initNav = function(){
	var n = $i("nav");
	if(!n){
		setTimeout(mfe.initNav,50);
		return false;
	}
			
	mfe.nav.actuSel=$c("li","selected",$i("nav"));

	mfe.nav.L2 = $c("ul","L2_",$i("navL1"));
	mfe.nav.L3 = $c("ul","L3_",$i("navL1"));
	mfe.nav.a = $t("a",n);
	
	var aLst = mfe.nav.a;
	for(var i=0;i<aLst.length;i++){
		var t=aLst[i].innerHTML;
		aLst[i].innerHTML = '<span class="firstChar">'+t.charAt(0)+'</span>'+t.substr(1,t.length);
		aLst[i].lvl = aLst[i].className.indexOf("L1_")!=-1?1:aLst[i].className.indexOf("L2_")!=-1?2:aLst[i].className.indexOf("L3_")!=-1?3:0; 
		aLst[i].onmouseover=function(){
			mfe.nav.actuSel[this.lvl-1]=this.parentNode;
			for(var i=this.lvl;i<mfe.nav.actuSel.length;i++)mfe.nav.actuSel[i]=null;
			mfe.expandNav(this.lvl,this.parentNode);

			for(var i=0;i<mfe.nav.a.length;i++){
				mfe.nav.a[i].style.opacity="0.5";
				mfe.nav.a[i].style.filter="Alpha(opacity=50)";
			}
			for(var i=0;i<mfe.nav.actuSel.length;i++){
				if(mfe.nav.actuSel[i]){
					$t1L("a",mfe.nav.actuSel[i])[0].style.opacity="1";
					$t1L("a",mfe.nav.actuSel[i])[0].style.filter="Alpha(opacity=100)";
				}
			}
		}; 
	};
	
	n.onmouseover=function(){
		mfe.nav.isover=true;
		clearTimeout(mfe.nav.navTout);
	};
	n.onmouseout=function(){
		mfe.nav.isover=false;
		mfe.nav.navTout = setTimeout(function(){
			if(!mfe.nav.isover)mfe.resetNav();
		},500);
	};


};

mfe.resetBgCnt=function(){
	$i("contentBg").style.width=$i("cBR").offsetLeft+$i("cBR").offsetWidth+"px";
	$i("contentBg").style.height=$i("cBR").offsetTop+$i("cBR").offsetHeight+"px";
	
	if(CEDjs.IE6){$i("contentBg").style.zIndex=-1;}
};

mfe.initH1=function(){
	var aLst=$t("h1");
	for(var i=0;i<aLst.length;i++){
		var t=aLst[i].innerHTML;
		var frstChar = 0;
		while(frstChar<t.length && !t.charAt(frstChar).match(new RegExp("[A-Za-z0-9]")))frstChar++;
		if(t.charAt(frstChar).match(new RegExp("[0-9]")))continue;
		aLst[i].innerHTML = '<span class="firstChar">'+t.charAt(frstChar)+'</span>'+t.substr(frstChar+1,t.length);
	};
};

mfe.initExpandBlocks=function(){
	var b=$c("div","expandBlocks");
	for(var k=0;k<b.length;k++){
		var bD=$t1L("div",b[k]);
		for(var i=0;i<bD.length;i++){
			var fctTmp = function(){
				this.parentNode.className=this.parentNode.className=="expand"?"":"expand";
				mfe.resetBgCnt();
			};
			$t1L("h2",bD[i])[0].onclick=fctTmp;
			$t1L("h2",bD[i])[0].style.cursor="pointer";
			$c("div","collapsed",bD[i])[0].onclick=fctTmp;
			$c("div","collapsed",bD[i])[0].style.cursor="pointer";
		};
	};
};

mfe.initHome=function(){
	if($i("boxesHome")){
		var ul=$t("ul",$i("boxesHome"));
		for(var k=0;k<ul.length;k++){
			ul[k].li=$t1L("li",ul[k]);
			ul[k].index=-2;
			if(ul[k].li.length>1){
				for(var i=0;i<ul[k].li.length;i++){
					ul[k].li[i].style.display="none";				
				};
				ul[k].k=k;
				ul[k].sShow=function(){
					var lclThis=this;
					if(this.index++==-2){setTimeout(function(){lclThis.sShow();},this.k*300);}
					else{
						if(this.index>=this.li.length)this.index=0;
						CEDjs.Timer.appear(this.li[this.index],5,0,100,function(){
							setTimeout(function(){
								CEDjs.Timer.appear(lclThis.li[lclThis.index],-5,100,0,function(){lclThis.sShow();});
							},4000);
						});
					};
				};
				ul[k].sShow();
			};
		};
	};
};

mfe.initVideoBox=function(){
	var vb=$c("div","videoBox");
	for(var i=0;i<vb.length;i++){
		vb[i].style.cursor="pointer";
		vb[i].onclick=function(){
			this.onclick=null;
			this.style.cursor="default";
			this.swfCnt=$c("div","movie",this)[0];
			this.swf=$t("object",this.swfCnt)[0];
			if(this.swf){
				this.trgH=parseInt(this.swf.height);
			}else{
				this.trgH=this.offsetHeight;
			}

			var lclThis=this;
			CEDjs.Timer.appear($c("div","starter",this)[0],-10,100,0,function(){
				CEDjs.Timer.displace(lclThis,"height",lclThis.offsetHeight,15,lclThis.trgH,function(){
					lclThis.swfCnt.style.display="block";
					if(lclThis.swf)lclThis.swf.runVideo();
					mfe.resetBgCnt();
				},mfe.resetBgCnt);
			});
		};
	}
};

mfe.initExpandCollapse=function(){
	var b=$c("div","expandCollapse");
	for(var k=0;k<b.length;k++){
		if(b[k].className.indexOf("protected")==-1){
			var t=$c("div","title",b[k])[0];
			t.style.cursor="pointer";
			t.onmouseover=function(){this.style.color="#FA5F09"};
			t.onmouseout=function(){this.style.color=""};
			t.onclick = function(){
				if(this.parentNode.className.indexOf(" expand") == -1){
					this.parentNode.className+=" expand";
				}else{
					this.parentNode.className=this.parentNode.className.replace(/ expand/g,"");
				};
				mfe.resetBgCnt();
			};
		};
	};
};


mfe.processContact=function(){

	var mF = $i('mainForm');
	var inp=$c("input","mandat",mF);
	var txa=$c("textarea","mandat",mF);
	var sel=$c("select","mandat",mF);
	
	var isOK=true;
	
	for(var i=0;i<inp.length;i++){
		var tmpBool=inp[i].value!="";
		for(var o=inp[i].parentNode;o && o!=mF;o=o.parentNode){
			if(o.style.display=="none"){
				tmpBool=true;
				break;
			}
		}
		
		/*highlight*/if(!tmpBool){inp[i].parentNode.style.borderRight="solid 5px #fa5f09";}else{inp[i].parentNode.style.borderRight="";}
		
		isOK = isOK && tmpBool;
	}
	
	for(var i=0;i<txa.length;i++){
		var tmpBool=txa[i].value!="";
		for(var o=txa[i].parentNode;o && o!=mF;o=o.parentNode){
			if(o.style.display=="none"){
				tmpBool=true;
				break;
			}
		}

		/*highlight*/if(!tmpBool){txa[i].parentNode.style.borderRight="solid 5px #fa5f09";}else{txa[i].parentNode.style.borderRight="";}

		isOK = isOK && tmpBool;
	}

	for(var i=0;i<sel.length;i++){
		var selLcl=sel[i];
		
		var tmpBool=true;
		if(selLcl.selectedIndex>-1){
			tmpBool = tmpBool && selLcl.selectedIndex && selLcl[selLcl.selectedIndex] && selLcl[selLcl.selectedIndex].value!="-";
		}
		for(var o=sel[i].parentNode;o && o!=mF;o=o.parentNode){
			if(o.style.display=="none"){
				tmpBool=true;
				break;
			}
		}

		/*highlight*/if(!tmpBool){sel[i].parentNode.style.borderRight="solid 5px #fa5f09";}else{sel[i].parentNode.style.borderRight="";}

		isOK = isOK && tmpBool;
	}
	
	if (isOK) {
		$i('mainForm').submit();		
	}else{
		if(lg=="fr"){
			alert("Veuillez remplir tous les champs obligatoire marqués d'une *");
		}else if(lg=="en"){
			alert("Please fill all mandatory fields marked with *")
		}
		
	}

};

mfe.resetTopAnchor=function(){
	var t = $i("gotoTop");
	var sT = CEDjs.getBody().scrollTop;
	if (document.documentElement.scrollTop && document.documentElement.scrollTop > sT) {
		sT = document.documentElement.scrollTop;
	}

	if (document.documentElement.clientHeight < document.documentElement.scrollHeight && sT>0 && document.documentElement.scrollHeight>800) {
		var reset=false;
		if(t.style.display != "block"){
			t.style.display = "block";
			reset=true;
		}
		var trg = sT + document.documentElement.clientHeight - (t.offsetParent.offsetTop + 25);
		if (trg > t.offsetParent.offsetHeight - 25) {
			trg = (t.offsetParent.offsetHeight - 25);
		}
		
		if(reset){
			t.style.top = trg + "px";
		}else{
			CEDjs.Timer.displace(t,"top",t.offsetTop,30,trg);
		}
			
	}else{
		t.style.display = "none";
	}
};


// -----------------------------------------------------------------------------
// *** Flash Detection ***
// Globals
// Major version of Flash required
var requiredMajorVersion = 9;
// Minor version of Flash required
var requiredMinorVersion = 0;
// Minor version of Flash required
var requiredRevision = 0;
// -----------------------------------------------------------------------------

mfe.getFlash=function(){
	var WR="<div style='padding:15px;text-align:center;'>"
	if(lg=="fr"){
		WR+="Ce site requiert une version plus récente de Adobe Flash Player.";
	}else if(lg=="en"){
		WR+="This site requires a newer version of Adobe Flash Player.";
	}else if(lg=="es"){
		WR+="Este sitio requiere una versión más reciente de Adobe Flash Player.";
	}
	WR+='<br/><br/><a href="http://get.adobe.com/flashplayer/" target="_blank"><img src="'+rootPath+'img/get_flashplayer.gif" alt="Get flash player"/></a></div>';
	return WR;
}

mfe.init=function(){
	if(mfe.isInit)return true;
	mfe.isInit=true;
	mfe.initH1();
	mfe.resetBgCnt();
	mfe.initExpandBlocks();
	mfe.initHome();
	mfe.initVideoBox();
	mfe.initExpandCollapse();
	mfe.resetTopAnchor();
	
	if(CEDjs.IE6){
		CEDjs.IE6pngFix($i("CBR"));
		CEDjs.IE6pngFix($i("CTL"));
	}
};

mfe.initNav();

CEDjs.addOnloadEvent(function(){
	mfe.init();
});

CEDjs.addOnscrollEvent(mfe.resetTopAnchor);
CEDjs.addOnresizeEvent(mfe.resetTopAnchor);
