/* iubito's menu - http://iubito.free.fr/prog/menu.php - configuration du javascript */
var vertical = false;
var center_menu = false;
var menu_width = 95;
var menu_height = 25;
var submenu_width = 150;
var submenu_auto_width = true;
var menu_spacer = 5;
var top_menu = 2;
var top_ssmenu = top_menu + 28;
var left_menu = 0;
var left_ssmenu = menu_width+2;
var delay = 500; // en milliseconde
var top_margin = top_menu + 40;
var left_margin = menu_width + 10;
var follow_scroll = true;
var hide_select = true;
var nbmenu = 0; //Auto-calculé
var timeout; //ne pas toucher, c'est pour déclarer la variable
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 isSafari = (agt.indexOf('safari') != -1);
var reg = new RegExp("px", "g");

window.onscroll = function()
{
	if ( follow_scroll && (isIE || isIE5mac) )
	{
		if (isIE5mac) document.getElementById("menuframe").style.visibility = "hidden";
		var cpt = 0;
		for ( var i = 1; i <= nbmenu; i++ )
		{
			var scrollTop = (document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
			if ( !vertical )
			{
				document.getElementById("menu"+i).style.top = scrollTop + top_menu + "px";
				if ( document.getElementById("ssmenu"+i) )//undefined
					document.getElementById("ssmenu"+i).style.top = scrollTop + top_ssmenu + "px";
			}
			else
			{
				document.getElementById("menu"+i).style.top = scrollTop + (((i-1)*menu_spacer) + cpt + 1 + top_menu) + "px";
				if ( document.getElementById("ssmenu"+i) )//undefined
					document.getElementById("ssmenu"+i).style.top = scrollTop + (((i-1)*menu_spacer) + cpt + 1 + top_menu) + "px";
				cpt += isFinite(menu_height) ? menu_height : menu_height[i-1];
			}
		}
		if (isIE5mac) document.getElementById("menuframe").style.visibility = "visible";
	}
}

function preLoad()
{
	if ( document.getElementById("menuframe") )
		document.getElementById("menuframe").style.visibility = "hidden";
}

function load()
{
	nbmenu = 0;
	while ( document.getElementById("menu"+(nbmenu+1)) )
		nbmenu++;
	document.getElementById("menuframe").style.visibility = "hidden";
	trimespaces();
	setPosition();
	hideMenus();
	if ( isSafari )
		document.getElementById('menuframe').style.fontSize = '10px';
	document.getElementById("menuframe").style.visibility = '';
}
window.onresize = load;

function setPosition()
{
	var window_width;
	if ( document.documentElement && document.documentElement.clientWidth )
	{
		window_width = document.documentElement.clientWidth;
	}
	else if ( document.body && document.body.clientWidth)
	{
		window_width = document.body.clientWidth;
	}
	else if ( window.innerWidth )
	{
		window_width = window.innerWidth;
	}

	var window_height;
	if ( document.documentElement && document.documentElement.clientHeight )
	{
		window_height = document.documentElement.clientHeight;
	}
	else if ( document.body && document.body.clientHeight )
	{
		window_height = document.body.clientHeight;
	}
	else if ( window.innerHeight )
	{
		window_height = window.innerHeight;
	}

	if ( center_menu )
	{
		if ( !vertical )
		{
			var total_width = menu_spacer * (nbmenu-1);
			if ( isFinite(menu_width) )
			{
				total_width += menu_width * nbmenu;
			}
			else
			{
				for (i = 1; i <= nbmenu; i++)
					total_width += menu_width[i-1];
			}
			left_menu = (window_width - total_width)/2;
		}
		else
		{
			var total_height = menu_spacer * (nbmenu-1);
			if (isFinite(menu_height))
			{
				total_height += menu_height * nbmenu;
			}
			else
			{
				for (i = 1; i <= nbmenu; i++)
					total_height += menu_height[i-1];
			}
			top_menu = (window_height - total_height)/2;
		}
	}
	var cpt = 0;
	for ( var i = 1; i <= nbmenu; i++ )
	{
		with ( document.getElementById("menu"+i).style )
		{
			if ( !vertical )
			{
				top = top_menu + "px";
				left = (((i-1)*menu_spacer) + cpt + 1 + left_menu) + "px";
			}
			else
			{
				top = (((i-1)*menu_spacer) + cpt + 1 + top_menu) + "px";
				left = left_menu + "px";
			}
			if ( !follow_scroll || isIE || isIE5mac)
				position = "absolute";
			else
				position = "fixed";
			margin = "0";
			zIndex = "2";
			if ( vertical || isFinite(menu_width) )
				width = menu_width + "px";
			else
				width = menu_width[i-1]+"px";
			if ( (!vertical && isFinite(menu_width)) || (vertical && isFinite(menu_height)) )
			{
				cpt += (!vertical ? menu_width : menu_height);
			}
			else
			{
				cpt += (!vertical ? menu_width[i-1] : menu_height[i-1]);
				if ( vertical ) height = menu_height[i-1]+"px";
			}
		}
	}
	cpt = 0;
	for( i = 1; i <= nbmenu; i++ )
	{
		if ( document.getElementById("ssmenu"+i) )//undefined
		{
			with ( document.getElementById("ssmenu"+i).style )
			{
				if ( !follow_scroll || isIE || isIE5mac )
					position = "absolute";
				else
					position = "fixed";
				if ( !vertical )
				{
					top = top_ssmenu + "px";
					left = (((i-1)*menu_spacer) + cpt + 1 + left_menu) + "px";
				}
				else
				{
					left = left_ssmenu + "px";
					top = (((i-1)*menu_spacer) + cpt + 1 + top_menu) + "px";
				}
				if ( isIE || isOpera || isIE5mac || !submenu_auto_width )
				{
					if ( isFinite(submenu_width) )
						width = submenu_width + (submenu_width != "auto" ? "px" : "");
					else
						width = submenu_width[i-1] + (submenu_width[i-1] != "auto" ? "px" : "");
				}
				else
				{
					width = "auto";
				}
				if ( !vertical && !isIE5mac )
				{
					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 ( (!vertical && isFinite(menu_width)) || (vertical && isFinite(menu_height)) )
		{
			cpt += ( !vertical ? menu_width : menu_height);
		}
		else
		{
			cpt += ( !vertical ? menu_width[i-1] : menu_height[i-1]);
		}
	}
}
function showMenu(strMenu)
{
	cancelHide();
	hideMenus();
	if ( document.getElementById(strMenu) ) //undefined
		with ( document.getElementById(strMenu).style )
			visibility = "visible";
	selectVisible("hidden", document.getElementsByTagName('select'));
}

function hideDelay()
{
	timeout = setTimeout('hideMenus()', delay);
}
function cancelHide()
{
	if ( timeout )
		clearTimeout(timeout);
}
function hideMenus()
{
	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() {
	if ( 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 ( hide_select && (isIE||isIE5win) )
		for (var i=0;i<elem.length;i++) elem[i].style.visibility = v;
}
