////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// {{ Browser Detection
	ie=0;n=0;
	IE5=0;NS6=0;
	if (navigator.appVersion.indexOf("MSIE 4") != -1) {IE4=1;ie=1;}
	if (navigator.appVersion.indexOf("MSIE 5") != -1) {IE5=1;ie=1;}
	if (navigator.appVersion.indexOf("MSIE 6") != -1) {IE5=1;ie=1;}

	if (parseInt(navigator.appVersion) >= 5 &&
	    navigator.appVersion.indexOf("MSIE 5") == -1) NS6=1;
	if (ie) {fShow="visible";fHide="hidden";}

	if (!ie && !NS6) alert("Sorry, but this page can not be viewed with your current Internet browser\nPlease install Microsoft Internet Explorer 5 (http://www.microsoft.com) or\nNetscape Communicator 6.0 (http://www.netscape.com)");
// }}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// {{ Global variables
	var loaded = false;				// indicates whether menu is ready for use
	var HTMLstr = "";					// used to format HTML code in memory
	var activeMenus = new Array(5);		// used to keep references to active menus
	var curMenuDepth = 0;				// the depth of current menu panel
	var visMenuDepth = 0;				//

	var frame = "null";				// 

// }}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// {{ Menu Panel Style
function MenuStyle()
{
	this.idName     = null;				// unique ID of the menu panel
	this.idStyle     = null;			// horizontal or vertical
	this.bgColor     = "#000080";			// bgColor
	this.cellSpacing = 0;				// cellSpacing
	this.border = 2;					// border
	this.width = "100%";				// menu panel width

	this.position = "absolute";			//

	this.submenuFrame=null;				// 

	this.onmouseover = null;			//
	this.onmouseout  = null;			//
}
// }}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// {{ Menu Item Configuration
function MenuItem()
{
	this.idOwner  = null;				// menu panel where menu item is locates
	this.idName   = null;				// unique ID of the menu item
	this.text     = null;				// text
	this.hint     = null;				// hint
	this.bgColor  = "#c6c3c6";			// bgColor
	this.fgColor  = "#c00000";			// fgColor
	this.location = null;				// hRef
	this.target   = null;				// target
	this.subMenu  = null;				// sub-menu panel ID

	this.depth    = 1;				// the level of menu panel

	this.clsMenuItemIE = "class=clsMenuItemIE";	// IE4, IE5
	this.clsMenuItemNS = "class=clsMenuItemNS";	// NS6
}
// }}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// {{ SmartMenu v4.0 Object
function Menu()
{
	this.mitem = new MenuItem();			// default style of menu item

	this.newMenu    = newMenu;			//
	this.addItem    = addItem;			//
	this.addItemEx  = addItemEx;			//
	this.setItemStyle = setItemStyle;		//
}

function newMenu(mstyle)
{
if (ie) {
	frame = mstyle.submenuFrame;
	if (mstyle.level==1) {activeMenus[curMenuDepth] = mstyle.idName;curMenuDepth++;visMenuDepth++;}
/////////////////////////////////////////////////////////////////////
// SmartMenu template
	HTMLstr += "<!-- "+mstyle.idName+" MENU STYLE: "+mstyle.idStyle+" -->\n";
	HTMLstr += "<div id='"+mstyle.idName+"' ";
	if (mstyle.onmouseover != null) HTMLstr += "onmouseover=\""+mstyle.onmouseover+"\" ";
	if (mstyle.onmouseout != null)  HTMLstr += "onmouseout=\""+mstyle.onmouseout+"\" ";
	if (mstyle.position != null || mstyle.level>1 || mstyle.width != null)
	{
	  HTMLstr += "style='";
	  if (mstyle.position != null) HTMLstr += "position: "+mstyle.position+";";
	  if (mstyle.width != null) HTMLstr += "width: "+mstyle.width+";";
	  if (mstyle.level>1)        HTMLstr += "visibility: hidden";
	  HTMLstr += "'";
	}
	HTMLstr += ">\n";

	HTMLstr += "<table ";
	if (mstyle.width != null) {
	  HTMLstr += "width='"+mstyle.width+"' ";
	}
	HTMLstr += "bgcolor='"+mstyle.bgColor+"' border='"+mstyle.border+"' cellspacing=0 cellpadding=0>\n";
	HTMLstr += "<tr><td>\n";

	HTMLstr += "<table border=0";
	if (mstyle.idStyle != "horizontal") HTMLstr+=" width='100%'";
//	if (mstyle.bLeftAligned == 0) HTMLstr+=" width='100%'";
	HTMLstr += " bgcolor='"+mstyle.bgColor+"' cellspacing="+mstyle.cellSpacing+" cellpadding=0>\n";
	if (mstyle.idStyle == "horizontal") HTMLstr += "<tr>";
	HTMLstr += "<!-- MAIN MENU STARTS -->\n";
	HTMLstr += "<!-- MENU OF "+mstyle.idName+" -->\n";
	HTMLstr += "<!-- MAIN MENU ENDS -->\n";
	if (mstyle.idStyle == "horizontal") HTMLstr += "</tr>";
	HTMLstr += "</table>\n";

	HTMLstr += "</td></tr>\n";
	HTMLstr += "</table>\n";

	HTMLstr+= "</div>\n";
	HTMLstr += "<!-- MENU PANE DECALARATION ENDS -->\n";
} else if (NS6) {
	var menuHolder;
	if (mstyle.holder == "") {
	  menuHolder=document.createElement("DIV");
	  menuHolder.setAttribute("id", mstyle.idName);
	  menuHolder.setAttribute("idStyle", mstyle.idStyle);
	  menuHolder.setAttribute("depth", mstyle.level);
	  menuHolder.style.position="absolute";

	  document.getElementsByTagName("body").item(0).appendChild(menuHolder);
	} else menuHolder=document.getElementById(mstyle.holder);

	if (mstyle.level > 1) menuHolder.style.display="none";
	else {
	  activeMenus[curMenuDepth]=menuHolder;
	  curMenuDepth++;
	}

	menuTable=document.createElement("TABLE");
	menuTable.setAttribute("cellSpacing", "0");
	menuTable.setAttribute("cellPadding", "2");
	if (mstyle.idStyle != "horizontal")
	  menuTable.setAttribute("width", mstyle.width);
	menuTable.setAttribute("bgColor", mstyle.bgColor);
	menuTable.setAttribute("border", mstyle.border);
	menuTable_body=document.createElement("TBODY");
	menuTable.appendChild(menuTable_body);
	if (mstyle.idStyle == "horizontal") {
	  menuTable_tr=document.createElement("TR");
	  menuTable_td=document.createElement("TD");
	  menuTable_tr.appendChild(menuTable_td);
	  menuTable_body.appendChild(menuTable_tr);

	  menuTable1 = document.createElement("TABLE");
	  menuTable1.setAttribute("bgColor", mstyle.bgColor);
	  menuTable1.setAttribute("width", mstyle.width);
	  menuTable1_body = document.createElement("TBODY");
	  menuTable1_tr = document.createElement("TR");
	  menuTable1_td = document.createElement("TD");
	  menuTable1_tr.appendChild(menuTable1_td);
	  menuTable1_body.appendChild(menuTable1_tr);
	  menuTable1.appendChild(menuTable1_body);
	  menuTable1.setAttribute("border", "0");
	  menuTable1.setAttribute("cellSpacing", "0");
	  menuTable1.setAttribute("cellPadding", "0");

	  menuTable1_td.appendChild(menuTable);
	  menuHolder.appendChild(menuTable1);
	} else
	  menuHolder.appendChild(menuTable);
}
}

function addItem(idOwner, idName, depth, text, hint, location, target, subMenu, alignment)
{
	var menuitem		= this.mitem;	// Load menu item default style

	menuitem.idOwner		= idOwner;
	menuitem.idName		= idName;
	menuitem.depth		= depth;
	menuitem.text		= text;
	menuitem.hint		= hint;
	menuitem.location		= location;
	menuitem.target		= target;
	menuitem.subMenu	= subMenu;
	menuitem.alignment	= alignment;
	addItemEx(menuitem);
}

function addItemEx(mitem)
{
if (ie) {
	var Lookup = "<!-- ITEM "+mitem.idName+" -->";
	if (HTMLstr.indexOf(Lookup) != -1)
	{
		alert(mitem.idName + " already exists");
		return;
	}

	var bHorizontal = false;
	Lookup = "<!-- "+mitem.idOwner+" MENU STYLE: horizontal -->\n";
	if (HTMLstr.indexOf(Lookup) != -1)
	 bHorizontal = true;

	var MENUitem = "";
	MENUitem += "\n<!-- ITEM "+mitem.idName+" -->\n";
	if (!bHorizontal) MENUitem += "<tr>";
	MENUitem += "<td bgColor='"+mitem.bgColor+"' onMouseOver=\"setCellColor(this, '"+mitem.fgColor+"');\" onMouseOut=\"setCellColor(this, '"+mitem.bgColor+"');\">\n";
	MENUitem += "<div id='"+mitem.idName+"' style='font: "+mitem.menuFont+";'>\n";

	MENUitem += "<a "+mitem.clsMenuItemIE+" ";
	if (mitem.target != null) MENUitem += "target="+mitem.target+" ";
	if (mitem.hint != null)
		MENUitem += "title=\""+mitem.hint+"\" ";
	if (mitem.location != null)
		MENUitem += "href='"+mitem.location+"' ";
	else
	{
		MENUitem += "href='.' ";
		MENUitem += "onclick=\"return false;\" "
	}
	MENUitem += "onmouseover=\"";
	if (frame == "null" || frame == null)
		MENUitem += "hideSubMenus("+mitem.depth+"); ";
	else
		MENUitem += "if (parent."+frame+".loaded) parent."+frame+".hideSubMenus(0); ";
	if (mitem.subMenu != null) MENUitem += "displaySubMenu('"+mitem.idName+"', '"+mitem.subMenu+"', "+mitem.alignment+", null, '"+frame+"');";
	MENUitem += "\" ";

	if (mitem.subMenu == null || bHorizontal) MENUitem += ">&nbsp;|&nbsp;"+mitem.text+"</a>\n";
	else MENUitem += ">&nbsp;+&nbsp;"+mitem.text+"</a>\n";
	MENUitem += "</div>\n";
	MENUitem += "</td>\n";
	if (!bHorizontal) MENUitem += "</tr>";

	MENUitem += "<!-- END OF ITEM "+mitem.idName+" -->\n\n";
	MENUitem += "<!-- MENU OF "+mitem.idOwner+" -->\n";

	HTMLstr = HTMLstr.replace("<!-- MENU OF "+mitem.idOwner+" -->\n", MENUitem);
} else if (NS6) {
	menuPanel=document.getElementById(mitem.idOwner)
	bHorizontal = (menuPanel.getAttribute("idStyle") == "vertical") ? 0 : 1;
	if (menuPanel.getAttribute("idStyle") == "vertical")
	  menuItem2 = document.createElement("TR");
	menuItem=document.createElement("TD");
	menuLink = document.createElement("A");
	if (mitem.location != "") menuLink.setAttribute("href", mitem.location);
	else                menuLink.setAttribute("href", "javascript:void()");
	menuLink.setAttribute("id", mitem.idName);
	menuLink.setAttribute("class", "clsMenuItemIE");
	menuLink.setAttribute("submenu", mitem.subMenu);
	menuLink.setAttribute("highlight", mitem.fgColor);
	menuLink.setAttribute("normal", mitem.bgColor);
	menuLink.setAttribute("submenustyle", mitem.alignment);
	menuLink.setAttribute("menudepth", mitem.depth);
	if (NS6) {menuLink.addEventListener("mouseover", onMouseOverNS, false);menuLink.addEventListener("mouseout", onMouseOutNS, false);}
	var menuText;
	if (mitem.subMenu == null || bHorizontal) menuText=document.createTextNode("| "+mitem.text);
	else menuText=document.createTextNode("+ "+mitem.text);
	menuLink.appendChild(menuText);
	menuItem.appendChild(menuLink);
	if (menuPanel.getAttribute("idStyle") == "vertical")
	  menuItem2.appendChild(menuItem);

	if (menuPanel.getAttribute("idStyle") != "vertical")
	  menuPanel.childNodes.item(0).childNodes.item(0).childNodes.item(0).childNodes.item(0).childNodes.item(0).childNodes.item(0).childNodes.item(0).appendChild(menuItem);
//	  menuPanel.childNodes.item(0).childNodes.item(0).childNodes.item(0).appendChild(menuItem);
	if (menuPanel.getAttribute("idStyle") == "vertical")
	  menuPanel.childNodes.item(0).childNodes.item(0).appendChild(menuItem2);
}
}

function setItemStyle(mistyle) {this.mitem = mistyle;}
function showMenu() {if (ie) {document.writeln(HTMLstr);HTMLstr = "";loaded = true;}}
// }}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// {{ functions used to display submenu

// IE
function displaySubMenu(idMainMenu, idSubMenu, showMode, parentLayer, frameMenu)
{
	if (idSubMenu == "null") return;

	var menu;
	var submenu;

	menu = eval(idMainMenu);
	var x, y;

	x = calculateSumOffset(menu, 'offsetLeft');
	if (showMode == 1) x = calculateSumOffset(menu, 'offsetLeft')+130;
	if (showMode == 2) x = calculateSumOffset(menu, 'offsetLeft')-130;

	y = calculateSumOffset(menu, 'offsetTop');
	if (frameMenu == "null") y += menu.offsetHeight+7;
	if (showMode == 1 || showMode == 2) y = calculateSumOffset(menu, 'offsetTop');

	if (frameMenu != "null")
	{
		x += eval("parent."+frameMenu).document.body.scrollLeft;
		y += eval("parent."+frameMenu).document.body.scrollTop;
	}

	displayMenuXY(idSubMenu, x, y, frameMenu);
}

function displayMenuXY(idMenu, x, y, frameMenu)
{
	var submenu;
	submenu = getMenu(idMenu, frameMenu);
	if (submenu == null) return;
	submenu.style.posLeft = x;
	submenu.style.posTop  = y;

	submenu.style.visibility = fShow;

	if (frameMenu == "null" || frameMenu == null) {activeMenus[curMenuDepth] = idMenu;curMenuDepth = curMenuDepth-1+2;}
	else {eval("parent."+frameMenu).activeMenus[eval("parent."+frameMenu).curMenuDepth] = idMenu;eval("parent."+frameMenu).curMenuDepth = eval("parent."+frameMenu).curMenuDepth-1+2;}
}

// NS
function onMouseOverNS (e) {
	currentTD=e.target.parentNode;
	currentTD.style.background = currentTD.getAttribute("highlight");
	currentTD.parentNode.setAttribute("bgColor", currentTD.getAttribute("highlight"));

	submenu_id = currentTD.getAttribute("submenu");
	depth = currentTD.getAttribute("menudepth");
	hideSubMenus(depth+1);
	alignment = currentTD.getAttribute("submenustyle");
	ss = document.getElementById(submenu_id);
	if (ss != null) {
	  offLeft = calculateSumOffset(currentTD, "offsetLeft");
	  offTop  = calculateSumOffset(currentTD, "offsetTop");

	  submenu_depth = ss.getAttribute("depth");

//	  hideSubMenus(submenu_depth);

	if (alignment == 0) {
	  ss.style.left = offLeft;
	  ss.style.top  = offTop+currentTD.offsetHeight+3;
	} else if (alignment == 1) {
	  ss.style.left = offLeft+140;
	  ss.style.top  = offTop;
	} else {
	  ss.style.left = offLeft-140;
	  ss.style.top  = offTop;
	}
	  ss.style.display = "";
	  activeMenus[curMenuDepth]=ss;
	  curMenuDepth++;
	}
}

function onMouseOutNS (e) {
	currentTD=e.target.parentNode;
	currentTD.style.background=currentTD.getAttribute("normal");
	currentTD.parentNode.setAttribute("bgColor", currentTD.getAttribute("normal"));
}
// }}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// {{ functions used to hide submenus
function hideAll(){if (ie) {hideSubMenus(visMenuDepth);}else if (NS6) {hideSubMenus(2);}}

function hideSubMenus(level)
{
if (ie) {
	for (i=curMenuDepth-1; i>=level;i--) document.all[activeMenus[i]].style.visibility = "hidden";
	curMenuDepth=level;
} else if (NS6) {
depth = level;
  if (curMenuDepth == 0) return;
  for (i=curMenuDepth-1; i>=0 && activeMenus[i].getAttribute("depth")>=depth; i--)
	activeMenus[i].style.display="none";
  curMenuDepth=i+1;
}
}
// }}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// {{ Calculate layer position
function calculateSumOffset(idItem, offsetName)
{
	var totalOffset = 0;
	var item = idItem;
	do
	{
		totalOffset += eval('item.'+offsetName);
		item = eval('item.offsetParent');
	} while (item != null);
	return totalOffset;
}
// }}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// {{ Update menu
function updateIt(e)
{
if (ie) {
	var x = window.event.clientX+document.body.scrollLeft;
	var y = window.event.clientY+document.body.scrollTop;

	bHide = true;
	var rightX, leftX, topY, bottomY;
	for (i=0; i<curMenuDepth;i++)
	{
		submenu = document.all[activeMenus[i]];
		leftX = submenu.offsetLeft;
		rightX = leftX+submenu.offsetWidth;
		topY   = submenu.offsetTop;
		bottomY = topY+submenu.offsetHeight;

		if (x < rightX && x > leftX && y+20 > topY && y < bottomY) bHide=false;
	}
	if (bHide) hideAll();
} else if (NS6) {
	x = e.pageX;
	y = e.pageY;

	bHide=1;
	for (i=curMenuDepth-1; i>=0; i--) {
		offLeft = calculateSumOffset(activeMenus[i], "offsetLeft");
		offTop  = calculateSumOffset(activeMenus[i], "offsetTop");
		if (	offLeft-5 < x && x < offLeft+activeMenus[i].offsetWidth+5 &&
			offTop-5 < y  && y < offTop+activeMenus[i].offsetHeight+5
			) bHide=0;
		}
	if (bHide) hideAll();
}
}
// }}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function setCellColor(cell, color) {/*if (!cell.contains(event.fromElement))*/ {cell.bgColor = color;}}
function setColor(idOwner, idName, color){document.layers[idOwner].layers[idOwner+"Pane"].layers[idName].layers[idName+"Pane"].bgColor = color;}

function getMenu(idMenu, frameMenu)
{
  var submenu = null;
  if (frameMenu != "null") submenu = (eval("parent."+frameMenu)).document.all[idMenu];
  else submenu = document.all[idMenu];
  return submenu;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// {{
if (NS6) {document.addEventListener("mousemove", updateIt, false);}
if (ie) {
	document.body.onclick=hideAll;
	document.body.onscroll=hideAll;
	document.body.onmousemove=updateIt;
}
// }}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
