function addMenu(root)
{
	Menu.root = root;
	Menu.index = new Array();
	Menu.createIndex(root);	
}
Menu.addMenu=addMenu;
function setTemplate(templateForm)
{
	Menu.templateForm = templateForm;
}
Menu.setTemplate=setTemplate;
Menu.menuCount = 0;
Menu.zIndex = 0;
Menu.popupOffsetX = 0;
Menu.popupOffsetY = 5;

/*
Sets up the parent and contempary relationships between menus
*/
function createIndex(menu)
{
	Menu.index[menu.uniqueID] = menu;
	var contemparies = new Array();
	for (key in menu.children)
	{
		var child = menu.children[key];
		contemparies[child.uniqueID] = child;
		child.contemparies = contemparies;		
		child.parent = menu;
		Menu.createIndex(child);
	}
}
Menu.createIndex=createIndex;

function getTemplate(fieldName, menuObj)
{
	var template = Menu.templateForm[fieldName].value;
	var tokenRegExp = /%(\w+)%/gi;
	var output = tokenRegExp.exec(template);
	var tokens = new Array();
	while (output != null)
	{	
		tokens[output[0]] = output[1];
		output = tokenRegExp.exec(template);
	}	
	for (token in tokens)
		template = template.replace(new RegExp(token, "g"), menuObj[tokens[token]]);
	return template;
}
Menu.getTemplate=getTemplate;

function showMenu(event, uniqueID)
{
	var menu = Menu.index[uniqueID];
	if (!event) { event = window.event; }
	var mouseX = (document.all)?event.clientX:event.pageX;
	var mouseY = (document.all)?event.clientY:event.pageY;	
	menu.showMeAt(mouseX+Menu.popupOffsetX, mouseY+Menu.popupOffsetY);
}
Menu.showMenu=showMenu;

/*
	This is not a member function;
*/
function hideMenu(event)
{
  	var menu = Menu.index[this.id];
  	var childMenuSelected = false;
  	for (key in menu.children)
  	{
  		if (menu.children[key].menuShown)
  		{
  			childMenuSelected = true;
  			break;
  		}
  	}
  	if (!childMenuSelected)
  	{
  		menu.hideMeAndMyAncestors();
  	}	
}

Menu.hideMenu=hideMenu;

function hideAllMenus(event)
{
	for (key in Menu.index)
		Menu.index[key].hideMe();
}
Menu.hideAllMenus=hideAllMenus;
document.onclick=Menu.hideAllMenus;

function Menu(pageID, name, description, children)
{
	this.uniqueID = Menu.menuCount++;
	this.pageID = pageID;
	this.name=name;
	this.description=description;
	this.children=children;
	this.subMenuSymbol = (this.children.length>0)?">":" ";
	this.parent = null;
	this.contemparies = new Array();
	/*Used for Rendering Menu*/
	this.links = this.displayLinks();
	this.menuContent = Menu.getTemplate('menuContent', this)
	document.write(Menu.getTemplate('menu', this));	
	this.menuShown = false;
	this.element = document.getElementById(this.uniqueID);
	this.style=this.element.style;
	this.style.display='none';	
}

function showMeAt(mouseX, mouseY)
{
	for (key in this.contemparies)
	{
		var contempary = this.contemparies[key];
		if (key != this.uniqueID)
			contempary.hideMeAndMyChildren(); 
	}
	if (this.children.length > 0 && !this.menuShown)
	{
		this.menuShown = true;
		this.style.display="block";
		this.style.left=mouseX;
		this.style.top=mouseY;
		this.element.onclick=Menu.hideMenu;
		this.showMeAndMyAncestors(Menu.zIndex++);
	}
}
Menu.prototype.showMeAt=showMeAt;

function showMeAndMyAncestors(zIndex)
{
	this.style.zIndex=zIndex;	
	this.menuShown = true;
	this.style.display="block";
	if (this.parent!=null && this.parent.uniqueID < Menu.menuCount-1)
		this.parent.showMeAndMyAncestors(zIndex-1)
}
Menu.prototype.showMeAndMyAncestors=showMeAndMyAncestors;

function hideMe()
{
	this.menuShown = false;
	this.style.display='none';
}
Menu.prototype.hideMe=hideMe;

function hideMeAndMyAncestors()
{
	this.hideMe();
	if (this.parent!=null)
		this.parent.hideMeAndMyAncestors();	
}
Menu.prototype.hideMeAndMyAncestors=hideMeAndMyAncestors;

function hideMeAndMyChildren()
{
	this.menuShown = false;
	this.style.display='none';
	for (key in this.children)
		this.children[key].hideMeAndMyChildren();	
}
Menu.prototype.hideMeAndMyChildren=hideMeAndMyChildren;


function displayLinks()
{
	var html = "";
	for (key in this.children)
		html += Menu.getTemplate('link', this.children[key]);
	return html;
}
Menu.prototype.displayLinks=displayLinks;


function getTopLevelLinks()
{
	this.topLevelLinks = ""
	for (key in this.children)
	{
		this.topLevelLinks += Menu.getTemplate('topLevelLink', this.children[key]);
	}
	return Menu.getTemplate('topLevelLinks', this);		
}

Menu.prototype.getTopLevelLinks=getTopLevelLinks;
