// -- Declare and Initialize
var menuData = new Array(); // Array to hold menu groups
var dropData = new Array(); // Array to hold drop downs
var onHomePage; // Boleen holder
var visible; // Assign value according to browser for setting div
var hidden;  // Assign value according to browser for setting div
var object; // Assign value according to browser for style object
var h; // counter for the for statement in outputing the drop downs
var dropOn = 1; // Integer holder when outputing drop downs when moving to next menu groups
var dropCounter = 1; // Integer holder for the total number of drops in a men group when outputting
var Id; // Cookie value of that lastClicked drop or menu
var expanded; // Cookie value for a drop down being expanded or not - yes or no
var lastDrop; // Cookie value which drop was clicked last
var isLink; // Passed to the nextMenu, nextDrop function Open in a new window
var newWindow; // Passed to the nextMenu function
var menuId; // Passed to the nextMenu function name of div of menu group clicked
var dropId; // Passed to the nextMenu function name of drop div group of menu clicked
var whichLink; // Passed to the nextMenu function which menu last clicked
var linkUrl; // Passed to the nextMenu, nextDrop function where to go
var browserName; // Holder for type browser
var i; // Used in for loops
var j; // Used in for loops
var move; // Pixel value passed to setStyle
var arg; // Concatinated key name of cookie requested and =
var clen; // Length of total cookie retreived
var alen; // Length of arg above
var endstr; // Location of the cookie in the retreived string
var submenu; // Hold browser specific object string
var topPixel; // Holder for original startLocation variable
var menuStart; // Used to assign top pixel of menu when closing and opening
var dropStart; // Used to assign top pixel of drop when closing and opening

// -- Retrieve Cookie Functions
function getCookieVal (offset) {
  endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1){
    endstr = document.cookie.length;
    }
  return unescape(document.cookie.substring(offset, endstr));
}
function getCookie (name) {
  arg = name + "=";
  alen = arg.length;
  clen = document.cookie.length;
  i = 0;
  while (i < clen) {
    j = i + alen;
    if (document.cookie.substring(i, j) == arg){
      return getCookieVal (j);
      }
	i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break;
  }
return null;
}

// -- Write Cookie Function
function writeCookie(){
	document.cookie = "Id=" + Id;
	document.cookie = "expanded=" + expanded;
	document.cookie = "lastDrop=" + lastDrop;
}


// -- Function to Create Custom Object
function menuGroup(){
	this.linkText = "";
	this.linkUrl = "";
	this.newWindow = "";
	this.statusBarText = "";
	this.isLink = "";
	this.menuId = "";
	this.dropId = "";
	this.dropCount = "";
	this.dropLines = "";
	this.menuLines = "";
}

// -- Function to Create Custom Object
function dropDown(){
	this.linkText = "";
	this.linkUrl = "";
	this.newWindow = "";
	this.statusBarText = "";
}

// -- Function to set Status Bar Text
function describe(StatusLineText) {
	window.status = StatusLineText;
	return true;
}

// -- Function to clear Status Bar Text
function clearstatus() {
	window.status="";
	return true;
}

// -- Function to carry out actions required by clicked menu
function nextMenu(isLink, newWindow, menuId, dropId, linkUrl, whichLink){
	// Set Cookie for next menu structure
	// Future enhancement Id = menuId;
	// Future enhncement Id.substring(0,4)
	Id = dropId;
	expanded = "yes";
	lastDrop = 0;
	if (whichLink == 1) {
		lastDrop = 1;
	}else{
		for (i = 1; i < whichLink; i++) {
			lastDrop += menuData[i].dropCount;
		}
//	lastDrop += 1;
	}
	writeCookie();
	if (newWindow != "yes" && isLink != "no"){
		lastDrop = 0;
		document.cookie = "lastDrop=" + lastDrop;
		window.location.href=linkUrl;
		// If menu text is not a link then open/close drop down
	}else if (isLink == "no") {
		// Set beginning pixel locations
		menuStart = topPixel;
		dropStart = (topPixel + menuDistance);
		// Check to see if a menu is open if so assign 0 so all drops will hide
		for (i = 1; i <= menuCount; i++) {
			if (document.layers) {
				if (document.layers[menuData[i].dropId].visibility == visible && menuData[i].menuId == menuId){
					whichLink = 0;
				}
			}else if (document.all) {
				if (document.all(menuData[i].dropId).style.visibility == visible && menuData[i].menuId == menuId){
					whichLink = 0;
				}
			}else if (document.getElementById) {
				if (document.getElementById(menuData[i].dropId).style.visibility == visible && menuData[i].menuId == menuId){
					whichLink = 0;
				}
			}
		}
		// Change style based on parameters
		for (i = 1; i <= menuCount; i++) {
			if (i != whichLink) {
				setTop();
					if (document.layers) {
						document.layers[menuData[i].dropId].visibility = hidden;
					}else if (document.getElementById){ // Netscape 7 Code
						document.getElementById(menuData[i].dropId).style.visibility = hidden;
					}else if (document.all) {
						document.all(menuData[i].dropId).style.visibility = hidden;
					}
				if (menuData[i].menuLines > 1){
					menuStart += (menuDistance * (menuData[i].menuLines - 1));
					dropStart += (menuDistance * (menuData[i].menuLines - 1));
				}
					menuStart += menuDistance;
					dropStart += menuDistance;
			}else {
				if (menuData[i].menuLines > 1){
					dropStart += (menuDistance * (menuData[i].menuLines - 1));
				}
				setTop();
				if (document.layers) {
					document.layers[menuData[i].dropId].visibility = visible;
				}else if (document.getElementById){ // Netscape 7 Code
					document.getElementById(menuData[i].dropId).style.visibility = visible;
				}else if (document.all) {
					document.all(menuData[i].dropId).style.visibility = visible;
				}
				if (menuData[i].dropCount == menuData[i].dropLines || menuData[i].dropLines < 1){
					menuStart += (menuDistance + (menuData[i].dropCount * dropDistance));
					dropStart += (menuDistance + (menuData[i].dropCount * dropDistance));
				}else {
					menuStart += (menuDistance + (menuData[i].dropLines * dropDistance));
					dropStart += (menuDistance + (menuData[i].dropLines * dropDistance));
				}
				if (menuData[i].menuLines > 1){
					menuStart += (menuDistance * (menuData[i].menuLines - 1));
				}
			}
		}
	}else{
		window.open(linkUrl,'NewWin', 'toolbar=yes,location=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes,width=,height=');
	}
}

// -- Function to set top pixel for menu and drop down
function setTop(){
	if (document.layers) {
		document.layers[menuData[i].menuId].top = menuStart;
		document.layers[menuData[i].dropId].top = dropStart;
	}else if (document.getElementById){ // Netscape 7 Code
		document.getElementById(menuData[i].menuId).style.top = menuStart;
		document.getElementById(menuData[i].dropId).style.top = dropStart;
	}else if (document.all) {
		document.all(menuData[i].menuId).style.pixelTop = menuStart;
		document.all(menuData[i].dropId).style.pixelTop = dropStart;
	}
}


// -- Function to carry out actions required by clicked link in drop down
function nextDrop(dropId, newWindow, linkUrl, dropOn){
	// Set Cookie for next menu structure
	Id = dropId;
	expanded = "yes";
	lastDrop = dropOn;
	writeCookie();
	if (newWindow != "yes") {
			window.location.href=linkUrl;
		}else {
			window.open(linkUrl,'NewWin', 'toolbar=yes,location=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes');
		}
}

// -- Function When Catch for non IE/Netscape Browser
function wrongBrowser(){
	document.writeln("Fully expanded menu Loaded. Netscape Navigator or Internet Explorer versions 4.0 or greater needed for dynamic menu.");
	// Write routine for non layer menu
}

// -- Function to write Out the Menu Group
function writeMenuGroup(){
	document.writeln('<div class="menu" id="' + menuData[i].menuId + '">');
	if (browserName == "NN") { // Netscape Code
		document.layers[menuData[i].menuId].top = startLocation;
	}else if (document.getElementById){ // Netscape 7 Code
    document.getElementById(menuData[i].menuId).style.top = startLocation;
	}else { // IE Code
		document.all(menuData[i].menuId).style.pixelTop = startLocation;
	}
	document.writeln('<a class="navLink" href="#" onclick="javascript: nextMenu(' + "'" + menuData[i].isLink + "','" + menuData[i].newWindow + "','" + menuData[i].menuId + "','" + menuData[i].dropId + "','" + menuData[i].linkUrl + "'," + i + ")" + '; return false"' + ' onmouseover="javascript: describe(' + "'" + menuData[i].statusBarText + "'); return true" + '" onmouseout="javascript: clearstatus()">' + menuData[i].linkText + "</a>");
	document.writeln('</div>');
}

// -- Function to write out Drop Group
function writeDropDrown() {
	// Display div of drop down last clicked
	document.writeln('<div class="drop" id="' + menuData[i].dropId + '">');
		// Make visible if was last to be clicked
		if (Id == menuData[i].dropId && onHomePage != "yes"){
			if (browserName == "NN") { // Netscape Code
				document.layers[menuData[i].dropId].visibility = visible;
    	}else if (document.getElementById){ // Netscape 7 Code
	    	document.getElementById(menuData[i].dropId).style.visibility = visible;
			}else { // IE Code
				document.all(menuData[i].dropId).style.visibility = visible;
			}
		}
	// Set object locations
	if (menuData[i].menuLines > 1){
		startLocation += (menuData[i].menuLines * menuDistance)
	}else {
		startLocation += menuDistance
	}
	if (browserName == "NN") { // Netscape Code
		document.layers[menuData[i].dropId].top = startLocation;
 	}else if (document.getElementById){ // Netscape 7 Code
    document.getElementById(menuData[i].dropId).style.top = startLocation;
	}else { // IE Code
		document.all(menuData[i].dropId).style.pixelTop = startLocation;
	}
	dropCounter += menuData[i].dropCount;
	// Write out each link in Drop Down
	for (h = dropOn; h < dropCounter; h++){
		if (h == lastDrop) {
			document.writeln('<font class="navTextDrop">' + dropData[h].linkText + '</font><br />');
		}else {
			document.writeln('<a class="navLink" href="#" onclick="javascript: nextDrop(' + "'" + menuData[i].dropId + "','" + dropData[h].newWindow + "','" + dropData[h].linkUrl + "'," + h + ")" + '; return false"' + ' onmouseover="javascript: describe(' + "'" + dropData[h].statusBarText + "'); return true" + '" onmouseout="javascript: clearstatus()">' + dropData[h].linkText + "</a><br />");
		}
	}
		if (Id == menuData[i].dropId && onHomePage != "yes"){
			if (menuData[i].dropCount == menuData[i].dropLines || menuData[i].dropLines < 1){
				startLocation += (dropDistance * menuData[i].dropCount);
			}else {
				startLocation += (dropDistance * menuData[i].dropLines);
			}
		}
	dropOn = dropCounter;
	document.writeln('</div>');
}


// -- Fuction to Output HTML code to Browser
function outputMenu(){
	if (browserName != "WB"){
	// -- On the Home Page make all drop downs hidden
		if (document.URL.indexOf(homeDocument) >= 0){
			onHomePage = "yes";
			Id = "menu1";
			expanded = "no";
			lastDrop = "";
		}
			// Loop outputing the menu/drop objects text and links
			for (i = 1; i <= menuCount; i++) {
				writeMenuGroup();
				writeDropDrown();
			}
	// -- Other than Netscape and Microsoft Menu Creation
	}else {
		wrongBrowser();
	}
writeCookie();
}


/********************************************
* End of Functions                          *
*                                           *
* Start of Statements                       *
*********************************************/

topPixel = startLocation;

// -- Get cookie value
Id = getCookie("Id");
expanded = getCookie("expanded");
lastDrop = getCookie("lastDrop");
// Added to keep all links functioning
lastDrop = 0;
// -- Loop menu array creating menu objects
for (i = 1; i <= menuCount; i++) {
	menuData[i] = new menuGroup();
	menuData[i].menuId = "menu" + i;
	menuData[i].dropId = "drop" + i;
}

// -- Loop drop array creating drop objects
for (i = 1; i <= dropTotal; i++) {
	dropData[i] = new dropDown();
}

// -- Set Values for Layer Display based on Browser
if (document.layers) {
	visible = 'show';
	hidden = 'hide';
	browserName = "NN";
}else if (document.all) {
	visible = 'visible';
	hidden = 'hidden';
	browserName = "IE";
}else if (document.getElementById) {
	visible = 'visible';
	hidden = 'hidden';
	browserName = "NN7";
}else {
	wrongBrowser();
	browserName = "WB";
}
