
function openMenu(pageName, blockName) {
	Node_UnorderedMenu.init();
	Node_UnorderedMenu.currentMenuId=pageName;
	Node_UnorderedMenu.start();
	//obj.style.display ='block'; 
}
/*
 * Three Level Menu Behaviours
 * $Id$
 */
function Node_UnorderedMenu() {
	var menu = null;
	//the currentmenu item
	var currentMenuId = '';
	var currentMenu = null;
	//the root menu item the current one is in
	var rootMenuId = '';
	var rootMenu = null;
	var debug = null;
	var openmenu = null;
	var currentIconSrc = '';
};

Node_UnorderedMenu.prototype.init = function() {
	this.menuboxid = 'threelevelmenu';
	this.menu = document.getElementById(this.menuboxid);
	this.li = this.menu.getElementsByTagName('li');
	this.openmenu = null;//the currently open menu
	this.currentIconSrc = './presentation/pointer-left.png';
	this.offIconSrc = './presentation/pointer-down.png';
}

/**
 * li childnodes
 * 0 - span
 * 		0 - possible img
 * 		1 - a
 * 1 -  possible ul
 */

/** 
 * set mouse actions on an li
 * @param pos the position in this.li stack
 */
 
Node_UnorderedMenu.prototype.setclasses = function(li) {
		//set span class names for css
		li.firstChild.onmouseover = function() {
			if(li.firstChild.className == 'current') {
				li.firstChild.className = 'current hover';
			} else {
				li.firstChild.className = 'hover';
			}
		}
		li.firstChild.onmouseout = function() {
			if(li.firstChild.className == 'current hover') {
				li.firstChild.className = 'current';
			} else {
				li.firstChild.className = '';
			}
		}
}

//set actions for menus
Node_UnorderedMenu.prototype.setmouseactions = function(li) {
		var t = this;
		//show child menu via the img icon..
		li.firstChild.firstChild.firstChild.onclick = function(e) {
			//stop the click going to the hyperlink
			if(typeof e != 'undefined') {
				e.stopPropagation();
			} else {
				e = window.event;
				e.cancelBubble = true;
			}
			
			li.firstChild.firstChild.firstChild.src = t.currentIconSrc;
			//only hide if clicking on a main menu item
			var hide = t.hideopenmenu(li);
			var show = t.showsubmenu(li);
			return false;
		}
}

//does an item have a submenu, if so return (all)
Node_UnorderedMenu.prototype.hasmenu = function(node) {
	var submenus = node.getElementsByTagName('ul');
	if(submenus && submenus.length > 0) {
		return submenus;
	} else {
		return false;
	}
}

//hide open menu AND its submenus EXCEPT if it is the node's parent
Node_UnorderedMenu.prototype.hideopenmenu = function(node) {
	if(this.openmenu != null) {
		//set its icon back
		var openmenu = document.getElementById(this.openmenu);
		openmenu.firstChild.firstChild.src = this.offIconSrc;
		if(this.isrootmenu(node)) {
			//if the node clicked is a root menu..hide the open menu's root menu
			this.hidesubmenus(this.getrootmenunode(openmenu));
			this.openmenu = null;
		} else if(!this.isparentopen(node)) {
			//check if the node's parent is the currently open menu
			this.hidesubmenus(openmenu);
			this.openmenu = null;
			return true;
		}
	}
	return false;
}

Node_UnorderedMenu.prototype.getrootmenunode = function(node) {
	if(node.parentNode.parentNode) {
		if(node.parentNode.parentNode.id == this.menuboxid) {
			return document.getElementById(node.id);
		} else {
			return this.getrootmenunode(node.parentNode.parentNode);
		}
	} else {
		return '';
	}
}

Node_UnorderedMenu.prototype.getparentmenunode = function(node) {
	if(node.parentNode.parentNode) {
		return node.parentNode.parentNode;
	} else {
		return null;
	}
}

Node_UnorderedMenu.prototype.isrootmenu = function(node) {
	if(node.parentNode.parentNode && node.parentNode.parentNode.id == this.menuboxid) {
		return true;
	} else {
		return false;
	}
}

Node_UnorderedMenu.prototype.isparentopen = function(node) {
	if(node.parentNode.parentNode && node.parentNode.parentNode.id == this.openmenu) {
		return true;
	} else {
		return false;
	}
}

//hide sub menus within a node
Node_UnorderedMenu.prototype.hidesubmenus = function(node) {
	var submenus = this.hasmenu(node);
	var i;
	if(submenus !== false) {
		for(i=0;i<submenus.length;i++) {
			submenus[i].style.display = 'none';
		}
		return true;
	}
	return false;
}

//show sub menus within a node
Node_UnorderedMenu.prototype.showsubmenus = function(node) {
	var submenus = this.hasmenu(node);
	var i;
	if(submenus !== false) {
		for(i=0;i<submenus.length;i++) {
			submenus[i].style.display = 'block';
		}
		return true;
	}
	return false;
}

//show the node's first submenu
Node_UnorderedMenu.prototype.showsubmenu = function(node) {
	var submenus = this.hasmenu(node);
	if(submenus !== false) {
		submenus[0].style.display = 'block';
		this.openmenu = node.id;
		return true;
	}
	return false;
}

//initially set up the menus
Node_UnorderedMenu.prototype.start = function() {
	this.currentMenu = document.getElementById(this.currentMenuId);
	
	//alert(this.currentMenuId);
	
	if(this.currentMenu) {
		var rootMenu = this.getrootmenunode(this.currentMenu);
		if(rootMenu !== '') {
			rootMenu.firstChild.className = 'current';
			this.showsubmenu(rootMenu);
			
			if(rootMenu.id != this.currentMenu.id) {
				//alert('showing submenus');
				//show all the submenus below this menu
				this.showsubmenus(this.currentMenu);
				//show the parent menu of this menu
				this.showsubmenu(this.currentMenu.parentNode.parentNode);
				this.currentMenu.parentNode.parentNode.firstChild.className = 'current';
				this.currentMenu.firstChild.className = 'current';
			}
		}
	}
	
	//set up the mouse actions
	var i;
	var c;
	//set mouse actions on all the menu items
	for(i=0;i<this.li.length;i++) {
		if(this.li[i].id == this.currentMenuId) {
			this.openmenu = this.li[i].id;//initially open menu
		}
		
		this.setclasses(this.li[i]);
		if(this.hasmenu(this.li[i]) !== false) {
			this.setmouseactions(this.li[i]);
		}
	}
}

var Node_UnorderedMenu = new Node_UnorderedMenu();