﻿//+--------------------------------------------------------------------------+
//| Menu.js							                                         |
//+--------------------------------------------------------------------------+
var navTimer = 0;
var navSubTimer = 0;
var menus = new Object();
var delay = 1000;
window.currentMenu = null;
window.currentSubMenu =  null;
window.staticMenu = null;
window.staticSubMenu = null;

document.hideMenu = function(){
	if ( window.currentMenu != null ) {
		debug('a');
		var off = "turnOff('" + window.currentMenu + "');"
		navTimer = setTimeout(off, delay);
	}
	if ( window.currentSubMenu != null )
	{
		debug('b');
		var offSub = "turnOff('" + window.currentSubMenu + "');"
		navSubTimer = setTimeout(offSub, delay);
	}
}

function turnOff(div){
	toggleLayer(div, 'none');
	if ( div == window.currentMenu )
	{
		setCurrentMenu(null);
	}
	if ( div == window.currentSubMenu )
	{
		setCurrentSubMenu(null);
	}
	
	//if static menu is set, turn that back on
	if (window.staticMenu != null) {
		
		toggleLayer(window.staticMenu,'block');
	}
	if (window.staticSubMenu != null) {
		toggleLayer(window.staticSubMenu,'block');
	}
	
}

function toggleLayer (layer, toggle) {
	var div = document.getElementById(layer);
	if ( div != null ) {
		div.style.display = toggle; 
	}
}

function statify(layerName) {
	if ( document.getElementById(layerName) != null ) {
		debug('statified: ' + layerName);
		window.staticMenu = layerName;
		toggleLayer(window.staticMenu,'block');
	}
	if ( arguments.length == 2 ) {
		if ( document.getElementById(arguments[1]) != null ) {
			debug('statified: ' + arguments[1]);
			window.staticSubMenu = arguments[1];
			toggleLayer(window.staticSubMenu,'block');
		}
	}
}

function prepareNavEvents(){	

	// Get children for primary section
	var children = document.getElementById('primary').getElementsByTagName("LI");	
	for (var i=0; i < children.length; i++)
	{
		// Loop Primary Nav LI
		var child = children[i];
		var img = child.getElementsByTagName('IMG')[0];

		if ( child.getAttribute("rel") )
		{

			var menu = child.getAttribute("rel");
			child.setAttribute("menuName", menu);
			
			if ( img != null )
			{
				// Primary Events for Items With Children
				img.onmouseover = function(){
					if ( typeof navTimer == 'number' ) clearTimeout(navTimer);
					if ( typeof navSubTimer == 'number' ) clearTimeout(navSubTimer);
					if (window.currentMenu != null)
					{
						toggleLayer (window.currentMenu, 'none');
						setCurrentMenu(null);
					}
					if (window.currentSubMenu != null)
					{
						toggleLayer (window.currentSubMenu, 'none');
						setCurrentSubMenu(null);
					}
					if (window.staticMenu != null)
					{
						toggleLayer (window.staticMenu, 'none');
					}
					if (window.staticSubMenu != '')
					{
						toggleLayer (window.staticSubMenu, 'none');
					}

					var activate = this.parentNode.parentNode.getAttribute("menuName");
					setCurrentMenu(activate);
					toggleLayer(activate,'block');
				}
				img.onmouseout = function(){
					//setCurrentMenu(this.parentNode.parentNode.getAttribute("menuName"));
					if ( typeof document.hideMenu == "function" ) document.hideMenu();
				}
				
				// Apply Secondary Events
				var childrensub = document.getElementById(menu).getElementsByTagName("LI");	
				
				// Loop Secondary Nav LI
				for ( var x=0;x<childrensub.length; x++ )
				{
					var childsub = childrensub[x];
					childsub.setAttribute("menuName", menu);
					
					var imgsub = childsub.getElementsByTagName('IMG')[0];
					
					if ( childsub.getAttribute('rel') )
					{
						var subMenu = childsub.getAttribute('rel');
						childsub.setAttribute("menuSubName", subMenu);

						// Set 2nd Level Mouseovers With Tertiary
						imgsub.onmouseover = function(){
							if ( typeof navTimer == 'number' ) clearTimeout(navTimer);
							if ( typeof navSubTimer == 'number' ) clearTimeout(navSubTimer);
							if ( window.staticSubMenu != null)
							{
								toggleLayer (window.staticSubMenu, 'none');
							}
							var activateSub = this.parentNode.parentNode.getAttribute("menuSubName");
							setCurrentSubMenu(activateSub);
							toggleLayer(activateSub,'block');
						}
						imgsub.onmouseout = function(){
							//setCurrentMenu(this.parentNode.parentNode.getAttribute("menuName"));
							//setCurrentSubMenu(this.parentNode.parentNode.getAttribute("menuSubName"));
							if ( typeof document.hideMenu == "function" ) document.hideMenu();
						}

						var childrenSubSub = document.getElementById(subMenu).getElementsByTagName("LI");

						for ( var y=0;y<childrenSubSub.length; y++ ){
							
							var childsubsub = childrenSubSub[y];
							childsubsub.setAttribute('menuName',menu);
							childsubsub.setAttribute('menuSubName',subMenu);
							
							//debug(childsubsub.innerHTML);
							var hrefs = childsubsub.getElementsByTagName('A')[0];
							
							hrefs.setAttribute('menuName', 't');

							// Set 3nd Level Mouseovers
							hrefs.onmouseover = function() {
								//setCurrentMenu(this.parentNode.getAttribute("menuName"));
								//setCurrentSubMenu(this.parentNode.getAttribute("menuSubName"));
								if ( typeof navTimer == 'number' ) clearTimeout(navTimer);
								if ( typeof navSubTimer == 'number' ) clearTimeout(navSubTimer);
							}
							hrefs.onmouseout = function() {
								if ( typeof document.hideMenu == "function" ) document.hideMenu();
							}
						}

					}else{
						// Set 2nd Level Mouseovers Without Tertiary
						if ( imgsub != null )
						{
							imgsub.onmouseover = function(){
								if ( window.currentSubMenu != null)
								{
									toggleLayer (window.currentSubMenu, 'none');
								}
								setCurrentSubMenu(null);
								if ( window.staticSubMenu != null)
								{
									toggleLayer (window.staticSubMenu, 'none');
								}
								setCurrentMenu(this.parentNode.parentNode.getAttribute("menuName"));
								setCurrentSubMenu(null);
								if ( typeof navTimer == 'number' ) clearTimeout(navTimer);
								if ( typeof navSubTimer == 'number' ) clearTimeout(navSubTimer);
							}
							imgsub.onmouseout = function(){
								//setCurrentMenu(this.parentNode.parentNode.getAttribute("menuName"));
								if ( typeof document.hideMenu == "function" ) document.hideMenu();
							}
						}
					}
				}
			}
		} else {
			
			// Events for Primary Items Without Children
			if ( img != null )
			{
				// apply primary events
				img.onmouseover = function(){
					if ( typeof navTimer == 'number' ) clearTimeout(navTimer);
					if ( typeof navSubTimer == 'number' ) clearTimeout(navSubTimer);
					if (window.currentMenu != null)
					{
						debug('1');
						toggleLayer (window.currentMenu, 'none');
						setCurrentMenu(null);
					}
					if (window.currentSubMenu != null)
					{
						debug('2');
						toggleLayer (window.currentSubMenu, 'none');
						setCurrentSubMenu(null);
					}
					if (window.staticMenu != null)
					{
						toggleLayer (window.staticMenu, 'none');
					}
					if (window.staticSubMenu != '')
					{
						toggleLayer (window.staticSubMenu, 'none');
					}
				}
				img.onmouseout = function(){
					if ( typeof document.hideMenu == "function" ) document.hideMenu();
				}
			}
		}
	}
}

function setCurrentMenu(div) {
	//debug("CurrentMenu:"+div);
	window.currentMenu = div;
}

function setCurrentSubMenu(div) {
	//debug("setCurrentSubMenu:"+div);
	window.currentSubMenu = div;
}

function debug(s){
	//document.getElementById('d').value += s + "\n";
}

document.addEvent(window,'load', prepareNavEvents, false);