/**
 * JavaScript Document
 * Title: alm.horiz-menu.js
 * Purpose: Adds interactivity to a menu styled with a
 * customized version of 'alm.horiz-menu.css'
 * @author Justin Webb - ALM.com
 *
 */


/*
	Load external JS APIs into browser 
*/
if (typeof(jQuery) == "undefined")
{
	document.write('<scr'+'ipt type="text/javascript" src="js/jquery-1.3.2.min.js"></scr'+'ipt>');
}


/* 
	Global entry method
*/
$(document).ready(
	
	initHorizMenu //Entry method for 'alm.horiz-menu.js'
		  
);

/*
	Define event handler functions
*/
function initHorizMenu(event)
{
	var $li = jQuery('div.horiz-menu#horizontalnav ul > li');
	
	//Bind event handlers to horiz-menu HTML, set initial viewstate
	if (jQuery('li#here').length == 0)
	{
		$li.bind('mouseover', setSubnavByMainLI);
		jQuery('div.horiz-menu#horizontalnav').mouseleave(unsetSubnav);
	}
	else
	{
		$li.bind('mouseover', setSubnavByMainLI);
		jQuery('div.horiz-menu#horizontalnav').mouseleave(setSubnavByHereID);
		setSubnav('li#here');
	}
}
function setSubnavByHereID(event)
{	
	setSubnav('li#here');
}
function unsetSubnav(event)
{
	//Remove hover class
	jQuery('li.primarynavHover').removeClass('primarynavHover');
	jQuery('#secondarynav').empty().append('<li class="spaceholder"><a href="">&nbsp;</a></li>');
}
function setSubnavByMainLI(event)
{	
	setSubnav(this);
}


/* 
	Define utility functions
*/
function setSubnav(mainLITarget)
{
	//Switch hover class
	jQuery('li.primarynavHover').removeClass('primarynavHover');
	jQuery(mainLITarget).addClass('primarynavHover');
	
	//Get the target main list item's subnav and transfer
	//it to the proxy list
	var subnavLIs = jQuery(mainLITarget).children('ul').html();
	jQuery('#secondarynav').empty().append(subnavLIs);
}
