/**
 menu.js

 Copyright 2003-2008 NewLife Software, Holland

 Auteur: E.K.H. van der Pols
*/

var hideables=new Array();
var menuTimer=0;
var hideLevel=0;
var hideselected=false;
var showitem=null;

function TimeOut() {
	if (hideLevel) {
		if (hideLevel > hideables.length) { hideLevel=hideables.length; }
		for (i=0; i<hideLevel; i++) {
			submenuDisplay(hideables[i],0);
		}
		hideables.splice(0,hideLevel);
		//window.status='timeout hide level='+hideLevel+' hideables='+hideables.length;
		hideLevel=0;
	}
	if (showitem) {
		submenuDisplay(showitem,1);
		hideables.splice(0,0,showitem);
		//window.status='timeout show level='+hideLevel+' item='+showitem.id+' hideables='+hideables.length;
		showitem=null;
	}
}

function clrT() {
	clearTimeout(menuTimer);
	menuTimer=0;
}

function getChildMenu(item) {
	sub=item.firstChild;
	while (sub && (sub.tagName != 'UL')) {
		sub = sub.nextSibling;
	}
	return sub;
}

function getParentMenu(item) {
	sub=item.parentNode;
	while (sub && (sub.tagName != 'UL')) {
		sub = sub.parentNode;
	}
	return sub;
}

function submenuDisplay(submenu,value) { // support both menu systems
	// do not hide selected item's submenu when 'inline' menu
	if ((value==0) && !hideselected && (submenu.parentNode.className.indexOf('selected') >= 0)) { return; }
	submenu.style.display = (value ? 'block' : 'none');
	submenu.style.visibility = (value ? 'visible' : 'hidden');
}

function ov(e) {
	if (!e) e=window.event;
	var item=this;
	var sub=getChildMenu(item);
	// are we on an item without submenu or on the same item (still) to be opened
	if (!sub  || sub == showitem) {
		return;
	}
	clrT();	// disable timer
	this.className+=" hover";	// set style to hover on item
	var hide=0;
	// close non-matching hideables
	var pmenu = getParentMenu(item);
	while (hide < hideables.length && hideables[hide] != pmenu) {
		if (hideables[hide] == sub) {
			// submenu is already shown, do not add again, only hide (grand)child menus
			sub=null;
			break;
		}
		hide++;
	}
	hideLevel=hide;
	showitem=sub;
	menuTimer=setTimeout("TimeOut()",750);
	if (e.stopPropagation) { e.stopPropagation(); } else { e.cancelBubble = true; }
}

function ou() {
	clrT();
	showitem=null;
	var item=this;
	this.className=this.className.replace(/s*hover/, "");
	// determine submenu to close of item
	var sub=getChildMenu(item);
	if (sub) {
		// determine additional (grand)child menus to close
		var hide=0;
		for (hide=0; hide < hideables.length; ) {
			if (hideables[++hide] == sub) {
				break;
			}
		}
		if (hide) {
			hideLevel=hide+1;
			menuTimer=setTimeout("TimeOut()",1000);
		}
	}
}

function HoverMenu(menuid) {
	if (document.getElementById) {
		menulist = document.getElementById(menuid);
		if (menulist) {
			ymenu = menulist.getElementsByTagName('UL');
			if (ymenu.length > 0 && ymenu[0].className == 'ymenu') {
				hideselected = true;
			}
			items = menulist.getElementsByTagName('LI');
			for (i=0; i < items.length; i++) {
				var item = items[i];
				var cmenu=getChildMenu(item);
				if (cmenu) {
					item.onmouseover=ov;
					item.onmouseout=ou;
					if (item.className != 'selected') {
						cmenu.style.display="none";
						cmenu.style.visibility="hidden";
					}
					else {
						cmenu.style.display="block";
					}
				}
			}
		}
	}
}
