// Variables
var overColor     = '#F6F6F6'
var outColor      = '#E8E8E8';
var path = "/icopal_sk/icopal_sk.nsf/"
// border space
var borderSpace   = 2;
// Max text length before warp
var maxTextLength = 21;
// the height for the cells
var cellHeight1   = 21;
var cellHeight2   = 31;
var menuHeight    = 90;
var cellWidth     = 143;
var topCellWidth  = 119;
// Menu start
var menuStartLeft = 158;
var menuStartTop  = 35;
var menuNumber    = 0;
var menuHidden    = new Array();
var menuLevel     = new Array();
var counter		= 0;
// menu indicator for showing submenus
var menuIndicator = "<img src=\"" + path + "arrow.gif\" width=\"8\" height=\"8\">";
// Start with vertical menu
var isVertical = false;
var menuTimer = 0;
// Finding the browsere info
var isDOM = (document.getElementById ? true : false);
var isIE4 = ((document.all && !isDOM) ? true : false);
var isNS4 = (document.layers ? true : false);
function getRef(id) {
	if (isDOM) return document.getElementById(id);
	if (isIE4) return document.all[id];
	if (isNS4) return document.layers[id];
}
// getting the style id object
function getStyle(id) {
	return (isNS4 ? getRef(id) : getRef(id).style);
}
function wordCount(str)
{
	var strArray = new Array;
	strArray = str.split(" ")
	return strArray.length
}
function popOver(menuID, color, parentID, subMenu, parentIDs) {
	clearTimeout(menuTimer);
	var menuIDStr = "menu" + menuID;
	var menuStyle = getStyle(menuIDStr);
	if (isNS4)
		menuStyle.bgColor = color;
	else
		menuStyle.backgroundColor = color;
	parentIDs = parentIDs + "*" + menuID + "*" 
	menu0.showMenu(parentIDs);
}
function popOut(menuID, color, parentID, subMenu, parentIDs) {
	var menuIDStr = "menu" + menuID;
	var menuStyle = getStyle(menuIDStr);
	if (isNS4)
		menuStyle.bgColor = color;
	else
		menuStyle.backgroundColor = color;
	menuTimer = setTimeout('hideMenus()', 500);
}
function showMenu(parentIDs) {
	for(i=1; i < menuLevel.length; i++) {
		var subMenuID = "menu" + i;
		var subMenuStyle = getStyle(subMenuID);
		var key1 = "*" + menuLevel[i] + "*";
		if(parentIDs.indexOf(key1) != -1) {
			subMenuStyle.visibility = "visible";
		} else {
			subMenuStyle.visibility = "hidden";
		}
	}
}
function hideMenu(menuID) {
	for(i=0; i< menuLevel.length; i++) {
		if(menuLevel[i] == menuID) {
			var subMenuID = "menu" + i;
			var subMenuStyle = getStyle(subMenuID);
			subMenuStyle.visibility = "hidden";
		}
	}
}
function hideMenus() {
	for(i=0; i < menuHidden.length; i++) {
		var subMenuStyle = getStyle(menuHidden[i]);
		subMenuStyle.visibility = "hidden";
	}
}
var popOldWidth = window.innerWidth;
nsResizeHandler = new Function('if (popOldWidth != window.innerWidth) location.reload()');
if (isNS4) document.captureEvents(Event.CLICK);
	document.onclick = clickHandle;
function clickHandle(evt)
{
 	if (isNS4) document.routeEvent(evt);
	hideMenus();
}
function Menu(text, href, target, width, height, spacing, outColor, overColor, borderClass, textClass) {
	this.text = text;
	this.href = href;
	this.target = target;
	this.width = width;
	this.height = height;
	this.spacing = spacing;
	this.outColor = outColor;
	this.overColor = overColor;
	this.borderClass = borderClass;
	this.textClass = textClass;
	this.ref = null;
	this.id = menuNumber++;
	this.parent = false;
	this.parentID = 0;
	this.isVertical = isVertical;
	this.subMenu = new Array;
	this.printMenu = printMenu;
	this.showMenu  = showMenu;
	this.hideMenu  = hideMenu;
}
function addMenu(menu, newSubMenu) {
	menu.subMenu[menu.subMenu.length]=newSubMenu;
}
function printMenu(level,cellNumber,vertical,parentMenuLeft,parentMenuTop,parentID, parentIDs)
{
	// Stop if the browser is not supported
	if (!isDOM && !isIE4 && !isNS4) return;
	var i;
	var menuID = "menu" + this.id;
	var str = "";
	
	
	this.parentID = parentID;
	if(parentID != 0)
	{
		menuHidden[counter++] = menuID;
	}
	menuLevel[this.id] = parentID;
	if(this.subMenu.length!=0)
		this.parent = true;
	if(level != 0)
	{
		if (isDOM || isIE4) {

			str += "<div id=\"" + menuID + "\" style=\"position: absolute; left: " + parentMenuLeft + "; top: " + parentMenuTop + "; width: " + (this.width+borderSpace) + "; height: " + (this.height) + "; visibility: " + (level==1? "visible" : "hidden" )  + "; z-index: " + (level + 1) + "; ";
			if (this.outColor) str += 'background: ' + this.outColor + '; ';
			str += "\" ";
		}
		if (isNS4) {
			str += "<layer id=\"" + menuID + "\" left=\"" + parentMenuLeft  + "\" top=\"" + parentMenuTop + "\" width=\"" +  (this.width) + "\" height=\"" + (this.height) + "\" visibility=\"" + (level==1? "visible" : "hidden" )  + "\" ";
			if (this.outColor) str += "bgcolor=\"" + this.outColor + "\" ";
		}
		if (this.borderClass != '')
		str += "class=\"" + this.borderClass + "\" ";
		// Add mouseover handlers
		str += "onMouseOver=\"popOver(" + this.id + ", '" + this.overColor + "', " + this.parentID + ", " + (this.parent? "1" : "0") + ",'" + parentIDs + "')\" onMouseOut=\"popOut(" + this.id + ",'" + this.outColor + "', " + this.parentID + ", " + (this.parent? "1" : "0") + ",'" + parentIDs + "')\">";
		if(level == 1)
		{
			if(cellNumber == 0)
				str += "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td width=\"1\"><img src=\"" + path + "blank.gif\" width=\"1\" height=\"13\"></td><td width=\"" + (this.width-1) + "\" height=\"" + (this.height-borderSpace) + "\" align=\"center\"><a class=\"" + this.textClass + "\" href=\"" + this.href + "\"" + (this.target ? " target=\"" + this.target + "\">" : ">") + this.text + "</a></td>";
			else
				str += "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td width=\"1\" valign=\"middle\"><img src=\"" + path + "menu_pipe.gif\" width=\"1\" height=\"13\"></td><td width=\"" + (this.width-1) + "\" height=\"" + (this.height-borderSpace) + "\" align=\"center\"><a class=\"" + this.textClass + "\" href=\"" + this.href + "\"" + (this.target ? " target=\"" + this.target + "\">" : ">") + this.text + "</a></td>";
		}
		else
		{
			if (isDOM || isIE4)
				str += "<table width=\"" + this.width + "\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><td align=\"left\" valign=\"top\"><tr><td width=\"6\"><img src=\"" + path + "blank.gif\" width=\"6\" height=\"" + (this.height-borderSpace) + "\"></td><td><a class=\"" + this.textClass + "\" href=\"" + this.href + "\"" + (this.target ? " target=\"" + this.target + "\">" : ">") + this.text + "</a></td>";
			if (isNS4) 
				str += "<table width=\"" + (this.width-4-borderSpace) + "\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" align=\"left\" valign=\"top\" class=\"tableTransparent\"><tr><td align=\"left\" valign=\"middle\"><img src=\"" + path + "blank.gif\" width=\"6\" height=\"" + (this.height-7-borderSpace) + "\"><a class=\"" + this.textClass + "\" href=\"" + this.href + "\"" + (this.target ? " target=\"" + this.target + "\">" : ">") + this.text + "</a></td>";
		}
		// Add a arrow indicator for the menus that have a submenu
		if(this.subMenu.length != 0 && level != 1){
			str += "<td align=\"right\" valign=\"bottom\" width=\"8\">" + menuIndicator + "</td>";
		}
		str += "</tr></table>" + (isNS4 ? "</layer>" : "</div>");
	}
	//Draw the menu
	document.writeln(str)
	// If there is any submenus
	if(this.subMenu.length != 0)
	{
		level++;
		parentID = this.id;
		if(this.id != 0)
			parentIDs =  parentIDs + parentID + "*";
		if(level == 1)
			vertical = false;
   		else
			vertical = true;
		if(level==2)
		{
			parentMenuTop += this.height;
		}
		if(level>2)
		{
			parentMenuLeft += this.width+borderSpace;
		}
		//Draw the submenus
		var subMenuID = "subMenu" + this.id;
		var menuLeft =  parentMenuLeft;
		var menuTop = parentMenuTop;
		for(i=0; i < this.subMenu.length; i++)
		{
			this.subMenu[i].printMenu(level,i,vertical,menuLeft,menuTop,parentID,parentIDs);
			// Calculate the next items start
			if (vertical)
				menuTop += this.subMenu[i].height;
			else
				menuLeft += (this.subMenu[i].width + this.spacing + borderSpace);
		}
	}
}
function initializeMenus(menu)
{
	
	menu.printMenu(0,0,isVertical,menuStartLeft,menuStartTop,0,"*0*");
	hideMenus();
}
function addMenuItem(menuText, menuLink, menuTarget, level)
{
	var levelNumber = (level*1) - 1;
	var menuHeight = cellHeight1;
	//if text length is longer than the max length then make cell height bigger 
	if(menuText.length > maxTextLength && wordCount(menuText) >2) {
		menuHeight = cellHeight2;
		var indexBlank = menuText.lastIndexOf(" ");
		menuText = menuText.substring(0,indexBlank) + "<br>" + menuText.substring(indexBlank);
	}
	if(levelNumber >= 1)
	{
		if(levelNumber == 1)
		{
			menu1 = new Menu(menuText,menuLink,menuTarget,  topCellWidth, cellHeight1, 1, "#FFFFFF", "#FFFFFF", "", "tm_text");
			addMenu(menu0, menu1);
		}
		else
		{
			tempStr = "menu" + levelNumber + " = new Menu(\"" + menuText + "\", \"" + menuLink + "\", \"" + menuTarget + "\", cellWidth, " + menuHeight + ", 0, outColor, overColor, \"tm_border\", \"tm_text\");"
			
			eval(tempStr)
			tempStr = "addMenu(menu" + (levelNumber-1) + ", menu" + levelNumber + ");";
			eval(tempStr)
		}
	}	
}
// Building the menu

function initializeMenu()
{
	initializeMenus(menu0)
}
