/*
 * Constatns
 */

// This controls where the menus appear relative to their links
var MENU_OFFSET_X = 0;
var MENU_OFFSET_Y = 17;
// This controls where the submenus appear relative to the top level menus
var SUBMENU_OFFSET_X = 130;
var SUBMENU_OFFSET_Y = 0;
var MENU_TIMEOUT = 500; // Milliseconds

function CheckBrokenBrowser()
{
	if (! document.getElementById)
		return true;
	// TODO possibly: some tests to check that our GetElementX and Y routines
	// Actually work in the current browser.
	// Kludge: detect if we're in MacIE
	// Note: IE for the Mac seems to have a broken offsetParent property,
	// Which makes our positioning calculation go all wrong.
	// We can't detect this, so we use browser detection instead.
	var userAgent = navigator.userAgent;
	var isMac = userAgent.indexOf("Mac") != -1;
	var isIe = userAgent.indexOf("MSIE") != -1;
	if (isMac && isIe) {
		return true;
	}
	return false;
}

function MouseOverFunc(ev)
{
	ShowMenu(this.linkName, this.linkLevel);
}

var timerId = 0;

function CancelMenus()
{
	ShowMenu(0,0);
}

// When the pointer leaves a menu, set up a timeout.
function MenuMouseOutFunc()
{
	// In case there's a timer running already.
	clearTimeout(timerId);
	timerId = setTimeout("CancelMenus()", MENU_TIMEOUT);
}

function MenuMouseOverFunc()
{
	// Ok, the pointer came back. Cancel the timeout.
	clearTimeout(timerId);
}


function SetMenuEvents(obj)
{
	obj.onmouseover = MenuMouseOverFunc;
	obj.onmouseout = MenuMouseOutFunc;
}

function InitMenus()
{
	// if (CheckBrokenBrowser())
   //		return;
	// alert(GetElementX(document.getElementById("link_2")));
	// return;
	// document.write("link_1 X is " + GetElementX(document.getElementById("link_1")));
	// document.write("link_4 X is " + GetElementX(document.getElementById("link_4")));
	// return;
	var num=1;
	do {
		var objLink = document.getElementById("link_" + num);
		if (! objLink) return;
		var objMenu = document.getElementById("menu_" + num);
		if (! objMenu) return;
		objLink.onmouseover = MouseOverFunc;
		objLink.linkName = num;
		objLink.linkLevel = 0;
		SetMenuEvents(objMenu);
		var subnum=1;
		do {
			var name = num + "_" + subnum;
			var objLink = document.getElementById("link_" + name);
			if (! objLink) break;
			var objMenu = document.getElementById("menu_" + name);
			if (! objMenu) break;
			objLink.onmouseover = MouseOverFunc;
			objLink.linkName = name;
			objLink.linkLevel = 1;
			SetMenuEvents(objMenu);
			subnum ++;
		} while (1);
		num ++;
	} while (1);
}

// Array of which menu is open at which level.
var previousMenu = Array();

// Set the position of obj1 to the position of obj2, with offset.
function SetPosition(obj1, obj2, offsetx, offsety)
{
	offsetx += GetElementX(obj2);
	offsety += GetElementY(obj2);
	// alert("X=" + offsetx + "Y=" + offsety);
	obj1.style.left = offsetx;
	obj1.style.top = offsety;
}

function ShowMenu(name, level)
{
	// Hide the old menu at this level
	var objOldMenu = document.getElementById("menu_" + previousMenu[level]);
	if (objOldMenu) objOldMenu.style.visibility = "hidden";
	objOldMenu = document.getElementById("menu_" + previousMenu[level+1]);
	if (objOldMenu) objOldMenu.style.visibility = "hidden";
	var objMenu = document.getElementById("menu_" + name);
	if (objMenu)
	{
		var objLink = document.getElementById("link_" + name);
		if (objLink)
		{
			SetPosition(objMenu, objLink,
				level>0 ? SUBMENU_OFFSET_X : MENU_OFFSET_X,
				level>0 ? SUBMENU_OFFSET_Y : MENU_OFFSET_Y);
		}
		objMenu.style.visibility = "visible";
	}
	previousMenu[level] = name;
}

function GetElementX(obj)
{
	var x=0;
	do {
		// alert(obj.offsetLeft);
		x += obj.offsetLeft;
		obj = obj.offsetParent;
	} while (obj);
	return x;
}

function GetElementY(obj)
{
	var y=0;
	do {
		y += obj.offsetTop;
		obj = obj.offsetParent;
	} while (obj);
	return y;
}


