var g_timeoutHide = 0;
var g_timeoutShow = 0;
var g_nShowMenuDelay = 300;
var g_nHideMenuDelay = 600;
var g_sMenuPrefix = "MB_mnu";
var g_fShowMenu = false;
var g_sMenuToShow;
var g_fShowMenuOnRight;
var g_oLastLink = null;
var g_sLastClass = 'tab';

var MB_clrMenuBack = 0xFFFFFF; 
var MB_clrMenuText = 0x044937; 
var MB_clrMenuSelBack = 0x044937; 
var MB_clrMenuSelText = 0xFFFFFF; 
var MB_fontFace = 'Verdana'; 
var MB_fontSize = 11; 
var MB_fHorizontal = 0; 
var MB_textXMargin = 5; 
var MB_textYMargin = 2; 
var MB_borderWidth = 1; 
var MB_clrBorder = 0x044937; 
var MB_msMenuAnimation = 200;

function MB_getFrameWindow(iLevel)
{    
	if (window.frames)
		return window.frames[g_sMenuPrefix + iLevel];
	return null;
}

function MB_getFrame(iLevel)
{   
	if (document.getElementById)
		return document.getElementById(g_sMenuPrefix + iLevel)
	if (document.all)
		return document.all[g_sMenuPrefix + iLevel];
	return null;
}

function MB_setVisibility(iLevel, fVisible)
{   
	var oFrame = MB_getFrame(iLevel);
	if (oFrame != null && typeof(oFrame) == 'object')
	{
		if (oFrame.style)
			oFrame.style.visibility = fVisible ? 'visible' : 'hidden';
	}
}

function MB_cancelShow()
{   
	if (g_timeoutShow != 0)
	{
		clearTimeout(g_timeoutShow);
		g_timeoutShow = 0;
	}
}

function MB_cancelHide()
{   
	if (g_timeoutHide != 0)
	{
		clearTimeout(g_timeoutHide);
		g_timeoutHide = 0;
	}
}

function MB_showMenu(sMenuName, left, top, parentWidth, minWidth)
{   
	g_timeoutShow = 0;
	g_sMenuToShow = "";
	sMenuName = "" + sMenuName;
	var iLevel = sMenuName.split('_').length;
	if (iLevel == 1)
		g_fShowMenuOnRight = true;
    
	var oFrameWindow = MB_getFrameWindow(iLevel);
	if (oFrameWindow == null)
		return;
	
	for (var iLevelHide = MB_cLevels; iLevelHide > iLevel; iLevelHide--)
		MB_setVisibility(iLevelHide, false);
	if (MB_mnuAll.indexOf(',' + sMenuName + ',') == -1)
	{
		MB_setVisibility(iLevel, false);
		return;
	}
    
	var objMenu = eval('MB_obj' + sMenuName);
	var sMenu = eval('MB_ary' + sMenuName);
	var aryMenu = sMenu.split('|');
	var oFrameDoc = oFrameWindow.document;
	
	oFrameWindow.setMenuColors(colorToHex(MB_clrMenuText), colorToHex(MB_clrMenuBack),colorToHex(MB_clrMenuSelText), colorToHex(MB_clrMenuSelBack));
	
	MB_getFrame(iLevel).style.width = 0;
	
	oFrameDoc.body.innerHTML = makeMenuDiv(sMenuName, objMenu, aryMenu);
	
	var oMenu = null;
	if (document.getElementById)
		oMenu = oFrameDoc.getElementById('menu');
	else if (document.all)
		oMenu = oFrameDoc.all['menu'];
	var nWidth = oMenu.offsetWidth;
	var nHeight = oMenu.offsetHeight;
	// This next line is required because on IE 5.5, the offsetWidth property does
	// not return a valid value the first time it is called.
	nWidth = oMenu.offsetWidth;
	if (document.all && nWidth < minWidth)
	{
		// This apparently only works in IE, since Netscape never makes the iframes smaller again.
		nWidth = minWidth;
		oMenu.style.width = nWidth;
	}

	var nBodyWidth = 0, nBodyHeight = 0;
	if (document.getElementById)
	{
		nBodyWidth = window.innerWidth ? window.innerWidth : document.body.clientWidth;
		nBodyHeight = window.innerHeight ? window.innerHeight : document.body.clientHeight;
	}
	else if (document.all)
	{
		nBodyWidth = document.body.offsetWidth;
		nBodyHeight = document.body.offsetHeight;
	}

	// Adjust the left position.
	if (g_fShowMenuOnRight)
	{
		if (left + nWidth > nBodyWidth)
		{
			if (parentWidth == 0)
			{
				left = nBodyWidth - nWidth;
			}
			else if (left >= parentWidth + nWidth)
			{
				left -= (parentWidth + nWidth);
				g_fShowMenuOnRight = false;
			}
		}
	}
	else if (parentWidth > 0)
	{
		//alert('test: pw = ' + parentWidth + ', left = ' + left + ', nWidth = ' + nWidth);
		if (left >= parentWidth + nWidth)
			left -= (parentWidth + nWidth);
		else
			g_fShowMenuOnRight = true;
	}

	// Adjust the top position.
	if (top + nHeight > nBodyHeight)
		top = nBodyHeight - nHeight;

	//alert('left = ' + left + ', top = ' + top + ', nWidth = ' + nWidth + ', nHeight = ' + nHeight);
	var oFrame = MB_getFrame(iLevel);
	oFrame.style.left = left;
	oFrame.style.top = top;
	oFrame.style.width = nWidth;
	oFrame.style.height = nHeight;
	oFrame.style.visibility = 'visible';

	oFrameWindow.window.notifyMenuPosition(left, top, nWidth, aryMenu.length / 3);
}

function MB_showMenuDelay(sMenuName, left, top, parentWidth, minWidth)
{   
    MB_cancelHide();
	MB_cancelShow();
	g_sMenuToShow = sMenuName;
	g_fShowMenu = true;

	sMenuName = ("" + sMenuName);
	var iLevel = sMenuName.split('_').length;

	var oFrameParent = MB_getFrameWindow(iLevel - 2);
	if (oFrameParent != null)
	{
		var ichMe = sMenuName.lastIndexOf('_');
		var ichParent = sMenuName.lastIndexOf('_', ichMe - 1);
		var ibtnInParent = parseInt(sMenuName.substring(ichParent + 1, ichMe));

		var sParentMenuName = sMenuName.substr(0, ichParent);
		if (sParentMenuName.length > 0)
		{
			var objMenu = eval('MB_obj' + sParentMenuName);
			oFrameParent.setHighlight(ibtnInParent, colorToHex(MB_clrMenuSelText), colorToHex(MB_clrMenuSelBack));
		}
	}

	g_timeoutShow = setTimeout("MB_showMenu('" + sMenuName + "', " + left + ", " + top + ", " + parentWidth + ", " + minWidth + ")", g_nShowMenuDelay);
	MB_cancelHide();
}

function MB_hideMenu(sMenuName)
{   
    if (g_timeoutShow && g_sMenuToShow == sMenuName)
	{
		// We have moused off before the menu can be shown, so don't show it.
		MB_hideEverything();
	}
	else if (!g_timeoutShow)
	{
		// We have moused off after the menu has been shown, so hide it after a delay.
		MB_hideEverythingDelay();
	}
}

function MB_hideEverythingDelay()
{   
	MB_cancelHide();
	g_timeoutHide = setTimeout("MB_hideEverything()", g_nHideMenuDelay);
}

function MB_hideEverything()
{   
	MB_cancelShow();
	for (var iLevel = MB_cLevels; iLevel > 0; iLevel--)
		MB_setVisibility(iLevel, false);
	onHideEverything();
}

function onTabOver(oLink, iTab, fOver, sTitle) {
	   
	if (fOver && oLink.className == 'hilitedtab')
	{
		// Hide everything except the first dropdown.
		for (var iLevel = MB_cLevels; iLevel > 1; iLevel--)
			MB_setVisibility(iLevel, false);
		setTimeout('MB_cancelHide()', 200);
		return;
	}
	
	if (fOver) {
		if (document.getElementById) {
			if (g_oLastLink != null)
				g_oLastLink.parentNode.className = g_oLastLink.className = g_sLastClass;
			g_sLastClass = oLink.className;
			oLink.parentNode.className = oLink.className = 'hilitedtab';
		}
		else if (document.all) {
			if (g_oLastLink != null)
				g_oLastLink.parentElement.className = g_oLastLink.className = g_sLastClass;
			g_sLastClass = oLink.className;
			oLink.parentElement.className = oLink.className = 'hilitedtab';
		}
		g_oLastLink = oLink;
	}
	var oCell = null;
	if (document.getElementById)
		oCell = oLink.parentNode;
	else if (document.all)
		oCell = oLink.parentElement;
	if (oCell != null)
	{
		if (fOver)
			showMenu('' + iTab, oCell);
		else
			MB_hideMenu('' + iTab);
	}
	else if (!fOver)
	{
		onHideEverything();
	}
	//window.status = fOver ? sTitle : window.defaultStatus;
	return true;
}

function showMenu(sMenuName, element)
{   
	for (var iLevel = MB_cLevels; iLevel > 0; iLevel--)
		MB_setVisibility(iLevel, false);
	if (element == null && window.event)
		element = window.event.fromElement;
		
	var left = 0;
	var top = element ? element.offsetHeight : 0;
	var minWidth = element ? element.offsetWidth : 0;
	
	while (element != null)
	{
		left += element.offsetLeft;
		top += element.offsetTop;
		element = element.offsetParent;
	}
	
	MB_showMenuDelay(sMenuName, left, top, 0, minWidth);
	setTimeout("MB_cancelHide()", 200);
	//MB_cancelHide();
}

function onHideEverything()
{   
	if (g_oLastLink != null)
	{
		if (document.getElementById)
			g_oLastLink.parentNode.className = g_oLastLink.className = g_sLastClass;
		else if (document.all)
			g_oLastLink.parentElement.className = g_oLastLink.className = g_sLastClass;
		g_oLastLink = null;
	}
}


function makeMenuDiv(sMenuName, objMenu, aryMenu, sClass)
{   
	
	
	if (!sClass || sClass == '')
		sClass = 'menu';
	var sCellStyle = 
		'background-color:' + colorToHex(MB_clrMenuBack) + '; ' + 
		'cursor:hand;';
	var sStyle = 
		'color:' + colorToHex(MB_clrMenuText) + '; ' + 
		'font-family:' + MB_fontFace + '; ' + 
		'font-size:' + MB_fontSize + '; ' +
		'text-decoration:none';
	var sHTML = '<table class="' + sClass + '" id="menu" bgcolor=' + colorToHex(MB_clrMenuBack) + ' cellpadding=0; cellspacing=0 border=0 style="background-color:' + colorToHex(MB_clrMenuBack) + '; border-width:' + MB_borderWidth + 'px; border-color:' + colorToHex(MB_clrBorder) + '">';
	var i, c = aryMenu.length;
	var irow = 0;
	for (i = 0; i < c; i += 3)
	{
		if (aryMenu[i] == ' ')
		{
			sHTML += 
				'<tr class="' + sClass + '_sep" ' + 
				'   onmouseover="mouseOver(\'' + sMenuName + '_' + irow + '\', ' + -1 + ')" ' + 
				'   onmouseout="mouseOut(' + irow + ')">' + 
				'<td style="height:4px" colspan=2><hr></td></tr>';
		}
		else
		{
			/*sHTML += 
				'<tr class="' + sClass + '" id="row' + irow + '" style="' + sCellStyle + '; " ' + 
				'   onclick="MB_menuClick(\'' + sMenuName + '\', \'' + aryMenu[i + 1] + '\'); return false" ' + 
				'   onmouseover="mouseOver(\'' + sMenuName + '_' + irow + '\', ' + irow + ')" ' + 
				'   onmouseout="mouseOut(' + irow + ')">' + 
				'<td class="' + sClass + '" nowrap style="padding-left:' + objMenu.MB_textXMargin + 'px; ' + 
				'   padding-top:' + objMenu.MB_textYMargin + 'px; padding-bottom:' + objMenu.MB_textYMargin + 'px;">' + 
				'   <a id="link' + irow + '" href="../' + aryMenu[i + 1] + '" target=_parent ' + 
				'      onclick="MB_menuClick(\'' + sMenuName + '\', \'' + aryMenu[i + 1] + '\'); return false" ' + 
				'      style="' + sStyle + '">' + aryMenu[i] + '</a></td>' + 
				'<td align="right" style="' + sStyle + '; padding-right:' + objMenu.MB_textXMargin + 'px; ' + 
				'   padding-top:' + objMenu.MB_textYMargin + 'px; padding-bottom:' + objMenu.MB_textYMargin + 'px;">';*/
			/*sHTML += 
				'<tr class="' + sClass + '" id="row' + irow + '" style="' + sCellStyle + '; " ' + 
				'   onclick="MB_menuClick(\'' + sMenuName + '\', \'' + aryMenu[i + 1] + '\'); return false" ' + 
				'   onmouseover="mouseOver(\'' + sMenuName + '_' + irow + '\', ' + irow + ')" ' + 
				'   onmouseout="mouseOut(' + irow + ')">' + 
				'<td class="' + sClass + '" nowrap style="padding-left:' + objMenu.MB_textXMargin + 'px; ' + 
				'   padding-top:' + objMenu.MB_textYMargin + 'px; padding-bottom:' + objMenu.MB_textYMargin + 'px;">' + 
				'   <a id="link' + irow + '" href="../' + aryMenu[i + 1] + '" target=_parent ' + 
				'      onclick="MB_menuClick(\'' + sMenuName + '\', \'' + aryMenu[i + 1] + '\'); return false" ' + 
				'      style="' + sStyle + '">' + aryMenu[i] + '</a>' + 
				'<span align="right" style="position:absolute; right:0px; ' + sStyle + '; padding-right:' + objMenu.MB_textXMargin + 'px; ' + 
				'   padding-top:' + objMenu.MB_textYMargin + 'px; padding-bottom:' + objMenu.MB_textYMargin + 'px;">';*/
			sHTML += 
				'<tr class="' + sClass + '" id="row' + irow + '" style="' + sCellStyle + '; " ' + 
				'   onclick="MB_menuClick(\'' + sMenuName + '\', \'' + aryMenu[i + 1] + '\'); return false" ' + 
				'   onmouseover="mouseOver(\'' + sMenuName + '_' + irow + '\', ' + irow + ')" ' + 
				'   onmouseout="mouseOut(' + irow + ')">' + 
				'<td class="' + sClass + '" nowrap style="padding-left:' + MB_textXMargin + 'px; ' + 
				'   padding-top:' + MB_textYMargin + 'px; padding-bottom:' + MB_textYMargin + 'px;">' + 
				'   <table border=0 cellpadding=0 cellspacing=0 width=100%><tr>' +
				'   <td nowrap><a id="link' + irow + '" href="../' + aryMenu[i + 1] + '" target=_parent ' + 
				'      onclick="MB_menuClick(\'' + sMenuName + '\', \'' + aryMenu[i + 1] + '\'); return false" ' + 
				'      style="' + sStyle + '">' + aryMenu[i] + '</a></td>' + 
				'   <td width=1 align="right" style="' + sStyle + '; padding-left:5px; padding-right:' + MB_textXMargin + 'px">';
			if (aryMenu[i + 2] != '0')
			{
				var nArrowHeight = MB_fontSize - 4;
				if ((nArrowHeight % 2) == 0)
					nArrowHeight++;
				var nArrowWidth = (nArrowHeight + 1) / 2;
				sHTML += '<img id="arrow' + irow + '" src="images/submenu-off.gif" width=' + nArrowWidth + ' height=' + nArrowHeight + '>';
			}
			else
			{
				sHTML += '<span></span>';
			}
			sHTML += '</td></tr></table></td></tr>';
		}
		irow++;
	}

	sHTML += '</table>';

	return sHTML;
}

function colorToHex(clrText)
{   
	var ksHexValues = '0123456789ABCDEF';
	var sHex = '';
	while (clrText > 0)
	{
		var nRem = clrText % 16;
		sHex = ksHexValues.substr(nRem, 1) + sHex;
		clrText = parseInt(clrText / 16);
	}
	while (sHex.length < 6)
		sHex = '0' + sHex;
	return sHex;
}

