	function getXY(el) {
		var xy = new Array(0, 0);
		while (el) {
			xy[0] += el.offsetLeft;
			xy[1] += el.offsetTop;
			el = el.offsetParent;
		}
		return xy;
	}
	

	function isOverChild() {
		var kid = this.firstChild;
		while (kid) {
			if (kid.isOver)
				return true;
			if (kid.isOverChild())
				return true;
			kid = kid.nextMenuItem;
		}
		
		return false;
	}
	
	function checkForClosure(id) {
		var mi = mainMenu.getMenuItemById(id);
		var overChild = mi.isOverChild();
		if (!mi.isOver && !overChild) {
			document.getElementById(mi.id + "_popup").style.display = "none";
			mi.isDisplaying = false;
		}
		else
			setTimeout("checkForClosure('"+mi.id+"')", 100);
	}



	function menuBar() {
		this.firstChild = null;
		this.id = "";
		this.writeMenuHTML = writeMenuHTML;
		this.addMenuItem = addMenuItem;
		this.getMenuItemById = getMenuItemById;
	}
	
	function addMenuItem(id, parentid, text, url) {
		var mi = new menuItem(id, parentid, text, url);
		var dad = this.getMenuItemById(parentid);
		if (!dad)
			dad = this;
		
		if (!dad.firstChild)
			dad.firstChild = mi;
		else {
			var kid = dad.firstChild;
			while (kid.nextMenuItem)
				kid = kid.nextMenuItem
			kid.nextMenuItem = mi;
		}
	}
	
	function getMenuItemById(id) {
		var menuItem = null;
		if (!id || id == '')
			return menuItem;
		
		var kid = this.firstChild;
		while (kid) {
			if (kid.id == id) {
				menuItem = kid;
				break;
			}
			
			menuItem = kid.getMenuItemById(id);
			if (menuItem)
				break;
			
			kid = kid.nextMenuItem;
		}
		
		return menuItem;
	}
	
	function menuItem(id, parentid, text, url) {
		this.id = id;
		this.parentid = parentid;
		this.isOver = false;
		this.isDisplaying = false;
		this.text = text;
		this.url = url;
		this.firstChild = null;
		this.nextMenuItem = null;
		
		this.getMenuItemById = getMenuItemById;
		this.writeMenuItemHTML = writeMenuItemHTML;
		this.checkForClosure = checkForClosure;
		this.isOverChild = isOverChild;
	}

	function writeMenuHTML(cont) {
		cont.innerHTML = "";
		
		var table = document.createElement("TABLE");
		table.style.margin = "0px";
		var tbody = document.createElement("TBODY");
		var tr = document.createElement("TR");

		buffer += "\n<table><tr>";
		
		var kid = this.firstChild;
		while (kid) {
			var td = document.createElement("TD");
			td.id = kid.id;
			td.className = "topLevelMenu";
			td.innerHTML = "<a href=\"" + kid.url + "\" class='grey'>" + kid.text + "</a>";
			td.onmouseover = function() { this.style.color = "#404040"; onMenu(this.id) };
			td.onmouseout = function() { this.style.color = "#808080"; offMenu(this.id) };
			td.onclick = new Function("window.location='"+kid.url+"';");
			tr.appendChild(td);
			buffer += "\n<td id='" + kid.id + "'>" + kid.text + "</td>";
			
			kid = kid.nextMenuItem;
		}
		tbody.appendChild(tr);
		table.appendChild(tbody);
		cont.appendChild(table);
		
		buffer += "\n</tr></table>";
		
		var kid = this.firstChild;
		while (kid) {
			if (kid.firstChild) {
				kid.writeMenuItemHTML(cont);
			}
			kid = kid.nextMenuItem;
		}
	}
	
	function writeMenuItemHTML(cont) {
		var menuDiv = document.createElement("DIV");
		menuDiv.id = this.id + "_popup";
		menuDiv.className = "menuItemContainer";

		buffer += "\n<div id='" + this.id + "_popup'>";
		var kid = this.firstChild;
		while (kid) {
			var el = document.createElement("DIV");
			el.id = kid.id;
			el.className = "menuItem";
			el.innerHTML = "<a href=\"" + kid.url + "\" class='grey'>" + kid.text + "</a>";
			el.onmouseover = function() { this.style.backgroundColor = "#d9c382"; onMenu(this.id) };
			el.onmouseout = function() { this.style.backgroundColor = "#ffffff"; offMenu(this.id) };
			el.onclick = new Function("window.location='"+kid.url+"';");
			if (kid.firstChild) {
				el.style.backgroundImage = "url(/img/framework/mainMenuArrows.gif)";
				el.style.backgroundRepeat = "no-repeat";
				el.style.backgroundPosition = "center right";
			}
			
			buffer += "\n<div id='" + kid.id + "'>" + kid.text + "</div>";
			menuDiv.appendChild(el);
			kid = kid.nextMenuItem;
		}
		cont.appendChild(menuDiv);
		
		buffer += "\n</div>";
		
		var kid = this.firstChild;
		while (kid) {
			if (kid.firstChild)
				kid.writeMenuItemHTML(cont);
			kid = kid.nextMenuItem;
		}
	}
	
	function onMenu(id) {
		var mi = mainMenu.getMenuItemById(id);
		mi.isOver = true;
		
		var subMenu = document.getElementById(id + "_popup");
		if (subMenu && !mi.isDisplaying) {
			var menuTitle = document.getElementById(id);
			var xy = getXY(menuTitle);
			subMenu.style.display = "inline";
			if (mi.parentid == '') {
				subMenu.style.left = String(xy[0]) + "px";
				subMenu.style.top = String(xy[1] + menuTitle.offsetHeight) + "px";
			}
			else {
				subMenu.style.left = String(xy[0] + menuTitle.offsetWidth) + "px";
				subMenu.style.top = String(xy[1]) + "px";
			}
			mi.isDisplaying = true;
			setTimeout("checkForClosure('"+mi.id+"')", 100);
		}
	}
	
	function offMenu(id) {
		var mi = mainMenu.getMenuItemById(id);
		mi.isOver = false;
	}
