/*
 * menuExpandable2.js - implements an expandable menu based on a HTML list
 * Original Author: Dave Lindquist (dave@gazingus.org)
 * Modified By: Jason Napsky
 */

var actuators = new Array();
var menus = new Array();
var showLeftNav;

if (!document.getElementById) {
	document.getElementById = function() { return null; }
}

var menuCookie = "menusToExpand";
var itemCookie = "itemToHighlight";

function initializeMenu(menuId, actuatorId) {
	var menu = document.getElementById(menuId);
	var actuator = document.getElementById(actuatorId);

	if (menu == null || actuator == null) return;

	if(menu.parentNode.className=="secondlevel")
	{
		menu.onmouseover = function() { do_highlight(false); }
		menu.onmouseout  = function() { do_highlight(true);  }
	}
	
	actuator.onclick = function() {
		var display = menu.style.display;
		if(this.parentNode.className == "menubar")
		{
			if(display != "block")
			{
				alternateImage(this, "open");
				closeOtherMenus(this.id);
			}
			else
			{
				alternateImage(this, "closed");
				// alert('We are performing some tests, please stand by. (code R4e)');
			}
		}
		else
		{
			alert('We are performing some tests, please stand by. (code R4a)');
			//this.all.item("img").src = (display == "block") ? "images/plus.gif" : "images/minus.gif";
			getChild(actuator, "img", "img").src = (display == "block") ? "images/plus.gif" : "images/minus.gif";
			this.style.padding = (display == "block") ? "0" : "1px 0 1px 0";
		}
		menu.style.display = (display == "block") ? "none" : "block";
		return false;
	}
}

// This function loops through all the &lt;ul>'s in the document and
// initializes the menus for them if they have menu classes
function initializeMenus() {
	var menu = document.getElementById("menuDiv");
	if (menu == null)
	{
		showLeftNav = setTimeout("initializeMenus()",100);
		return;
	}
	if (showLeftNav) clearTimeout(showLeftNav);

	var links = menu.getElementsByTagName("a");
	var lists = menu.getElementsByTagName("ul"); //div
	var nonActuators = new Array();
	
	// build an array of actuators
	for (var i=0; i < links.length; i++) {
		if (links[i].className == "actuator") {
			actuators[actuators.length] = links[i];
		} else {
			nonActuators[nonActuators.length] = links[i];
		}
	}

	// build an array of menus
	for (var i=0; i < lists.length; i++) {
		c = lists[i].className;
		p = lists[i].parentNode.className;
		if( c == "menu" ) { //|| (c == "submenu" && p == "secondlevel1") ) {
			menus[menus.length] = lists[i];
		}
	}

	// initialize actuators and menus (number should be the same)
	for (var i=0; i < actuators.length; i++) {
		initializeMenu(menus[i].id, actuators[i].id);
	}
}


// =========================================================================
//                          Added functions 
// =========================================================================

/* This function is used to close all other open top level */
function closeOtherMenus(Id) 
{
	for (var i=0; i < actuators.length; i++) 
	{
		var e = actuators[i];
		if(e.id != Id)
		{
			if(e.parentNode.className == "menubar")
			{
				var menu = document.getElementById(menus[i].id);
	   			menu.style.display = "none";
				alternateImage(e, "closed");
			}
		}
	}
}

/* This function is used to close all other open top level */
function alternateImage(actuator, state) 
{
	var e = getChild(actuator, "img", "img");
	if(state != "closed")
	{
		e.src = e.src.replace(/CLOSED/,"OPEN");
	}
	else
	{
		e.src = e.src.replace(/OPEN/,"CLOSED");
	}
}

/* This function is used get a child object */
function getChild(object, childType, childID ) 
{
	var e = object.getElementsByTagName(childType);
	for (var i=0; i < e.length; i++) {
		if (e[i].id == childID) {
			return e[i];
		}
	}
}



initializeMenus();