//globel variables
var MENUS = new Array();

//Class: CMenu
//description:	Menu 
//parameter:	id - unique menu id
//				width - width of menu
//				height - height of menu
//				left - left position
//				top - top position
//				visibility - true or false
//				command - url 
//				description - url title
function CMenu(id, width, height, left, top, visibility, command, description)
{
	//property
	this.id = id;
	this.parent;
	this.width = width;
	this.height = height;
	this.left = left;
	this.top = top;
	this.visibility = CMenu_GetVisibility(visibility);
	this.command = command;
	this.description = description;
	this.menuItems = new Array();
	this.oBrwInfo = new CBrowserInfo();
	this.backgroundcolor = "#639ACE";
	this.activecolor = "#639ACE";
	this.active;
	this.timerID;
	this.offImage = "";
	
	MENUS[id] = this;
	
	//methods, events
	this.onmouseover = CMenu_OnMouseOver;
	this.onmouseout = CMenu_OnMouseOut;
	this.createMenuUI = CMenu_CreateMenuUI;  //create menu user interface
	this.createMenuItemUI = CMenu_CreateMenuItemUI; //create user interface for menu item
	this.getMenuObject = CMenu_GetMenuObject;
	
	this.onTimedClose = CMenu_OnTimedClose;
	this.cancelTimedClose = CMenu_CancelTimedClose;
	this.setBGColor = CMenu_SetBGColor;
	this.onLayerColor = CMenu_OnLayerColor;
	this.offLayerColor = CMenu_OffLayerColor;
	this.setMenuVisible = CMenu_SetMenuVisible;	
	this.isMenuActive = CMenu_IsMenuActive;
	this.deactivateMenu = CMenu_DeactivateMenu;
	
	this.appendMenu = CMenu_AppendMenu;
	this.createMenuItem = CMenu_CreateMenuItem;
}


function CMenu_OnMouseOver( bHover)
{
	//set properties
	this.active = true;
	this.cancelTimedClose();

	//set menu visual effects (rollover effects)
	if(bHover)
	{
		this.onLayerColor("MENU_" + this.parent);
	}
	//activate sub menus if any
	if(this.menuItems.length)
	{
		this.setMenuVisible("MENU_" + this.id, "on");
	}
}

function CMenu_OnMouseOut(bHover)
{
	//set properties
	this.active = false;

	if(bHover)
	{
		this.offLayerColor("MENU_" + this.parent);
	}

	//deactive menu or submenu
	if(this.menuItems.length)
	{
		//activate timed close
		this.onTimedClose('off', .2);
	}
}

function CMenu_CreateMenuUI()
{
	var i;
	var sHTML="", sMenu="";
//	var sMouseOverCmd = 'clearTimeout(UI.timerID);UI.onLayer=true;UI.setMenuVisible(\'MENU_' + this.id + '\', \'on\')';
//	var sMouseOutCmd = 'clearTimeout(UI.timerID);UI.onLayer=false;UI.timedClose(\'MENU_' + this.id + '\', \'off\', .2)';
	
	var sMouseOverCmd = "MENUS['"  + this.id + "'].onmouseover(false)";
	var sMouseOutCmd = "MENUS['"  + this.id + "'].onmouseout(false)";

	//create group
	//if ie
	if(this.oBrwInfo.browser == "ie" || this.oBrwInfo.browser == "ns6")
	{
		//set menu structure
		sMenu += '<div id="MENU_' + this.id + '" style="position:absolute; background-color:' + this.backgroundcolor + '; width:' + this.width;
		sMenu += ';height:' + this.height + ';left:' + this.left + ';top:' + this.top;
		sMenu += ';visibility:' + this.visibility + ';z-index:1000;" ';
		sMenu += 'onMouseOver="' + sMouseOverCmd + '" onMouseOut="' + sMouseOutCmd + '">'; //events
		
		//menu table
		sMenu += '<table border="0" cellspacing="0" cellpadding="0" width="100%">';
		sMenu += '<tr>';
		sMenu += '<td bgcolor="' + this.backgroundcolor + '" colspan="3" height="5">&nbsp;</td>';
		sMenu += '</tr>';
			//loop to create all menu items	
			for (i = 0; i<this.menuItems.length;i++)
			{
				sMenu += '<tr>';
				sMenu += '<td bgcolor="' + this.backgroundcolor + '" nowrap>&nbsp;</td><td bgcolor="' + this.backgroundcolor + '">';

				sMenu += this.menuItems[i].createMenuItemUI(this.id);
				if(this.menuItems[i].menuItems.length)
				{
					//create sub menu
					this.menuItems[i].createMenuUI();
					sMenu += '</td><td bgcolor="' + this.backgroundcolor + '"><img src="images/arrow.gif" border="0"></td>'
				}
				else
				{
					sMenu += '</td><td bgcolor="' + this.backgroundcolor + '">&nbsp;</td>'
				}

				sMenu += '</tr>';
			}	
		sMenu += '<tr>';
		sMenu += '<td bgcolor="' + this.backgroundcolor + '">&nbsp;</td><td bgcolor="' + this.backgroundcolor + '">&nbsp;</td><td bgcolor="' + this.backgroundcolor + '">&nbsp;</td>';
		sMenu += '</tr>';
		sMenu += '</table>';
		sMenu += '</div>';
	}
	else
	{
		//else ns 
		//set menu structure
		sMenu += '<layer name="MENU_' + this.id + '" width="' + this.width + '" ';
		sMenu += 'height="' + this.height + '" left="' + this.left + '" top="' + this.top + '" ';
		sMenu += 'visibility="' + this.visibility + '" ';
		sMenu += 'onMouseOver="' + sMouseOverCmd + '" onMouseOut="' + sMouseOutCmd + '">'; //events
		
		//menu table
		sMenu += '<table border="0" width="100%" cellspacing="0" cellpadding="0">';
		sMenu += '<tr>';
		sMenu += '<td bgcolor="' + this.backgroundcolor + '" colspan="3" height="5">&nbsp;</td>';
		sMenu += '</tr>';
			//loop to create all menu items	
			for (i = 0; i<this.menuItems.length;i++)
			{
				sMenu += '<tr>';
				sMenu += '<td bgcolor="' + this.backgroundcolor + '" nowrap>&nbsp;</td><td bgcolor="' + this.backgroundcolor + '">';

				sMenu += this.menuItems[i].createMenuItemUI(this.id);
				if(this.menuItems[i].menuItems.length)
				{
					//create sub menu
					this.menuItems[i].createMenuUI();
					sMenu += '</td><td bgcolor="' + this.backgroundcolor + '"><img src="images/arrow.gif" border="0"></td>'
				}
				else
				{
					sMenu += '</td><td bgcolor="' + this.backgroundcolor + '">&nbsp;</td>'
				}
				sMenu += '</tr>';
			}	
		sMenu += '<tr>';
		sMenu += '<td bgcolor="' + this.backgroundcolor + '">&nbsp;</td><td bgcolor="' + this.backgroundcolor + '">&nbsp;</td><td bgcolor="' + this.backgroundcolor + '">&nbsp;</td>';
		sMenu += '</tr>';
		sMenu += '</table>';
		sMenu += '</layer>';
	}
	//output menu
	
	document.write(sMenu);

}
function CMenu_CreateMenuItemUI(sMenuName)
{
//	var sMouseOutCmd = 'UI.offLayerColor(\'' + sMenuName + '\', \'' + this.id + '\')';
//	var sMouseOverCmd = 'UI.onLayerColor(\'' + sMenuName + '\', \'' + this.id + '\')';

	var sMouseOverCmd = "MENUS['"  + this.id + "'].onmouseover(true)";
	var sMouseOutCmd = "MENUS['"  + this.id + "'].onmouseout(true)";

	//else menu item
	if (this.oBrwInfo.browser == "ie" || this.oBrwInfo.browser == "ns6")
	{
		sHTML = '<div id="' + this.id + '" onmouseover="' + sMouseOverCmd + '" onmouseout="' + sMouseOutCmd + '" style="width:100%"><a href="' + this.command + '" class="menulink">' + this.description + '</a></div>';
	}
	else if (this.oBrwInfo.browser == "ns")
	{
		sHTML = '<ilayer name="i_' + this.id + '"><layer name="' + this.id + '" onmouseover="' + sMouseOverCmd + '" onmouseout="' + sMouseOutCmd + '" width="100%"><a href="' + this.command + '" class="menulink">' + this.description + '</a></layer></ilayer>';
	}
	else //can't create menu
	{
	}
	return sHTML;
}

function CMenu_GetVisibility(visibility)
{
	var oBrwInfo = new CBrowserInfo();

	if(visibility.toLowerCase() == "hidden")
	{
		if (oBrwInfo.browser == "ie" || oBrwInfo.browser == "ns6")
		{
			return "hidden";
		}
		else
		{
			return "hide";
		}
	}
	else
	{
		if (oBrwInfo.browser == "ie" || oBrwInfo.browser == "ns6")
		{
			return "visible";
		}
		else
		{
			return "show";
		}
	}
}

function CMenu_AppendMenu(mItem)
{
	this.menuItems[mItem.id] = mItem;
}

function CMenu_CreateMenuItem(id, width, height, left, top, visibility, command, description)
{
	var iNextItem = this.menuItems.length;
	var oMenuItem = new CMenu(id, width, height, left, top, visibility, command, description);
	oMenuItem.parent = this.id;
	this.menuItems[iNextItem] = oMenuItem;
	return oMenuItem;
}

function CMenu_OnTimedClose(mState, iSeconds)
{
	var sCmd;

	if(!this.active)
	{
		sCmd = "MENUS['" + this.id + "'].setMenuVisible('MENU_" + this.id + "', '" + mState + "')";
		
		this.timerID = setTimeout(sCmd, 1000 * iSeconds);
	}
}

function CMenu_CancelTimedClose()
{
	clearTimeout(this.timerID);
	if(this.parent)
	{
		MENUS[this.parent].cancelTimedClose();
	}
}


function CMenu_SetBGColor(obj, color)
{
	var oLayer = this.getMenuObject(obj);
	if (this.oBrwInfo.browser == 'ie' || this.oBrwInfo.browser == 'ns6')
	{
		oLayer.backgroundColor = color;	
	}
	else if(this.oBrwInfo.browser == 'ns')
	{
		oLayer.bgColor = color;
	}
}

function CMenu_OnLayerColor(sMenuGroup)
{
	var layerName;

	if(this.oBrwInfo.browser == 'ns')
	{
		layerName = eval("document." + sMenuGroup + ".document.i_" + this.id + ".document." +this.id);
	}
	else
	{
		layerName = this.id;
	}

	this.setBGColor(layerName, this.activecolor);
}

function CMenu_OffLayerColor(sMenuGroup)
{
	var layerName;

	if(this.oBrwInfo.browser == 'ns')
	{
		layerName = eval("document." + sMenuGroup + ".document.i_" + this.id + ".document." + this.id);
	}
	else
	{
		layerName = this.id;
	}

	this.setBGColor(layerName, this.backgroundcolor);

}

function CMenu_SetMenuVisible(mItem, mState)
{
	var obj;

	obj = this.getMenuObject(mItem);
	if(obj)
	{
	
		if(mState == 'on')
		{
			if (this.oBrwInfo.browser =='ie' || this.oBrwInfo.browser == 'ns6')
			{
				obj.visibility = "visible";
			}
			else
			{
				obj.visibility = "show";
			}
		}
		else
		{
			if (this.oBrwInfo.browser =='ie' || this.oBrwInfo.browser == 'ns6')
			{
				obj.visibility = "hidden";
			}
			else
			{
				obj.visibility = "hide";
			}
			if(!this.isMenuActive())
			{
				this.deactivateMenu();
			}
		}	
	}

}


function CMenu_GetMenuObject(obj)
{
	if(typeof obj == "string")
	{
		if(document.getElementById)
		{
			//netscape 6+ and IE 5+
			return document.getElementById(obj).style;

		}
		else if(document.all)
		{
			//ie 4+
			return document.all[obj].style;
		}
		else if(document.layers)
		{
			//netscape 4+
			return document.layers[obj];
		}
	}
	else
	{
		return obj;
	}
}

function CMenu_IsMenuActive()
{
	if(this.active)
	{
		return true;
	}
	else if (this.parent)
	{	
		return MENUS[this.parent].isMenuActive();
	}
	else
	{
		return false;
	}

}

function CMenu_DeactivateMenu()
{
	if(this.parent)
	{
		MENUS[this.parent].setMenuVisible('MENU_' + this.parent, 'off');
	}
	else
	{
		//var sOffImg = "menu_" + this.id + ".gif";

		eval('document.ROOT_' + this.id.toUpperCase() + '.src = "images/' + this.offImage + '"');	
	}
}
