// make sure popwin initializes (see onlaod.js)
if (initialize) { initialize.navigation = true }

var navigation = new Object();

/**
* This function initializes [1] rollover images in all browsers, [2] rollover input buttons in all browsers, and [3] the primary/secondary navigation dhtml menu in IE-only. The primary/secondary navigation menu is also CSS driven for browsers that understand li:hover (Safari, Firefox, IE7, etc).
*/
navigation.ini =function()
{
	// exit function if you are not a modern browser
	if (!document.getElementById) return;

	// get a collections of images
	var cImages = document.getElementsByTagName('IMG');

	// attach images states to images in collection
	navigation.initializeImageStates( cImages );

	// get a collections of inputs
	var cInputs = document.getElementsByTagName('INPUT');

	// attach images states to images in collection
	navigation.initializeImageStates( cInputs );

	
	// detect IE, but block IE7 (both bugs are fixed in IE7)
	if (document.attachEvent&&!window.XMLHttpRequest)
	{
		oNav = document.getElementById("nav1");
		if ( oNav )
		{
			for (i=0; i<oNav.childNodes.length; i++)
			{
				node = oNav.childNodes[i];
				if (node.nodeName=="LI")
				{
					// insert iframe to block select boxes from showing through menu
					var oFrame = document.createElement('IFRAME');
					if ( location.href.indexOf('https://') > -1 ) { oFrame.src = "/blank.html"; } else { oFrame.src = "about:blank"; }
					//oFrame.src = "/blank.html";
					oFrame.scrolling = "no";
					oFrame.frameborder = "0";
					node.insertBefore(oFrame,node.firstChild);
					node.shieldDeployed = false;
					
					// store nav1 anchor tag so it can be sticky when menu is down
					node.nav1_oA = node.getElementsByTagName("A")[0];

					// attach events to show/hide menu. IE6 does not understand li:hover
					// use onmouseenter and onmouseleave because they fire less often (better performance)
					node.onmouseenter = navigation.showMenu;
					node.onmouseleave = navigation.hideMenu;
				}
			}
		}
	}

	// detect Safari to prevent SWF/dHTML issues onHover
	if ( navigator.vendor && navigator.vendor.indexOf('Apple') != -1 )
	{
		// get all the stylesheets on a given page
		var styleSheets = document.styleSheets;

		// loop through those stylesheets
		for (var x = 0; x < styleSheets.length; x++)
		{
			// if the current stylesheet is foundation.css (skip all others to save time)
			if (styleSheets[x].href && styleSheets[x].href.indexOf('foundation.css') != -1 )
			{
				// collection of rules from foundation.css
				var cssRules = styleSheets[x].cssRules;

				var cssLookup = new Object();
				// selectorText for MacOS 10.4
				cssLookup['#nav1 li li a:hover'] = 1;
				cssLookup['#nav1 li li a:hover span'] = 1;
				cssLookup['#nav1 li li a.current:hover'] = 1;
				cssLookup['#nav1 li li a.current:hover span'] = 1;
				// selectorText for MacOS 10.3
				cssLookup['*[ID"nav1"] LI LI A:'] = 1;
				cssLookup['*[ID"nav1"] LI LI A: SPAN'] = 1;
				cssLookup['A.current[CLASS"current"]*[ID"nav1"] LI LI :'] = 1;
				cssLookup['A.current[CLASS"current"]*[ID"nav1"] LI LI : SPAN'] = 1;

				// same as above, except these selectors are the last to be found so they will break the loop below
				var cssStop = new Object();
				// selectorText for MacOS 10.4
				cssStop['#nav1 li li a.current:hover span'] = 1;
				// selectorText for MacOS 10.3
				cssStop['A.current[CLASS"current"]*[ID"nav1"] LI LI : SPAN'] = 1;

				// loop through the rules in foundation.css and delete the :hover styles to prevent flicker over a SWF
				for (var y = 0; y < cssRules.length; y++)
				{
					var rule = cssRules[y];

					if (cssLookup[rule.selectorText] == 1 )
					{
						rule.style.cssText = '';
						if (cssStop[rule.selectorText] == 1 )	{ break; }
					}
				}

				// place DIV tags inside SPAN tags to replicate functionality lost above
				var oNav = document.getElementById("nav1");
				if ( oNav )
				{
					for (var i=0; i<oNav.childNodes.length; i++)
					{
						var node = oNav.childNodes[i];
						if (node.nodeName=="LI")
						{
							var oUL = node.getElementsByTagName("UL")[0];
							var cSP = oUL.getElementsByTagName("SPAN");

							for (var j=0; j<cSP.length; j++)
							{
								var oSP = cSP[j];
								oSP.innerHTML = oSP.innerHTML + '<div>' + oSP.innerHTML + '</div>';
							}
						}
					}
				}

			} // close if foundation.css
		} // close loop through stylesheets
	} // close safari detection if statement
}

/**
* This function cycles through the collection of HTML DOM elements to attach images states (default and onmouseover) if necessary/possible.
* @param    cElements    a collection of HTML DOM elements (IMGs or INPUTs)
*/
navigation.initializeImageStates = function( cElements )
{
	var aPreLoad = new Array();
	var sTempSrc;

	for (var i = 0; i < cElements.length; i++)
	{
		var oElement = cElements[i];
		
		if ( oElement.src )
		{
			var sDefaultSrc = oElement.src;
		
			if (sDefaultSrc.lastIndexOf('_0.gif') == sDefaultSrc.length-6)
			{
				var sHoverSrc = sDefaultSrc.replace('_0.gif', '_1.gif');

				oElement.src_0 = sDefaultSrc;
				oElement.src_1 = sHoverSrc;
				
				aPreLoad[i] = new Image();
				aPreLoad[i].src = sHoverSrc;
				
				oElement.onmouseover = navigation.toHoverState;
				oElement.onmouseout  = navigation.toDefaultState;
			}
			else if(sDefaultSrc.lastIndexOf('_0.png') == sDefaultSrc.length-6)
			{
				var sHoverSrc = sDefaultSrc.replace('_0.png', '_1.png');

				oElement.src_0 = sDefaultSrc;
				oElement.src_1 = sHoverSrc;
				
				aPreLoad[i] = new Image();
				aPreLoad[i].src = sHoverSrc;
				
				oElement.onmouseover = navigation.toHoverState;
				oElement.onmouseout  = navigation.toDefaultState;
			}
			
		}
	}
}

/**
* This function changes an IMG or INPUT tag source to the default state.
*/
navigation.toDefaultState = function()
{
	this.src = this.src_0;
}

/**
* This function changes an IMG or INPUT tag source to the hover state.
*/
navigation.toHoverState = function()
{
	this.src = this.src_1;
}

/**
* This function is used to SHOW the primary/secondary navigation menus by browsers that do not understand li:hover in CSS.
*/
navigation.showMenu = function()
{
	this.className += " over";
	this.nav1_oA.className += " over";

	// only deploy/initialize shield once
	// nav2 menu must be visible for it to have dimensions that Javascript can read/apply to the iFrame
	if (this.shieldDeployed == false)
	{
		var oFrame = this.getElementsByTagName("IFRAME")[0];
		var oMenu = this.getElementsByTagName("UL")[0];
		
		if( oFrame && oMenu )
		{
			oFrame.style.left = oMenu.offsetLeft + "px";
			oFrame.style.width = oMenu.offsetWidth + "px";
			oFrame.style.height = oMenu.offsetHeight + "px";
		}
		this.shieldDeployed = true;
	}
}

/**
* This function is used to HIDE the primary/secondary navigation menus by browsers that do not understand li:hover in CSS.
*/
navigation.hideMenu = function()
{
	this.className = this.className.replace(" over", "");
	this.nav1_oA.className = this.nav1_oA.className.replace(" over", "");
}