/*
 * Popup Navigation Menus
 * for SCI Web Site
 * by Jamie Green
 *
 * 5/6/05 JCG:
 * First release
 *
 * 5/11/05 JCG:
 * Fixed a bug which caused a menu to disappear if the user moused onto a main menu item that
 * didn't have a submenu, and back quickly.
 * 
 * 5/20/05 JCG:
 * iCab thinks it can do the menus, but it's wrong.
 */


/****************
 * 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 = 500; // The amount of time to wait before closing the menu, in milliseconds.

// Determine whether the browser can operate the menus.
// Any browser that has the "getElementById" function should be able. Except iCab. :P
var canDoMenus = document.getElementById
                 && navigator.userAgent.indexOf("iCab") == -1;


/****************
 * 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
 * Create and show/hide the menu.
 */

//--------
// Make the HTML for the popup nav menus. Call this code in the 'menu-items' div.
// By doing this, we allow the client to cache the menus locally, keeping pages smaller.
// The "thisSection" parameter specifies which section of the site the current page is in,
// so the menu for that section won't activate.
function nav_makeMenus( thisSection ) {
	
 //	Only make menus if the browser can pop them up. iCab thinks it can, but it's wrong :)
	if( canDoMenus ) {
		
	 // Make an array to hold the preloaded menu images.
		var menuImages = new Array();
		var j = 0;
		
	 //	A handy little function to add images to the array.
	 //	Takes the section as the first parameter and a list of menu image names.
		function preloadImages( theSection ) {
			for( i = 1; i < arguments.length; i++ ) {
				menuImages[j] = new Image();
				menuImages[j++].src = '/images/menu/' + theSection + '/' + arguments[i] + '.gif';
			}
		}
		
	 //	I use tables instead of divs because IE 5 for the Mac renders the menus full width of the
	 //	parent div otherwise.

	 //	Only write menus for other sections of the site.
		if( thisSection != 'about' ) {
			
			preloadImages( 'about', 'news', 'press', 'customers', 'partners', 'email_list', 'contact', 'about_this_site' );
			
			document.writeln("	<table id='about-popup' class='popup-menu' border='0' cellspacing='0' cellpadding='0' onMouseOver='nav_cancelHiding()' onMouseOut='nav_hideDelay()'>" );
			document.writeln("	<tr><td><a href='/about/news/' onClick='nav_hideOpenMenu()'><img src='/images/menu/about/news.gif' width='38' height='15' border='0' alt='[NEWS]'></a></td></tr>" );
			document.writeln("	<tr><td><a href='/about/press/' onClick='nav_hideOpenMenu()'><img src='/images/menu/about/press.gif' width='40' height='15' border='0' alt='[PRESS]'></a></td></tr>" );
			document.writeln("	<tr><td><a href='/about/customers.html' onClick='nav_hideOpenMenu()'><img src='/images/menu/about/customers.gif' width='69' height='15' border='0' alt='[CUSTOMERS]'></a></td></tr>" );
			document.writeln("	<tr><td><a href='/about/partners.html' onClick='nav_hideOpenMenu()'><img src='/images/menu/about/partners.gif' width='62' height='15' border='0' alt='[PARTNERS]'></a></td></tr>" );
			document.writeln("	<tr><td><a href='http://lists.sophisticated.com/' onClick='nav_hideOpenMenu()'><img src='/images/menu/about/email_list.gif' width='62' height='15' border='0' alt='[EMAIL LIST]'></a></td></tr>" );
			document.writeln("	<tr><td><a href='/about/contact.html' onClick='nav_hideOpenMenu()'><img src='/images/menu/about/contact.gif' width='56' height='15' border='0' alt='[CONTACT]'></a></td></tr>" );
			document.writeln("	<tr><td><a href='/about/this_site.html' onClick='nav_hideOpenMenu()'><img src='/images/menu/about/about_this_site.gif' width='93' height='15' border='0' alt='[ABOUT THIS SITE]'></a></td></tr>" );
			document.writeln("	</table>");
			document.writeln("	");
		}
		
		if( thisSection != 'products' ) {
			
			preloadImages( 'products', 'reliability', 'power_control', 'automation', 'accessories' );
			
			document.writeln("	<table id='products-popup' class='popup-menu' border='0' cellspacing='0' cellpadding='0' onMouseOver='nav_cancelHiding()' onMouseOut='nav_hideDelay()'>" );
			document.writeln("	<tr><td><a href='/products/reliability.html' onClick='nav_hideOpenMenu()'><img src='/images/menu/products/reliability.gif' width='67' height='15' border='0' alt='[RELIABILITY]'></a></td></tr>" );
			document.writeln("	<tr><td><a href='/products/powercontrol.html' onClick='nav_hideOpenMenu()'><img src='/images/menu/products/power_control.gif' width='97' height='15' border='0' alt='[POWER CONTROL]'></a></td></tr>" );
			document.writeln("	<tr><td><a href='/products/automationtools.html' onClick='nav_hideOpenMenu()'><img src='/images/menu/products/automation.gif' width='75' height='15' border='0' alt='[AUTOMATION]'></a></td></tr>" );
			document.writeln("	<tr><td><a href='/products/accessories.html' onClick='nav_hideOpenMenu()'><img src='/images/menu/products/accessories.gif' width='76' height='15' border='0' alt='[ACCESSORIES]'></a></td></tr>" );
			document.writeln("	</table>");
			document.writeln("	");
		}
		
		if( thisSection != 'purchase' ) {
			
			preloadImages( 'purchase', 'order_form', 'north_amer_resellers', 'worldwide_resellers', 'partners' );
			
			document.writeln("	<table id='purchase-popup' class='popup-menu' border='0' cellspacing='0' cellpadding='0' onMouseOver='nav_cancelHiding()' onMouseOut='nav_hideDelay()'>" );
			document.writeln("	<tr><td><a href='https://secure.sophisticated.com/ordering/order_form.html' onClick='nav_hideOpenMenu()'><img src='/images/menu/purchase/order_form.gif' width='75' height='15' border='0' alt='[ORDER FORM]'></a></td></tr>" );
			document.writeln("	<tr><td><a href='/purchase/naresellers.html' onClick='nav_hideOpenMenu()'><img src='/images/menu/purchase/north_amer_resellers.gif' width='159' height='15' border='0' alt='[NORTH AMERICAN RESELLERS]'></a></td></tr>" );
			document.writeln("	<tr><td><a href='/purchase/wwresellers.html' onClick='nav_hideOpenMenu()'><img src='/images/menu/purchase/worldwide_resellers.gif' width='128' height='15' border='0' alt='[WORLDWIDE RESELLERS]'></a></td></tr>" );
			document.writeln("	<tr><td><a href='/purchase/partners.html' onClick='nav_hideOpenMenu()'><img src='/images/menu/purchase/partners.gif' width='62' height='15' border='0' alt='[PARTNERS]'></a></td></tr>" );
			document.writeln("	</table>");
			document.writeln("	");
		}
		
		if( thisSection != 'support' ) {
			
			preloadImages( 'support', 'downloads', 'faq', 'knowledge_base', 'product_manuals', 'register' );
			
			document.writeln("	<table id='support-popup' class='popup-menu' border='0' cellspacing='0' cellpadding='0' onMouseOver='nav_cancelHiding()' onMouseOut='nav_hideDelay()'>" );
			document.writeln("	<tr><td><a href='/support/downloads/' onClick='nav_hideOpenMenu()'><img src='/images/menu/support/downloads.gif' width='72' height='15' border='0' alt='[DOWNLOADS]'></a></td></tr>" );
			document.writeln("	<tr><td><a href='/support/faq/' onClick='nav_hideOpenMenu()'><img src='/images/menu/support/faq.gif' width='29' height='15' border='0' alt='[FAQ]'></a></td></tr>" );
			document.writeln("	<tr><td><a href='/support/kbase/' onClick='nav_hideOpenMenu()'><img src='/images/menu/support/knowledge_base.gif' width='101' height='15' border='0' alt='[KNOWLEDGE BASE]'></a></td></tr>" );
			document.writeln("	<tr><td><a href='/support/manuals.html' onClick='nav_hideOpenMenu()'><img src='/images/menu/support/product_manuals.gif' width='109' height='15' border='0' alt='[PRODUCT MANUALS]'></a></td></tr>" );
			document.writeln("	<tr><td><a href='/register/reg_form.html' onClick='nav_hideOpenMenu()'><img src='/images/menu/support/register.gif' width='56' height='15' border='0' alt='[REGISTER]'></td></tr>" );
			document.writeln("	</table>");
			document.writeln("	");
		}
	}
}


//--------
// 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();
		}
	}
}


