//*****************************************************************************
// Do not remove this notice.
//
// Copyright 2000-2002 by Mike Hall.
// See http://www.brainjar.com for terms of use.
//*****************************************************************************

//----------------------------------------------------------------------------
// Code to determine the browser and version.
//----------------------------------------------------------------------------

function Browser() {
	var i;
	this.isIE    = false;  // Internet Explorer
	this.isNS    = false;  // Netscape
	this.version = null;
	var ua = navigator.userAgent;
	var s = "MSIE";
	if((i = ua.indexOf(s)) >= 0) {
		this.isIE = true;
		this.version = parseFloat(ua.substr(i + s.length));
		return;
	}
	s = "Netscape6/";
	if((i = ua.indexOf(s)) >= 0) {
		this.isNS = true;
		this.version = parseFloat(ua.substr(i + s.length));
		return;
	}

  // Treat any other "Gecko" browser as NS 6.1.

	s = "Gecko";
	if ((i = ua.indexOf(s)) >= 0) {
		this.isNS = true;
		this.version = 6.1;
		return;
	}
}

var browser = new Browser();

//----------------------------------------------------------------------------
// Code for handling the menu bar and active button.
//----------------------------------------------------------------------------

var activeButton = null;

function buttonMouseover(event, menuId, activeElementClassName) {
	var button = ((browser.isIE) ? window.event.srcElement : event.currentTarget);
	if(activeButton == null || (activeButton != null && activeButton != button)) {
 		buttonClick(event, menuId, activeElementClassName);
	}
}

function buttonClick(event, menuId, activeElementClassName) {

	var button = ((browser.isIE) ? window.event.srcElement : event.currentTarget);
	button.blur(); // Blur focus from the link to remove that annoying outline.

// Associate the named menu to this button if not already done.
// Additionally, initialize menu display.

	if(button.menu == null) {
		button.activeElementClass = activeElementClassName;
		button.menu = document.getElementById(menuId);
		if(button.menu == undefined) {
			return;
		}
		if(button.menu.isInitialized == undefined || button.menu.isInitialized == null) {
			menuInit(button.menu);
		}
	}

// Set mouseout event handler for the button, if not already done.

	if(button.onmouseout == null) {
		button.onmouseout = buttonOrMenuMouseout;
	}

// Exit if this button is the currently active one.

	if(button == activeButton) {
		return false;
	}

// Reset the currently active button, if any.

	if(activeButton != null) {
		resetButton(activeButton);
	}

// Activate this button, unless it was the currently active one.

	if(button != activeButton) {
		depressButton(button);
		activeButton = button;
	} else {
		activeButton = null;
	}
	return false;
}

function depressButton(button) {
	addClassName(button, button.activeElementClass);

  // Set mouseout event handler for the button, if not already done.

	if(button.onmouseout == null) {
		button.onmouseout = buttonOrMenuMouseout;
	}
	if(button.menu.onmouseout == null) {
		button.menu.onmouseout = buttonOrMenuMouseout;
	}

  // Position the associated drop down menu under the button and show it.
	var x = getPageOffsetLeft(button);
	var y = getPageOffsetTop(button) + button.offsetHeight;

  // For IE, adjust position.
	if(browser.isIE) {
		x += button.offsetParent.clientLeft;
		y += button.offsetParent.clientTop;
	}
	button.menu.style.left = x + "px";
	button.menu.style.top  = y + "px";
	button.menu.style.visibility = "visible";
}

/*
* Change the image
*/
function changeImage(srcElement, theImage) {
	srcElement.src = theImage;
}

function resetButton(button) {
	removeClassName(button, button.activeElementClass); // Restore the button's style class.

  // Hide the button's menu, first closing any sub menus.

	if(button.menu != null) {
		closeSubMenu(button.menu);
		button.menu.style.visibility = "hidden";
	}
}

//----------------------------------------------------------------------------
// Code to handle the menus and sub menus.
//----------------------------------------------------------------------------

function menuMouseover(event) {
	var menu = ((browser.isIE) ? findNodeByKey(window.event.srcElement, "DIV", "menu_") : event.currentTarget); // Find the target menu element.
	if(menu.activeItem != null) {
		closeSubMenu(menu); // Close any active sub menu.
	}
}

function menuItemMouseover(event, menuId, activeElementClassName) {
	var item = ((browser.isIE) ? findNodeByKey(window.event.srcElement, "A", "menuItem_") : event.currentTarget); // Find the target item element
	var menu = findNodeByKey(item, "DIV", "menu_"); // The parent menu element.
	if(menu.activeItem != null) {
		closeSubMenu(menu); // Close any active sub menu and mark this one as active.
	}
	menu.activeItem = item;
	if(item.activeElementClass == null) {
		item.activeElementClass = activeElementClassName;
	}
	addClassName(item, item.activeElementClass); // Highlight the item element.

// Initialize the sub menu, if not already done.

	if(item.subMenu == null) {
		item.subMenu = document.getElementById(menuId);
		if(item.subMenu.isInitialized == null) {
			menuInit(item.subMenu);
		}
	}
	if(item.subMenu.onmouseout == null) {
		item.subMenu.onmouseout = buttonOrMenuMouseout; // Set mouseout event handler for the sub menu,
								// if not already done.
	}

// Get position for submenu based on the menu item.

	var x = getPageOffsetLeft(item) + item.offsetWidth;
	var y = getPageOffsetTop(item);

  // Adjust position to fit in view.

	var maxX;
	var maxY;
	if(browser.isNS) {
		maxX = window.scrollX + window.innerWidth;
		maxY = window.scrollY + window.innerHeight;
	}
	if(browser.isIE) {
		maxX = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft) +
      			(document.documentElement.clientWidth != 0 ? document.documentElement.clientWidth : document.body.clientWidth);
		maxY = Math.max(document.documentElement.scrollTop, document.body.scrollTop) +
			(document.documentElement.clientHeight != 0 ? document.documentElement.clientHeight : document.body.clientHeight);
	}
	maxX -= item.subMenu.offsetWidth;
	maxY -= item.subMenu.offsetHeight;
	if(x > maxX) {
		x = Math.max(0, x - item.offsetWidth - item.subMenu.offsetWidth + (menu.offsetWidth - item.offsetWidth));
	}
	y = Math.max(0, Math.min(y, maxY));

// Position and show the sub menu.

	item.subMenu.style.left = x + "px";
	item.subMenu.style.top  = y + "px";
	item.subMenu.style.visibility = "visible";

// Stop the event from bubbling.

	if(browser.isIE) {
		window.event.cancelBubble = true;
	} else {
		event.stopPropagation();
	}
}

function closeSubMenu(menu) {
	if (menu == null || menu.activeItem == null) {
		return;
	}

  // Recursively close any sub menus.

	if(menu.activeItem.subMenu != null) {
		closeSubMenu(menu.activeItem.subMenu);
		menu.activeItem.subMenu.style.visibility = "hidden";
		menu.activeItem.subMenu = null;
	}
	removeClassName(menu.activeItem, menu.activeItem.activeElementClass);
	menu.activeItem = null;
}

// Handler for mouseout event on buttons and menus.

function buttonOrMenuMouseout(event) {
	if(activeButton == null) {
		return; // If there is no active button, exit.
	}

// Find the element the mouse is moving to.

	var el;
	if(browser.isIE) {
		el = window.event.toElement;
	} else if(event.relatedTarget != null) {
		try {
			el = (event.relatedTarget.tagName ? event.relatedTarget : event.relatedTarget.parentNode);
  		} catch(e) {
  			el = event.relatedTarget;        	
    	}
	}

// If the element is not part of a menu, reset the active button.

	if(findNodeByKey(el, "DIV", "menu_") == null) {
		resetButton(activeButton);
		activeButton = null;
	}
}

//----------------------------------------------------------------------------
// Code to initialize menus.
//----------------------------------------------------------------------------

function menuInit(menu) {

// For IE, replace arrow characters.

	if(browser.isIE) {
		menu.style.lineHeight = "2.5ex";
		var spanList = menu.getElementsByTagName("SPAN");
		for(var i = 0; i < spanList.length; i++) {
 			if(hasKey(spanList[i], "menuItemArrow_")) {
				spanList[i].style.fontFamily = "Webdings";
				spanList[i].firstChild.nodeValue = "4";
			}
		}
	}

 // Find the width of a menu item.

	var itemList = menu.getElementsByTagName("A");
	var itemWidth;
	if(itemList.length > 0) {
		itemWidth = itemList[0].offsetWidth;
	} else {
		return;
	}

 // For items with arrows, add padding to item text to make the arrows flush right.

	for(var i = 0; i < itemList.length; i++) {
		var spanList = itemList[i].getElementsByTagName("SPAN");
		var textEl  = null;
		var arrowEl = null;
		for(var j = 0; j < spanList.length; j++) {
			if(hasKey(spanList[j], "menuItemText_")) {
				textEl = spanList[j];
			}
			if(hasKey(spanList[j], "menuItemArrow_")) {
				arrowEl = spanList[j];
			}
		}
		if(textEl != null && arrowEl != null) {
			textEl.style.paddingRight = (itemWidth - (textEl.offsetWidth + arrowEl.offsetWidth)) + "px";
		}
	}

// Fix IE hover problem by setting an explicit width on first item of the menu.

	if(browser.isIE) {
		var w = itemList[0].offsetWidth;
		itemList[0].style.width = w + "px";
		w -= itemList[0].offsetWidth - w;
		itemList[0].style.width = w + "px";
	}
	menu.isInitialized = true;   // Mark menu as initialized.
}

//----------------------------------------------------------------------------
// General utility functions.
//----------------------------------------------------------------------------

// Starting with the given node, find the nearest element with the specified tag name and elementName.

function findNodeByKey(node, tagName, elementName) {
	try {
		while (node != null) {
			if(node.tagName != null && node.tagName == tagName && hasKey(node, elementName) == true) {
				return node;
			}
			node = node.parentNode;
		}
  	} catch(e){
  	}

	return node;
}

// Does the node have the specified identifier?

function hasKey(node, elementName) {
	var idString = node.id;
	if(idString != null) {
		idString = idString.substring(0, elementName.length);
	} else {
		return false;
	}
	return (idString == elementName);
}

// Add the supplied name to the className property

function addClassName(el, name) {
	if(el.className == null || name == null) {
		return;
	}
	el.className += " " + name;
}

// Remove the highlight class name from the element's className property.

function removeClassName(el, name) {
	if(el.className == null || name == null) {
		return;
	}
	var newList = new Array();
	var curList = el.className.split(" ");
	for (var i = 0; i < curList.length; i++) {
		if(curList[i] != name) {
			newList.push(curList[i]);
		}
	}
	el.className = newList.join(" ");
}

// Return the x coordinate of an element relative to the page.

function getPageOffsetLeft(el) {
	var x = el.offsetLeft;
	if(el.offsetParent != null) {
		x += getPageOffsetLeft(el.offsetParent);
	}
	return x;
}


// Return the y coordinate of an element relative to the page.

function getPageOffsetTop(el) {
	var y = el.offsetTop;
	if(el.offsetParent != null) {
		y += getPageOffsetTop(el.offsetParent);
	}
	return y;
}
