if (document.all)    {n=0;ie=1;fShow="visible";fHide="hidden";}
else                 {n=1;ie=0;fShow="show";   fHide="hide";}

var HTMLstr = "";

var loaded = false;

var menuDepth = 5;
var activeMenus = new Array(menuDepth+1);

var frame = "null";

var curMenuDepth = 0;
var visMenuDepth = 0;

function MenuStyle()
{
	this.idName      = null;
	this.idStyle     = "horizontal";
	this.bVisible    = false;
	this.position    = "absolute";

	this.bgColor     = "#336666";
	this.fgColor     = "#336699";

	this.submenuFrame="null";

	this.cellSpacing = 0;

	this.mainMenuWidth = "100%";

	this.mainMenuBorder = 1;
	this.subMenuBorder  = 1;
	this.menuDelta = 3;

	this.subMenuWidth = 150;

	this.onmouseover = null;
	this.onmouseout  = null;

	this.bMenuStatic = 0;
	this.bLeftAligned = 1;
	this.extraStyle = null;
}

function MenuItem()
{
	this.idOwner  = null;
	this.idName   = null;
	this.depth    = 0;
	this.text     = null;
	this.hint     = null;
	this.location = null;
	this.target   = null;
	this.subMenu  = null;
	this.alignment= 0;

	this.bgColor     = "#c6c3c6";
	this.fgColor     = "#808080";

	this.menuFont;
	this.menuFontIE  = "bold xx-small Verdana";
	this.menuFontNS  = "bold x-small Verdana";

	this.clsMenuItemIE = "class=clsMenuItemIE";
	this.clsMenuItemNS = "class=clsMenuItemNS";
}

/////////////////////////////////////////////////////////////////////
// Menu Constructor
function Menu()
{
	this.mitem = new MenuItem();

	this.newMenu    = newMenu;
	this.addItem    = addItem;
	this.addItemEx  = addItemEx;
	this.addSeparator= addSeparator;
	this.addCustomItem= addCustomItem;
	this.setItemStyle = setItemStyle;
}

function newMenu(mstyle)
{
	frame = mstyle.submenuFrame;
	if (mstyle.bVisible) {activeMenus[curMenuDepth] = mstyle.idName;curMenuDepth++;visMenuDepth++;}
/////////////////////////////////////////////////////////////////////
// SmartMenu template
	HTMLstr += "<!-- "+mstyle.idName+" MENU STYLE: "+mstyle.idStyle+" -->\n";
	if (ie)
	{
	  HTMLstr += "<div id='"+mstyle.idName+"' ";
	  if (mstyle.onmouseover != null) HTMLstr += "onmouseover=\""+mstyle.onmouseover+"\" ";
	  if (mstyle.onmouseout != null)  HTMLstr += "onmouseout=\""+mstyle.onmouseout+"\" ";
	  if (mstyle.menuBorder != null) HTMLstr += "class="+mstyle.menuBorder+" ";
	  if (mstyle.position != null || !mstyle.bVisible || mstyle.mainMenuWidth != null || mstyle.extraStyle)
	  {
	    HTMLstr += "style='";
	    if (mstyle.position != null) HTMLstr += "position: "+mstyle.position+";";
	    if (mstyle.extraStyle != null) HTMLstr += mstyle.extraStyle+";";
	    if (mstyle.mainMenuWidth != null) HTMLstr += "width: "+mstyle.mainMenuWidth+";";
	    if (!mstyle.bVisible)        HTMLstr += "visibility: "+fHide+";";
	    HTMLstr += "'";
	  }
	  HTMLstr += ">\n";
	}
	if (n)
	{
	  if (mstyle.position == "absolute") HTMLstr += "<layer name='"+mstyle.idName+"' ";
	  else  HTMLstr += "<ilayer name='"+mstyle.idName+"' ";
	  if (mstyle.onmouseover != null) HTMLstr += "onMouseover=\""+mstyle.onmouseover+"\" ";
	  if (mstyle.onmouseout != null)  HTMLstr += "onMouseout=\""+mstyle.onmouseout+"\" ";
	  if (mstyle.extraStyle != null) HTMLstr += mstyle.extraStyle+" ";
	  if (!mstyle.bVisible) HTMLstr += "visibility='"+fHide+"'";
	  HTMLstr += ">";
	  HTMLstr += "<layer name='"+mstyle.idName+"Pane'>\n";
	}

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

	HTMLstr += "<table border=0";
	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";

	if (ie) HTMLstr+= "</div>\n";
	if (n)
	{
	  if (mstyle.bMenuStatic > 0) HTMLstr+= "</layer></layer>\n";
	  else HTMLstr+= "</layer></ilayer>\n";
	}
	HTMLstr += "<!-- MENU PANE DECALARATION ENDS -->\n";
}

function addItem(idOwner, idName, depth, text, hint, location, target, subMenu, alignment, bgColor, fgColor)
{
	var menuitem = this.mitem;
	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;
	menuitem.bgColor = "#007070";  // ckk - change bgColor here
	menuitem.fgColor = "#000000";  // ckk - change fgColor here
	if (bgColor) {menuitem.bgColor=bgColor;}
	if (fgColor) {menuitem.fgColor=fgColor;}
	addItemEx(menuitem);
}

function addCustomItem(idOwner, idName, itemSrc)
{
	var bHorizontal = false;
	Lookup = "<!-- "+idOwner+" MENU STYLE: horizontal -->\n";
	if (HTMLstr.indexOf(Lookup) != -1)
	 bHorizontal = true;

	var MENUitem = "";
	MENUitem += "\n<!-- ITEM "+idName+" -->\n";
	if (bHorizontal) MENUitem += "<tr>";
	MENUitem += "<td>";
	MENUitem += itemSrc;
	MENUitem += "</td>";
	if (bHorizontal) MENUitem += "</tr>";
	MENUitem += "<!-- MENU OF "+idOwner+" -->\n";
	HTMLstr = HTMLstr.replace("<!-- MENU OF "+idOwner+" -->\n", MENUitem);
}

function addSeparator(idOwner)
{
	var MENUitem = "";
	MENUitem += "<tr height=2><td><hr color='#cccccc' width=95%></td></tr>";
	MENUitem += "<!-- MENU OF "+idOwner+" -->\n";
	HTMLstr = HTMLstr.replace("<!-- MENU OF "+idOwner+" -->\n", MENUitem);
}

function addItemEx(mitem)
{
	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 (n)
	{
		if (!bHorizontal) MENUitem += "<tr>";
		MENUitem += "<td bgColor='"+mitem.bgColor+"'>\n";
		MENUitem += "<ilayer name="+mitem.idName+"><layer name="+mitem.idName+"Pane onMouseOver=\"setColor('"+mitem.idOwner+"', '"+mitem.idName+"', '"+mitem.fgColor+"')\"  onMouseOut=\"setColor('"+mitem.idOwner+"', '"+mitem.idName+"', '"+mitem.bgColor+"')\">";

		MENUitem += "<a "+mitem.clsMenuItemNS+" ";
		if (mitem.target != null) MENUitem += "target="+mitem.target+" ";
		if (mitem.location != null)
			MENUitem += "href='"+mitem.location+"' ";
		else
		{
			MENUitem += "href='.' ";
			MENUitem += "onclick=\"return false;\" ";
		}
		MENUitem += "onmouseover=\"";
		if (frame == "null")
			MENUitem += "hideSubMenus("+mitem.depth+"); ";
		else
			MENUitem += "if (parent."+frame+".loaded) parent."+frame+".hideSubMenus(0); ";
		if (mitem.subMenu) MENUitem += "displaySubMenu('"+mitem.idName+"', '"+mitem.subMenu+"', "+mitem.alignment+",document.layers['"+mitem.idOwner+"'].layers['"+mitem.idOwner+"Pane'], '"+frame+"');";
		MENUitem += "\">";

		if (mitem.subMenu == null || mitem.depth == visMenuDepth) MENUitem += "&nbsp;|&nbsp;";
		else MENUitem += "&nbsp;>&nbsp;";
		MENUitem += mitem.text;
		MENUitem += "</a>";

		MENUitem += "</layer></ilayer>";
		MENUitem += "</td>\n";
		if (!bHorizontal) MENUitem += "</tr>";
	}
	if (ie)
	{
		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")
			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 || mitem.depth == visMenuDepth) 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);
}

function setItemStyle(mistyle)
{
	this.mitem = mistyle;
}

function showMenu()
{
	document.writeln(HTMLstr);
	HTMLstr = "";

	loaded = true;
//	if (mstyle.bMenuStatic > 0) UpdateIt();
}

function hideAll(){hideSubMenus(visMenuDepth);}

function hideSubMenus(level)
{
	for (i=curMenuDepth-1; i>=level;i--){if (n) document.layers[activeMenus[i]].visibility = fHide; else (getMenu(activeMenus[i], frame)).style.visibility = fHide;}
	curMenuDepth=level;
}

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

	  submenu.style.visibility = fShow;
	} else if (n) {
	  submenu = getMenu(idMenu, frameMenu);
	  if (submenu == null) return;
	  submenu.left = x;
	  submenu.top  = y;

	  submenu.visibility = fShow;
	}
	if (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;}

}

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

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

	var menu;
	var submenu;
	if (n)
	{
		var x, y;
		if (showMode == 1) {
			x = parentLayer.document.layers[idMainMenu].pageX+parentLayer.document.layers[idMainMenu].clip.width;
			y  = parentLayer.document.layers[idMainMenu].pageY+3;
		} else if (showMode == 2) {
			x = parentLayer.document.layers[idMainMenu].pageX-parentLayer.document.layers[idMainMenu].clip.width;
			y  = parentLayer.document.layers[idMainMenu].pageY+3;
		} else {
			x = parentLayer.document.layers[idMainMenu].pageX;
			y = parentLayer.document.layers[idMainMenu].pageY;
			if (frameMenu == "null") y += parentLayer.document.layers[idMainMenu].clip.height+5;
		}
		if (frameMenu != "null")
		{
			x += eval("parent."+frameMenu).pageXOffset;
			y += eval("parent."+frameMenu).pageYOffset;
		}
		displayMenuXY(idSubMenu, x, y, frameMenu);
	} else if (ie) {
		menu = eval(idMainMenu);
		var x, y;

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

		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 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 calculateSumOffset(idItem, offsetName)
{
	var totalOffset = 0;
	var item = eval('idItem');
	do
	{
		totalOffset += eval('item.'+offsetName);
		item = eval('item.offsetParent');
	} while (item != null);
	return totalOffset;
}


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();
	}
	if (n)
	{
		var x = e.pageX;
		var y = e.pageY;

		bHide = true;
		var rightX, leftX, topY, bottomY;
		for (i=0; i<curMenuDepth;i++)
		{
		  leftX   = document.layers[activeMenus[i]].pageX;
		  rightX  = leftX + document.layers[activeMenus[i]].clip.width;
		  topY    = document.layers[activeMenus[i]].pageY;
		  bottomY = topY + document.layers[activeMenus[i]].clip.height;
		  if (x < rightX && x > leftX && y+20 > topY && y < bottomY) bHide=false;
		}
		if (bHide) hideAll();
	}
}

if (ie)
{
	document.body.onclick=hideAll;
	document.body.onscroll=hideAll;
	document.body.onmousemove=updateIt;
}
if (n)
{
	document.onmousedown=hideAll;
	window.captureEvents(Event.MOUSEMOVE);
	window.onmousemove=updateIt;
}