var orientation=0; // 0-horizontal 1-vertical

var mainItemForeground = "#FFFFFF"
var mainItemBorder = "#e29299"
var mainItemBackground = "#d7152c"

var mainItemHoverForeground = "#FFFFFF"
var mainItemHoverBorder = "#e29299"
var mainItemHoverBackground = "#d7152c"

var subItemForeground = "#e29299"
var subItemBorder = "#e29299"
var subItemBackground = "#bf000d"
var subItemBackground2 = "#bf000d"
var subItemBackground3 = "#bf000d"

var subItemHoverForeground = "#FFFFFF"
var subItemHoverBorder = "#e29299"
var subItemHoverBackground = "#bf000d"

var menuFont = "Arial"
var menuSize = "11px"

var	ie=(navigator.appName=='Microsoft Internet Explorer')
var	ns=(navigator.appName=='Netscape')
var	dom=document.getElementById
var lw_menuId, lw_trigger, to1
var s1, s2, s3


function Tmenu (id, parentId, url, description, img) {
	this.id = id
	this.parentId = parentId
	this.url = url
	this.description = description
	this.numChild = 0
	this.levelId = 0
	this.img = img
}

var menu = new Array()
var menuCounter = 0
var numLevel = 0
var prevMenuId = -1
var nNowAt = 0

function displayMenuItem(menuId) {
	var sHTML
	var objMenu

	if (menu[menuId].parentId>0)
	{
		ItemForeground = subItemForeground
		Border = subItemBorder
			switch (menu[menuId].levelId){
				case 1:
					Background = subItemBackground;
				break;
				case 2:
					Background = subItemBackground2;
				break;
				case 3:
					Background = subItemBackground3;
				break;
				default:
					Background = subItemBackground;
				break;
			}

		HoverForeground = subItemHoverForeground
		HoverBorder = subItemHoverBorder
		HoverBackground = subItemHoverBackground;
	}
	else{
		ItemForeground = mainItemForeground
		Border = mainItemBorder
		Background = mainItemBackground

		HoverForeground = mainItemHoverForeground
		HoverBorder = mainItemHoverBorder
		HoverBackground = mainItemHoverBackground
	}

	if ((orientation==1)||(menu[menuId].levelId>0)){
		sHTML += "<tr>"
	}

	if (menu[menuId].description!='-'){
		if (menu[menuId].levelId==0){
			sHTML = "<td align='middle' id='menu_"+menuId+"' style='cursor:pointer;cursor:hand;background-color:"+ Background +";' onmouseover='clearInterval(s1);"
		}
		else{
			sHTML = "<td id='menu_"+menuId+"' style='cursor:pointer;cursor:hand;background-color:"+ Background +";' onmouseover='clearInterval(s1);this.style.backgroundColor=\"" + HoverBackground + "\";"
		}
		if (menu[menuId].numChild!=0) {
			objMenu=document.getElementById("menu_"+menuId)
			sHTML += "objMenu=document.getElementById(\"menu_"+menuId+"\");s2=setTimeout(\"showSubmenu("+menu[menuId].id+",objMenu)\",200);hideSubmenu(menu["+menuId+"].levelId+1);"
		}
		else{
			sHTML += "prevMenuId = "+menuId+";hideSubmenu(menu["+menuId+"].levelId);"
		}

		sHTML += ";nNowAt="+menuId+";clearAll("+menu[menuId].levelId+","+menu[menuId].parentId+")' onclick='document.location.href=\""+menu[menuId].url+"\"' onmouseout='s1=setInterval(\"resetMenu();hideSubmenu(0)\",1000);clearTimeout(s2)'><table cellpadding=0 cellspacing=0 border=0><tr>"

		if (menu[menuId].levelId>0){
//			sHTML += "<td width=15 style='border:solid 1px black;'><img src='images/black_square.gif' width='5' height='5' border='0' alt=''></td>"
/*
			sHTML += "<td style='padding-left:3px' width=20><img src='"
			if (menu[menuId].img=="")
			{
				sHTML += imgPath + "trans.gif"
			}
			else
			{
				sHTML += imgPath + menu[menuId].img
			}
			sHTML += "' width=16 height=16 border=0></td>"
			*/
		}
		else{
			if(menuId>0){
				sHTML += "<!--<td nowrap valign=bottom><img src='" + imgNavDivide + "' alt='' width='2' height='28'></td>-->"
			}
		}

		if ((orientation==0) && (menu[menuId].levelId==0)){
			nArrowWidth = 0
		}
		else{
			nArrowWidth = 25
		}
		if (menu[menuId].levelId==0){
			sHTML += "<td align='middle' width='100%' class='linkwhite' onmouseover='this.style.textDecoration=\"underline\";' onmouseout='this.style.textDecoration=\"none\";'><b>" + menu[menuId].description.replace(" ","&nbsp;") + "</b></td><td>"
		}
		else{
			sHTML += "<td align='left' width='100%' class='linkwhite' onmouseover='this.style.color=\"#e29299\"' onmouseout='this.style.color=\"white\"' height=20><!--&nbsp;&nbsp;<img src='" + imgBlackBox + "' width='5' height='5' border='0' alt=''>-->&nbsp;&nbsp;" + menu[menuId].description.replace(" "," ") + "&nbsp;&nbsp;</td><td>"
		}
/*
		if (menu[menuId].numChild>0)
		{
			if ((orientation==0)&&(menu[menuId].levelId==0))
			{
				sHTML += "<img src='" + imgPath + "arrow_down.gif'>"
			}
			else
			{
				sHTML += "<img src='" + imgPath + "arrow_right.gif'>"
			}
		}
		else
		{
			sHTML += "&nbsp;"
		}
		*/
				if (menu[menuId].levelId>0){
					if (ns)
					{
						sHTML += "</td></tr><tr><td width='100%' colspan='2' style=\"background: url(" + imgDropDownDotDivide + ");background-repeat: repeat-x\"><img height='1' src='" + imgBlankImage + "' width='180' alt=''/></td></tr></table></td>"
					}
					else {
						sHTML += "</td></tr><tr><td width='100%' colspan='2' style=\"background: url(" + imgDropDownDotDivide + ");background-repeat: repeat-x\"><img height='1' src='" + imgBlankImage + "' alt=''/></td></tr></table></td>"
					}
					
				}
				else{
					sHTML += "</td></tr></table></td>"
				}
	}
	else
	{
		sHTML = "<td></td></tr><tr><td onmouseover='' bgcolor='#d0d0d0'></td></tr><tr><td></td>"
	}

	if ((orientation==1)||(menu[menuId].levelId>0))
	{
		sHTML += "</tr>"
	}
	return sHTML
}

function clearAll(levelId, parentId) {

	if (levelId>0)
	{
		Border = subItemBorder
		Background = subItemBackground
		switch (levelId){
			case 1:
				Background = subItemBackground;
			break;
			case 2:
				Background = subItemBackground2;
			break;
			case 3:
				Background = subItemBackground3;
			break;
			default:
				Background = subItemBackground;
			break;
		}
	}
	else
	{
		Border = mainItemBorder
		Background = mainItemBackground
	}

	for (i=0;i<menuCounter;i++)
	{
		if (menu[i].levelId==levelId)
		{
			if ((i!=nNowAt) && (menu[i].parentId==parentId) && (menu[i].description!="-")) {
				lw_getObj("menu_"+i).backgroundColor=Background;
//				lw_getObj("menu_"+i).borderColor=Border
				//if (menu[i].levelId>0) lw_getObj("menu_"+i).backgroundImage=imgDropDownDotDivide;
//				if (menu[i].levelId>0) lw_getObj("menu_"+i).borderBottomStyle="dashed";
			}
		}
	}

}

function mapID (id) {
	for (var i=0;i<menuCounter;i++)
	{
		if (menu[i].id==id)
		{
			return i
		}
	}
	return -1
}

function showSubmenu(menuId,trigger) {

	lw_menuId = menuId
	lw_trigger = trigger

	if (ns)
	{
		to1 = setTimeout("showActualSubmenu(lw_menuId,lw_trigger)",50)
	}
	else
	{
		showActualSubmenu(lw_menuId,lw_trigger)
	}

}

function showActualSubmenu(menuId,trigger) {
	var nLevel = 0
	var leftpos = 0
	var nIndex=0

	if (menuId>0)
	{
		lw_calcpos(trigger)

		for (var i=0;i<menuCounter;i++)
		{
			if (menu[i].id==menuId)
			{
				nLevel = menu[i].levelId
				nIndex = i
			}
		}

		if ((orientation==1)||(menu[nIndex].parentId>0))
		{
			lw_getObj("menu_level_"+nLevel).top=lw_toppos-2
			leftpos = lw_leftpos + lw_width 

			if (nLevel==0) {
				leftpos -= 3
			}
		}
		else {
			lw_getObj("menu_level_"+nLevel).top = lw_toppos + lw_height
			leftpos = lw_leftpos + 13
		}

		lw_getObj("menu_level_"+nLevel).left=leftpos

		sHTML = "<table cellpadding=0 cellspacing=0 border=0>"
		for (var i=0;i<menuCounter;i++)
		{
			if (menu[i].parentId==menuId)
			{
				sHTML += displayMenuItem (i)
			}
		}
		sHTML += "</table>"

		document.getElementById("menu_level_"+nLevel+"_content").innerHTML=sHTML
		lw_showLayer('menu_level_'+nLevel)
	}
}

function hideSubmenu(levelId) {
	for (var cnt=levelId; cnt<numLevel; cnt++){
		lw_hideLayerName("menu_level_"+cnt)
	}
}

function DrawMenu ()  {

	for (var i=0; i<numLevel ; i++){
		lw_createLayer("menu_level_"+i,0,0,0,0,"#ffffff",subItemBorder,100)
	}

	sHTML="<table cellpadding=0 cellspacing=0 border=0 width=100%><tr><td width=405> </td><td class='linkgrey'><b><a href='/index.htm'>Home</a></b> &nbsp;&nbsp;&nbsp;</td>"
	for (var i=0; i<menuCounter; i++){
		if (menu[i].parentId==0){
			sHTML += displayMenuItem (i)
		}
		else if (menu[i].parentId==-1){
			sHTML += "<tr><td></td></tr><tr><td class='linkwhite'><b>" + menu[i].description + "</b></td></tr>"
		}
	}
	sHTML += "</tr></table>"
	document.writeln(sHTML)
}

function getLevel (menuId) {
	var pId=menuId
	var nLevel=0
	while (pId!=0){
		nLevel++
		for (var i=0;i<menuCounter;i++){
			if (menu[i].id==pId){
				pId = menu[i].parentId
			}
		}
	}

	return nLevel
}

function AddMenuItem (id, parentId, url, description, img)  {

	menu[menuCounter++] = new Tmenu (id, parentId, url, description, img)

	if (parentId>0)
	{
		for (i=0;i<menuCounter;i++)
		{
			if (menu[i].id==parentId)
			{
				menu[i].numChild++
			}
		}
		menu[menuCounter-1].levelId = getLevel(parentId)

		if ( numLevel < menu[menuCounter-1].levelId)
		{
			numLevel = menu[menuCounter-1].levelId
		}
	}
	else if (parentId==0)
	{
		menu[menuCounter-1].levelId = 0
	}
	else
	{
		menu[menuCounter-1].levelId = -1
	}
}

function handleonclick() {
	if (ns)
	{
		lw_closeAllLayers()
	}
	else
	{
		lw_closeAllLayers(event)
	}

	resetMenu()
}

function handlekeypress(e) {
	if (ns)
	{
		var keyCode = e.keyCode?e.keyCode:e.which?e.which:e.charCode;
		if ((keyCode==27)||(keyCode==1))
		{
			handleonclick()
		}
	}
	else
	if ((event.keyCode==0)||(event.keyCode==27))
	{
		handleonclick()
	}
	resetMenu()
}

function resetMenu () {
	for (i=0;i<menuCounter;i++)
	{
		if (menu[i].levelId==0)
		{
			lw_getObj("menu_"+i).backgroundColor=mainItemBackground
//			lw_getObj("menu_"+i).borderColor=mainItemBorder
		}
	}
}

document.onkeypress = handlekeypress
document.onclick = handleonclick