var timer;

var mySlider = new fx.Width(document.getElementById('menu') , {duration: 500});
var myFastSlider = new fx.Width(document.getElementById('menu') , {duration: 100});
var opMenu = new fx.Opacity(document.getElementById('menuOut') , {
	duration: 300,
	onComplete: function()
  {
		document.getElementById('menuOut').style.display='none';
		document.getElementById('ariane').style.display='block';
  }
  });

var opMenu2 = new fx.Opacity(document.getElementById('menuOut') , {
	duration: 300,
	onComplete: function()
  {
		document.getElementById('menuOut').style.display='block';
		document.getElementById('ariane').style.display='none';
  }
  });

isDisplayed = 1;

function displayMenu() {
	
	if(typeof(timer) != "undefined") { clearInterval(timer); }

	document.getElementById('menuOut').style.display='block';
	document.getElementById('ariane').style.display='none';
	//opMenu.clearTimer();
	//if(typeof(opTimer) != "undefined") { clearInterval(opTimer); }
	//if(isDisplayed == 0) {
	//	opTimer = setTimeout("opMenu2.custom(0,1)", 100);
	//}
	//isDisplayed = 1;
	
}

function hideMenu() {
		
		if(typeof(timer) != "undefined") { clearInterval(timer); }


		/*document.getElementById('menu').style.width = '206px';*/
		var myloc = window.location.href;
		//alert(' on cache ');
		if((myloc != 'http://127.0.0.1/voreppe/index.php') && (myloc != 'http://127.0.0.1/voreppe/') && (myloc != 'http://voreppe.en-test.net/index.php') && (myloc != 'http://voreppe.en-test.net/') && (myloc != 'http://www.voreppe.fr/')&& (myloc != 'http://www.voreppe.fr/index.php')){
			//alert('Ici, c\'est votre message!\nSympa non ?');					
			document.getElementById('menuOut').style.display='none';
			document.getElementById('ariane').style.display='block'; 
		}
		
		//if(typeof(opTimer) != "undefined") { clearInterval(opTimer); }
		//opMenu2.clearTimer();
		//if(isDisplayed == 1) {
		//	opTimer = setTimeout("opMenu.custom(1,0)", 0);
		//}
		//isDisplayed = 0;
}


function openS(s) {
	
	mWidth = document.getElementById('menu').offsetWidth;
	if(typeof(slideTimer) != "undefined") {
		clearTimeout(slideTimer);
	}
	mySlider.clearTimer();
	
	if(document.getElementById('menu').offsetWidth > 206) {
		if(document.getElementById('menu').offsetWidth < 300) {
			myFastSlider.custom(document.getElementById('menu').offsetWidth, 206);
		} else {
			mySlider.custom(document.getElementById('menu').offsetWidth, 206);
		}
		
		for(i=0; i < 10; i++) {
			if(document.getElementById('s'+i) != null) {
				document.getElementById('s'+i).style.display = 'none';
			}
		}

		if(typeof(blockTimer) != "undefined") {
			clearTimeout(blockTimer);
		}
		blockTimer = setTimeout("document.getElementById('s"+s+"').style.display = 'block';", 510);
		
		if(document.getElementById('s'+s) != null) {
			slideTimer = setTimeout("mySlider.custom(206, 386)", 510);
		}
	} else {
		if(typeof(blockTimer) != "undefined") {
			clearTimeout(blockTimer);
		}
	for(i=0; i < 10; i++) {
		if(document.getElementById('s'+i) != null) {
			document.getElementById('s'+i).style.display = 'none';
		}
	}
		document.getElementById('s'+s).style.display = 'block';
		mySlider.custom(206, 386);
	}
}

function openSs(s, ss) {
	for(i=0; i < 10; i++) {
		if(document.getElementById('s'+s+'s'+i) != null) {
			document.getElementById('s'+s+'s'+i).style.display = 'none';
		}
	}

	if(document.getElementById('s'+s+'s'+ss) != null) {
		menuSlide(545);
		document.getElementById('s'+s+'s'+ss).style.display = 'block';
	} else {
		menuSlide(386); // ancinenne valeur = 366
	}
}

sTimer = 0;

function menuSlide(nWidth) {
	/* nWidth : new Width */
	/*document.getElementById('menu').style.width = nWidth+'px';*/
	/* oWidth : original width */
	oWidth = document.getElementById('menu').offsetWidth;
	clearInterval(sTimer);
	sTimer = setInterval("slider("+oWidth+", "+nWidth+")", 1);
}

function slider(oWidth, nWidth) {
	if(nWidth == 545) {
		ws = 386;
	} else if(nWidth == 386) {
		if(oWidth < nWidth) {
			ws = 206;
		} else {
			ws = 386;
		}
	} else {
		ws = 206;
	}

	oWidth = oWidth - ws;
	nWidth = nWidth - ws;
	/* cWidth : current Width */
	cWidth = document.getElementById('menu').offsetWidth;
	cWidth = cWidth - ws;
	//alert("cWidth:"+cWidth+", oWidth:"+oWidth+", nWidth:"+nWidth);
	if(oWidth > nWidth) {
		cWidth = cWidth - 2;
		document.getElementById('menu').style.width = (cWidth / 1.2 + ws)+'px';
		if(cWidth <= nWidth) {
			document.getElementById('menu').style.width = (nWidth + ws)+'px';
			clearInterval(sTimer);
		}
	} else if(oWidth < nWidth) {
		cWidth = cWidth + 2;
		document.getElementById('menu').style.width = (cWidth * 1.2 + ws)+'px';
		/*alert("cWidth:"+cWidth+", oWidth:"+oWidth+", nWidth:"+nWidth+", res:"+(cWidth * 1.1 + ws));*/
		if(cWidth >= nWidth) {
			document.getElementById('menu').style.width = (nWidth + ws)+'px';
			clearInterval(sTimer);
		}
	}
	if(document.getElementById('menu').offsetWidth < 206) {
		clearInterval(sTimer);
		document.getElementById('menu').style.width = '206px';
	}
}


/* HANDLERS */

window.onload = function(){
		
		$S('#ariane').action({
			onmouseout: function(){
				clearInterval(timer);
				hideMenu();
			},
			
			onmouseover: function(){
				//timer = setInterval("displayMenu()", 200);
			}
		});
		
		$S('#menu').action({
			onmouseover: function(){
				displayMenu();
			},
			
			onmouseout: function(){
				if(typeof(timer) != "undefined") { clearInterval(timer); }
				timer = setInterval("hideMenu()", 400);				
			}
		});

	
		$S('#mainMenu a.bLevel0').each(function(element){
			element.onmouseover = function(){
				displayMenu();
				myId = element.id;
				myId = myId.replace('m', '');
				//if(typeof(mmTimer) != "undefined") { clearTimeout(mmTimer); }
				//mmTimer = setTimeout("openS("+myId+")", 100);*/
				openS(myId);

			}
			element.onmouseout = function(){
				/*if(typeof(mTimer) != "undefined") {
					clearTimeout(mTimer);
					mmTimer = setTimeout("openS("+myId+")", 100);
				}*/
			}
		});

		$S('#mainMenu a.bLevel1').each(function(element){
			element.onmouseover = function(){
				displayMenu();
				myIds = element.id;
				s = myIds.charAt(1);
				b = myIds.charAt(3);
				openSs(s, b);
			}
		});

		
		$S('#bMenu').action({
			onmouseover: function(){
				timer = setInterval("displayMenu()", 300);
			},

			onclick: function() {
				clearInterval(timer);
				displayMenu();
			}
			
		});
		
		
		$S('#menuOut').action({
			onmouseout: function(){
				if(typeof(timer) != "undefined") { clearInterval(timer); }
				timer = setInterval("hideMenu()", 400);
			},
			
			onmouseover: function(){
				displayMenu();
			}
		});

}