var nSelectedTabNum = -1;
var nHighlightedTabNum = -1;
var aSubMenus = new Array();
var iHideTimer = 0;

function CreateLink(clink, ispopup) {
	// builds a link based on whether the menu is popup or not
	if (ispopup) {
		clink = "window.open('" + clink + "')";
	} else {
		clink = "document.location='" + clink + "'";
	}
	return clink;
}

function CreateMenu(ctext, imenuid, clink, ispopup) {
	clink = CreateLink(clink, ispopup);
	document.write('<td id="MenuTabTable' + imenuid + '"><table border="0" width="100%" cellspacing="0" cellpadding="4"><tr><td id="MenuTab' + imenuid + '" class="MenuTabUnselected"><span onclick="' + clink + '" onmouseover="HighlightTab(' + imenuid + ')" onmouseout="RevertTab()">' + ctext + '</span></td></tr></table></td>');

	aSubMenus[imenuid] = "";
}

function CreateSubMenu(ctext, iparentid, clink, ispopup, imenuid) {
	clink = CreateLink(clink, ispopup);
	cmenu = "'HM_Menu" + imenuid + "'";
	sLink = '<span id="SubLink' + imenuid + '" onclick="' + clink + '" onmouseover="HighlightSub(' + imenuid + ');popUp(' + cmenu + ', event)"  onmouseout="UnHighlightSub(' + imenuid + ');popDown(' + cmenu + ', event)">' + ctext + '</span>';

	// add the link to the appropriate submenu layer
	if (aSubMenus[iparentid] == "") {
		aSubMenus[iparentid] = sLink;
	} else {
		aSubMenus[iparentid] = aSubMenus[iparentid] + '&nbsp;&nbsp;<font class="MenuTabSubDivider">|</font>&nbsp;&nbsp;' + sLink;
	}
}

function HighlightTab (tabNumber) {
	// Gives 'Focus' to the selected tab
	UnHighlight();

	if (tabNumber != nSelectedTabNum) {
		document.getElementById("MenuTab" + tabNumber).className = "MenuTabHighlight";
		CancelRevert();
	}

	// Show the sublayer
	document.getElementById("MenuTabLayer" + tabNumber).style.display = "block";

	nHighlightedTabNum = tabNumber;
	AdjustSubLayer(tabNumber);
}

function UnHighlight () {
	// Removes 'Focus' from the tab that has it
	if (nHighlightedTabNum > -1) {
		if (nHighlightedTabNum != nSelectedTabNum) {
			// Change to the 'Unfocused' font if not the Selected tab
			document.getElementById("MenuTab" + nHighlightedTabNum).className = "MenuTabUnselected";
		}
		// Hide the sublayer
		document.getElementById("MenuTabLayer" + nHighlightedTabNum).style.display = "none";

		nHighlightedTabNum = -1;
	}
}

function RevertTab() {
	// to be called on mouseout to highlight the selected tab
	if (iHideTimer == 0) {
		iHideTimer = setTimeout("HighlightTab(nSelectedTabNum)", 800);
	}
}
function CancelRevert() {
	// Cancels the call to RevertTab above allowing the menu to stay up longer
	if (iHideTimer > 0) {
		clearTimeout(iHideTimer);
		iHideTimer = 0;
	}
}

function AdjustSubLayer (layerID) {
	// Position the sublayer centered under the menutab
	var padding = 10;
	var oMenu = document.getElementById("MenuTabTable" + layerID);
	var oSubMenu = document.getElementById("MenuTabSubTable" + layerID);
	var menuCenter = oMenu.offsetLeft + (oMenu.offsetWidth /  2);
	var subRadius = oSubMenu.offsetWidth /  2
	var newLeft = menuCenter - subRadius;
	var newRight = menuCenter + subRadius;
	var iOffset = 0;
	if (newLeft < padding) {
		iOffset = newLeft - padding;
	} else if (newRight > (iMaxWidth - padding)) {
		iOffset = (newRight - iMaxWidth) + padding;
	}
	document.getElementById("MenuTabLayer" + layerID).style.left = newLeft - iOffset;
}

function TurnTabOn (tabNumber) {
	// Selects the tab
	if (tabNumber != nSelectedTabNum) {
		TurnTabOff(nSelectedTabNum);
		nSelectedTabNum = tabNumber;

		HighlightTab(tabNumber);

		var oMenuTab = document.getElementById("MenuTab" + tabNumber);
		oMenuTab.className = "MenuTabSelected";
		if (selectedImage != "") {
			if (tabNumber > 1) {
				oMenuTab.style.background = strSiteColor + " url(" + selectedImage + ") no-repeat bottom left";
			}
		}
	}
}

function TurnTabOff (tabNumber) {
	if (tabNumber > -1) {
		var oMenuTab = document.getElementById("MenuTab" + tabNumber);
		oMenuTab.className = "MenuTabUnselected";
		oMenuTab.background="";
	}
}

function HighlightSub (subMenuID) {
	oSubLink = document.getElementById("SubLink" + subMenuID);
	oSubLink.style.textDecoration = "underline";
	CancelRevert();

	// Now position the HM menu layer under that
	try {
		var HM_Menu = document.getElementById("HM_Menu" + subMenuID);
		var SubLinkLeft = document.getElementById("SubLink" + subMenuID).offsetLeft + 3;
		HM_Menu.tree.MenuLeft = SubLinkLeft;
	} catch (e) {}
}

function UnHighlightSub (subMenuID) {
	document.getElementById("SubLink" + subMenuID).style.textDecoration = "none";
	RevertTab();
}


function initSubTabs () {
	// Sends html for sublayers
	for (i=1; i < aSubMenus.length; i++) {
		if (document.layers) {
		} else {
			document.write('<div id="MenuTabLayer' + i + '" style="display:none; position:relative;"><table cellpadding="0" cellspacing="0" border="0"><tr><td class="MenuTabSub" id="MenuTabSubTable' + i + '">' + aSubMenus[i] + '&nbsp;</td></tr></table></div>');
		}
	}
}

function bodyLoaded() {
	AdjustSubLayer(nHighlightedTabNum);
}
