/*
 * Drop-down Navigation Menus
 * for www.transformational-health.com
 * by Jamie Green
 *
 * 2010.02.08 JCG:
 * First release
 */


/****************
 * Variables and constants
 * Assorted values used by the script.
 */

var theDelay = 0; // The delay timer itself.
var theOpenMenu = 'none'; // Which menu is currently open.
var theDelayValue = 200; // The amount of time to wait before closing the menu, in milliseconds.

// Determine whether the browser can display and operate the menus.
// Only Internet Explorer 7.0 or newer can properly display the menus.
// Any browser that has the "getElementById" function should be able to operate the menus.
var ieversion = 99;
if( /MSIE (\d+\.\d+);/.test( navigator.userAgent ) ) // Test for "MSIE x.x;".
	ieversion = RegExp.$1 // capture "x.x" portion and store as a number

var canDoMenus = ( ieversion >= 6 )
                 && ( document.getElementById != null );

function nav_test() {
	document.write( ieversion + ' - ' + ( document.getElementByID != null )
	+ ' - ' + canDoMenus );
}


/****************
 * Delay functions
 * Delay hiding the menu when the mouse moves outside it, to facilitate staying
 * open when the mouse moves from the main menu to the submenu.
 */

//--------
// Set the timer.
// Used when the mouse moves off of a menu or submenu.
function nav_hideDelay() {

 //	Only do anything if a menu is currently open. (This situation crops up if
 //	the page is resumed by the user clicking the browsers back button.)
	if( theOpenMenu != 'none' )
		theDelay = setTimeout( "nav_hideOpenMenu()", theDelayValue );
}


//--------
// Clear the timer.
// Called when the mouse moves back onto a menu that is currently open.
// Also called when a menu is hidden, to clear any lingering timer.
function nav_cancelHiding() {

 //	Only do anything if there is an active timer.
	if( theDelay != 0 ) {
		clearTimeout( theDelay );
		theDelay = 0;
	}
}


/****************
 * Menu functions
 * Show/hide the menu.
 */

//--------
// Pop up the navigation menu.
function nav_showMenu( theMenuID ) {
	
	if( canDoMenus ) {
		
	 //	If the desired menu is already open, just cancel its hiding timer.
		if( theOpenMenu == theMenuID )
			nav_cancelHiding();
		
		else {
			
		 //	If another menu is open, hide it immediately.
			if( theOpenMenu != 'none' ) {
				nav_cancelHiding();
				nav_hideOpenMenu();
			}
		
		 //	Make the desired menu visible.
			var theMenu = document.getElementById( theMenuID );
			theMenu.style.display = 'block';
			theOpenMenu = theMenuID;
		}
	}
}


//--------
// Hide the open menu.
function nav_hideOpenMenu() {
	
	if( canDoMenus ) {
		
	 //	Only hide the window if one is open.
		if( theOpenMenu != 'none' ) {
			var theMenu = document.getElementById( theOpenMenu );
			theMenu.style.display = 'none';
			theOpenMenu = 'none';
		
		 //	Cancel any hiding timer that may be running
		 //	(i.e. if we were called from a menu button).
			nav_cancelHiding();
		}
	}
}

