/*

=================================================================================================
>> XHTML/CSS/DHTML ADA Complient top horizontal menu 
=================================================================================================
Description:	This script takes a set of <ul>s and turns it into a fully functional
		Horizontal DHTML menu. Browser Resolution detection directs to seperate CSS 
		sheets to control font size and display properties. All that is required is the 
		correct use of class names, and the application of some CSS. 
		
Use:		For use on very large sites. Original menu designed for Otero Junior College.
		

Copyright:	This script is copyright 2008 - 2009 grandallwebdesign.com. All rights reserved.


--------------------------------------------------------------------------------------------------
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
--------------------------------------------------------------------------------------------------
Credits: 	Inspiration from design of various News and Newspaper web sites
--------------------------------------------------------------------------------------------------
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
--------------------------------------------------------------------------------------------------

==================================================================================================
>> TRANSPORTING THE MENU
		Complete Menu with Browser Resolution Styles consists of Three Folders:

		Menu Folder (Menu Script and Menu CSS: gwd_closethree.js, gwd_closethree.css)

		CSS Folder (Browser Resolution Style Sheets: body800.css, body1024.css, body1152.css,
			body1280.css, body1360.css, body1600.css)

		Scripts Folder (Browser Resolution Script: gwdresolutionSHEET.js)
 
		The Menu CSS (gwd_closethree.css) is called from this script rather than in the html document to 
		allow for non-javascript browsers to render a seperately styled non-interactive 
		list rather than the DHTML menu. The Menu JavaScript and Menu CSS and Menu Images 
		should be kept together in their own Menu Folder for easy transportation. 

		Additionally optional resolution styles should be kept in separate CSS Folder and 
		called from the Resolution Script. The Resolution Script should be kept in a 
		separate Scripts Folder.
		PLACE BETWEEN HEAD TAGS:
		<!--
		<script language="javascript" type="text/javascript" src="scripts/gwd_resolution.js"></script>
		<script language="javascript" type="text/javascript" src="menu/gwd_closethree.js"></script>
		-->

		!!! IMPORTANT !!!
		To initilize the menu on page load the menuID array must be set manually in the 
		function findMenus() below. The array numbers must equal the number of Primary Tabs. For example
		if there are 5 Primary Tabs the array must have 5 items to match those Primary Tabs:
		var menuID = new Array("mnGWDsub01", "mnGWDsub02", "mnGWDsub03", "mnGWDsub04", "mnGWDsub05");
==================================================================================================

*/

	// Load up the stylesheet for the menus.
	document.write("<link rel='stylesheet' href='http://www.ojc.edu/menu/gwd_closethree.css' type='text/css' />");

	//FIRST TWO LEVELS VARIABLES
	var selectedPrimary = '';
	var selectedSecondaryContainer = '';
	var selectedThirdContainer = '';
	
	var hoverPrimary = '';
	var hoverSecondaryContainer = '';
	var previousPrimary = '';

	var selectedControlOn = '';
	var mnGWDtrewrap = String('mnGWDtrewrap');
	
	//NEW THIRD LEVEL VARIABLES
	var selectedSecondaryActive = '';
	var hoverSecondaryActive = '';
	var lastSecondaryActive = '';	
	var selectedThirdActive = '';
	var hoverThirdActive = '';
	var lastThirdActive = '';
	var searchcontainer = String('searchcontainer');

	// TIMER VARIABLES
	var menuTimer;
	var stopMenu;
	
	//FIRST LEVEL MENU DISPLAY
	function showmenu(element) {
		var element = String(element.id);		
		selectedPrimary = element;
		selectedSecondaryContainer = element + "_secondary";
		selectedThirdContainer = element + "_third";
		
		menuTimer = setTimeout("showmenu_primary()", 200); //RESET TIMER FUNCTION OLD VALUE 400
		stopMenu = setTimeout("stopCount()", 400); // STOP TIMER FUNCTION IF MOUSE ON TAB MORE THAN 1 SEC OLD VALUE 800
	} //END SHOW MENU TIMER
	
    function stopCount() {
		clearTimeout(menuTimer);
	} //END FUNCTION STOP COUNT
	
        function showmenu_primary() {			
			//TURN OFF THIRD MENU ROW ON MOUSEOVER PRIMARY TAB
			if (selectedControlOn == '') {
				document.getElementById(mnGWDtrewrap).style.display = "none";
			} else {	
				document.getElementById(mnGWDtrewrap).style.display = "none";
				document.getElementById(searchcontainer).style.margin = "0px";
				document.getElementById(lastSecondaryActive).style.background = "";
			}
			//DISPLAYS ACTIVE PRIMARY TAB SECOND LEVEL ITEMS
			if ((hoverPrimary != '') && (hoverSecondaryContainer != selectedSecondaryContainer)) {
				off_assignment(document.getElementById(hoverPrimary));
				off_assignment(document.getElementById(selectedPrimary));

				document.getElementById(hoverSecondaryContainer).style.display = "none";
				document.getElementById(selectedSecondaryContainer).style.display = "block";
			}
			hoverPrimary = selectedPrimary;
			hoverSecondaryContainer = selectedPrimary+"_secondary";
			//FLIPS AND DISPLAYS ACTIVE PRIMARY TAB HIGHLIGHT
			flip_assignment(document.getElementById(selectedPrimary));
			document.getElementById(hoverSecondaryContainer).style.display = "block";
		}//END FUNCTION SHOWMENU_PRIMARY

// FIRST LEVEL SPECIFIC FUNCTIONS ****************************************************
		function primary_null(element) {
			
		var element = String(element.id);
		selectedPrimary = element;
		defaultTab = 'nav_tab01';
		//TURN OFF THIRD MENU ROW ON MOUSEOVER SINGLE PRIMARY TAB
		if (selectedControlOn == '') {
			document.getElementById(mnGWDtrewrap).style.display = "none";
		} else {
			document.getElementById(mnGWDtrewrap).style.display = "none";
			document.getElementById(searchcontainer).style.margin = "0px";
			document.getElementById(lastSecondaryActive).style.background = "";			
			}		
			showmenu(document.getElementById(defaultTab));			
		} //END PRIMARY_NULL FUNCTION		
		
	//FLIPS AND DISPLAYS ACTIVE PRIMARY TAB HIGHLIGHT		
	function flip_assignment(element) {
			element.className = "mnGWDon";
	}
	
	//DISPLAYS ACTIVE PRIMARY TAB SECOND LEVEL ITEMS
	function off_assignment(element) {
		element.className = "";
	}

	
// END FIRST LEVEL SPECIFIC FUNCTIONS ****************************************************

// START SHOWMENU THIRD LEVEL ADDED ******************************************************
// SELECTED PRIMARY DEFINED IN FUNCTION ABOVE

	function showmenu_third(element) {
		var element = String(element.id);
		selectedSecondaryActive = element;
		selectedThirdActive = element + "_third";
		hoverThirdActive = element + "_third";
		//CORRECTS ANOMOLY FROM SECONDARY TAB TO PRIMARY & BACK AGAIN
		if (selectedControlOn == selectedSecondaryContainer) {			
			//TURN ON THIRD MENU ROW CONTAINER WHEN SECONDARY ACTIVE HOVER			
			document.getElementById(mnGWDtrewrap).style.display = "block";
			//COLLAPSE (TURN OFF) SEARCHBAR DISPLAY CONTAINER WHEN SECONDARY ACTIVE HOVER
			document.getElementById(searchcontainer).style.margin = "-30px 0px 0px 0px";
			// DISPLAYS THIRD LEVEL ACTIVE LINKS TEXT			
			document.getElementById(selectedThirdActive).style.display = "block";
				
		}
		
		
		if (hoverSecondaryActive != selectedSecondaryActive) {			
			if (lastThirdActive != '')		
				document.getElementById(lastThirdActive).style.display = "none"; //LAST LEVEL THREE MENU ITEM DISPLAY OFF	
				document.getElementById(hoverThirdActive).style.display = "none"; //LAST LEVEL THREE MENU ITEM DISPLAY OFF		
				//TURN ON THIRD MENU ROW CONTAINER WHEN SECONDARY ACTIVE HOVER			
				document.getElementById(mnGWDtrewrap).style.display = "block";
				//COLLAPSE (TURN OFF) SEARCHBAR DISPLAY CONTAINER WHEN SECONDARY ACTIVE HOVER
				document.getElementById(searchcontainer).style.margin = "-30px 0px 0px 0px";
				// DISPLAYS THIRD LEVEL ACTIVE LINKS TEXT			
				document.getElementById(selectedThirdActive).style.display = "block";
				hoverSecondaryActive = element; // CONTROL
				// SETS CONTROL FOR LAST LEVEL THREE MENU ITEM FIRST LEVEL FUNCTION			
				lastThirdActive = element + "_third";
				//SELECTED SECONDARY CONTAINER SET IN FIRST LEVEL FUNCTION ABOVE
				selectedControlOn = selectedSecondaryContainer;	// SETS CONTROL VARIABLE FOR FIRST LEVEL FUNCTION ABOVE			
			}
			
			if (lastSecondaryActive != '') {	
				flip_assignment_secondary(document.getElementById(selectedSecondaryActive));
			} else {
				// SETS CONTROL FOR SECOND LEVEL TAB BACKGROUND AFTER FLIP FUNCTION			
				lastSecondaryActive = selectedSecondaryActive;				
				flip_assignment_secondary(document.getElementById(selectedSecondaryActive));
			}
		lastSecondaryActive = selectedSecondaryActive; // !!!! SETS CONTROL FOR SECOND LEVEL TAB BACKGROUND AFTER FLIP FUNCTION
		}//END FUNCTION SHOWMENU THIRD LEVEL

// SECOND LEVEL SPECIFIC FUNCTIONS ****************************************************
	function menu_null(element) {
		var element = String(element.id);
		selectedSecondaryActive = element;
		selectedThirdActive = element + "_third";
		hoverThirdActive = element + "_third";
	
		if (hoverSecondaryActive != selectedSecondaryActive) {			
			if (lastThirdActive != '')		
				document.getElementById(lastThirdActive).style.display = "none"; //LAST LEVEL THREE MENU ITEM DISPLAY OFF	
				document.getElementById(hoverThirdActive).style.display = "none"; //LAST LEVEL THREE MENU ITEM DISPLAY OFF		
				//TURN ON THIRD MENU ROW CONTAINER WHEN SECONDARY ACTIVE HOVER			
				document.getElementById(mnGWDtrewrap).style.display = "none";
				//TURN ON SEARCHBAR DISPLAY CONTAINER WHEN SECONDARY ACTIVE HOVER
				document.getElementById(searchcontainer).style.margin = "0px";
				// MAKE SURE THIRD LEVEL CONTAINER IS OFF			
				document.getElementById(selectedThirdActive).style.display = "none";
				hoverSecondaryActive = element; // CONTROL
				// SETS CONTROL FOR LAST LEVEL THREE MENU ITEM FIRST LEVEL FUNCTION			
				lastThirdActive = element + "_third";
				//SELECTED SECONDARY CONTAINER SET IN FIRST LEVEL FUNCTION ABOVE
				selectedControlOn = selectedSecondaryContainer;	// SETS CONTROL VARIABLE FOR FIRST LEVEL FUNCTION ABOVE			
			}
			
			if (lastSecondaryActive != '') {	
				flip_assignment_secondary(document.getElementById(selectedSecondaryActive));
			} else {
				// SETS CONTROL FOR SECOND LEVEL TAB BACKGROUND AFTER FLIP FUNCTION			
				lastSecondaryActive = selectedSecondaryActive;				
				flip_assignment_secondary(document.getElementById(selectedSecondaryActive));
			}

		lastSecondaryActive = selectedSecondaryActive; // !!!! SETS CONTROL FOR SECOND LEVEL TAB BACKGROUND AFTER FLIP FUNCTION	
	}//END FUNCTION MENU NULL

	function flip_assignment_secondary(element) {
		if (lastSecondaryActive != selectedSecondaryActive) {
			//SETS SECOND LEVEL ACTIVE TAB BACKGROUND ON HOVER or MOUSEOVER				
			document.getElementById(lastSecondaryActive).style.background = ""; // = "#b7c6ec"
			document.getElementById(selectedSecondaryActive).style.background = "#d1dbf3";			
		} else {
			//SETS SECOND LEVEL ACTIVE TAB BACKGROUND AFTER MOUSEOUT				
			document.getElementById(selectedSecondaryActive).style.background = "#d1dbf3";
		}
	}		

// END SECOND LEVEL SPECIFIC FUNCTIONS ****************************************************

// START ONMOUSEOVER THIRD LEVEL COLLAPSE ******************************************************
	function close_third(element) {
		if (lastSecondaryActive != '') {	
			document.getElementById(mnGWDtrewrap).style.display = "none";
			document.getElementById(searchcontainer).style.margin = "0px";
			document.getElementById(lastSecondaryActive).style.background = "";
		}		
		return;
	}
// END ONMOUSEOVER THIRD LEVEL COLLAPSE ******************************************************

// START ADA ONFOCUS FUNCTION ******************************************************

	function ada_showmenu(currentElement){
		var currentElement = String(currentElement.id);		
		element = "nav"+currentElement;
		selectedPrimary = element;		
		selectedSecondaryContainer = element + "_secondary";		
		selectedThirdContainer = element + "_third";		
		//TURN OFF THIRD MENU ROW ON MOUSEOVER PRIMARY TAB (NEW SCRIPT NEGATES BREADCRUMBS)
		if (selectedControlOn == '') {
			document.getElementById(mnGWDtrewrap).style.display = "none";
		} else {	
			document.getElementById(mnGWDtrewrap).style.display = "none";
			document.getElementById(searchcontainer).style.margin = "0px";
			document.getElementById(lastSecondaryActive).style.background = "";
		}

		//DISPLAYS ACTIVE PRIMARY TAB SECOND LEVEL ITEMS
		if ((hoverPrimary != '') && (hoverSecondaryContainer != selectedSecondaryContainer)) {
			off_assignment_ada(document.getElementById(hoverPrimary));
			off_assignment_ada(document.getElementById(selectedPrimary));

			document.getElementById(hoverSecondaryContainer).style.display = "none";
			document.getElementById(selectedSecondaryContainer).style.display = "block";			
		}
		hoverPrimary = element;		

		hoverSecondaryContainer = element+"_secondary";		

		//FLIPS AND DISPLAYS ACTIVE PRIMARY TAB HIGHLIGHT
		flip_assignment_ada(document.getElementById(selectedPrimary));
		document.getElementById(hoverSecondaryContainer).style.display = "block";
		}// END ADA ONFOCUS FUNCTION *********
		
// ADA ONFOCUS SPECIFIC FUNCTIONS ****************************************************

	//FLIPS AND DISPLAYS ACTIVE PRIMARY TAB HIGHLIGHT		
	function flip_assignment_ada(element) {
		element.className = "mnGWDon";
	}
	
	//DISPLAYS ACTIVE PRIMARY TAB SECOND LEVEL ITEMS
	function off_assignment_ada(element) {
		element.className = "";
	}
	
	function focus_null(element) {
			
		var element = String(element.id);
		selectedPrimary = element;
		defaultTab = 'nav_tab01';		
		//TURN OFF THIRD MENU ROW ON MOUSEOVER SINGLE PRIMARY TAB
		if (selectedControlOn == '') {
			document.getElementById(mnGWDtrewrap).style.display = "none";
		} else {
			document.getElementById(mnGWDtrewrap).style.display = "none";
			document.getElementById(searchcontainer).style.margin = "0px";
			document.getElementById(lastSecondaryActive).style.background = "";			
			}
		if (previousPrimary != '') {
			document.getElementById(previousPrimary).className = "";			
		}
			flip_focus(document.getElementById(selectedPrimary));
			previousPrimary = element;			
			showmenu(document.getElementById(defaultTab));			
	} //END PRIMARY_NULL FUNCTION
		
	//FLIPS AND DISPLAYS ACTIVE PRIMARY TAB HIGHLIGHT		
	function flip_focus(element) {
			element.className = "focuson";
	}
	
	function offactive(element) {
		var element = String(element.id);
		selectedPrimary = element;
		
		document.getElementById(selectedPrimary).className = "";
	}
	
// END ADA ONFOCUS SPECIFIC FUNCTIONS ****************************************************


//#############################################################################
//*****************************************************************************
	//INITIATE MENU FUNCTION ON PAGE LOAD
	//!!!IMPORTANT!!! MANUALLY SET menuID ARRAY in findMenus FUNCTION
	//TO MATCH NUMBER OF PRIMARY TABS (mnGWDsub01 through mnGWDsub07) !!!IMPORTANT!!!
//*****************************************************************************
//#############################################################################

	var defaultTab ='';	
	
	function init_nav()
	{
	setPage(); //CHECK TO SEE IF PRIMARY TAB ONLOAD
	if (defaultTab != '') {
		newpage(document.getElementById(defaultTab)); //IF PRIMARY TAB SET PAGE
	} else { //IF DEFAULT TAB IS EQUAL TO NULL FIND IF SECOND LEVEL TAB
		findMenus(); //CHECK TO SEE IF A SECONDARY LEVEL TAB ONLOAD
			if (defaultTab != '') {
			newpage(document.getElementById(defaultTab)); //IF SECONDARY TAB SET PAGE
			} else {//IF SECONDARY TAB IS EQUAL TO NULL FIND IF THIRD LEVEL TAB
				thirdMenus();//CHECK TO SEE IF A THIRD LEVEL TAB ONLOAD
					if (defaultTab != '') {
					newpage(document.getElementById(defaultTab));//IF THIRD TAB SET PAGE
					} else {//IF TAB STILL NULL SET NAVTAB01 AS PRIMARY					
						defaultTab = 'nav_tab01';
						newpage(document.getElementById(defaultTab));			
						}
			}
		}
//alert("default tab = " +defaultTab);			
	}

// PRIMARY TAB SET PAGE FUNCTION ************************************

function extractPageName(hrefString)
{
	var arr = hrefString.split('/'); //SPLIT THE HREFSTRING INTO ARRAY PARTS
	return  (arr.length<1) ? hrefString : arr[arr.length-1]; //FIND LAST STRING ITEM (PAGE HTML) GO TO setActiveMenu FUNCTION	
}

function setActiveMenu(arr, crtPage)
{
	for (var i=0; i<arr.length; i++) // SETS UP ARRAY
	{	
		// FIRST PASS GOES BACK UP TO EXTRACT PAGE NAME FUNCTION
		if(extractPageName(arr[i].href) == crtPage)// COMPARES CURRENT PAGE WITH PRIMARY TAB ARRAY HTML
		{
		// IF EQUAL GOES INTO SECOND IF STATEMENT			
			if (arr[i].parentNode.tagName != "DIV")// ARRAY SHOULD NOT BE DIV ARRAY
			var parentNode = arr[i].parentNode.tagName; //THIS SHOULD BE LI ARRAY		
			{
				defaultTab = arr[i].parentNode.id; //SET DEFAULT TAB TO PRIMARY TAB				
			}
		}
	}
}

function setPage()
{	
	hrefString = document.location.href ? document.location.href : document.location;//FIND CURRENT PAGE PATH 
	if (document.getElementById("menuContainer")!=null) //FIRST LEVEL CONTAINER FOR PRIMARY TABS
		setActiveMenu(document.getElementById("menuContainer").getElementsByTagName("a"), extractPageName(hrefString));
}

// END PRIMARY TAB SET PAGE FUNCTION ***************************************************************************



// FIND MENUS SECOND LEVEL FUNCTION **************************************************

function extractSecondaryName(hrefStringS)
{	
	var arrS = hrefStringS.split('/'); //SPLIT THE HREFSTRING INTO ARRAY PARTS
	return  (arrS.length<1) ? hrefStringS : arrS[arrS.length-1]; //FIND LAST STRING ITEM (PAGE HTML) GO TO setActiveMenu FUNCTION	
}

function setSecondaryMenu(arrS, crtPageS)
{
	for (var i=0; i<arrS.length; i++) // SETS UP ARRAY
	{
		// FIRST PASS GOES BACK UP TO EXTRACT PAGE NAME FUNCTION
		if(extractSecondaryName(arrS[i].href) == crtPageS)// COMPARES CURRENT PAGE WITH PRIMARY TAB ARRAY HTML
		{
		// IF EQUAL GOES INTO SECOND IF STATEMENT			
			if (arrS[i].parentNode.tagName != "DIV")// IS ANCHOR TAG THEN LI TAG
			{
				var secondaryTab = arrS[i].parentNode.id;				
				var ulParent = document.getElementById(secondaryTab).parentNode.id;
				defaultTab = "nav_"+ulParent;				
			}
		}
	}
}

function findMenus() {
	// !!! IMPORTANT !!! MANUALLY SET THIS ARRAY FOR NUMBER OF PRIMARY TABS !!! IMPORTANT !!!
	var menuID = new Array("mnGWDsub01", "mnGWDsub02", "mnGWDsub03", "mnGWDsub04", "mnGWDsub05", "mnGWDsub06", "mnGWDsub07"); // ("mnGWDsub01", "mnGWDsub02", "mnGWDsub03", "mnGWDsub04", "mnGWDsub05", "mnGWDsub06", "mnGWDsub07");	
	hrefStringS = document.location.href ? document.location.href : document.location;//FIND CURRENT PAGE PATH
	//SET UP menuID ARRAY HERE
		for (var i=0; i<menuID.length; i++)
		{
			if (document.getElementById(menuID[i])!=null) {
				setSecondaryMenu(document.getElementById(menuID[i]).getElementsByTagName("a"), extractSecondaryName(hrefStringS));
			}
		}	
}

 
//END FIND MENU FUNCTION ************************************************************


// THIRD MENUS THIRD LEVEL FUNCTION **************************************************
function extractThirdName(hrefStringT)
{
	var arrT = hrefStringT.split('/'); //SPLIT THE HREFSTRING INTO ARRAY PARTS
	return  (arrT.length<1) ? hrefStringT : arrT[arrT.length-1]; //FIND LAST STRING ITEM (PAGE HTML) GO TO setActiveMenu FUNCTION	
}

function setThirdMenu(arrT, crtPageT)
{
	for (var i=0; i<arrT.length; i++) // SETS UP ARRAY
	{
		// FIRST PASS GOES BACK UP TO EXTRACT PAGE NAME FUNCTION
		if(extractThirdName(arrT[i].href) == crtPageT)// COMPARES CURRENT PAGE WITH PRIMARY TAB ARRAY HTML
		{
		// IF EQUAL GOES INTO SECOND IF STATEMENT			
			if (arrT[i].parentNode.tagName != "DIV")// IS ANCHOR TAG THEN LI TAG
			{
				var listParent = arrT[i].parentNode.parentNode.className; //RETURNS UL Class Name			
				defaultTab = listParent; //SETS CLASS NAME AS DEFAULT PRIMARY TAB			
			}
		}
	}
}

function thirdMenus() {
	hrefStringT = document.location.href ? document.location.href : document.location;//FIND CURRENT PAGE PATH
	if (document.getElementById("third_nav")!=null)
	setThirdMenu(document.getElementById("third_nav").getElementsByTagName("a"), extractThirdName(hrefStringS));
}

// END THIRD MENUS THIRD LEVEL FUNCTION **************************************************



//************************************************	
	//NEW PAGE FIRST LEVEL MENU DISPLAY
//************************************************
function newpage(element) {		
		var element = String(element.id);
		selectedPrimary = element;
		selectedSecondaryContainer = element + "_secondary";
		selectedThirdContainer = element + "_third";
//alert("PRIMARY TAB = " +element);
		document.getElementById(mnGWDtrewrap).style.display = "none";
		hoverPrimary = element;	
		hoverSecondaryContainer = element+"_secondary";		

	//FLIPS AND DISPLAYS ACTIVE PRIMARY TAB HIGHLIGHT
		flip_newpage(document.getElementById(selectedPrimary));
		document.getElementById(hoverSecondaryContainer).style.display = "block";
}
	
//END FUNCTION ************************************		
		
	//FLIPS AND DISPLAYS ACTIVE PRIMARY TAB HIGHLIGHT		
	function flip_newpage(element) {		
			element.className = "mnGWDon";
	}
//END FUNCTION ************************************