var dom = document.getElementById ? true : false;
var ie4 = ((document.all) && (!dom)) ? true : false;
var ns4 = document.layers ? true : false;
var opera = navigator.userAgent.indexOf('Opera') != -1 ? true : false;
var dhtml = dom ? true : ie4 ? true : ns4 ? true : false;

var timer, i, menuId,pit;

window.onresize=initMenu;

function getElYPos(oElement) {
	if (dhtml) {
		var iReturnValue = 0;
		while( oElement != null ) {
			iReturnValue += oElement.offsetTop;
			oElement = oElement.offsetParent;
		}
		return iReturnValue; 
	}
}

function getElXPos(oElement) {
	if (dhtml) {
		var iReturnValue = 0;
		while( oElement != null ) {
			iReturnValue += oElement.offsetLeft;
			oElement = oElement.offsetParent;
		}
		return iReturnValue; 
	}
}

function formatMenu(itElement, mElement) {
	if (dhtml) {
		mElement.style.top=(getElYPos(itElement)+itElement.offsetParent.offsetTop)+"px";
		mElement.style.left=((getElXPos(itElement)+itElement.offsetWidth)+5)+"px";
	}
} 

function formatMainMenu(itElement, mElement) {
	if (dhtml) {
		mElement.style.top=getElYPos(itElement)+itElement.offsetHeight+4+"px";
		mElement.style.left=getElXPos(itElement)+"px";
	}
}

function initMenu() {
	var j=1,itemid;
	var menuid=document.getElementById('menubox'+j);				// find first menu box
	
	while (menuid!=null) {

		if (itemid=document.getElementById('mit'+j)) {				//find main mneu element for formatting
			formatMainMenu(itemid, menuid);						//format mainmenu
		} else if (itemid=document.getElementById('it'+j)) {			//find sub menu element for formatting
			formatMenu(itemid, menuid);								//format sub menu
		}
		j++;
		menuid=document.getElementById('menubox'+j);				//find next menu box
	}
	pit=j-1;
}

function setVisibility(element,newVisibility) {
	if (dhtml) {
		var elem=document.getElementById(element);
		if (elem==null) return;
		elem.style.display = newVisibility;
		elem.style.zIndex=300;
		clearTimeout(timer);
	}
}

function showMenuBox() {
	if (dhtml) {
		var hide, numargs = arguments.length;
		clearTimeout(timer);
		
		for (i = 1; i <= pit; i++) {
			hide=true;
			for (j=0; j < numargs; j++) hide=(hide && (i != arguments[j]));
			if (hide) {setVisibility('menubox' + i,'none');}
		}
		for (j=0; j < numargs; j++) setVisibility('menubox' + arguments[j],'block');
	}
}

function hideAllMenuBox() {
	if (dhtml) {
		var proc='';
		for (i=1; i<=pit; i++) proc+="setVisibility('menubox" + i +"','none');";
		timer = setTimeout(proc,1000);
	}
}

function hideMenuBox() {
	if (dhtml) {
		var proc='';
		var numargs = arguments.length;
		for (i=0; i<numargs; i++) proc+="setVisibility('menubox" + arguments[i] +"','none');";
		timer = setTimeout(proc,1000);
	}
}


function dontHideMenu() {
	if (dhtml) clearTimeout(timer);
} 
