/*=============================================================================
TITLE:			Simple Dropdown Menu Controller
VERSION:		2005.06.27
AUTHOR: 		Graham Wheeler / NetMediaOne - www.netmediaone.com
=============================================================================*/

var activeMenu = "NULL";  					// Holds the index of the active menu
var menuCollection = new Array(); 	// Holds references to each menu element
var menuPrefix = "menu";						// Sets the prefix used to identify menu elements
var menuHeaderPrefix = "nav";			// Sets the string prefix used to identify navigational elements bound to menus
var menuTimer = null;

// menuInit
// -Call this function using the onload attribute of the BODY tag
// -Looks for all DIV elements with class 'NavMenu' and hooks up the appropriate SHOW/HIDE logic
// -Looks for all elements in the nav bar and hooks them up to the appropriate menu
function menuInit()
{
	// Locate all menus and stuff them into the menuCollection
	tags = document.getElementsByTagName("DIV");
	for ( t = 0; t < tags.length; t++ )
	{
		if ( tags[t].className == "NavMenu" )
		{
		  menuElement = tags[t];
			menuCollection[menuCollection.length] = menuElement;
		}
	}
	// Loop through the menuCollection and attach menu event handlers to each menu and menu item
	for ( menuIndex = 0; menuIndex < menuCollection.length; menuIndex++ )
	{
		menu = menuCollection[menuIndex];
		menuHeader = document.getElementById( menuHeaderPrefix + menu.id.replace( menuPrefix, '' ) );
		menuHeader.onmouseover = menuShow;
		menuHeader.onmouseout = blurMenus;
		menuItems = menu.getElementsByTagName("LI");
		for ( i = 0; i < menuItems.length; i++ )
		{
			menuItems[i].onmouseover = function() {
																							menu = menuCollection[menuIndex];
																							menu.style.visibility = "visible";
																							activeMenu = menu.id; }
			menuItems[i].onmouseout = blurMenus;
	 	}
	}
}

// getMenuIndex
// Returns the position of a menu in the menuCollection array, given the menu's ID (or -1 if not found)
function getMenuIndex( menuID )
{
	menuIndex = -1;
	for ( i = 0; i < menuCollection.length; i++ )
	{
		if ( menuCollection[i].id == menuID )
		{
			menuIndex = i;
		}
	}
	return menuIndex;
}

// menuShow
// Sets the selected menu to visible
function menuShow( e )
{
	// Determine which element called the event (with support for various browser architectures)
	var targ;
	if ( !e ) { var e = window.event; }
	if ( e.target ) { targ = e.target; }
	else if( e.srcElement ) { targ = e.srcElement; }
	if ( targ.nodeType == 3 ) { targ = targ.parentNode; }
	
	// Translate the element id to a menu id and call up the appropriate menu
	clearTimeout(menuTimer);
	hideMenus();
	menuIndex = getMenuIndex( menuPrefix + targ.id.replace( menuHeaderPrefix, "" ) );
	menu = menuCollection[menuIndex];
	menu.style.visibility = "visible";
	activeMenu = menu.id;
}

// blurMenus
// Sets a timer which executes hideMenus() after the specified number of milliseconds
function blurMenus()
{
	activeMenu = 'NULL';
	clearTimeout(menuTimer);	
 	menuTimer = setTimeout( "hideMenus()", 500 );
}

// hideMenus
// Loops through the menuCollection, setting all menu elements invisible. If activeMenu has a
// value that corresponds with a menu ID, then that particular menu will be set visible.
function hideMenus()
{
	for ( i = 0; i < menuCollection.length; i++ )
	{
		menu = menuCollection[i];
		if ( activeMenu == menu.id )
		{
			menu.style.visibility = "visible"
		}
		else
		{
			menu.style.visibility = "hidden";
		}
	}
}

// Figures out which event model the user's browser supports and attaches an event to
// initialize the menu script on page load
if ( document.addEventListener )
{
	window.addEventListener( 'load', menuInit, false );
}
else if ( document.attachEvent )
{
	window.attachEvent( 'onload', menuInit );
}

