var lastpad = "";
var menu1 = 0;
var menu2 = 0;
// Hide timeout.
var popTimer = 0;
// Array showing highlighted menu items.
var litNow = new Array();
// These two lines handle the window resize bug in NS4. See <body onResize="...">.
// I recommend you leave this here as otherwise when you resize NS4's width menus are hidden.

var popOldWidth = window.innerWidth;
nsResizeHandler = new Function('if (popOldWidth != window.innerWidth) location.reload()');

//Item(text, href, frame, length, spacing, target)
//menu(isVert, popInd, left, top, width, overCol, backCol, borderClass, textClass)
//menu(isVert, popInd, left, top, width)
var menuArr = new Array();
menuArr[0] = new Array();
menuArr[0][0] = new Menu(false, '', 0, 0 , 20, 'L');

function createPad(pMenu1,pMenu2,pText,pHref,pFrame,pLength,pSpacing,pTargetMenu,pVert,pIndicate,pLeft,pTop,pWidth,pAlign)
{
	menuArr[pMenu1][pMenu2] = new Item(pText,pHref,pFrame,pLength,pSpacing,pTargetMenu);
	menuArr[pMenu2] = new Array();
	menuArr[pMenu2][0] = new Menu(pVert,pIndicate,pLeft,pTop,pWidth,pAlign);
}

function createSub(pMenu1,pMenu2,pText,pHref,pFrame,pLength,pSpacing,pTargetMenu,pVert,pIndicate,pLeft,pTop,pWidth,pAlign)
{
	menuArr[pMenu1][pMenu2] = new Item(pText,pHref,pFrame,pLength,pSpacing,pTargetMenu);
	menuArr[pTargetMenu] = new Array();
	menuArr[pTargetMenu][0] = new Menu(pVert,pIndicate,pLeft,pTop,pWidth,pAlign);
}

function createItem(ind1,ind2,pText,pHref,pFrame,pLength,pSpacing,pTargetMenu)
{
		//Item(text, href, frame, length, spacing, target)
	menuArr[ind1][ind2] = new Item(pText,pHref,pFrame,pLength,pSpacing,pTargetMenu);
}

// This is a quick snippet that captures all clicks on the document and hides the menus
// every time you click. Use if you want.

if (isNS4) document.captureEvents(Event.CLICK);
document.onclick = clickHandle;


function popOver(currObj, menuNum, itemNum)
{
	//alert("popOver: with " + menuNum + ',' + itemNum);
	clearTimeout(popTimer);
	hideAllBut(menuNum);
	litNow = getTree(menuNum, itemNum);
	changeCol(litNow, true);
	var divname = 'menu' + menuNum + 'item' + itemNum;
	targetNum = menuArr[menuNum][itemNum].target;
	if (!menuArr[targetNum][0].ref)
		return;
	//alert("popOver: with " + menuNum + ',' + itemNum + ',' + targetNum);
	if (targetNum > 0) // if there is a target number we have to display that menu.
	{
		//Now add the left of the current menu to left of the trigger item
		if (isNS4)
		{
			var thisX = currObj.x;
			var thisY = currObj.y;
			menuArr[menuNum][itemNum].ref = currObj;
		}
		else {
			var thisX = getGrossOffsetLeft(getRef(divname)) + menuArr[targetNum][0].x;
			var thisY = getGrossOffsetTop(getRef(divname)) + menuArr[targetNum][0].y;
		}


		if (menuArr[targetNum][0].align == 'R' && !isNS4)
		{
			thisX += getRef(divname).offsetWidth - menuArr[targetNum][0].width;
		}
		mess = 'popOver: with id=' + currObj.id;
		mess += ', thisX=' + thisX;
		mess += ', thisY=' + thisY;
		mess += ', menuNum=' + menuNum;
		mess += ', itemNum=' + itemNum;
		mess += ', targetmenu.x=' + menuArr[targetNum][0].x;
		mess += ', targetmenu.y=' + menuArr[targetNum][0].y;
		mess += ', targetwidth=' + menuArr[targetNum][0].width;
		mess += ', itemleft=' + menuArr[menuNum][itemNum].ref.left;
		mess += ', itemtop=' + menuArr[menuNum][itemNum].ref.top;
		mess += ', itempopInd=' + menuArr[menuNum][0].popInd;
		mess += ', itemoffsetleft=' + menuArr[menuNum][itemNum].ref.offsetLeft;
		mess += ', itemoffsettop=' + menuArr[menuNum][itemNum].ref.offsetTop;
		//document.getElementById('debug').innerHTML = mess;
		//alert(mess);
		if (is_ie4up)
			fiddle_down = 20;
		else
			fiddle_down = 16;
		if (menuArr[menuNum][0].popInd)
		{
			menuArr[targetNum][0].ref.left = parseInt(thisX);
			menuArr[targetNum][0].ref.top = parseInt(thisY);
		}
		else {
			menuArr[targetNum][0].ref.left = parseInt(thisX);
			menuArr[targetNum][0].ref.top = parseInt(thisY)+fiddle_down;
		}
		menuArr[targetNum][0].ref.visibility = 'visible';
	}
}
function popOut(menuNum, itemNum) {
	//alert("popOut: with " + menuNum + ',' + itemNum);
	if ((menuNum == 0) && !menuArr[menuNum][itemNum].target)
		hideAllBut(0)
	else
		popTimer = setTimeout('hideAllBut(0)', 500);
}
function getTree(menuNum, itemNum) {

	// Array index is the menu number. The contents are null (if that menu is not a parent)
	// or the item number in that menu that is an ancestor (to light it up).
	itemArray = new Array(menuArr.length);

	while(1) {
		itemArray[menuNum] = itemNum;
		// If we've reached the top of the hierarchy, return.
		if (menuNum == 0) return itemArray;
		//alert("get Tree: menunum=" + menuNum);
		itemNum = menuArr[menuNum][0].parentItem;
		menuNum = menuArr[menuNum][0].parentMenu;
	   }
}

function hideAllBut(menuNum) {
var keepMenus = getTree(menuNum, 1);
	for (count = 1; count < menuArr.length; count++)
		if (!keepMenus[count] && menuArr[count][0].ref)
			menuArr[count][0].ref.visibility = 'hidden';
	changeCol(litNow, false);
}

// Pass an array and a boolean to specify colour change, true = over colour.
function changeCol(changeArray, isOver) {

	for (menuCount = 0; menuCount < changeArray.length; menuCount++) {
		if (changeArray[menuCount]) {
			// Change the colours of the div/layer background.
			if (isOver)
				{
				setclass = 'MenuOver' + menuArr[menuCount][0].align;
				if (isNS4) {
					//menuArr[menuCount][changeArray[menuCount]].ref.className = 'MenuOver';
					}
				else {
					getRef('menu' + menuCount + 'item' + changeArray[menuCount]).className=setclass;
					}
				}
			else
				{
				setclass = 'MenuBox' + menuArr[menuCount][0].align;
				if (isNS4) {
					//menuArr[menuCount][changeArray[menuCount]].ref.className = 'MenuBox';
					}
				else {
					getRef('menu' + menuCount + 'item' + changeArray[menuCount]).className=setclass;
					}
				}
		    }
	   }
}


// *** MENU CONSTRUCTION FUNCTIONS ***
function Menu(isVert, popInd, x, y, width, align) {
	// True or false - a vertical menu?
	this.isVert = isVert;
	// The popout indicator used (if any) for this menu.
	this.popInd = popInd;
	// Position and size settings.
	this.x = x;
	this.y = y;
	this.width = width;
	this.align = align;
	// Parent menu and item numbers, indexed later.
	this.parentMenu = null;
	this.parentItem = null;
	// Reference to the object's style properties (set later).
	this.ref = null;
}

function Item(text, href, frame, length, spacing, target) {
	this.text = text;
	this.href = href;
	this.frame = frame;
	this.length = length;
	this.spacing = spacing;
	this.target = target;
	// Reference to the object's style properties (set later).
	this.ref = null;
}

function writeMainMenu() {
	if (!isDOM && !isIE4 && !isNS4) return;
	var str

	if (isNS4) {
		str = ''; //'<layer name=menuForm id=menuForm>';
		}
	else {
		str = '<div >';
		}
	str += '<table id="menu0item0" border="1" cellspacing="2" cellpadding="0" class=MenuTable width="95%" align="center"><tr>';
	document.write (str);
	var width = 100 / menuArr[0].length;
	for (currItem = 1; currItem < menuArr[0].length; currItem++) {
		var itemName = 'menu0item' + currItem;
		var tagID = ' id="' + itemName + '"';
		var tagName = ' name="' + itemName + '"';
		var hotspot = '" onMouseOver="popOver(this, 0,' + currItem + ')" onMouseOut="popOut(0,' + currItem + ')"';
		if (isNS4)
			{
			str = '';
			str += '<td class=MenuBox height=30 width=' + width + '%>';
			str += '<div ' + tagName + tagID + '>';
			str += '<a class="MenuHot" ' + hotspot + ' href="' + menuArr[0][currItem].href + '">' + menuArr[0][currItem].text;
			str += '</a></div></td>';
			document.write (str);
			//alert(str);
			// menuArr[0][currItem].ref = document.layers['menuDiv'];
			//mess = show_props(menuArr[0][currItem].ref , 'layers');
			//alert('looking for ' + itemName + ' have' + mess);
			}
		else
			{
			str = '<td class=MenuBox ' + tagID + hotspot +  'width=' + width + '%>';
			str += '<a class="MenuHot" href="' + menuArr[0][currItem].href + '">' + menuArr[0][currItem].text + '</a></td>';
			document.write (str);
			menuArr[0][currItem].ref = getSty('menu0item' + currItem);
			}
		}
	str = '</tr></table>';
	if (isNS4) {
		str += ''; //'</layer>';
		}
	else {
		str += '</div>';
		}
	document.write (str);

	if (isDOM || isIE4)
		{
		menuArr[0][0].ref = document.getElementById('menu0item0');
		menuArr[0][0].x = document.getElementById("menu0item0").offsetLeft;
		menuArr[0][0].y = document.getElementById("menu0item0").offsetTop;
		}
	if (isNS4)
		{
		menuArr[0][0].ref = document.forms["menu0item1"];
		//menuArr[0][0].x = document.layers["layer00"].left;
		//menuArr[0][0].y = document.layers["layer00"].top;
		}
}


function writeMenus() {
	if (!isDOM && !isIE4 && !isNS4 && !is_gecko) return;
	for (currMenu = 0; currMenu < menuArr.length; currMenu++) {
		// Variable for holding HTML for items and positions of next item.
		width = menuArr[currMenu][0].width;
		if (currMenu == 0)
			var str = '<table class=MenuTable width="95%" align="center">';
		else
			var str = '<table class=MenuTable width=' + width + ' border=1 cellspacing="0" cellpadding="0">';
		// Remember, items start from 1 in the array (0 is menu object itself, above).
		// Also use properties of each item nested in the other with() for construction.
		for (currItem = 1; currItem < menuArr[currMenu].length; currItem++) {
			var itemID = 'menu' + currMenu + 'item' + currItem;
			targetnum = menuArr[currMenu][0].target;
			setclass = 'MenuBox' + menuArr[currMenu][0].align;
			if (menuArr[currMenu][0].isVert)
				str += '<tr>';
			if (is_ie4up)
			{
				str += '<a class="MenuHot" href="' + menuArr[currMenu][currItem].href + '"' + (menuArr[currMenu][currItem].frame ? ' target="' + menuArr[currMenu][currItem].frame + '">' : '>');
				str += '<td class=' + setclass + ' id="' + itemID + '" onMouseOver="popOver(this, ' + currMenu + ',' + currItem + ')" onMouseOut="popOut(' + currMenu + ',' + currItem + ')">';
			}
			else {
				str += '<td class=' + setclass + ' id="' + itemID + '" onMouseOver="popOver(this, ' + currMenu + ',' + currItem + ')" onMouseOut="popOut(' + currMenu + ',' + currItem + ')">';
				str += '<a class="MenuHot" href="' + menuArr[currMenu][currItem].href + '"' + (menuArr[currMenu][currItem].frame ? ' target="' + menuArr[currMenu][currItem].frame + '">' : '>');
			}
			str += menuArr[currMenu][currItem].text
			if (menuArr[currMenu][currItem].target > 0) {

				// Set target's parents to this menu item.
				menuArr[menuArr[currMenu][currItem].target][0].parentMenu = currMenu;
				menuArr[menuArr[currMenu][currItem].target][0].parentItem = currItem;

				// Add a popout indicator.
				if (menuArr[currMenu][0].popInd) str +=  menuArr[currMenu][0].popInd;
				}
			if (is_ie4up)
			{
				str += '</td>';
				str += '</a>';
			}
			else {
				str += '</a>';
				str += '</td>';
			}
			if (menuArr[currMenu][0].isVert)
				str += '</tr>';
			}
		str += '</table>';
		if (currMenu == 0) continue;
		//alert(str);
		if (isDOM) {
			var newDiv = document.createElement('div');
			document.getElementsByTagName('body').item(0).appendChild(newDiv);
			newDiv.innerHTML = str;
			newDiv.align = "center";
			menuArr[currMenu][0].ref = newDiv.style;
			menuArr[currMenu][0].ref.position = 'absolute';
			menuArr[currMenu][0].ref.visibility = 'hidden';
			}

		// Insert a div tag to the end of the BODY with menu HTML in place for IE4.
		if (isIE4) {
			document.body.insertAdjacentHTML('beforeEnd', '<div class=MenuBox id="menu' + currMenu + 'div" ' + 'style="position: absolute; visibility: hidden">' + str + '</div>');
			menuArr[currMenu][0].ref = getSty('menu' + currMenu + 'div');
			}

		// In NS4, create a reference to a new layer and write the items to it.
		if (isNS4) {
			menuArr[currMenu][0].ref = new Layer(0);
			menuArr[currMenu][0].ref.document.write(str);
			menuArr[currMenu][0].ref.document.close();
			}
		for (currItem = 1; currItem < menuArr[currMenu].length; currItem++) {
			itemName = 'menu' + currMenu + 'item' + currItem;
			if (isDOM || isIE4) menuArr[currMenu][currItem].ref = getSty(itemName);
		   }
		}
	//for (currMenu = 0; currMenu < menuArr.length; currMenu++)
	//	alert("writeMenus: currmenu=" + currMenu + ", parentMenu" + menuArr[currMenu][0].parentMenu);
	//menuArr[0][0].ref.left = getRef("menuSpace").offsetLeft;
	//menuArr[0][0].ref.top = getRef("menuSpace").offsetTop;
	//menuArr[0][0].ref.visibility = 'visible';
}

function clickHandle(evt)
	{
	if (isNS4) document.routeEvent(evt);
	hideAllBut(0);
	}


//  End -->


