// JavaScript Document - Main Menu
// Josh Leroux, Required: JQUERY

var menuDiv = undefined;
var timerID = 0;
var activeLink = undefined;
var nextLink = undefined;

// Applies 
function setupMainNav()
{
    if( POOLGLOBAL.style != "STANDARD" )
    	return;

	$('#menuContainer').append("<div id='mainNavSubMenu'></div>");
	menuDiv = $('#mainNavSubMenu');
	
	menuDiv.bind('mouseout', hideMenu);
	menuDiv.bind('mouseover', cancelMenu);
	
	var mainMenuItems = $('#mainNav>ul').children();
	
	for(var n=0;n<mainMenuItems.length;n++)
	{
		if( mainMenuItems[n].firstChild.tagName == "A" )
		{
			$(mainMenuItems[n].firstChild).bind('mouseover', showMenu);	
			$(mainMenuItems[n].firstChild).bind('mouseout', hideMenu);	
		}
	}
	
	// Setup Segment Links
	$("#segmentLinks a").bind('mouseover', showMenu)
	$("#segmentLinks a").bind('mouseout', hideMenu);
}

function showMenu(e)
{
	cancelMenu();
	nextLink = this;
	timerID = setTimeout("_showMenu()", 250);
}

function _showMenu()
{
  	cancelMenu();
	
	if( activeLink != nextLink )
	{
		activeLink = nextLink;
		nextLink = undefined;
		var menuHTML = $('>ul',activeLink.parentNode).html();
		if( menuHTML != null )
		{
			var pos = $(activeLink.parentNode);
			var offset = pos.offset();
			menuDiv.css("top",offset.top + 30); // 30 is pos.height() hardwire for performance
			menuDiv.css("left",offset.left);
			
			menuDiv.removeClass();
			menuDiv.addClass($(activeLink).attr("class"));
			
			menuDiv.css( { "background-color": $(activeLink).css("background-color") });
			
			menuDiv.html("<ul>" + menuHTML + "</ul>");
			
			menuDiv.bgiframe(); // ie 6
			menuDiv.show();
		}
	}
}

function cancelMenu(e)
{
	if( timerID > 0 )
	{
	 clearTimeout(timerID);
	 timerID = 0;
	}
}

function hideMenu(e)
{
	if( timerID > 0 )
	  cancelMenu();
	else
	  timerID = setTimeout("_hideMenu()", 750);
}

function _hideMenu()
{
	cancelMenu();
	activeLink = undefined;
	//menuDiv.slideUp(100);
	menuDiv.hide();
}

function hideMenuNow()
{
    if( POOLGLOBAL.style == "STANDARD" )
    {
		cancelMenu();
		activeLink = undefined;
		if( menuDiv != undefined )
		  menuDiv.hide();
    }
}

$(document).ready(setupMainNav);
$(document).click(hideMenuNow);
