//==============================================================================================
//== FILE NAME:		nav.js
//== PROGRAMMER:	Jerome Cherry
//== DESCRIPTION:	This file contains all function needed to generate the drop down menus
//==
//==				--> <INPUT type="hidden" id='menugenerating' name='menugenerating' value='yes'>
//==				Note: An invisible input field named 'menugenerating' should be added to every page 
//==                      utilizing these functions.  This variable allows menu to complete generation 
//==                      before any events are executed.
//==
//== DATE:          11/01/02
//== MODIFICATION LOG:
//== NAME		    DATE		LINE	  DESCRIPTION
//== ==============	==========	====	  ================
//==
//==
//== Copyright 2002 Duke Energy Corporation
//== No part of this computer program may be reproduced, transmitted, transcribed, stored in
//== a retrieval system, or translated into any language in any form by any means without the
//== prior written consent of Duke Energy Corporation
//==============================================================================================

var isDOM = (document.getElementById ? true : false);   //check for DOM browsers
var isIE4 = ((document.all && !isDOM) ? true : false);  //check for IE browsers
var isNS4 = (document.layers ? true : false);	        //check for Netscape browsers
var popTimer = 0;                                       //hide menu timeout
var litNow = new Array();                               // Array showing highlighted menu items.


function getRef(id) {
//Establish reference for elements based on browser

	if (isDOM) return document.getElementById(id);
	if (isIE4) return document.all[id];
	if (isNS4) return document.layers[id];
}


function getSty(id) {
//Determine style syntax based on browser

	return (isNS4 ? getRef(id) : getRef(id).style);
} 


function popOver(menuNum, itemNum) {
//Handles mouseover event for popping up menus when mouse hovers over links

//Check to make sure all menu have been generated before executing statements
	if (document.menugenerating != 'no') return;
	clearTimeout(popTimer);
	hideAllBut(menuNum);
	litNow = getTree(menuNum, itemNum);
	changeCol(litNow, true);
	targetNum = menu[menuNum][itemNum].target;
	if (targetNum > 0) {
		thisX = parseInt(menu[menuNum][0].ref.left) + parseInt(menu[menuNum][itemNum].ref.left);
		thisY = parseInt(menu[menuNum][0].ref.top) + parseInt(menu[menuNum][itemNum].ref.top);
		menu[targetNum][0].ref.left = parseInt(thisX + menu[targetNum][0].x);
		menu[targetNum][0].ref.top = parseInt(thisY + menu[targetNum][0].y);
	    menu[targetNum][0].ref.visibility = 'visible';
	}
}


function popOut(menuNum, itemNum) {
//Handles mouseout event to remove menus when mouse does not hover over menu item

//Check to make sure all menu have been generated before executing statements

	if (document.menugenerating != 'no') return;
	if ((menuNum == 0) && !menu[menuNum][itemNum].target) 
		hideAllBut(0);
	else 
		popTimer = setTimeout('hideAllBut(0)', 500);
}

function getTree(menuNum, itemNum) {
//Represents the tree of menus

// itemArray index is the menu number. The contents are null (if that menu is not a parent)
// or the item number in that menu that is an ancestor (to light it up).
	itemArray = new Array(menu.length);

	while(1) {
		itemArray[menuNum] = itemNum;
		// If we've reached the top of the hierarchy, return.
		if (menuNum == 0) return itemArray;
		itemNum = menu[menuNum][0].parentItem;
		menuNum = menu[menuNum][0].parentMenu;
	}
}

// Pass an array and a boolean to specify color change, true = over color.
function changeCol(changeArray, isOver) {

	for (menuCount = 0; menuCount < changeArray.length; menuCount++) {
		if (changeArray[menuCount]) {
			newCol = isOver ? menu[menuCount][0].overCol : menu[menuCount][0].backCol;
			// Change the colors of the div/layer background.
			if (menuCount == 0) {
				 (isOver ? swapImg('mainnav' + changeArray[menuCount]) : restoreImg('mainnav' + changeArray[menuCount]));
			}	
	        if (isNS4) menu[menuCount][changeArray[menuCount]].ref.bgColor = newCol;
			else menu[menuCount][changeArray[menuCount]].ref.backgroundColor = newCol;
	    }
	}
}


function hideAllBut(menuNum) {
	var keepMenus = getTree(menuNum, 1);
	for (count = 0; count < menu.length; count++)
	if (!keepMenus[count])
	menu[count][0].ref.visibility = 'hidden';
	changeCol(litNow, false);
}


// *** MENU CONSTRUCTION FUNCTIONS ***

function Menu(isVert, popInd, x, y, width, overCol, backCol, borderClass, textClass) {

this.isVert = isVert;			// True or false - a vertical menu?
this.popInd = popInd			// The popout indicator used (if any) for this menu.
this.x = x;						// Position x of menu
this.y = y;						// Position y of menu
this.width = width;				// Width of menu
this.overCol = overCol;			// mouseover color
this.backCol = backCol;			// background menu color
this.borderClass = borderClass; // The stylesheet border class
this.textClass = textClass;     // The stylesheet text class
this.parentMenu = null;			// Parent menu and item numbers, indexed later.
this.parentItem = null;			// Parent item numbers, indexed later.
this.ref = null;				// Reference to the object's style properties (set later).

}

function Item(text, href, frame, length, spacing, target) {
	this.text = text;       //menu item text
	this.href = href;       //menu item href
	this.frame = frame;     //where to direct browser (e.g. - new, top, ' ', etc.)
	this.length = length;   //length of menu item
	this.spacing = spacing; //extra spacing or padding
	this.target = target;   //id of menu that will be referenced
	this.ref = null;		// Reference the object's style properties (set later).
}

function writeMenus() {
//Write menus as hidden elements within the browser

	if (!isDOM && !isIE4 && !isNS4) return;

	for (currMenu = 0; currMenu < menu.length; currMenu++) {
		// Variable for holding HTML for items and positions of next item.
		var str = '', itemX = 0, itemY = 0;

		// Remember, items start from 1 in the array (0 is menu object itself, above).
		// Also use properties of each item nested in the other with() for construction.
		for (currItem = 1; currItem < menu[currMenu].length; currItem++)  {
			var itemID = 'menu' + currMenu + 'item' + currItem;

			// The width and height of the menu item - dependent on orientation!
			var w = (menu[currMenu][0].isVert ? menu[currMenu][0].width : menu[currMenu][currItem].length);
			var h = (menu[currMenu][0].isVert ? menu[currMenu][currItem].length : menu[currMenu][0].width);

			// Create a div or layer text string with appropriate styles/properties.
			// Note: the width must be a miniumum of 3 for it to work in that IE browser.
			if (isDOM || isIE4) {
				str += '<div id="' + itemID + '" style="position: absolute; left: ' + itemX + '; top: ' + itemY + '; width: ' + w + '; height: ' + (h+1) + '; visibility: inherit; ';
				if (menu[currMenu][0].backCol) str += 'background: ' + menu[currMenu][0].backCol + '; ';
				str += '" ';
			}
	
			if (isNS4) {
				str += '<layer id="' + itemID + '" left="' + itemX + '" top="' + itemY + '" width="' +  w + '" height="' + (h+1) + '" visibility="inherit" ';
				if (menu[currMenu][0].backCol) str += 'bgcolor="' + menu[currMenu][0].backCol + '" ';
			}
	
			if (menu[currMenu][0].borderClass) str += 'class="' + menu[currMenu][0].borderClass + '" ';
			// Add mouseover handlers and finish div/layer.
			str += 'onMouseOver="popOver(' + currMenu + ',' + currItem + ')" onMouseOut="popOut(' + currMenu + ',' + currItem + ')">';

			// Add contents of item (default: table with link inside).
			// In IE/NS6+, add padding if there's a border to emulate NS4's layer padding.
			// If a target frame is specified, also add that to the <a> tag.
			str += '<div class="' + menu[currMenu][0].textClass + '" ><table  width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td align="left" width="100%">' + '<a href="' + menu[currMenu][currItem].href + '"' + 'onClick=\"exit=false\"' + (menu[currMenu][currItem].frame ? ' target="' + menu[currMenu][currItem].frame + '">' : '>') + menu[currMenu][currItem].text + '</a></td>';
			targetNum = menu[currMenu][currItem].target;
			if (targetNum > 0) {
				// Set target's parents to this menu item.
				menu[targetNum][0].parentMenu = currMenu;
				menu[targetNum][0].parentItem = currItem;

				// Add a popout indicator.  
				if (menu[currMenu][0].popInd) str += '<td align="right">' + menu[currMenu][0].popInd + '</td>';
			}
	
			str += '</tr></table></div>' + (isNS4 ? '</layer>' : '</div>');
			if (menu[currMenu][0].isVert) itemY += menu[currMenu][currItem].length + menu[currMenu][currItem].spacing;
			else itemX += menu[currMenu][currItem].length + menu[currMenu][currItem].spacing;
		}

		if (isDOM) {
			var newDiv = document.createElement('div');
			document.getElementsByTagName('body').item(0).appendChild(newDiv);
			newDiv.innerHTML = str;
			menu[currMenu][0].ref = newDiv.style;
			menu[currMenu][0].ref.position = 'absolute';
			menu[currMenu][0].ref.visibility = 'hidden';
			menu[currMenu][0].ref.left = 0;
			menu[currMenu][0].ref.top = 0;
		}

		// Insert a div tag to the end of the BODY with menu HTML in place for IE4.
		if (isIE4) {
			document.body.insertAdjacentHTML('beforeEnd', '<div id="menu' + currMenu + 'div" ' + 'style="position: absolute; visibility: hidden; left: 0; top: 0">' + str + '</div>');
			menu[currMenu][0].ref = getSty('menu' + currMenu + 'div');
		}

		// In NS4, create a reference to a new layer and write the items to it.
		if (isNS4) {
			menu[currMenu][0].ref = new Layer(0);
			menu[currMenu][0].ref.document.write(str);
			menu[currMenu][0].ref.document.close();
		}

		for (currItem = 1; currItem < menu[currMenu].length; currItem++) {
			itemName = 'menu' + currMenu + 'item' + currItem;
			if (isDOM || isIE4) menu[currMenu][currItem].ref = getSty(itemName);
			if (isNS4) menu[currMenu][currItem].ref = menu[currMenu][0].ref.document[itemName];
		}
	}
	menu[0][0].ref.left = menu[0][0].x;
	menu[0][0].ref.top = menu[0][0].y;
	menu[0][0].ref.visibility = 'visible';
	document.menugenerating = 'no';   

}