/*
 * Subnavigation popup script v 1.0
 * Copyright NCYCA www.nccamps.org
 */
// Navigation
var theSpeed = 300; //300;						// animation speed for showing menus
var closeSubMenuDelay = 400; //400;  // delay to close the current open menu after the mouse leaves it
var openSubMenuDelay = 100; //100; 	// delay to open the sub menu after the mouse goes over the main menu item

var subMenuActive = '';				// the currently active sub menu
var subMenuRequested = '';

/*
	Usage:  
	All main items must have a class of drop_menu and be assigned an id.  The id can be anything
	All sub menus must have a class of sub_nav.  The id must be the same as the main item id prepended with sub_.  
		thus main id of menu_about, sub menu div must have an id of sub_menu_about
	It is ok if a menu item does not have a sub menu
*/


function onOverMenu() {
	var itemId = $(this).attr('id');
	var targetId = "sub_" + itemId;

	// stop any pending events for this menu item
	var theTimer = $(this).attr('timerid');
	if(theTimer) {
		clearTimeout(theTimer);
	}
	
	// request this submenu to open
	subMenuRequested = targetId;
	$(this).attr('timerid', setTimeout(updateMenus, openSubMenuDelay) );
}


function onOutMenu() {
	// stop any pending events for this menu item
	subMenuRequested = '';
	var theTimer = $(this).attr('timerid');

	if(theTimer) {
		clearTimeout(theTimer);
	}

	// schedule to close the presently active sub menu
	$(this).attr('timerid', setTimeout(updateMenus, closeSubMenuDelay) );
}

function onOverSubMenu() {
	var targetId = $(this).attr('id');

	// override the menu requested to the sub menu entered
	subMenuRequested = targetId;
}

function onOutSubMenu() {
	// the submenu has been exited, start the timer to close the sub menu
	subMenuRequested = '';
	
	var theTimer = $(this).attr('timerid');
	if(theTimer) {
		clearTimeout(theTimer);
	}

	// schedule to close the presently active sub menu
	$(this).attr('timerid', setTimeout(updateMenus, closeSubMenuDelay) );
}

// called via timeout to control the sub menu display
function updateMenus() {
	if(subMenuActive == subMenuRequested) return;  // don't do anything if the menu to display is current
	subMenuActive = subMenuRequested;

	// show the requested menu
	if(subMenuActive != '')
		$('#' + subMenuActive).addClass('active').stop(true,true).slideDown(theSpeed);
		
	// hide all others
	$('.sub_nav').each(function(i) {
			if($(this).hasClass('active') && $(this).attr('id') != subMenuActive)
				$(this).removeClass('active').stop(true,true).slideUp(theSpeed);
		});
}


$(function(){	
	$('.drop_menu').hover(onOverMenu, onOutMenu);
	$('.sub_nav').hover(onOverSubMenu, onOutSubMenu);
	$('.drop_menu').click(function(e) {
		if($(e.target).attr('href') == '#')
			e.preventDefault();
		});
});

