function MenuClick(menu, item)
{
	this.clicker = function clk()
	{
		menu.SetActiveItem(item);
		menu.OnMenuClick(menu.name, item);
	};
}

function menuBox()
{
	this.menus = new Array();
	
	function my_Menu(name, divObject, OnMenuClick)
	{
		this.divObject = divObject;
		this.tableRowObject = null;
		this.activeItem = null;
		this.name = name;
		this.OnMenuClick = OnMenuClick;
		this.document = document;
		if (this.divObject.document)
			this.document = this.divObject.document;
		
		this.AddItem = function my_AddItem(item)
		{
			firstItem = this.tableRowObject.childNodes.length == 0;
			if (!firstItem)
			{
				// add a separator
				newTD = this.document.createElement("TD");
				newTD.innerHTML = "&nbsp;-&nbsp;";
				this.tableRowObject.appendChild(newTD);
			}
			newTD = this.document.createElement("TD");
			newTD.className = firstItem ? "menuitem_active" : "menuitem_default";
			newTD.name = item;
			if (firstItem)
				this.activeItem = newTD;
				
			myclick = new MenuClick(this, item);
				
			newTD.onclick = myclick.clicker;
			newTD.innerHTML = item;
			
			this.tableRowObject.appendChild(newTD);
		}
		
		this.SetActiveItem = function my_SetActiveItem(item)
		{
			for(i=0; i<this.tableRowObject.childNodes.length; i++)
			{
				var tdObj = this.tableRowObject.childNodes[i];
				if (tdObj.name == item)
				{
					if (tdObj != this.activeItem)
					{
						this.activeItem.className = "menuitem_default";
						tdObj.className = "menuitem_active";
						this.activeItem = tdObj;
					}
				}
			}
		}

		this.GetActiveItem = function my_GetActiveItem()
		{
			return this.activeItem.name;	
		}

		this.SetAlertItem = function my_SetAlertItem(item)
		{
			for(i=0; i<this.tableRowObject.childNodes.length; i++)
			{
				var tdObj = this.tableRowObject.childNodes[i];
				if (tdObj.name == item)
				{
					if (tdObj != this.activeItem)
					{
						tdObj.className = "menuitem_alert";
					}
				}
			}
		}

		this.ClrAlertItem = function my_ClrAlertItem(item)
		{
			for(i=0; i<this.tableRowObject.childNodes.length; i++)
			{
				var tdObj = this.tableRowObject.childNodes[i];
				if (tdObj.name == item)
				{
					if (tdObj != this.activeItem)
					{
						tdObj.className = "menuitem_default";
					}
				}
			}
		}
		
		// Create table
		newTable = this.document.createElement("TABLE");
		this.divObject.appendChild(newTable);
		newTBody = this.document.createElement("TBODY");
		newTable.appendChild(newTBody);
		this.tableRowObject = this.document.createElement("TR");
		newTBody.appendChild(this.tableRowObject);
	}
	
	this.NewMenu = function my_NewMenu(name, divObject, OnMenuClick)
	{
		if (this.menus[name])
			return false;
			
		this.menus[name] = new my_Menu(name, divObject, OnMenuClick);
		
		return true;
	}
	
	this.DestroyMenu = function my_DestroyMenu(name)
	{
		this.menus[name] = null;
	}
	
	this.AddItem = function my_AddItem(menu, item)
	{
		if (this.menus[menu])
			this.menus[menu].AddItem(item);
	}
	
	this.SetActiveItem = function my_SetActiveItem(menu, item)
	{
		if (this.menus[menu])
			this.menus[menu].SetActiveItem(item);
	}

	this.GetActiveItem = function my_GetActiveItem(menu)
	{
		if (this.menus[menu])
			return this.menus[menu].GetActiveItem();
	}

	this.SetAlertItem = function my_SetAlertItem(menu, item)
	{
		if (this.menus[menu])
			this.menus[menu].SetAlertItem(item);
	}
	this.ClrAlertItem = function my_ClrAlertItem(menu, item)
	{
		if (this.menus[menu])
			this.menus[menu].ClrAlertItem(item);
	}
	this.ClickMenuItem = function(menu, item)
	{
		if (this.menus[menu])
		{
			this.menus[menu].SetActiveItem(item);
			this.menus[menu].OnMenuClick(menu, item);
		}
	}
}

RegisterMenu(new menuBox());

