// check browser type and set n or ie variables for later processing

if (document.all)    						{n=0;ie=1;fShow="visible";fHide="hidden";} // ie
if (document.layers) 						{n=1;ie=0;fShow="show";   fHide="hide";} // netscape 4
if (document.getElementById&&!document.all) {n=1;ie=0;fShow="show";   fHide="hide";} // netscape 6

// This function build the code to display the main menu navigation bar

function Menu()
{

// this sets the background color of the menu bar and the dropdown menu

  	this.bgColor          = "#54AAED"; // background color for main menu bar
        this.subMenubgColor   = "#ffbb33"; // background color for submenu

	if (ie) this.menuFont = "bold x-small Arial";
	if (n)  this.menuFont = "bold x-small Arial";

//	this.fontColor   = "#FFFFFF"; // not applicable in this script
	this.addItem    = addItem;
	this.addSubItem = addSubItem;
	this.showMenu   = showMenu;
	this.mainPaneBorder = 0; // border width of menu
	this.subMenuPaneBorder = 0; // border width of submenu

	this.subMenuPaneWidth = 150; // width of submenu

	lastMenu = null;
	
	rightY = 0;
	rightX = 0;
	leftY = 0;
	leftX = 0;

// HTMLstr contains the html built code passed to function showmenu() for display

	HTMLstr = "";
	HTMLstr += "<!-- MENU PANE DECLARATION BEGINS -->\n";
	HTMLstr += "\n";

//      width=document.body.clientWidth to solve an auto resize overflow in navigation bar
//      onresize must be set in body of every html page using the navigation bar

	if (ie) HTMLstr += "<div id='MainTable' style='z-index:100; position:absolute; top=0; left=0; width=" + document.body.clientWidth + "'>\n";
//	if (n)  HTMLstr += "<layer name='MainTable'>\n";

//	onmouseover the main menu bar, we hide flash0 layer if any
//	onmouseout  the main menu bar, we show flash0 layer if any

	HTMLstr += "<table width='100%' bgcolor='"+this.bgColor+"' border='"+this.mainPaneBorder+"' onmouseover=\"if (document.all.flash0){document.all.flash0.style.visibility='hidden'};\" onmouseout=\"if (document.all.flash0){document.all.flash0.style.visibility='visible'};\">\n";
	HTMLstr += "<tr>";

	if (n) HTMLstr += "<td>&nbsp;";

	HTMLstr += "<!-- MAIN MENU STARTS -->\n";
	HTMLstr += "<!-- MAIN_MENU -->\n";
	HTMLstr += "<!-- MAIN MENU ENDS -->\n";

	if (n) HTMLstr += "</td>";

	HTMLstr += "</tr>\n";
	HTMLstr += "</table>\n";
	HTMLstr += "\n";
	HTMLstr += "<!-- SUB MENU STARTS -->\n";
	HTMLstr += "<!-- SUB_MENU -->\n";
	HTMLstr += "<!-- SUB MENU ENDS -->\n";
	HTMLstr += "\n";

	if (ie) HTMLstr+= "</div>\n";
//	if (n)  HTMLstr+= "</layer>\n";

	HTMLstr += "<!-- MENU PANE DECALARATION ENDS -->\n";
}

// This function add items into the main menu navigation bar
// The function is called in menucontext.js script file

function addItem(idItem, text, hint, location, altLocation)
{
	var Lookup = "<!-- ITEM "+idItem+" -->";
	if (HTMLstr.indexOf(Lookup) != -1)
	{
		alert(idParent + " already exist");
		return;
	}
	var MENUitem = "";
	MENUitem += "\n<!-- ITEM "+idItem+" -->\n";
	if (n)
	{
		MENUitem += "<ilayer name="+idItem+">";
		MENUitem += "<a href='.' class=clsMenuItemNS onmouseover=\"displaySubMenu('"+idItem+"')\" onclick=\"return false;\">";
		MENUitem += "| ";
		MENUitem += text;
		MENUitem += "</a>";
		MENUitem += "</ilayer>";
	}
	if (ie)
	{
		MENUitem += "<td>\n";
		MENUitem += "<div id='"+idItem+"' style='z-index:101; font: "+this.menuFont+";'>\n";
		MENUitem += "<a ";
		MENUitem += "class=clsMenuItemIE ";
//		MENUitem += "style='text-decoration: none; font: "+this.menuFont+"; color: "+this.fontColor+"; cursor: hand;' ";
		if (hint != null)
			MENUitem += "title='"+hint+"' ";
		if (location != null)
		{
			MENUitem += "href='"+location+"' ";
			MENUitem += "onmouseover=\"hideAll()\" ";
		}
		else
		{
			if (altLocation != null)
				MENUitem += "href='"+altLocation+"' ";
			else
				MENUitem += "href='.' ";
			MENUitem += "onmouseover=\"displaySubMenu('"+idItem+"')\" ";
			MENUitem += "onclick=\"return false;\" "
		}
		MENUitem += ">";
		MENUitem += "| \n";
		MENUitem += text;
		MENUitem += "</a>\n";
		MENUitem += "</div>\n";
		MENUitem += "</td>\n";
	}
	MENUitem += "<!-- END OF ITEM "+idItem+" -->\n\n";
	MENUitem += "<!-- MAIN_MENU -->\n";

	HTMLstr = HTMLstr.replace("<!-- MAIN_MENU -->\n", MENUitem);
}

// This function add submenu items into the submenu navigation bar
// The function is called in menucontext.js script file

function addSubItem(idParent, text, hint, location)
{
	var MENUitem = "";
	Lookup = "<!-- ITEM "+idParent+" -->";
	if (HTMLstr.indexOf(Lookup) == -1)
	{
		alert(idParent + " not found");
		return;
	}
	Lookup = "<!-- NEXT ITEM OF SUB MENU "+ idParent +" -->";
	if (HTMLstr.indexOf(Lookup) == -1)
	{
		if (n)
		{
			MENUitem += "\n";
			MENUitem += "<layer id='"+idParent+"submenu' visibility=hide bgcolor='"+this.subMenubgColor+"'>\n";
			MENUitem += "<table border='"+this.subMenuPaneBorder+"' bgcolor='"+this.subMenubgColor+"' width="+this.subMenuPaneWidth+">\n";
			MENUitem += "<!-- NEXT ITEM OF SUB MENU "+ idParent +" -->\n";
			MENUitem += "</table>\n";
			MENUitem += "</layer>\n";
			MENUitem += "\n";
		}
		if (ie)
		{
//			onmouseover the main menu bar, we hide flash0 layer if any
//			onmouseout  the main menu bar, we show flash0 layer if any

			MENUitem += "\n";
			MENUitem += "<div id='"+idParent+"submenu' style='z-index:102; position:absolute; visibility: hidden; width: "+this.subMenuPaneWidth+"; font: "+this.menuFont+"; top: -300;' onmouseover=\"if (document.all.flash0){document.all.flash0.style.visibility='hidden'};\" onmouseout=\"if (document.all.flash0){document.all.flash0.style.visibility='visible'};\">\n";
			MENUitem += "<table border='" + this.subMenuPaneBorder + "' bgcolor='" + this.subMenubgColor + "' width=" + this.subMenuPaneWidth + ">\n";
			MENUitem += "<!-- NEXT ITEM OF SUB MENU "+ idParent +" -->\n";
			MENUitem += "</table>\n";
			MENUitem += "</div>\n";
			MENUitem += "\n";
		}
		MENUitem += "<!-- SUB_MENU -->\n";
		HTMLstr = HTMLstr.replace("<!-- SUB_MENU -->\n", MENUitem);
	}

	Lookup = "<!-- NEXT ITEM OF SUB MENU "+ idParent +" -->\n";
	if (n)  MENUitem = "<tr><td><a class=clsSubMenuItemNS title='"+hint+"' href='"+location+"'>"+text+"</a><br></td></tr>\n";
	if (ie) MENUitem = "<tr><td><a class=clsSubMenuItemIE title='"+hint+"' href='"+location+"'>"+text+"</a><br></td></tr>\n";
	MENUitem += Lookup;
	HTMLstr = HTMLstr.replace(Lookup, MENUitem);

}

// This function displays the main menu navigation bar
// The function is called in menucontext.js script file

function showMenu()
{
	document.writeln(HTMLstr);
}

// This function add the code to display the submenu
// The function is called in function additem in this script file

function displaySubMenu(idMainMenu)
{
	var menu;
	var submenu;
	if (n)
	{
		submenu = document.layers[idMainMenu+"submenu"];
		if (lastMenu != null && lastMenu != submenu) hideAll();
		submenu.left = document.layers[idMainMenu].pageX;
		submenu.top  = document.layers[idMainMenu].pageY + 25;
		submenu.visibility = fShow;

		leftX  = document.layers[idMainMenu+"submenu"].left;
		rightX = leftX + document.layers[idMainMenu+"submenu"].clip.width;
		leftY  = document.layers[idMainMenu+"submenu"].top+
			document.layers[idMainMenu+"submenu"].clip.height;
		rightY = leftY;
	} else if (ie) {
		menu = eval(idMainMenu);
		submenu = eval(idMainMenu+"submenu.style");
		submenu.left = calculateSumOffset(menu, 'offsetLeft');
//		submenu.top  = calculateSumOffset(menu, 'offsetTop') + 30;
		submenu.top  = menu.style.top+21;
		submenu.visibility = fShow;
		if (lastMenu != null && lastMenu != submenu) hideAll();

// leftX   contient la limite gauche du sous-menu
// rightX  contient la limite droite du sous-menu
// leftY   contient la limite supérieure du sous-menu
// rightY  contient la limite inférieure du sous-menu

		leftX  = document.all[idMainMenu+"submenu"].style.posLeft;
		rightX = leftX + document.all[idMainMenu+"submenu"].offsetWidth;

		leftY  = document.all[idMainMenu+"submenu"].style.posTop+
			document.all[idMainMenu+"submenu"].offsetHeight;
		rightY = leftY;
	}

// this variable contains the last sub menu name. It is used in HideAll function
// to know which menu to hide when mouse moves !

	lastMenu = submenu;
}

function hideAll()
{
	if (lastMenu != null) {lastMenu.visibility = fHide;lastMenu.left = 0;}
}

// This function calculate an offset position to display the submenu
// The function is called in displaysubmenu fonction in this script

function calculateSumOffset(idItem, offsetName)
{
	var totalOffset = 0;
	var item = eval('idItem');
	do
	{
		totalOffset += eval('item.'+offsetName);
		item = eval('item.offsetParent');
	} while (item != null);
	return totalOffset;
}

// This function captures mouse coordinates and trigger itself on mouse move !
// The function is called from the main html page

function updateIt(e)
{
	if (ie)
	{

// clientX: Abscisse de l'evenement par rapport au document
// clientY: Ordonnée de l'evenement par rapport au document

		var x = window.event.clientX;
		var y = window.event.clientY;

// on n'envisage de cacher le sous menu que si les variables de limites existent

		if (typeof(rightX) == "undefined" || typeof(leftX) == "undefined" || typeof(rightY) == "undefined") {}
		else {
			if (x > rightX || x < leftX) hideAll();
			else if (y > rightY) hideAll();
		}
	}
	if (n)
	{
//		var x = e.pageX;
//		var y = e.pageY;

//		if (x > rightX || x < leftX) hideAll();
//		else if (y > rightY) hideAll();
	}
}

if (document.all)
{

// If one clicks or scroll on the document, the last submenu displayed is hidden
// If the mouse is moved, updateit is called to evaluate if mouse is out of the
// submenu area.

	document.body.onclick=hideAll;
	document.body.onscroll=hideAll;
	document.body.onmousemove=updateIt;
}
if (document.layers)
{
	document.onmousedown=hideAll;
	window.captureEvents(Event.MOUSEMOVE);
	window.onmousemove=updateIt;
}
if (document.getElementById&&!document.all)
{
	document.onmousedown=hideAll;
	window.captureEvents(Event.MOUSEMOVE);
	window.onmousemove=updateIt;
}
