﻿// this contains all of the script functions to create a menubar.

function debugMenuList(menuList){
//this function will print out an alert with a menu list converted to names

var output = "";
for (i = 0; i < menuList.length; i++)
//alert(menuList[1].name);
{output = output + menuList[i].name;
if ((i+1) < menuList.length){output = output + ", ";}
}
alert(output);
}



//initialize global variables
// create debug flag
var debug = false;
// now set menu show 1 of 2 ways
// the default way is for the menu to be inline, and the position to be relative
var menuShow = "visibility: hidden; display: none";
var menuShow_Position = "";

/*
// comment out the following code if you are not useing relative indexing
//an alternate definition of menuShow is for the position to be absolute, and the menu to have a z index
var menuShow_Position = "absolute";
var menuShow_Z = 3; // Set z-index to 3 to allow menu to overlay z indicies 1 and 3
var menuShow_Top = ""; //  position menu 53 pixels from the top of the page
var menuShow_Left = "";	// postion menu 0 pixels from the left of the page
var menuShow_minWidth = "760px"; // allow menu to span xx% of  it's element
var menuShow_maxWidth = "1000px";//allow menu to take up a maz of maxWidth pixels
var menuShow_marginRight = "6px";// right margin
var table_width_adjustment = ".0";//compensator to reduce menu size by fraction of percent
menuShow = menuShow + "; position: " + menuShow_Position + "; z-index: " + menuShow_Z + "; top: " + menuShow_Top + "; left: " + menuShow_Left + "; min-width: " + menuShow_minWidth + "; max-width: " + menuShow_maxWidth+ "; margin-right: " + menuShow_marginRight ;

// end of area that should be commented out if not using relative indexing

*/

// now if debug - true, set all menus to be visible
if(debug) {menuShow = "visibility: visible; display: inline";}

// now set up the menu time out characteristice
// if menuTimeout == NULL there will be no time out effect
// if menuTimeout is a number, the menu will time out to the top menu  on a mouse out from the menu after the number of milliseconds specified by menuTimeout
var menuTimeout = "Null";
menuTimeout = 2500;
var timeoutID = false

var undefined_variable;
// now create an array of all of the div's that need to be made
var div_array = new Array(1);
var named_div_array = new Array(1);
// now create an array to stack variables
var stack = new Array();

// Now create menu styles
createStyles();


// new function sjowDV()
function showDIV(szDivID){
// this function will take and array of DivID's and 
// set sxDivID to be visible and display to inline
// all other DivID's will be set hidden and none
// now free to do work
//first define the array
var arraysize = named_div_array.length;
// add 1 additional elements to array item for Top_Menu and no_beans
arraysize = arraysize + 1;
// set default to hide
var showHide = 0;
// create the array of DivID's
var DivID = [arraysize];
// set next to last element in array to no_beans
//DivID[arraysize-2] = "Top_Menu";
// set next to last element in array to no_beans
DivID[arraysize-1] = "no_beans";
// now set up the other elements in the array
// now copy named_div_array into DivId	
for (divcount = 0; divcount < named_div_array.length; divcount++)
	{DivID[divcount] = named_div_array[divcount];
	}
// first, show selected DivID
var turnon = szDivID;
var turnoff = "";
//alert ("Turn On " + turnon);
toggleBox(szDivID, 1);
toggleDisplay(szDivID,1);
// now turn everything else off
for (i=0; i < arraysize; i++) {
turnoff = DivID[i];
//alert("Turn On " + turnon + " Turn off " + turnoff);
//find the entry in the table
if(DivID[i] != szDivID) {toggleBox(DivID[i], showHide);
						toggleDisplay(DivID[i], showHide);
						}
}

// now determine if a menu is supposed to time out
if(menuTimeout != "Null")
	{// the menu time out finction is active
	// chect to see if the time has been set
	if(timeoutID != false)
		{// clear out menu timer
		clearMenuTimeout();
		}
	}
return;
}

// ---- new function toggleDisplay()
function toggleDisplay(szDivID, iState) {
// this function will change the display attribute of a Div class
// to either show inline or block the div.
// it attempts to be browser independent
// input sxDivId is the DivId that is to be manupulated
// input iState = 0 for None or 1 for Inline
if(document.layers)    //NN4+
    {
       document.layers[szDivID].display = iState ? "inline" : "none";
    }
    else if(document.getElementById)      //gecko(NN6) + IE 5+
    {
        var obj = document.getElementById(szDivID);
        obj.style.display = iState ? "inline" : "none";
    }
    else if(document.all)       // IE 4
    {
        document.all[szDivID].style.display = iState ? "inline" : "none";
    }
return;
}

// ---- new function toggleBox()
function toggleBox(szDivID, iState) {
// this function will change the visibility attribute of a Div class
// to either show or hide the div.
// it attempts to be browser independent
// input sxDivId is the DivId that is to be manupulated
// input iState = 0 for Hide/Hidden or 1 for show/visible
//alert("szDivID = " + szDivID + " iState = " + iState);
if(document.layers)    //NN4+
    {
       document.layers[szDivID].visibility = iState ? "show" : "hide";
    }
    else if(document.getElementById)      //gecko(NN6) + IE 5+
    {
        var obj = document.getElementById(szDivID);
//       alert(obj);
//       alert(szDivID);
        obj.style.visibility = iState ? "visible" : "hidden";
    }
    else if(document.all)       // IE 4
    {
        document.all[szDivID].style.visibility = iState ? "visible" : "hidden";
    }
return;
}

function toggleDiv(div, state){
// this function will toggle the state of a div
// if state = 0, the div will be set to be invisible
// and also to not have display space allocated
// if state = 1, the dive will be visible
// and display space will be allocated
// this function calls functions located in new_menubar.js
toggleDisplay(div, state);
toggleBox(div, state);
return;
}

// new function setHBTimeout()
function setMenuTimeout(menuTimeDelay) {
// this function set the headerbar timer to time out on a mouse out
// by menuTimeDelay

timeoutID = window.setTimeout("makeMenuTop()", menuTimeDelay);
return(timeoutID);
}

// new function clearHBTimeout()
function clearMenuTimeout() {
// this function clears the headerbar timer to time out on a mouse out

window.clearTimeout(timeoutID);
timeoutID = false;
return(timeoutID);
}

// new function HBTop()
function makeMenuTop() {
// this function sets the headerbar to the top menu

showDIV('Top_Menu');
return;
}


// new function scanTop_MenuObject(match_Object) 
function scanTop_MenuObject(match_object) {
// this function will scan the Top_Menu to determine is a "match_object" exists
// this function will return the found_object which is a number represent the offset for object_item

var found_object = 0;
for(i = 0; i < Top_Menu.size; i++) {
	scanned_object_offset = i + 1;
	if(Top_Menu["menu" + scanned_object_offset].name == match_object)
		{ found_object = scanned_object_offset;
		}
}
return(found_object);
}

//New Function findFileName()
function findFilename(tr){
// this function will return the file name of a file url
// if the input tr is undefined, it will look fr the file name in the current URL

if(typeof tr == "undefined"){tr = window.location.pathname;}
//if(tr === undefined){tr = window.location.pathname;}
len = tr.length; 
rs = 0;
for (i = len; i > 0; i--) { 
vb = tr.substring(i,i+1); 
if (vb == "/" && rs == 0) { 
return(tr.substring(i+1,len)); 
rs = 1 ;
} 
} 
return(null);
}



//New Function search_div_array() ---
function search_div_array(div_array, menu){
//this function with search the div_array to determine if the menu has already been defined as a div
//if it has, the function will return true
//if it has not, the function will return false
return_value = false;
for (i = 0;  i <div_array.length; i++)
	{if(div_array[i] == menu)
		{return_value = true;
		}
	}
	return(return_value);
}

//new Function loop_menu() ---
function loop_menu(div_array, object, menu_show, stack, div_style_buffer){
		// now call function to do recursion on this object name
		for(var menu_count = 0; menu_count < object.size; menu_count++)
			{var menu_offset = menu_count +1;
			var new_object = object["menu" + menu_offset];
			if(new_object != "none")
				{if(new_object !== undefined)
					{// recurse the loop
					div_style_buffer = find_newmenu(div_array, new_object, menu_show, stack, div_style_buffer);
					}
				}
			}
return(div_style_buffer);
}


//New Function find_newmenu() ---
function find_newmenu(div_array, object, menu_show, stack, div_style_buffer){
//this function will seek out menu's in objects
// if a menu is discovered, it will create a new div for it
// look into just defined div

	if(!search_div_array(div_array, object))
		{//alert(search_div_array(div_array, object));
		// the menu is not defined... define the div here
		// first point to the correct element in the array
		var array_pointer = div_array.length - 1;
//		put object into div array so we know we created the style
		div_array[array_pointer] = object;
		named_div_array[array_pointer] = object.name;
		div_style_buffer = div_style_buffer + '#' + object.name + " { " + menu_show +' }\r';
		// now add additional element to div_array
		div_array.length++;
		div_style_buffer = loop_menu(div_array, object, menu_show, stack, div_style_buffer);
		}

return(div_style_buffer);
}

//New Function selectclassSuffix()
function selectclassSuffix(menu_level, createdMenu, menuContents, menu_list, list_start, list_end) {
// this function will select the navTable class based upon the state of the menu selection and cell contents
// if the menu being created us the same as the Top_menu_level from createMenu
// if the cell contains valid text
// this routine will select the nav class to be navtable_select
// otherwise it will select the nav class to be navtable
// if the cell does not contain valid text, the nav class will be set to navtable_empty
//alert("menu_level = " + menu_level + " createdMenu = " + createdMenu);
var undefined_variable;
if(menuContents === undefined_variable) {return("_empty");} // cell is empty
if(menuContents == "none") {return("_select");} // cell is end of line
//if(menu_level.name == createdMenu.name){return("_select");}
//if(createdMenu.name == "Top_Menu") {return("_select");}
// now scan menu to select menu path.
var select = "";
for (selectTest = list_start; selectTest < list_end; selectTest++)
	{ if(menuContents == menu_list[selectTest]) {select = "_select";}
	}
return(select);
}



// new Function createStyles()
function createStyles() {
// this function will create styles to be placed in the head section of the rendered document
// it should be called by a script call in the header of the page

// start identfying the div's
// start at the top menu and go recursively through each menu
// initialize div_style_bufer
var div_style_buffer = '<style type="text/css"><!--\r';
div_style_buffer = find_newmenu(div_array, Top_Menu, menuShow, stack, div_style_buffer);
div_style_buffer = div_style_buffer + '--></style>\r';
//alert (div_style_buffer);

document.write(div_style_buffer);
return;	
}



// new function createNew_Menu(menu_name, url_adjust)
function createNew_Menu(menu_name, url_adjust){
// this function will create the html code for a new menu, based upon the menu name, the url_adjust 
// it will return a string menu_buffer the html for the menu

// now initialize Menu_Buffer to open div
var Menu_buffer  = openNew_Menu(menu_name);
// now create cells
Menu_buffer = Menu_buffer + createNew_Menu_Cells(menu_name, menu_name, url_adjust);
// now close div
Menu_buffer = Menu_buffer + closeNew_Menu();
return(Menu_buffer);
}

// new function openNew_Menu(menu_name)
function openNew_Menu(menu_name){
//this function returns the heml to open a new div for the menu name.
return("<div id='" + menu_name.name + "'>");
}

// new function closeNew_Menu()
function closeNew_Menu(){
//this function returns the heml to open a new div for the menu name.
return("</div>\r");
}


// mew function createNew_Menu_Cells((menu_name, url_adjust)
function createNew_Menu_Cells(menu_name, div_name,  url_adjust, menu_list, list_start, list_end){
// this function with compute the cells for a menu layout and create the html
// now calculate cell spacing
// Menu_list is the total menu list created by Scan_for_MultiLevel_Menus
//list start and list_end are the ranges of menu's beingcreated.
var cellSpace = 0;
var cellContents = "";
var Atagpresent = false;
var url_adjustor = "";
var table_width = 100;
if(menuShow_Position == "absolute")
	{table_width = table_width - table_width_adjustment;}
// now initialize Menu_buffer and open table
var Menu_buffer = "<table border='0' cellpadding='0' cellspacing='0' width='" + table_width + "%'><tr align='center' valign='middle'>";
var undefined_variable;
var Menu_cellSpace = Math.round(table_width/menu_name.size);
var Menu_cellSpace_adjust = table_width - (menu_name.size * Menu_cellSpace);
var classSuffix = "";
var mouseOutTimer = "";
// now set up the loop to create the table
for (menu_loop = 0; menu_loop < menu_name.size; menu_loop++)
	{menu_offset = menu_loop + 1;
	// first compute cell space
	cellSpace = Menu_cellSpace;
	// correct for rounding of last cell
	if(menu_offset == menu_name.size)
		{cellSpace = Menu_cellSpace + Menu_cellSpace_adjust;
		}
	// now set up table element

// define the class suffix
classSuffix = selectclassSuffix(menu_name, div_name, menu_name["menu" +menu_offset], menu_list, list_start, list_end);



//classSuffix = "";
	Menu_buffer = Menu_buffer + '<td class="' + "navtable" + classSuffix + '" width="' + cellSpace + '%">';
		
	// reset noAtag
	Atagpresent = false;	
	// now create cell elements  first check to see if the cell exists
	if(menu_name["cell" +menu_offset] === undefined_variable) 
		{cellContents = " ";}
	else {
	cellContents = '<span class="navbar'+ classSuffix + '">'+menu_name["cell" +menu_offset] + '</span>';

	// then check to see if we have a link or a rollover action
	if((menu_name["link" +menu_offset] != "none") || (menu_name["mover" +menu_offset] != "none"))
		{Atagpresent = true;
		Menu_buffer = Menu_buffer + '<a';
			if(menu_name["link" +menu_offset] != "none")
			// now check to see if the link is relative, or if it is to another web site
				{url_adjustor = url_adjust;
				if(menu_name["link" +menu_offset].search(/www/i) == 0)
					{url_adjustor = "http://";
					}
				// now create link
				Menu_buffer = Menu_buffer + ' href="' + url_adjustor + menu_name["link" +menu_offset] + '" target="'+ menu_name["target" +menu_offset]+ '"';
				}
				// before setting up mouse over action...check to see if we have a time out function
			if(menuTimeout != "Null")
				{// we have a time out action
				mouseOutTimer = 'onmouseout="setMenuTimeout(' + menuTimeout + ');"';
				// now check to see if we have a mouse over action
				// if we do not, set up a mouse over action to clear the timer
				if(menu_name["mover" +menu_offset] == "none")
					{menu_name["mover" +menu_offset] = "clearMenuTimeout()";
					}
				}

			if(menu_name["mover" +menu_offset] != "none")
				{Menu_buffer = Menu_buffer + mouseOutTimer +  ' onmouseover="' + menu_name["mover" +menu_offset] + ';"';
				}
			Menu_buffer = Menu_buffer + '>';
		}
		}
	// now get the cell name and close out the cell
	Menu_buffer = Menu_buffer + cellContents;
	// now check to see if we created the A tag
	if(Atagpresent)
		{Menu_buffer = Menu_buffer + "</a>";
		}
	Menu_buffer = Menu_buffer + "</td>\r";
	}
// now close table
Menu_buffer = Menu_buffer + "</tr></table>";
return(Menu_buffer);
}

//new function scanFor_MultiLevelMenu(Menu_Obj)
function scanFor_MultiLevelMenu(Menu_Objects){
// this function will scan for multilevel menus startingwith Menu_OBJ as the route.
// it will return a list of menu objects with the menu items in sequence of the form:
//Top_Menu, Top_Menu, Object1, Top_Menu, Object1, Object2 Top_Menu, Object3, Top_Menu, Object...N, Top_Menu

//initialize variables
//read the last memory object
var searchQueue = new Array(1);
var leaves = new Array(0);
var moreBranches = new Array();
var moreBranchessearchQueue = new Array();
var menuObjectsSize = Menu_Objects.length;
var branchElement = new Array(1);
var resultsTree = new Array(1);
if(menuObjectsSize == undefined)
	{branchElement[0] = Menu_Objects;
	searchQueue[0] = Menu_Objects;
	resultsTree[0] = Menu_Objects;
	}
else 
	{branchElement = Menu_Objects;
	searchQueue = Menu_Objects;
	resultsTree = Menu_Objects;
	}
// Identify how long the Menu_Obj list is
// now preload searchQueue and results tree
var branchLength = branchElement.length;
var searchListsize = branchElement[branchLength-1].listsize;
var potentialLeaf = new Object();
var leaf = new Object();
var potentialLeafid = 0;
var resultsSearchid = 0;
// now determine if the search branch has any leaves
// if it does, add the leaves to the search queue to look for other leaves

for (var leafSearch = 0; leafSearch < searchListsize; leafSearch++)
	{ 
	potentialLeafid = leafSearch + 1;
	potentialLeaf = branchElement[branchLength-1]["menu" + potentialLeafid];
	// now check to see if potentialLeaf is a real Leaf
	if((potentialLeaf != "none") && (potentialLeaf != undefined))
		{
		// the potential leaf is a real leaf.  
		//Add it to the search que and continue
			leaf = [branchElement[0], potentialLeaf];
			searchQueue= searchQueue.concat(leaf);
		} // end of found leaf if statement	
	} //end leaf search
	
// now check to see if the leaf has any branches
if(searchQueue.length >1)
	{
	// yes, we have added to the searchQueue
	// collect leaves
	for(var leafcount = 0; leafcount < searchQueue.length; leafcount++)
		{if(searchQueue[leafcount] != branchElement[0])
			{leaves = leaves.concat(searchQueue[leafcount]);
			}// end found leaf		
		}// end leaf count
	}
resultsTree = searchQueue;	
	// now go out and search the leaves for more branches
	for(var findmoreBranches = 0; findmoreBranches< leaves.length; findmoreBranches++)
		{
		// now search the leaves for more branches
		moreBranches = scanFor_MultiLevelMenu(leaves[findmoreBranches]);
		if (moreBranches.length != 2)
			{// if only equal to 2 did not find any branches
			// more branches have been found
			// now manipulate result to and add to results tree
			// strip out first and last item which was used as a delimiter
			for(var foundmoreBranches = 1; foundmoreBranches < moreBranches.length-1; foundmoreBranches++)
				// strip out front and back and create new array
				{if (moreBranches[foundmoreBranches] == leaves[findmoreBranches])
					{
					// insert Menu Objects
					moreBranchessearchQueue = moreBranchessearchQueue.concat(Menu_Objects, moreBranches[foundmoreBranches]);		
					}//end insert Menu Objects
				else{moreBranchessearchQueue = moreBranchessearchQueue.concat(moreBranches[foundmoreBranches]);}				
				}// end foundmoreBranches
				// now add the moreBranchessearhQueue to the results tree
				resultsTree = resultsTree.concat(moreBranchessearchQueue);				
			} // end more branches test			
		}// search for more leaves test
// now all done.  Results should be in resultsTree
resultsTree = resultsTree.concat(Menu_Objects);
return (resultsTree);
}


// new Function generate_MultiLevelMenu(Menu_Obj)
function generate_MultiLevelMenu(Menu_Obj, url_adjust){
// this function will generate the html for all of the levels of a multi-level menu
// the input is a Menu_Object which represents the top level of the menu
// url_adjust represents the url level versus the home directory of the site
// first scan the menu object to identify all of the menu levels
var Menu_list_buffer = "";
var MultiLevelMenu_List = new Array();
MultiLevelMenu_List = scanFor_MultiLevelMenu(Menu_Obj);
//debugMenuList(MultiLevelMenu_List);
// 			
// now find a menu slice which represents the menu's to create
var Menu_list_start = 0;
var Menu_list_end = 0;
for (var Menu_list = 0; Menu_list < MultiLevelMenu_List.length; Menu_list++)
	{
//	alert("MultiLevelMenu_List[Menu_list].name = " + MultiLevelMenu_List[Menu_list].name);
	// set menu list end to current Menu_list pointer
	Menu_list_end = Menu_list;
	//test to see if we have found a Menu_Obj
	// if not, keep on going
	// if yes, then prepare to print out menu
	if(MultiLevelMenu_List[Menu_list] == Menu_Obj)
		{// we have found a complete menu tree
		// prepare to print it
		// what we know is..
		// MultilevelMenu_List[list_start] = Menu_obj 
		// MultilevelMenu_List[...] are intermediate menu objects
		// MultilevelMenu_List[Menu_list_end-1] = the div identifying menu
		// now set up loop to print out menu
		// first test the special case when Menu_list_start == Menu_list_end == 0
		// which signifies the first menu toputput
		if(Menu_list_start == 0)
			{//special case
			Menu_list_buffer = Menu_list_buffer + createNew_Menu(MultiLevelMenu_List[Menu_list_start], url_adjust );
			Menu_list_start = Menu_list_end + 1;
			} // end if statement for special case
		
		else if(Menu_list_start == Menu_list_end)
			{// need to skip to next item
			continue;
			}		
		else 
			{// we have a multilevel list.  Loop through it for printing
				// start by creating a div for the menu
//				alert("menu_list_start = " + Menu_list_start + "menu_list_end = " + Menu_list_end + " new div should be = " + MultiLevelMenu_List[Menu_list_end-1].name);
				Menu_list_buffer = Menu_list_buffer + openNew_Menu(MultiLevelMenu_List[Menu_list_end-1]);
				for (var Menu_print_start = Menu_list_start; Menu_print_start < Menu_list_end; Menu_print_start++)
					{Menu_list_buffer = Menu_list_buffer + createNew_Menu_Cells(MultiLevelMenu_List[Menu_print_start], MultiLevelMenu_List[Menu_list_end-1],  url_adjust, MultiLevelMenu_List, Menu_list_start, Menu_list_end);
					}// end for Menu_print_start
				Menu_list_buffer = Menu_list_buffer + closeNew_Menu();
				Menu_list_start = Menu_list_end;
			} // end else statement for printing
		
		// adjust menu start and begin search for new menu or end
		//Menu_list_start = Menu_list_end + 1;
		} // end if statement finding Menu_obj on list
	else
		{// we are still searching for a menu tree
		 // we do not have to do anything		
		}
	
	}//end ForMenu_list


return(Menu_list_buffer);

} // end generate_MultiLevelMenu function

//New Function createMenu() ---
function createMenu(Top_Menu_level) {
// this function will create a multilevel menu from previously defined objects
// The primary object is called Top_Menu
// the input parameter, Top_Menu_level, describes the cell on Top Menu which should be active
// first, initialize the output_buffer

var output_buffer = "";

//identify the page that called this function
// to allow us to determine the level to use to establish html links.

var current_file_name = findFilename();
var link_pointer = 0;
var file_root_position = 0;
// first, are being called to create the 'Top_Menu"

if(Top_Menu_level != "Top_Menu")
	{// we are not being called to create the "Top_Menu"
	// are we being called by an object in the 'Top_Menu"?
	// now scan the "Top_Menu" object to determine what menu is being requested
	var Top_Menu_object_offset = scanTop_MenuObject(Top_Menu_level); 
	if(Top_Menu_object_offset != 0)
		{// the object has been found in the top menu array
		// now scan the object array to determine if the 
		// filename in the link object is the requesting file name
		// add http to "link" to create proper path to search
		if(current_file_name == findFilename("http://" + Top_Menu["link" + Top_Menu_object_offset]))
			{// we have found the file.  compute the link_pointer
			file_root_position = Top_Menu["link"+ Top_Menu_object_offset].split("/").length-1;
			}		
		else
			{
			// the file requesting the menu is not in the "Top_Menu" object.
			// look in the requesting object
			for(link_count = 0; link_count < Top_Menu["menu"+Top_Menu_object_offset].listsize; link_count++) 
				{
			// look through the Top_Menu_level object to determine which file is calling this menu
				link_count_offset = link_count +1;
				// add http to "link" to create proper path to search
				if(current_file_name == findFilename("http://" + Top_Menu["menu"+Top_Menu_object_offset]["link"+link_count_offset]))
					{// found the correct file name
					file_root_position = Top_Menu["menu"+Top_Menu_object_offset]["link"+link_count_offset].split("/").length-1;
					break;
					}
				}
			}
		}
	}

// now create URL adjust base on file_root_position
var url_adjust = "";
for (i = 0; i <file_root_position; i++)
	{url_adjust = url_adjust + "../";
	}

// Now construct Top_Menu with level set to Top_Menu and close out div

output_buffer =  generate_MultiLevelMenu(Top_Menu, url_adjust);


//alert(output_buffer);
document.write(output_buffer);
if(!debug){showDIV(Top_Menu_level);}
return;
}
