var mwidth =new Array(145,168,99,145);
var mtop_page = 0;
var mleft_page = 0;
var list_select=true;
var timeout;
var agt = navigator.userAgent.toLowerCase();
var isMac = (agt.indexOf('mac') != -1);
var isOpera = (agt.indexOf("opera") != -1);
var IEver = parseInt(agt.substring(agt.indexOf('msie ') + 5));
var isIE = ((agt.indexOf('msie')!=-1 && !isOpera && (agt.indexOf('webtv')==-1)) && !isMac);
var isIE5win = (isIE && IEver == 5);
var isIE5mac = ((agt.indexOf("msie") != -1) && isMac);
var blnOk=true;
var reg = new RegExp("px", "g");

window.onscroll = function()
{
	if (blnOk && scroll && (isIE || isIE5mac))
	{
		if (isIE5mac) document.getElementById("menucontainer").style.visibility="hidden";
		var sum=0;
		for(i=1;i<=nbmenu;i++)
		{
			if (!mvertical) {
				document.getElementById("menu"+i).style.top = document.body.scrollTop + mtop + "px";
				if (document.getElementById("ssmenu"+i))//undefined
					document.getElementById("ssmenu"+i).style.top = document.body.scrollTop + submtop + "px";
			} else {
				document.getElementById("menu"+i).style.top = document.body.scrollTop
							+(((i-1)*mspace)+sum+1+mtop)+"px";
				if (document.getElementById("ssmenu"+i))//undefined
					document.getElementById("ssmenu"+i).style.top = document.body.scrollTop
							+(((i-1)*mspace)+sum+1+mtop)+"px";
				sum += isFinite(mheight)?mheight:mheight[i-1];
			}
		}
		if (isIE5mac) document.getElementById("menucontainer").style.visibility="visible";
	}
}

function preLoadMenu()
{
	if (document.getElementById("menucontainer"))
	{
		document.getElementById("menucontainer").style.visibility="hidden";
		//IE5 mac has a bug : when a text is in an element with float attribute, it's not visible
		/*if (isIE5mac)
		{
			document.getElementById("menucontainer").style="";
		}*/
	}
}

function LoadMenu() {
	if (!blnOk) {
		if(document.body.style.backgroundColor!="") { blnOk=false; }
		if(document.body.style.color!="") { blnOk=false; }
		if(document.body.style.marginTop!="") { blnOk=false; }
		if(document.getElementById) {
			with(document.getElementById("menucontainer").style) {
				if(position!="" || top!="" || left!=""
						|| width!="" || height!="" || zIndex!=""
						|| margin!="" || visibility!="") {
					blnOk=false;
				}
			}
		}
		else{
			blnOk=false;
		}
	}

	if(blnOk)
	{
		document.getElementById("menucontainer").style.visibility="hidden";
		
		trimespaces();
		
		with(document.body.style) {
			if (!mvertical) marginTop=mtop_page+"px";
			else		   marginLeft=mleft_page+"px";
		}
		
		MenuPosition();
		HideMenu();
	}

	// as we stopped flashing, now we can make menu appear ;-)
	document.getElementById("menucontainer").style.visibility='';
}
window.onresize = LoadMenu;

/*
 * Place elements of menu correctly, on load, on scroll, on redimension of window
 */
function MenuPosition() {
	var window_width = (isIE?document.body.clientWidth:window.innerWidth);
	var window_height = (isIE?document.body.clientHeight:window.innerHeight);
	if (mcenter) {
		if (!mvertical) {
			var total_width = mspace * (nbmenu-1);
			if (isFinite(mwidth))
				total_width += mwidth * nbmenu;
			else {
				for (i = 1; i <= nbmenu; i++)
					total_width += mwidth[i-1];
			}
			mleft = (window_width - total_width)/2 -40;
		} else {
			var total_height = mspace * (nbmenu-1);
			if (isFinite(mheight))
				total_height += mheight * nbmenu;
			else {
				for (i = 1; i <= nbmenu; i++)
					total_height += mheight[i-1];
			}
			mtop = (window_height - total_height)/2;
		}
	}
	
	//Menus
	var sum = 0;
	for(i=1;i<=nbmenu;i++) {
		with(document.getElementById("menu"+i).style) {
			if (!mvertical) {
				top=mtop+"px";
				//left=(((i-1)*(mwidth+mspace))+1+mleft)+"px";
				left=(((i-1)*mspace)+sum+1+mleft)+"px";
			} else {
				//top=(((i-1)*(mheight+mspace))+1+mtop)+"px";
				top=(((i-1)*mspace)+sum+1+mtop)+"px";
				left=mleft+"px";
			}
			if (!scroll || isIE || isIE5mac)
				position="absolute";
			else position="fixed";
			//if (mvertical) height=mheight+"px";
			margin="0";
			zIndex="2";
			if (mvertical || isFinite(mwidth))
				width=mwidth+"px";
			else
				width=mwidth[i-1]+"px";
			if ((!mvertical && isFinite(mwidth)) || (mvertical && isFinite(mheight))) {
				sum += (!mvertical?mwidth:mheight);
			}
			else {
				sum += (!mvertical?mwidth[i-1]:mheight[i-1]);
				if (mvertical) height=mheight[i-1]+"px";
			}
		}
	}
	
	//Sub-menus
	sum = 0;
	for(i=1;i<=nbmenu;i++) {
		if (document.getElementById("ssmenu"+i))//undefined
		{
			with(document.getElementById("ssmenu"+i).style) {
				if (!scroll || isIE || isIE5mac)
					position="absolute";
				else position="fixed";
				if (!mvertical) {
					top=submtop+"px";
					//left=(((i-1)*(mwidth+mspace))+1+mleft)+"px";
					if(isIE || isIE5mac)left=(((i-1)*mspace)+sum+1+mleft)+"px";
					else left=(((i-1)*mspace)+sum+1+mleft)+"px";
				} else {
					left=submleft+"px";
					//top=(((i-1)*(mheight+mspace))+1+mtop)+"px";
					top=(((i-1)*mspace)+sum+1+mtop)+"px";
				}
				if (isIE || isOpera || isIE5mac || !auto_dim) {
					if (isFinite(submwidth))
						width = submwidth+(submwidth!="auto"?"px":"");
					else
						width = submwidth[i-1]+(submwidth[i-1]!="auto"?"px":"");
				}
				else width = submwidth[i-1] -40 + "px";//"auto";
				if (!mvertical && !isIE5mac) {
					//reposition if owerflowed to right
					if ((width != "auto")&&((left.replace(reg,'').valueOf()*1 + width.replace(reg,'').valueOf()*1) > window_width))
						left = (window_width-width.replace(reg,'').valueOf())+"px";
				}
				margin="0";
				zIndex="3";
			}
		}
		if ((!mvertical && isFinite(mwidth)) || (mvertical && isFinite(mheight))) {
			sum += (!mvertical?mwidth:mheight);
		}
		else {
			sum += (!mvertical?mwidth[i-1]:mheight[i-1]);
		}
	}
}


function ShowMenu(strMenu) {
	if(blnOk) {
		CancelHideMenu();
		HideMenu();
		if (document.getElementById(strMenu))//undefined
			with (document.getElementById(strMenu).style)
				visibility="visible";
	}
	SelectVisible("hidden",document.getElementsByTagName('select'));
}

function HideMenuDelay() {
	if (blnOk) {
		timeout = setTimeout('HideMenu()',mdelay);
	}
}
function CancelHideMenu() {
	if (blnOk && timeout) {
		clearTimeout(timeout);
	}
}
function HideMenu() {
	if(blnOk) {
		for(i=1;i<=nbmenu;i++) {
			if (document.getElementById("ssmenu"+i))//undefined
				with(document.getElementById("ssmenu"+i).style)
					visibility="hidden";
		}
	}
	SelectVisible("visible",document.getElementsByTagName('select'));
}

function trimespaces() {
	//Against a bug in IE5/win... it doesn't make css work fine for tags <li>, so we dismiss them !
	if(blnOk&&isIE5win) {
		for(i=1;i<=nbmenu;i++) {
			if (document.getElementById("ssmenu"+i))//undefined
				with(document.getElementById("ssmenu"+i))
					innerHTML = innerHTML.replace(/<LI>|<\/LI>/g,"");
		}
	}
}

function SelectVisible(v,elem) {
	if (blnOk && list_select && (isIE||isIE5win))
		for (var i=0;i<elem.length;i++) elem[i].style.visibility=v;
}
