// -------------------------------------------------------------
// menu builder functions
// -------------------------------------------------------------

var m_header		= null;
var m_menu			= null;
var m_subMenu		= null;
var m_subMenuEvtCtl	= null;
var m_hilite		= null;
var m_colorDepth	= 0;
var m_stdTarget		= " target=\"_blank\"";
var m_stdEmpty		= "";
var m_stdOffImg;

function renderMenuStripSep()
{
	document.write( "<TD bgcolor=\"#FFFFFF\" style=\"width:2px\"></TD>" );
}

function menuGoto( url )
{
	hideMenus();
	
	document.location = url;
	
	if( event != null )
	{
		event.cancelBubble = true;
	}
	
	return false;
}

function menuWinOpen( url )
{
	hideMenus();
	
	window.open( url );
	
	return false;
}

function renderMenuStrip()
{
	var menuHeaderID;
	var menuItem;
	
	if( isIE6 )
	{
		var bodytags = getObjectsByTag( "body" );
		
		bodytags[0].style.behavior	= "url(#default#clientCaps)";
		m_colorDepth			= bodytags[0].colorDepth;
	}
	
	for( n = 0; n < m_menuBar.length; n++ )
	{
		if( n > 0 )
		{
			renderMenuStripSep();
		}
		
		menuItem = m_menuBar[n];
		
		menuHeaderID = menuItem.Id + "Hdr";

		if( menuItem.Icon == null ){
			document.write( "<TD class=\"siteMenuLeft\">&nbsp;</TD>" );
		}
		else{
			document.write( "<TD class=\"siteMenuLeft\" align=\"center\" valign=\"middle\">" );
			if( menuItem.Icon.indexOf(".swf") > 0 ){
				writeFlash(m_imgPfx + "/Icons/" + menuItem.Icon, m_imgPfx + "/Icons/" + menuItem.Icon.replace(".swf", ".gif"), 22, 22, "", "") //ver, params
			}
			else{
				document.write( "<IMG border=\"0\" width=\"22\" height=\"22\" src=\"" + m_imgPfx + "/Icons/" + menuItem.Icon + "\">" );
			}
			document.write( "</TD>" );
		}
		
		if( isDHTML )
		{
			if( menuItem.MenuItems )
			{
				document.write( "<TD class=\"siteMenuBtn\" id=\"" + menuHeaderID + "\" onmouseover=\"showMenu(event, '" + menuHeaderID + "', '" + menuItem.Id + "' )\" onmouseout=\"resetMenu(event)\" onclick=\"" );
			}
			else
			{
				document.write( "<TD class=\"siteMenuBtn\" id=\"" + menuHeaderID + "\" onclick=\"" );
			}
			
			if( menuItem.TargetHtml )
			{
				document.write( "return menuWinOpen( \'" + menuItem.Href + "\' )" );
			}
			else
			{
				document.write( "return menuGoto( \'" + menuItem.Href + "\' )" );
			}

			document.write( "\" align=\"center\" nowrap>" );
		}
		else
		{
			document.write( "<TD class=\"siteMenuBtn\" id=\"" + menuHeaderID + "\" align=\"center\" nowrap>" );
		}
		
		document.write( "<A href=\"" + menuItem.Href + "\" class=\"siteMenuBtnLink\" " + this.TargetHtml + ">" + menuItem.Text );
		
		if( menuItem.OffSite )
		{
			document.write( "&nbsp;" + m_stdOffImg );
		}
		
		document.write( "</A></TD><TD class=\"siteMenuRight\">&nbsp;</TD>" );
	}
}

function renderItems( menu, z )
{
	renderSubItems( menu, z, "siteMenuItem" );
}

function renderSubItems( menu, z, css )
{
	if( css == "siteMenuItem" )
	{
		document.write( "<TABLE border=\"0\" cellspacing=\"0\" cellpadding=\"4\" width=\"150\" class=\"" + css + "Container\" id=\"" + menu.Id + "\" style=\"position:absolute;top:0;left:0;z-index:" + z + ";visibility:hidden;\" onmouseout=\"resetMenu(event)\" summary=\"Table for the " + menu.Id + "\">" );
	}
	else
	{
		document.write( "<TABLE border=\"0\" cellspacing=\"0\" cellpadding=\"4\" width=\"150\" class=\"" + css + "Container\" id=\"" + menu.Id + "\" style=\"position:absolute;top:0;left:0;z-index:" + z + ";visibility:hidden;\" onmouseout=\"resetMenu(event)\" summary=\"Table for the " + menu.Id + "\">" );
	}

	for( var n = 0; n < menu.MenuItems.length; n++ )
	{
		var	item = menu.MenuItems[n];
	
		var selected;
		if( item.Selected )
		{
			selected = "Selected";
		}
		else
		{
			selected = "";
		}
		
		if( item.IsSeparator )
		{
    	document.write( "<TR><TD valign=\"middle\" nowrap background=\"" + m_imgPfx + "/menu_isep.gif\"><IMG src=\"" + m_imgPfx + "/spacer.gif\" height=\"2\" width=\"1\"></TD></TR>" );
		}
		else if( item.IsCaption )
		{
			if( n > 0 )
			{
				document.write( "<TR><TD valign=\"middle\" nowrap>&nbsp;</TD></TR>" );
			}

			document.write( "<TR><TD valign=\"middle\" align=\"center\" nowrap class=\"" + css + "\"><I>" + item.Text + "</I></TD></TR>" );

		}
		else if( item.MenuItems )
		{
			document.write( "<TR><TD valign=\"middle\" nowrap class=\"" + css + "" + selected + "\" onmouseover=\"showSubMenu(event, '" + item.Id + "')\"><TABLE border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\" class=\"subContainer\"><tr><td nowrap=\"1\"><A class=\"" + css + "Link\" href=\"" + item.Href + "\"" + item.TargetHtml + ">" + item.Text + "</A></TD><TD nowrap=\"1\" align=\"right\" valign=\"middle\">&nbsp;&nbsp;<IMG src=\"" + m_imgPfx + "/Images/Bullets/ArrowsDots.gif\" width=\"8\" height=\"9\"></TD></TR></TABLE></TD></TR>" );
		}
		else
		{
			document.write( "<TR><TD valign=\"middle\" nowrap class=\"" + css + "" + selected + "\" onclick=\"" );
			
			if( item.TargetHtml )
			{
				document.write( "return menuWinOpen( \'" + item.Href + "\' )" );
			}
			else
			{
				document.write( "return menuGoto( \'" + item.Href + "\' )" );
			}
			
			document.write( "\"><A class=\"" + css + "Link\" href=\"" + item.Href + "\"" + item.TargetHtml + ">" + item.Text );
			
			if( item.OffSite )
			{
				document.write( "&nbsp;" + m_stdOffImg );
			}
			
			document.write( "</A></TD></TR>" );
		}
	}
	
	document.write( "</TABLE>" );

	for( var n = 0; n < menu.MenuItems.length; n++ )
	{
		var	item = menu.MenuItems[n];
		
		if( item.MenuItems )
		{
			renderSubItems( item, z + 1, css );
		}
	}
}

function menuRef( id, text, href, items, selected, target, icon )
{
	this.Id				= id;
	this.Text			= text;
	this.Href			= href; //mhFixupLink( href, "&~ck=mn" );
	this.IsSeparator	= false;
	this.IsCaption		= false;
	this.MenuItems		= items;

	this.OffSite		= false;
	this.TargetHtml		= m_stdEmpty;

	if( ( typeof(target) != "undefined" ) && target )
	{
		this.OffSite	= ( target == "offsite" );
		this.TargetHtml	= " target=\"" + target + "\"";
	}
	
	if( ( typeof(selected) != "undefined" ) && selected )
	{
		this.Selected	= selected;
	}
	else
	{
		this.Selected	= false;
	}
	
	if( ( typeof(icon) != "undefined" ) && icon )
	{
		this.Icon	= icon;
	}
	else
	{
		this.Icon	= null;
	}
}

function menuItem( text, href, selected, target, icon )
{
	this.Text			= text;
	this.Href			= href; //mhFixupLink( href, "&~ck=mn" );
	this.IsSeparator	= false;
	this.IsCaption		= false;
	this.MenuItems		= null;
	this.LastOpenMenuItem	= null;
	this.LastOpenMenuEvtCtl = null;
	
	this.OffSite		= false;
	this.TargetHtml		= m_stdEmpty;

	if( ( typeof(target) != "undefined" ) && target && target != "")
	{
		this.OffSite	= ( target == "offsite" );
		this.TargetHtml	= " target=\"" + target + "\"";
	}
	
	if( ( typeof(selected) != "undefined" ) && selected )
	{
		this.Selected	= selected;
	}
	else
	{
		this.Selected	= false;
	}
	
	if( ( typeof(icon) != "undefined" ) && icon )
	{
		this.Icon	= icon;
	}
	else
	{
		this.Icon	= null;
	}
}

function menuCaption( text )
{
	this.Text			= text;
	this.Href			= null;
	this.IsSeparator	= false;
	this.IsCaption		= true;
	this.MenuItems		= null;
}

function menuSep()
{
	this.IsSeparator	= true;
	this.IsCaption		= false;
}

// -------------------------------------------------------------
// menu display/hiding functions
// -------------------------------------------------------------

function showSubMenu( evt, menuID)
{
	if( isDHTML )
	{
		evt			= evt ? evt : event;
		
		var top		= -1;
		var left;
		var currentEle;

		var newMenu = getRawObject( menuID );

		if( m_menu.id == m_menuBar[m_menuBar.length - 1].Id )
		{
			left =  m_header.offsetLeft + m_header.offsetWidth - m_menu.offsetWidth - newMenu.offsetWidth + 5 + document.body.offsetLeft;
		}
		else
		{
			left = m_header.offsetLeft + m_menu.offsetWidth + 3;
		}
		
		if( subtimerID )
		{
			if( newMenu && newMenu == m_subMenu )
			{
				return;
			}		
		
			clearTimeout( subtimerID );
			subtimerID = null;
		}
	
		if( m_subMenu != null)
		{
			if( m_subMenu == newMenu )
			{
				evt.cancelBubble = true;
				return;
			}
			
			if( isVisible(m_subMenu) )
			{
				hideSubMenu();
			}
		}
		

		if( m_hilite )
		{
			m_hilite.className = "";
			m_hilite = null;
		}
		
		m_subMenuEvtCtl = eventToElement( evt );
		m_subMenu		= newMenu;
		
		currentEle		= getHilite( evt );
			
		rowHeight		= ( ( m_menu.offsetHeight ) / ( m_menu.rows.length ) );

		top			+= m_menu.offsetTop - 1;
		top			+= currentEle.rowIndex * rowHeight;

		shiftTo( m_subMenu, left, top );
	
		if( !isVisible(m_subMenu) )
		{
			subtimerID = setTimeout( "showSubMenuTimed()", 100 );
		}
				
		evt.cancelBubble = true;
	}
}

function showSubMenuTimed()
{
	if( m_subMenu == null )
	{
		return;
	}

	if( isIE6 && m_subMenu.filters && m_colorDepth > 8 )
	{
		m_subMenu.filters.item(0).Apply();
		m_subMenu.filters.item(1).Apply();
	}

	show(m_subMenu);

	if( isIE6 && m_subMenu.filters && m_colorDepth > 8 )
	{
		m_subMenu.filters.item(0).Play();
		m_subMenu.filters.item(1).Play();
	}
}

// defines/shows the current header and menu
//
function showMenu( evt, menuHeaderID, menuID )
{
	if( isDHTML )
	{
		evt = evt ? evt : event;

		if( timerID )
		{
			clearTimeout( timerID );
			timerID = null;
		}

		if( subtimerID )
		{
			clearTimeout( subtimerID );
			subtimerID = null;
		}

		timerID = setTimeout( "showMenuTimed( '" + menuHeaderID + "', '" + menuID + "')", 100 );

		evt.cancelBubble = true;
	}
}

function showMenuTimed( menuHeaderID, menuID )
{
	var top		= 0;
	var left	= 0;
	var currentEle;

	var newMenu = getRawObject( menuID );

	if(m_header != null && m_menu != null && m_menu != newMenu)
	{
		if( isVisible(m_menu) )
		{
			hideMenu();
			showSelectCtrl();
		}
	}

	m_header			= getRawObject( menuHeaderID );
	m_menu				= newMenu;
	m_header.className	= "siteMenuBtnHover";
	
	currentEle	= m_header;
		
	// work out the position of the header and its parent elements
	//
	while( currentEle && currentEle.tagName.toLowerCase() != 'body' )
	{
		top			+= currentEle.offsetTop;
		left		+= currentEle.offsetLeft;
		currentEle	 = currentEle.offsetParent;
	}

	top			+= currentEle.offsetTop;
	left		+= currentEle.offsetLeft;
	
	//ezGlobe design specific:
	//left		-= 16;
	
	/*if( menuHeaderID == ( m_menuBar[m_menuBar.length - 1].Id + "Hdr" ) )
	{
		left += m_header.offsetWidth - m_menu.offsetWidth;
	}*/
	
	// add the width of the header, and width of extra image.
	//
	top += (m_header.offsetHeight);
				
	shiftTo( m_menu, left, top );

	hideSelectCtrl();

	if( !isVisible(m_menu) )
	{
		if( isIE6 && typeof(m_menu.filters) != "undefined" && m_menu.filters && m_colorDepth > 8 )
		{
			m_menu.filters.item(0).Apply();
			m_menu.filters.item(1).Apply();
		}
		
		show(m_menu);

		if( isIE6 && typeof(m_menu.filters) != "undefined" && m_menu.filters && m_colorDepth > 8 )
		{
			m_menu.filters.item(0).Play();
			m_menu.filters.item(1).Play();
		}
	}
}

// Hide the current menu
//
function hideMenu()
{
	if( isDHTML && m_menu )
	{
		hideSubMenu();

		hide(m_menu);
		m_header.className			= "siteMenuBtn";
		m_menu						= null;
	}
}

function hideSubMenu()
{
	if( isDHTML && m_subMenu )
	{
		hide(m_subMenu);
		m_subMenuEvtCtl				= null;
		m_subMenu					= null;

		clearHilite( m_menu );
	}
}

function hideMenus()
{
	if( timerID )
	{
		clearTimeout( timerID );
		timerID = null;
	}
	
	hideMenu();
}

// hide/reset the current menu, but only if we're
// not moving onto the menu itself
//
function resetMenu( evt )
{
	if( isDHTML )
	{
		evt = evt ? evt : event;

		if( timerID )
		{
			clearTimeout( timerID );
			timerID = null;
		}

		if( m_header != null && m_menu != null )
		{
			var	dest		= eventToElement( evt );
			
			var notSubMenu	= ( m_subMenu != dest && !contains( m_subMenu, dest ) );
			
			// hide the submenu if necessary
			//
			if( m_subMenu && m_subMenuEvtCtl && notSubMenu && m_subMenuEvtCtl != dest && !contains( m_subMenuEvtCtl, dest ) && m_subMenu != dest && !contains( m_subMenu, dest ) )
			{
				hideSubMenu();
			}	

			// proceed if we're not moving onto a menu item
			//
			if( ( !m_subMenu || notSubMenu ) && dest && m_header != dest && !contains( m_header, dest ) && m_menu != dest && !contains( m_menu, dest ) )
			{
				hideSubMenu();

				hide(m_menu);
				m_header.className			= "siteMenuBtn";

				if( m_hilite )
				{
					m_hilite.className = "";
					m_hilite = null;
				}

				m_header							= null;
				m_menu								= null;
				m_hilite							= null;
				showSelectCtrl();
			}
			// work out what dest highlight
			//
			else if( m_menu || m_subMenu )
			{
				var hilite = getHilite( evt );
				
				if( hilite )
				{
					if( m_hilite && ( !m_subMenu || contains( m_subMenu, m_hilite ) ) )
					{
						m_hilite.className = "siteMenuItem";
						m_hilite = null;
					}

					var content = hilite.innerHTML;
					
					if( content.indexOf( "siteMenuItem" ) > 0 )
					{
						m_hilite = hilite;
						m_hilite.className = "siteMenuItemHover";
					}
				}
			}

			evt.cancelBubble = true;
		}
	}
}

function clearHilite( table )
{
	var	cell, row, count, ix;
	
	count = table.rows.length;
	
	for( ix = 0; ix < count; ix++ )
	{
		table.rows[ix].className = "";
	}
}

function getHilite( evt )
{
	evt			= evt ? evt : event;
	
	var hilite	= null;
	var	to		= eventToElement( evt );
	
	if( to && ( m_menu && contains( m_menu, to ) ) || ( m_subMenu && contains( m_subMenu, to ) ) )
	{
		hilite = to;
		
		while( hilite && hilite.tagName.toLowerCase() != "tr" )
		{
			hilite = parentNode( hilite );
		}
		
		if( hilite )
		{
			var menuTable = parentNode( parentNode( hilite ) );
			
			if( menuTable && menuTable.className && menuTable.className == "subContainer" )
			{
				var container = parentNode( menuTable );
				
				if( container.tagName.toLowerCase() == "td" )
				{
					hilite = parentNode( container );
				}
			}
		}
	}
	
	return hilite;
}







// -------------------------------------------------------------
// HTML workarounds
// -------------------------------------------------------------

// Show SELECT controls (dropdown lists) when menu is hidden
//
function showSelectCtrl()
{
	var obj;
	var tags = getObjectsByTag("select");
	
	for( var i = 0; i <tags.length; i++ )
	{
		obj = tags[i];
		if(obj && obj.offsetParent)
		{
			show(obj);
		}
	}
}

// Hide SELECT controls (dropdown lists), otherwise the Select will
// appear on top of the menu (HTML workaround)
//
function hideSelectCtrl()
{
	hideCtrl( getObjectsByTag("select") );
}

function hideCtrl( tags )
{
	var obj;
	var currentEle;
	var menuHeight;
	var timeout;
	var top			= 0;
	var left		= 0;
	
	for( var i = 0; i < tags.length; i++ )
	{
		obj			= tags[i];
		currentEle	= obj;
	
		while( currentEle && currentEle.tagName.toLowerCase() != 'body' )
		{
			top			+= currentEle.offsetTop;
			left		+= currentEle.offsetLeft;
			currentEle	 = currentEle.offsetParent;
		}

		if(m_menu != null)
		{
			menuHeight = ( m_menu.offsetTop + m_menu.offsetHeight );
			
			if( top < menuHeight )
			{
				if((left < (m_menu.offsetLeft + m_menu.offsetWidth)) && (left + obj.offsetWidth > m_menu.offsetLeft)) 
				{
					hide(obj);
				}
			}
		}

		top		= 0;
		left	= 0;
	}
}

// -------------------------------------------------------------
// client-side masthead
// -------------------------------------------------------------

var m_production	= true;

function writeMH()
{
	autoconfig();

	if( !m_production && typeof(m_menuBar) == "undefined" )
	{
		document.write( "<div class=\"para\" style=\"color:red; font-weight:bold\">There is a problem with the menu definition.</div>" );
		return;
	}

	m_stdOffImg = "<img src=\"" + m_imgPfx + "/smextlink.gif\", width=\"16\" height=\"9\" border=\"0\"/>";

	if( isDHTML )
	{
		for( var n = 0; n < m_menuBar.length; n++ )
		{
			if( m_menuBar[n].MenuItems )
			{
				renderItems( m_menuBar[n], 100 );
			}
		}
	}
	
	if( document.body )
	{
		document.body.onmouseover	= resetMenu;
		document.body.onmouseout	= resetMenu;
	}
	else
	{
		var bodytags = getObjectsByTag( "body" );
		
		if( bodytags && bodytags.length > 0 )
		{
			bodytags[0].onmouseover	= resetMenu;
			bodytags[0].onmouseout	= resetMenu;
		}
	}
	
	// nav strip
	document.write( "<TABLE border=\"0\" align=\"left\" cellpadding=\"0\" cellspacing=\"0\" style=\"height:24px;\"><TR>" );
	document.write( "<TD align=\"center\" valign=\"middle\" style=\"width:17px;\"><IMG src=\"" + m_imgPfx + "/Bullets/ArrowsDblOrg.gif\" width=\"11\" height=\"9\"></TD>" );
		
	if( typeof(m_menuBar) != "undefined" && m_menuBar && m_menuBar.length > 0 )
	{
		renderMenuStrip();
	}
	
	document.write( "</TR></TABLE>" );
}
