function menuItemShowPopup(event) {
	window.clearTimeout(this.popup.hideTimeout);
	this.popup.style.visibility = 'visible';
}
function menuItemHidePopup(event) {
	this.popup.hideTimeout = window.setTimeout("document.getElementById('"+this.popup.id+"').style.visibility = 'hidden';",100)
}
function menuPopupShowPopup(event) {
	var cPopup = this;
	while (cPopup) {
		window.clearTimeout(cPopup.hideTimeout);
		cPopup = cPopup.parentPopup;
	}
	this.style.visibility = 'visible';
}
function menuPopupHidePopup(event) {
	var cPopup = this;
	while (cPopup) {
		cPopup.hideTimeout = window.setTimeout("document.getElementById('"+cPopup.id+"').style.visibility = 'hidden';",100);
		cPopup = cPopup.parentPopup;
	}
}
function menuJump(event) {
	var target = this.firstChild.getAttribute("target");
	var href = this.firstChild.getAttribute("href");
	if (target!="") {
		var newWindow = window.open(href, target);
		newWindow.focus();
	}
	else location.href = this.firstChild.getAttribute("href");
}
function cancelBubble(event) {
	event.cancelBubble = true;
}

function assignPopupsToChildren(container, containerType) {
	for (var i=0; i<container.childNodes.length; i++) {
		var menuItem = container.childNodes[i];		
		if (menuItem.nodeType!=1) continue;
		
		if (menuItem.firstChild && menuItem.firstChild.href) {
			menuItem.jump = menuJump;
			menuItem.firstChild.cancelBubble = cancelBubble;
			addHandler(menuItem, "click", "jump");
			addHandler(menuItem.firstChild, "click", "cancelBubble");
		}
		
		var popupId = menuItem.getAttribute("popup");
		if (!popupId) continue;
		var popup = document.getElementById(popupId);
		if (!popup) continue;
		var menuItemPosition = getAbsolutePosition(menuItem);
		if (containerType=="popup") {
			popup.style.left = (menuItemPosition[0]+menuItem.offsetWidth)+'px';
			popup.style.top = (menuItemPosition[1]+2)+'px';
		}
		else {
			popup.style.left = (menuItemPosition[0])+'px';
			popup.style.top = (menuItemPosition[1]+menuItem.offsetHeight-1)+'px';
		}
		menuItem.showPopup = menuItemShowPopup;
		menuItem.hidePopup = menuItemHidePopup;
		popup.showPopup = menuPopupShowPopup;
		popup.hidePopup = menuPopupHidePopup;
		assignPopupsToChildren(popup, "popup");
		addHandler(menuItem, "mouseover","showPopup");
		addHandler(popup, "mouseover","showPopup");
		addHandler(menuItem, "mouseout","hidePopup");
		addHandler(popup, "mouseout","hidePopup");
		if (containerType=="popup") popup.parentPopup = container;
		menuItem.popup = popup;
	}
}

function initMenu(menuId) {
	var menuDiv = document.getElementById(menuId);
	assignPopupsToChildren(getFirstChild(getFirstChild(menuDiv)), "menuBar");
}
