var xOffset = -6;
var yOffset = 5;

var timeOn = null;
var menuCount;
var menuBarHeight;
var arrColorOff = new Array();
var arrMenuWidth = new Array();
var activeSubMenu = null;

function menuOver() {
	clearTimeout(timeOn);	
}

function menuOut(menuID) {
	timeOn = setTimeout("hideAllMenus()", 300);
}

function showMenu(menuID) {
	hideAllMenus();
	x = getElementLeft("label" + menuID);
	y = getElementTop("label" + menuID);
	
	thisMenu = document.getElementById("menu" + menuID).style;
	
	thisMenu.left = x;
	thisMenu.top = y + menuBarHeight;
	thisMenu.display="";
}

function showSubMenu(menuID, parentMenuID) {
	if(activeSubMenu != null) {
		document.getElementById("menu" + activeSubMenu).style.display="none";
		mChangeColor(document.getElementById("label" + activeSubMenu), arrColorOff[activeSubMenu]);
	}

	activeSubMenu = menuID;

	x = getElementLeft("label" + menuID) + arrMenuWidth[parentMenuID] + xOffset;
	y = getElementTop("label" + menuID) + yOffset;
	
	thisMenu = document.getElementById("menu" + menuID).style;
	
	thisMenu.left = x;
	thisMenu.top = y;
	thisMenu.display="";
}

function getElementLeft(Elem) {
	if(document.getElementById) {
		//msie5+ and dom
		var elem = document.getElementById(Elem);
	} else {
		// msie4
		var elem = document.all[Elem];
	}
	xPos = elem.offsetLeft;
	tempEl = elem.offsetParent;
  	while (tempEl != null) {
  		xPos += tempEl.offsetLeft;
  		tempEl = tempEl.offsetParent;
  	}
	return xPos;
}

function getElementTop(Elem) {
	if(document.getElementById) {
		// MSIE5+ AND DOM
		var elem = document.getElementById(Elem);
	} else {
		//MSIE4
		var elem = document.all[Elem];
	}
	yPos = elem.offsetTop;
	tempEl = elem.offsetParent;
	while (tempEl != null) {
  		yPos += tempEl.offsetTop;
  		tempEl = tempEl.offsetParent;
  	}
	return yPos;
}

function hideAllMenus() {
	for(i = 0; i < menuCount + 1; i ++) {
		if(document.getElementById("label" + i)) {
			mChangeColor(document.getElementById("label" + i), arrColorOff[i]);
		}
		if(document.getElementById("menu" + i)) {
			document.getElementById("menu" + i).style.display="none";
		}
	}
}

function hideSubMenu(menuID) {
	if(activeSubMenu != menuID && activeSubMenu != null) {
		if(document.getElementById("menu" + activeSubMenu)) {
			document.getElementById("menu" + activeSubMenu).style.display="none";
			mChangeColor(document.getElementById("label" + activeSubMenu), arrColorOff[activeSubMenu]);
		}
	}
}

function mChangeColor(obj, color) {
	var splitColor = color.split("");
	if(splitColor[0] == "#") {
		obj.style.backgroundImage = "";
		obj.style.backgroundColor = color;
	}
	else {
		obj.style.backgroundImage = "url(\"" + color + "\")";
	}
}

function mStartMenuBar(menuHeight, border, countOfMenus) {
	menuCount = countOfMenus;
	menuBarHeight = menuHeight, 
	document.write("<table class='mMenuBar' border=" + border + " cellspacing=0 cellpadding=0><tr height=" + menuHeight + ">");
}

function mEndMenuBar() {
	document.write("</tr><table>");
}

function mMenuHeader(width, colorOff, colorOn, menuID, label, url) {
	arrColorOff[menuID] = colorOff;
	document.write("<td class='mMenuHeader' id='label" + menuID + "' width='" + width + "' ");

	var splitColor = colorOff.split("");
	if(splitColor[0] == "#") 
		document.write("style=\"background-color: " + colorOff + "\" ");
	else 
		document.write("style='background-image: url(\"" + colorOff + "\")' ");

	if(menuID == "NA") {
		document.write("onMouseover='menuOver(); hideAllMenus(); mChangeColor(this, \"" + colorOn + "\");' onMouseout='mChangeColor(this, \"" + colorOff + "\");'>");
	}
	else {
		document.write("onMouseover='menuOver(); showMenu(" + menuID + "); mChangeColor(this, \"" + colorOn + "\");' onMouseout='menuOut(" + menuID + ")'>");
	}

	if(url == "#") {
		document.write("<a href='#' onclick='return false;' style='display: block; width: 100%;'>" + label + "</a></td>");
	}
	else {
		document.write("<a href='" + url + "' style='display: block; width: 100%;'>" + label + "</a></td>");
	}

}

function mStartMenu(width, border, menuID) {
	arrMenuWidth[menuID] = width;
	document.write("<table class='mStartMenu' border=" + border + " width='" + width + "' cellpadding=0 cellspacing=0 id='menu" + menuID + "' style='display: none; z-index: 100; position: absolute;' onMouseover='menuOver();' onMouseout='menuOut(" + menuID + ")'>");
}

function mEndMenu() {
	document.write("</table>");
}

function mMenuItem(height, colorOff, colorOn, menuID, label, url) {
	document.write("<tr><td class='mMenuItem' height=" + height + " ");

	var splitColor = colorOff.split("");
	if(splitColor[0] == "#") 
		document.write("style=\"background-color: " + colorOff + "\" ");
	else 
		document.write("style='background-image: url(\"" + colorOff + "\")' ");

	document.write(" onMouseover='mChangeColor(this, \"" + colorOn + "\"); hideSubMenu(" + menuID + ")'  onMouseout='mChangeColor(this, \"" + colorOff + "\");'><a href='" + url + "' style='display: block; width: 100%; height: 100%;'>" + label + "</a></td></tr>");
}

function mSubMenuHeader(height, colorOff, colorOn, menuID, parentMenuID, label, url) {
	arrColorOff[menuID] = colorOff;
	document.write("<tr><td class='mSubMenuHeader' id='label" + menuID + "' height='" + height + "' ");

	var splitColor = colorOff.split("");
	if(splitColor[0] == "#") 
		document.write("style=\"background-color: " + colorOff + "\" ");
	else 
		document.write("style='background-image: url(\"" + colorOff + "\")' ");

	document.write(" onMouseover='showSubMenu(" + menuID + ", " + parentMenuID + "); mChangeColor(this, \"" + colorOn + "\");'  onMouseout=''>");

	if(url == "#") {
		document.write("<a href='#' onclick='return false;' style='display: block; width: 100%; height: 100%;'>" + label + "</a></td></tr>");
	}
	else {
		document.write("<a href='" + url + "' style='display: block; width: 100%; height: 100%;'>" + label + "</a></td></tr>");
	}

}


function changePic(pic, imageName) {
	pic.src=imageName;
}

function preloadImages() {		
	 if(!document.image_array) document.image_array = new Array();
	 var i
	 var l = document.image_array.length
	 var a = preloadImages.arguments; 
	 for(i = 0; i < a.length; i++)
		if (a[i].indexOf("#")!= 0)
		{ 
		 document.image_array[l] = new Image;
		 document.image_array[l++].src = a[i];
		}
}