if(window["gMenuScriptLoaded"] != true)
{
	window["gMenuScriptLoaded"] = true;
	var gMenu_DocumetnLoaded = false;
	
	var gMenu_GlobalTempObject = null;
	
	registerWindowOnLoad
	(
	 function()
	 {
		 gMenu_DocumetnLoaded=true;
		}
	);
	
	function Menu(x,y)
	{
		this.itemHeight = '16px';
		
		this.type = 'Menu';
		
		this.container = document.createElement('div');
		this.container.style.width = '150px';
		//this.container.style.height = '100px';
		this.container.style.background = 'Menu';
		this.container.style.border = 'solid 1px black';
		this.container.style.color = 'MenuText';
		this.container.style.position = 'absolute';
		this.container.style.left = x+'px';
		this.container.style.paddingBottom = '2px';
		this.container.style.top = y+'px';
		this.container.style.zIndex = '99999';
		this.container.style.fontFamily = 'verdana';
		this.container.style.fontSize = '10px';

		this.container.object = this;

		if(!gMenu_DocumetnLoaded && document.selection)
		{
			gMenu_GlobalTempObject = this.container;
			registerWindowOnLoad(function(){document.body.appendChild(gMenu_GlobalTempObject);});
		}
		else
			document.body.appendChild(this.container);

this.items = new Array();

		this.destroy = function()
		{
			try
			{
				this.container.parentNode.removeChild(this.container);
			}
			catch(e){}
		}

		this.appendMenuItem = function (menuitem)
		{
			//Trace(menuitem.type);
			this.items[this.items.length] = menuitem;
			this.regenerate();
		}
		
		this.hideMenu = function()
		{
			this.container.style.display = 'none';
		}
		this.showMenu = function()
		{
			this.container.style.display = 'block';
		}
		
		this.regenerate = function()
		{
			this.container.innerHTML = '';
			
			for(var i = 0; i < this.items.length; i++)
			{
				if(this.items[i].type == 'MenuSeparator')
				{
					var mItem = document.createElement('hr');
					/*mItem.style.margin='3px';
					mItem.style.width='95%';
					mItem.style.height='1px';
					mItem.style.backgroundColor='#000000';
					mItem.style.fontSize='0px';
					*/this.container.appendChild(mItem);
					
				}
				else if(this.items[i].type == 'MenuItem' || this.items[i].type == 'SubMenu')
				{
					var mItem = document.createElement('div');
					mItem.object = this.items[i];
					mItem.style.width = '100%';
					mItem.style.height = this.itemHeight;
					mItem.style.verticalAlign = 'middle';
					mItem.style.paddingLeft = '5px';
					//mItem.style.paddingTop = '2px';
					mItem.style.verticalAlign = 'middle';
					//mItem.style.paddingRight = '20px';
					
					mItem.style.cursor = 'default';

					if(document.selection)
						mItem.onselectstart = function() {return false};
					else
						mItem.style.MozUserSelect='none';


					var subMenuExtra = "";
					if(this.items[i].type == 'SubMenu')
					{
						subMenuExtra = '<td width="16"><b>&gt;</b></td>';

						mItem.onmouseover = function()
						{
							
							if(this.parentNode.onchangefocus)
								this.parentNode.onchangefocus();
							this.style.background = 'Highlight';
							this.style.color = 'HighlightText';
							
							this.object.newMenu = new Menu(parseInt(this.offsetLeft)+parseInt(this.parentNode.offsetLeft)+parseInt(this.parentNode.offsetWidth),parseInt(this.offsetTop)+parseInt(this.parentNode.offsetTop));
							
							this.object.newMenu.parentObject = this.parentNode.object;
							for(var i = 0; i < this.object.items.length; i++)
							{
								this.object.newMenu.appendMenuItem(this.object.items[i]);
							}
							
							/*this.object.newMenu.container.onmouseout = function()
							{
								//Trace('submenu onmouseout');								
								this.parentNode.onchangefocus = null;
								this.object.newMenu = null;
								this.parentNode.removeChild(this);
							}*/

							this.parentNode.onchangefocusObj = this;
							this.parentNode.onchangefocus = function()
							{
								if(this.onchangefocusObj.object.newMenu.container.onchangefocus)
									this.onchangefocusObj.object.newMenu.container.onchangefocus();
								this.onchangefocusObj.object.newMenu.container.parentNode.removeChild(this.onchangefocusObj.object.newMenu.container);
								this.onchangefocusObj.object.newMenu = null;
								this.onchangefocus = null;
							}

						}
						
						mItem.onmouseout = function(ev)
						{
							this.style.background = 'Menu';
							this.style.color = 'MenuText';
						}

					}
					else
					{
						mItem.onmouseover = function()
						{
							if(this.parentNode.onchangefocus)
								this.parentNode.onchangefocus();
							this.style.background = 'Highlight';
							this.style.color = 'HighlightText';
						}
						
						mItem.onmouseout = function()
						{
							this.style.background = 'Menu';
							this.style.color = 'MenuText';
						}
								

						mItem.onclick = Function("Menu_WalkDownAndDestroy(this); this.parentNode.style.display='none'; "+this.items[i].action);
					}


					if(this.items[i].icon)
					{
						mItem.innerHTML = '<table border="0" cellpadding="0" cellspaceing="0" width="100%" height="100%"><tr><td width="16" valign="middle" align="left"><img border="0" src="'+this.items[i].icon+'" style="height:'+this.itemHeight+';" /></td><td valign="middle" align="left" style="font-size:'+this.container.style.fontSize+';">' +this.items[i].text + '</td>'+subMenuExtra+'</tr></table>';
					}
					else
						mItem.innerHTML = '<table border="0" cellpadding="0" cellspaceing="0" width="100%" height="100%"><tr><td valign="middle" align="left" style="font-size:'+this.container.style.fontSize+';">' +this.items[i].text + '</td>'+subMenuExtra+'</tr></table>';
					
					this.container.appendChild(mItem);
				}
			}
			//this.container.style.height = (parseInt(this.itemHeight)*this.items.length)+'px';
		}
		
	}

	function Menu_WalkDownAndDestroy(that)
	{
		var object = that.parentNode.object;
		while(object.parentObject && object.parentObject.container && object.parentObject.container.onchangefocus)
		{
			object = object.parentObject;
		}
		if(object.container.onchangefocus)
			object.container.onchangefocus();
		object.container.parentNode.removeChild(object.container);
		object = null;
	}


	function MenuItem(text, action, icon)
	{
		this.type = 'MenuItem';
		this.text = text;
		this.action = action;
		this.icon = icon;
	}
	function MenuSeparator()
	{
		this.type = 'MenuSeparator';
	}
	function SubMenu(text,icon)
	{
		this.type = 'SubMenu';
		this.text = text;
		this.icon = icon;
		this.items = new Array();
		
		this.appendMenuItem = function (menuitem)
		{
			this.items[this.items.length] = menuitem;
		}


	}


}
