/* iubito's menu - http://iubito.free.fr/prog/menu.php - javascript configuration */


/* true = to make menu vertical, on left.
   false = to make menu horizontal, on top */
// var vertical = false;

/* VERY IMPORTANT !
It's needed to assign in this variable number of menus, javascript code is not
capable of calculating itself :-p So if your html code goes until <p id="menu5"...> you have to assign 5. */
/* VERY IMPORTANT 2 by Bogac
I disabled this variable because my asp .net code will calculate number of menus and
generate required javascript code with this variable there */
// var nbmenu = 3;



/* Center the menu ? (true/false).
	Center vertically or horizontaly; according to chosen layout */
// var centrer_menu = false;

/* It's needed to define width for manus.
	To make them in different widths :
	var largeur_menu = new Array(largeur menu1, largeur menu2, largeur menu3...)
	You have to assign in that array as much values as number of menus !
	Attention, if you are in vertical mode, use fixed width (No Array) !
	*/
// var largeur_menu = 95;

/* In vertical mode, we've got to know height of each menu.
	Even if "boxes" are not measured in height. (Bogac's note: just translated, didn't really get)
	Add this variable where menus are too near each other in vertical mode.
	To make them in different heights:
	var hauteur_menu = new Array(hauteur menu1, hauteur menu2, hauteur menu3...)
	You have to assign in that array as much values as number of menus !
	Attention, if you are in horizontal mode, use fixed width (No Array) !
	*/
// var hauteur_menu = 25;

/* In horizontal mode.
	Width of sub-menus, for IE only, other browsers respect auto-width
	Use "auto" only if you are sure to have used &nbsp; instead of spaces in items.
	To make sub-menus in different widths :
	var largeur_sous_menu = new Array(largeur1, largeur2...).
	You have to assign in that array as much values as number of menus !
	If a menu doesn't have a sub-menu, still need to assign something !
	It's possible to use "auto" for some colums respecting the rule above.
	*/
var largeur_sous_menu = 150;

/* For browsers with automatic width capability (addapted by content), this
	option (active by default) let us get an automatic width. Other case
	(false), sub-menus will have value of largeur_sous_menu. */
var largeur_auto_ssmenu = true;

/* ... to get some space between menus ! */
var espace_entre_menus = 1;


/* position of menu by top of page
	0 = menu at very top. in px */
var top_menu = 5;
/* In horizontal mode.
	position of submenus by top of screen or page. You need to consider
	top of menus, so not to put 0, which will cause submenu on main menu. in px */
var top_ssmenu = top_menu + 20;

/* Position by left of screen, in px. */
var left_menu = 10;
/* In vertical mode.
	Position of submenus by left of screen, in px */
var left_ssmenu = largeur_menu+6;

/* Users might feel uncomfortable if submenu vanishes instantly, also bad for users with small screen like Palms,
	We can have a delay before submenu dissapears.
	500 ms is fine :-) */
var delai = 650; // in miliseconds

/* In horizontal mode.
	As it is possibe for page's content superposed by menu content it's a good idea to
	lower page contents (or adding some top margin for page content)
	about forty pixels would do the job. in px*/
var marge_en_haut_de_page = 0;
/* In vertical mode.
	We move document to right so that its content doesn't get superposed by menu */
var marge_a_gauche_de_la_page = largeur_menu + 10;


/* Put "true" if you want menu to be always visible.
	Put "false" if you don't want so. In that case when you move the page lower
	menu will disappear	*/
// var suivre_le_scroll=true;

/* Only for IE, <select> tags always get above of menu, so by default we hide dropdown list
	when menu is open, then we make them appear when menu is closed
	To avoid that use "false" */
var cacher_les_select=true;


var timeout; //don't touch it, this is to declare the 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 blnOk=true;
//to erase "px"s when calculating
var reg = new RegExp("px", "g");

// onScroll for Internet Explorer, position:fixed does same job in other browsers
// which are bount to CSS norms...
window.onscroll = function()
{
	if (blnOk && suivre_le_scroll && (isIE || isIE5mac))
	{
		if (isIE5mac) document.getElementById("conteneurmenu").style.visibility="hidden";
		var cumul=0;
		for(i=1;i<=nbmenu;i++)
		{
			if (!vertical) {
				document.getElementById("menu"+i).style.top = document.body.scrollTop + top_menu + "px";
				if (document.getElementById("ssmenu"+i))//undefined
					document.getElementById("ssmenu"+i).style.top = document.body.scrollTop + top_ssmenu + "px";
			} else {
				document.getElementById("menu"+i).style.top = document.body.scrollTop
							+(((i-1)*espace_entre_menus)+cumul+1+top_menu)+"px";
				if (document.getElementById("ssmenu"+i))//undefined
					document.getElementById("ssmenu"+i).style.top = document.body.scrollTop
							+(((i-1)*espace_entre_menus)+cumul+1+top_menu)+"px";
				cumul += isFinite(hauteur_menu)?hauteur_menu:hauteur_menu[i-1];
			}
		}
		if (isIE5mac) document.getElementById("conteneurmenu").style.visibility="visible";
	}
}

function preChargement()
{
	if (document.getElementById("conteneurmenu"))
	{
		document.getElementById("conteneurmenu").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("conteneurmenu").style="";
		}*/
	}
}

function Chargement() {
	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("conteneurmenu").style) {
				if(position!="" || top!="" || left!=""
						|| width!="" || height!="" || zIndex!=""
						|| margin!="" || visibility!="") {
					blnOk=false;
				}
			}
		}
		else{
			blnOk=false;
		}
	}

	if(blnOk)
	{
		document.getElementById("conteneurmenu").style.visibility="hidden";
		
		trimespaces();
		
		with(document.body.style) {
			if (!vertical) marginTop=marge_en_haut_de_page+"px";
			else		   marginLeft=marge_a_gauche_de_la_page+"px";
		}
		
		positionne();
		CacherMenus();
	}

	// as we stopped flashing, now we can make menu appear ;-)
	document.getElementById("conteneurmenu").style.visibility='';
}
window.onresize = Chargement;

/*
 * Place elements of menu correctly, on load, on scroll, on redimension
 * of window
 */
function positionne() {
	var largeur_fenetre = (isIE?document.body.clientWidth:window.innerWidth);
	var hauteur_fenetre = (isIE?document.body.clientHeight:window.innerHeight);
	if (centrer_menu) {
		if (!vertical) {
			var largeur_totale = espace_entre_menus * (nbmenu-1);
			if (isFinite(largeur_menu))
				largeur_totale += largeur_menu * nbmenu;
			else {
				for (i = 1; i <= nbmenu; i++)
					largeur_totale += largeur_menu[i-1];
			}
			left_menu = (largeur_fenetre - largeur_totale)/2;
		} else {
			var hauteur_totale = espace_entre_menus * (nbmenu-1);
			if (isFinite(hauteur_menu))
				hauteur_totale += hauteur_menu * nbmenu;
			else {
				for (i = 1; i <= nbmenu; i++)
					hauteur_totale += hauteur_menu[i-1];
			}
			top_menu = (hauteur_fenetre - hauteur_totale)/2;
		}
	}
	
	//Menus
	var cumul = 0;
	for(i=1;i<=nbmenu;i++) {
		with(document.getElementById("menu"+i).style) {
			if (!vertical) {
				top=top_menu+"px";
				//left=(((i-1)*(largeur_menu+espace_entre_menus))+1+left_menu)+"px";
				left=(((i-1)*espace_entre_menus)+cumul+1+left_menu)+"px";
			} else {
				//top=(((i-1)*(hauteur_menu+espace_entre_menus))+1+top_menu)+"px";
				top=(((i-1)*espace_entre_menus)+cumul+1+top_menu)+"px";
				left=left_menu+"px";
			}
			if (!suivre_le_scroll || isIE || isIE5mac)
				position="absolute";
			else position="fixed";
			//if (vertical) height=hauteur_menu+"px";
			margin="0";
			zIndex="2";
			if (vertical || isFinite(largeur_menu))
				width=largeur_menu+"px";
			else
				width=largeur_menu[i-1]+"px";
			if ((!vertical && isFinite(largeur_menu)) || (vertical && isFinite(hauteur_menu))) {
				cumul += (!vertical?largeur_menu:hauteur_menu);
			}
			else {
				cumul += (!vertical?largeur_menu[i-1]:hauteur_menu[i-1]);
				if (vertical) height=hauteur_menu[i-1]+"px";
			}
		}
	}
	
	//Sub-menus
	cumul = 0;
	for(i=1;i<=nbmenu;i++) {
		if (document.getElementById("ssmenu"+i))//undefined
		{
			with(document.getElementById("ssmenu"+i).style) {
				if (!suivre_le_scroll || isIE || isIE5mac)
					position="absolute";
				else position="fixed";
				if (!vertical) {
					top=top_ssmenu+"px";
					//left=(((i-1)*(largeur_menu+espace_entre_menus))+1+left_menu)+"px";
					left=(((i-1)*espace_entre_menus)+cumul+1+left_menu)+"px";
				} else {
					left=left_ssmenu+"px";
					//top=(((i-1)*(hauteur_menu+espace_entre_menus))+1+top_menu)+"px";
					top=(((i-1)*espace_entre_menus)+cumul+1+top_menu)+"px";
				}
				if (isIE || isOpera || isIE5mac || !largeur_auto_ssmenu) {
					if (isFinite(largeur_sous_menu))
						width = largeur_sous_menu+(largeur_sous_menu!="auto"?"px":"");
					else
						width = largeur_sous_menu[i-1]+(largeur_sous_menu[i-1]!="auto"?"px":"");
				}
				else width = "auto";
				if (!vertical && !isIE5mac) {
					//reposition if owerflowed to right
					if ((width != "auto")
						&& ((left.replace(reg,'').valueOf()*1 + width.replace(reg,'').valueOf()*1) > largeur_fenetre))
						left = (largeur_fenetre-width.replace(reg,'').valueOf())+"px";
				}
				margin="0";
				zIndex="3";
			}
		}
		if ((!vertical && isFinite(largeur_menu)) || (vertical && isFinite(hauteur_menu))) {
			cumul += (!vertical?largeur_menu:hauteur_menu);
		}
		else {
			cumul += (!vertical?largeur_menu[i-1]:hauteur_menu[i-1]);
		}
	}
}


function MontrerMenu(strMenu) {
	if(blnOk) {
		AnnulerCacher();
		CacherMenus();
		if (document.getElementById(strMenu))//undefined
			with (document.getElementById(strMenu).style)
				visibility="visible";
	}
	/*SelectVisible("hidden",document.getElementsByTagName('select'));*/
}

function CacherDelai() {
	if (blnOk) {
		timeout = setTimeout('CacherMenus()',delai);
	}
}
function AnnulerCacher() {
	if (blnOk && timeout) {
		clearTimeout(timeout);
	}
}
function CacherMenus() {
	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 && cacher_les_select && (isIE||isIE5win))
		for (var i=0;i<elem.length;i++) elem[i].style.visibility=v;
}

