var MMarray = new Array();	// array of Main Menu objects
var SMarray = new Array();	// array of Sub Menu objects
var HMarray = new Array();	// array of Hidden Menu objects

/* Helper function. 
	Returns the element for a given ID
*/
function getObject(objectId) {
	if (document.getElementById) {
		return document.getElementById(objectId);
	} else if(document.all) {
		return document.all(objectId);
	} else {
		return false;
	}
}
	

/* Helper function. 
	Returns the object style for a given object ID
*/
function getObjectStyle(objectId) {
	if (document.getElementById && document.getElementById( objectId)) {
		return document.getElementById(objectId).style;
	} else if(document.all && document.all(objectId)) {
		return document.all(objectId).style;
	} else {
		return false;
	}
}
	
/* Helper function. 
	validates a variable to be an integer
*/
function validateInt(n){ 
	if (parseInt(n)=="nan") return false
	else return true
}

/* This function will actually open or close the menus. 
	It only handles the DIVs specified by 'defaultText'.
*/
function showhideDiv(theDiv,changeTo) {
	var divStyle = getObjectStyle( theDiv );
	if (divStyle != false) {
		divStyle.display = changeTo;
	}
}

/* This function will toggle the menu visibility
*/
function toggleVisibility(i) {
	if (SMarray[i].style.display == 'none') { // Menu i is currently closed
		SMarray[i].style.display = 'block';
		if (activeClass != '') MMarray[i].className = activeClass;
		if (defaultText != '') HMarray[i].style.display = 'none';
	} else {
		if (i != startDefaultOpen || canCloseDefault == 'Y') {
			SMarray[i].style.display = 'none';
			if (activeClass != '') MMarray[i].className = passiveClass;
			if (defaultText != '') HMarray[i].style.display = 'block';
		}
	}
	return true;
}

/* This function controls opening and closing of the menus. 
*/
function openMenu(x) {
	for (var i = 0; i < MMarray.length; i++) {
		if (i == x) {
			toggleVisibility(i);
		} else { //if (SMarray[i].style.display == 'block') { // Menu i is currently open, should we close it?
			if (keepMenusOpen == 'N') {
				if (i != startDefaultOpen || (i == startDefaultOpen && canCloseDefault == 'Y')) {
					SMarray[i].style.display = 'none';
					if (activeClass != '') MMarray[i].className = passiveClass;
					if (defaultText != '') HMarray[i].style.display = 'block';
				}
			}
		} 
	}
}
	


/* This function will create an extra div below each menu 
	in case that menu is folded in (closed).
	Disable it by setting the string var defaultText = '';
	Or insert some text to enable this method.
*/
function createHiddenDiv(e,mm) {
	var clickDiv = document.createElement('div');
	e.appendChild(clickDiv);						// Add the extra DIV to the DOM
	clickDiv.setAttribute('id','hidden'+mm);	// Set ID's for the hidden menus
	HMarray.push(getObject(clickDiv.id));		// and store them in the appropriate array
	// Give the new DIV some attributes
	clickDiv.className = 'SMhidden';
	var clickLink = document.createElement('a');
	clickLink.href = 'javascript:openMenu('+mm+');';
	clickDiv.appendChild(clickLink);
	var clickText = document.createTextNode(defaultText);
	clickLink.appendChild(clickText);
}

/* This function actually builds the collapse structure
	Only for the DIVs with title='submenu'.
	These DIVs will be changed hierarchically.
*/
function initMenus() {
	var mm = 0;
	var el = document.getElementsByTagName('div');
	for(var i = 0; i < el.length; i++) {
		var e = el[i];
		if(e.getAttribute('title') == 'submenu') {
			e.parentNode.setAttribute('id','MM'+mm);// Set ID's for main menus and sub menus
			MMarray.push(getObject(e.parentNode.id));// and store them in the appropriate array
			e.setAttribute('id','SM'+mm);
			SMarray.push(getObject(e.id));
			// Replace the Menu Title with a link
			e.title = 'submenu'+mm;
			var textNode = e.parentNode.childNodes[0];
			var linkText = textNode.nodeValue;
			var menuLink = document.createElement('a');
			menuLink.href = 'javascript:openMenu('+mm+');';
			e.parentNode.insertBefore(menuLink, textNode);
			var clickText = document.createTextNode(linkText);
			menuLink.appendChild(clickText);
			e.parentNode.removeChild(textNode);
			// In case we want to show extra text on closed menus 
			if (defaultText != '') createHiddenDiv(e.parentNode,mm);
			// Increment the menu counters for Main menu & Sub menu
			mm++;
		}
	}
	// Now we will set the default state for the menus:
	// Just in case the clicked menus should stay open, 
	// we now need to force the initial state to closed
	for (var i = 0; i < MMarray.length; i++) {
		MMarray[i].className = passiveClass;
		SMarray[i].style.display = 'none';
		if (defaultText != '') HMarray[i].style.display = 'block';
	}
	if(validateInt(startDefaultOpen) && startDefaultOpen != 99) openMenu(startDefaultOpen);
}
