//**********************************************************************************************
//****						********************************************************************
//****		MenuItem        ********************************************************************
//****						********************************************************************
//**********************************************************************************************
function __TMainMenu(_container, _xmlMenu, _cb, _prefix)
{
//alert(1);
	var _c = _container;
	this.container = _c;
	_c.innerHTML = "";
	_c.style.position = 'relative';
	_c.style.overflow = 'hidden';
	_c.style.height = '';
	_c.style.width = '100%';
	var _pan=_c.parentElement;
	_c._xml = _xmlMenu.documentElement;
	_c._cb = _cb;
	_c._type = 'MAINMENU';
	_c._prefix = _prefix==null ? "" : _prefix;

	var _pixel	= _c._prefix + top.GetSimplePropValue(_c._xml, "Properties/G/P[@n='ImagePixel']", "/images/pixel.gif");	
	var _bone	= "<IMG src='"+_pixel+"' height=22 width=1 align=absmiddle vspace=1>";

	var _bgColor = "buttonface";//_pan.style.backgroundColor;if(_bgColor==null || _bgColor=="")_bgColor="#ffffff";
	var _fontFamily = _pan.style.fontFamily;if(_fontFamily==null || _fontFamily=="")_fontFamily="Arial";
	var _fontSize = _pan.style.fontSize;if(_fontSize==null || _fontSize=="")_fontSize="9pt";
	var _style1 = "POSITION:relative; BACKGROUND-COLOR:transparent; TOP:0px; LEFT:0px; WIDTH:100%; OVERFLOW:hidden; CURSOR:default;" +
		"BORDER-BOTTOM:buttonshadow solid 1px;" + 
		"BORDER-LEFT:buttonhighlight solid 1px;" + 
		"BORDER-RIGHT:buttonshadow solid 1px;" + 
		"BORDER-TOP: buttonhighlight solid 1px;" + 
		"FONT-FAMILY:" + _fontFamily + "; FONT-SIZE:" + _fontSize + ";";
	var _style2 = "POSITION:relative; BORDER:"+_bgColor+" solid 1px;";
	
	_c.innerHTML = "<TABLE width='100%' style='"+_style1+"' onselectstart='return false' ondragstart='return false' cellpadding=0 cellspacing=0></TABLE>";
	var _tbl = _c.children[0];
	_tbl.style.cursor = "default";

	
	var _row = _tbl.insertRow();
	var _td;
	//...draw items
	var _xItems = _c._xml.selectNodes("MainMenu/Item");
	for (var i1=0; i1 < _xItems.length; i1++)
	{
		var _icon	= _xItems[i1].getAttribute('icon');
		
		_td = _row.insertCell();
		_td.noWrap = true;
		_td.innerHTML = 
		"<SPAN style='" + _style2 + "'" + 
				" _type='Menu_MainItem'" +
				" _index='" + i1 + "'" + 
				" onmouseover='_Menu_MouseOver(this)'" + 
				" onmouseout='_Menu_MouseOut(this)'" + 
				" onmousedown='_Menu_MouseDown(this)'" + 
				" onmouseup='_Menu_MouseUp(this)' " + 
		">" +
				"" + 
				(_icon==null ? "&nbsp;&nbsp;" : "<img src='" + _icon + "' width=23 height=22 align=absmiddle vspace=0 hspace=0>") + 
				_xItems[i1].getAttribute("text") + _bone + "&nbsp;&nbsp;" +
		"</SPAN>" +
		"<SPAN style='font-size:1pt;'>&nbsp;</SPAN>";
		
		_td.children[0]._xItem=_xItems[i1];
		_td.children[0].style.cursor = "default";
	}

	_td = _row.insertCell();
	_td.width = '100%';
	_td.innerHTML = "&nbsp;";

	//...Methods
	this.SetItemStatus = _Menu_SetItemStatus;
	this.HasSuchShortcut = _Menu_HasSuchShortcut;
	this.ProcessShortcut = _Menu_ProcessShortcut;
}


//----------------------------------------------------------- 
//... GetSimplePropValue
//----------------------------------------------------------- 
function GetSimplePropValue(xProps, propFullName, defaultValue)
{	
	return GetXmlAttrValue(xProps, propFullName, defaultValue, 'v');
}
//----------------------------------------------------------- 
//... GetXmlAttrValue
//----------------------------------------------------------- 
function GetXmlAttrValue(xProps, nodeRelPath, defaultValue, attrName)
{
	try
	{	
		var x = xProps.selectNodes(nodeRelPath);	
		var val = x[0].getAttribute(attrName);
		return (val==null) ? defaultValue : val;
	}
	catch(e)
	{
		return defaultValue;
	}
}

//----------------------------------------------------------- 
//... BlockEvent
//----------------------------------------------------------- 
function BlockEvent(){ return false; }

//**********************************************************************************************
//****						********************************************************************
//****		Methods			********************************************************************
//****						********************************************************************
//**********************************************************************************************
function Menu_ShowContextMenu( _x1, _y1, _xml, _x, _elem, _cb, _prefix)
{
	_prefix = _prefix==null ? "" : _prefix;
	
	var _popup = _elem._Popup;

//	var _xProps		= _xml.selectSingleNode("//ContextMenus/Properties");
//	var _pixel		= _prefix + GetSimplePropValue(_xProps, "G/P[@n='ImagePixel']", "/images/pixel.gif");
//	var _imgNext	= _prefix + GetSimplePropValue(_xProps, "G/P[@n='ImageNextLevel']", "/Images/menuNextLevel.gif");
//	var _bgImage	= _prefix + GetSimplePropValue(_xProps, "G[@n='ContextMenu']/P[@n='BackgroundImage']", "/Images/menu_background.gif");
	var _arr = _x.selectNodes("Item");
	var _pixel		= "/images/pixel.gif";
	var _imgNext	= "/Images/menuNextLevel.gif";
	var _bgImage	= "/Images/menu_background.gif";

	
	_popup.document.body.innerHTML = "<DIV id='b' onselectstart='return false' ondragstart='return false' style='padding:1px;cursor:default; border:1px solid #555555; background:whitesmoke; background-image:url("+_bgImage+");'></DIV>";

	var _body = _popup.document.getElementById("b");
	var _scr = _popup.document.createElement("SCRIPT");
	_popup.document.body.appendChild(_scr);
	_scr.id = "_script";
	_scr.language = 'javascript';
	_scr.text = 
		BlockEvent + 
		_Menu_MouseUp + 
		_Menu_MouseOver +
		_Menu_MouseOut +
		_Menu_KeyPressed +
		Menu_ShowContextMenu +
		GetSimplePropValue +
		GetXmlAttrValue +
		"document.body.onkeyup = _Menu_KeyPressed;" +
		"document.body.oncontextmenu = BlockEvent;";
	var _d = _popup.document;
	_d._xml = _xml;
	_d._x = _x;
	_d._cb = 'parent.'+_cb;
	_d._RootPopup = _elem._RootPopup;
//	_d._SelectedBackColor = GetSimplePropValue(_xProps, "G/P[@n='SelectedBackColor']", "gold");
//	_d._BackColor = GetSimplePropValue(_xProps, "G/P[@n='BackColor']", "white");
	_d._SelectedBackColor = "gold";
	_d._BackColor = "white";
	_d._prefix = _prefix;

	var ss = "<TABLE id='tblItems' cellpadding=0 cellspacing=0 style='font-family:Arial; font-size:8pt;'>";

	for (var i1=0; i1 < _arr.length; i1++)
	{
		var _icon	= _arr[i1].getAttribute('icon');	_icon = _icon==null ? _pixel : (_prefix+_icon);
		var _name	= _arr[i1].getAttribute('text');
		var _shortcut= _arr[i1].getAttribute('shortcut'); _shortcut= _shortcut==null ? "" : _shortcut;
		var _guid	= _arr[i1].getAttribute('id');if(_guid==null)_guid=_arr[i1].getAttribute('guid');
		var _hasChilds	= (_arr[i1].selectNodes("Item").length > 0);
		var _disabled	= _arr[i1].getAttribute('disabled')=='y';

		if (_name == "" || _name == "-")
		{
			if (i1==0 || i1==_arr.length-1) continue;
			ss += 
			"<TR><TD colspan=3>" +
				"<DIV style='padding-left:30px;padding-right:2px;padding-top:1px;padding-bottom:1px;'>" +  
					"<SPAN style='background:black;'>" + 
						"<img src='" + _pixel + "' width='100%' height=1>"+
					"</SPAN>" +  
				"</DIV>" +
			"</TD></TR>";
		}
		else
		{
			ss += 
			"<TR _type='Menu_Item' " +
				"_guid='" + _guid + "' " + 
				"_hasChilds='" + (_hasChilds?'y':'n') + "' " + 
				"_disabled='" + (_disabled?'y':'n') + "' " +
				"_shortcut='" + _shortcut + "' " + 
				"_index='" + i1 + "' " +
				(!_disabled 
				? "onclick='_Menu_MouseUp(this)' onmouseover=_Menu_MouseOver(this) onmouseout=_Menu_MouseOut(this) "
				: "style='color:#999999'") + 
			">" +
				"<TD nowrap>" + 
						"<img src='" + _icon + "' width=23 height=22 align=absmiddle vspace=0 hspace=0" + 
							(_disabled ? " style='filter:alpha(opacity=25);'" : "") + 
						">" +
						"<img src='"+_pixel+"' width='7' height=1>" + 
						_name + 
						"<span id=test></span>"+
						"&nbsp;&nbsp;&nbsp;&nbsp;" +
				"</TD>" +
				"<TD nowrap align=right>" + _shortcut + "&nbsp;&nbsp;</TD>" +
				"<TD nowrap>" +
					(_hasChilds ? "<img src='"+_imgNext+"' width=13 height=22>" : "") + 
				"</TD>" + 
			"</TR>";
		}
	}
	ss += "</TABLE>";

	_body.innerHTML = ss;
	
	var _tbl=_body.children[0];
	for (var i1=0; i1 < _arr.length; i1++) _tbl.rows[i1]._xItem=_arr[i1];

	_popup.show(_x1,_y1, 1, 1, _elem);
	_popup.show(_x1,_y1, _popup.document.body.scrollWidth, _popup.document.body.scrollHeight, _elem);

	//_body.focus();

	this.expanded = true;
}
		
function _Menu_SetItemStatus(_guid,_isEnable)
{
	var _x = this.container._xml.selectSingleNode("MainMenu//Item[@id='" + _guid + "']");
	var _enabled = _isEnable==true || _isEnable=='y' || _isEnable=='Y' || _isEnable=='enabled' || _isEnable=='enable' || _isEnable==1 || _isEnable=='1';
	try{ _x.setAttribute('disabled', _enabled ? 'n' : 'y');	}catch(e){}
}
function _Menu_HasSuchShortcut(_shortcut)
{
	var _x = this.container._xml.selectNodes("MainMenu/Item[@shortcut='" + _shortcut + "']");
	return _x!=null && _x.length > 0;
}
function _Menu_ProcessShortcut(_shortcut)
{
	var _x = this.container._xml.selectNodes("MainMenu/Item[@shortcut='" + _shortcut + "']");
	if (_x==null || _x.length == 0) return;
	_x = _x[0];
	try
	{
		if (_x.getAttribute('disabled') == 'y')
			alert("Menu item '" + _x.getAttribute('text') + "' with shortcut pressed (" + _shortcut + ") is currently disabled");
		else
			eval(this.container._cb + "('" + _x.getAttribute('id') + "')");
	}
	catch(e)
	{
		alert("Error occured when processing shortcut '" + _shortcut + "': " + e.message);
	}
}
		
//---------------------------------------------------------------------------------
//---------------------------------------------------------------------------------
//---					-----------------------------------------------------------
//---		Events		-----------------------------------------------------------
//---					-----------------------------------------------------------
//---------------------------------------------------------------------------------
//---------------------------------------------------------------------------------
function _Menu_MouseOver(_o)
{
	try{
		while (_o._type==null || _o._type.indexOf("Menu_")!=0) _o=_o.parentElement;
		if (_o._type == 'Menu_MainItem')
		{
			_o._borderColor = _o.style.borderBottom;
			_o.style.borderBottom = _o.style.borderRight = "buttonshadow solid 1px";
			_o.style.borderLeft = _o.style.borderTop = "buttonhighlight solid 1px";
	
			var _c = _o; while (_c._type==null || _c._type!='MAINMENU') _c = _c.parentElement;

			if (_c._Popup.isOpen) _Menu_MouseUp(_o);
		}
		else if (_o._type == 'Menu_Item')
		{
			_o.style.background= (_o._disabled=='n') ? document._SelectedBackColor : document._BackColor;

			document._nowSelected = _o;
			var _prev = document._prevSelected;
			if (_prev != null && _prev != _o)
			{
				_Menu_MouseOut(_prev);
				try{if (_prev._Popup.isOpen) _prev._Popup.hide();}catch(e){}
			}
			document._prevSelected = _o;
		}
	}catch(e){}
}
function _Menu_MouseOut(_o)
{
	try{
		while (_o._type==null || _o._type.indexOf("Menu_")!=0) _o = _o.parentElement;
		if (_o._type == 'Menu_MainItem')
		{
			_o.style.border = _o._borderColor + " solid 1px";
		}
		else if (_o._type == 'Menu_Item')
		{
			_o.style.background='';
		}
	}catch(e){}
}
function _Menu_MouseDown(_o)
{
	try{
		while (_o._type==null || _o._type.indexOf("Menu_")!=0) _o = _o.parentElement;
		if (_o._type == 'Menu_MainItem')
		{
			_o.style.borderBottom = _o.style.borderRight = "buttonhighlight solid 1px";
			_o.style.borderLeft = _o.style.borderTop = "buttonshadow solid 1px";
		}
	}catch(e){}
}
function _Menu_MouseUp(_o)
{
	try{
		while (_o._type==null || _o._type.indexOf("Menu_")!=0) _o = _o.parentElement;
		
		if (_o._type == 'Menu_MainItem')
		{
			_o.style.borderBottom = _o.style.borderRight = "buttonshadow solid 1px";
			_o.style.borderLeft = _o.style.borderTop = "buttonhighlight solid 1px";

			var _c = _o; while (_c._type==null || _c._type!='MAINMENU') _c = _c.parentElement;

			try{if (_c._Popup.isOpen && _c._indexE==_o._index) return;}catch(e){}
			_c._indexE = _o._index;

			try{ _o._Popup.hide();}catch(e){}
			_o._Popup = window.createPopup();
			_o._RootPopup = _o._Popup;
			_c._Popup = _o._Popup;

			var _x = _c._xml.selectNodes("MainMenu/Item")[_o._index];
			if (_x.selectNodes("Item").length > 0)
				Menu_ShowContextMenu( 0,20, _c._xml, _x, _o, _c._cb);
			else
			{
				var _cb = _c._cb + "(_o._guid,_o._xItem)";
				eval(_cb);
			}
		}
		else if (_o._type == 'Menu_Item')
		{
			var _d = document;
//alert(_o._hasChilds + "       " + (_o._hasChilds == 'y'));
			if (_o._hasChilds == 'y')
			{
//alert(1);
//alert(_o.outerHTML);
				try{ _o._Popup.hide();}catch(e){}
				_o._Popup = window.createPopup();
				_o._RootPopup = _d._RootPopup;
//alert(2);
				var _x = _d._x.selectNodes("Item")[_o._index];
//alert((_x==null) + "     " + _o.outerHTML);
//alert(_d._cb + "\n\n" + _x.xml + "     " + _o.outerHTML + "     " + _d._xml.xml);
				Menu_ShowContextMenu(_o.offsetWidth-3,3, _d._xml, _x, _o, _d._cb, _d._prefix);
			}
			else
			{
				var _cb = _d._cb + "(_o._guid,_o._xItem)";
				try{ _d._RootPopup.hide();}catch(e){}
				eval(_cb);
			}
		}
	}catch(e){}
}		
function _Menu_KeyPressed()
{
	var _code = event.keyCode;
	var _o = document._nowSelected;

	event.cancelBubble = true;
	event.returnValue = false;
		
	//... 38-UP, 40-DOWN, 33-PgUp, 34-PgDn, 35-End, 36-Home
	if (_code == 38 || _code == 40)
	{
		var _tbl = document.all("tblItems");
		var _ind;
		var _cnt = 0;
		for (var i1=0; i1 < _tbl.rows.length; i1++)
		{
			_cnt+=(_tbl.rows(i1)._disabled=='n')?1:0;
		}
		if (_cnt <= 1) return;
		
		_Menu_MouseOut(_o);
		do
		{  
			if (_o!=null)
				_o = _code==38 ? _o.previousSibling : _o.nextSibling;
				
			if (_o==null)
			{
				_ind = (_code == 38) ? _tbl.rows.length-1 : 0;
				_o = _tbl.rows(_ind);
			}
		}while(_o._type==null || _o._type.indexOf("Menu_")!=0 || _o._disabled=='y')
		//}while(_o.className==null || _o.className.indexOf("Menu_")!=0 || _o._disabled=='y')
		
		_Menu_MouseOver(_o);
	}
	//...39-RIGHT
	else if (_code == 39 && _o!=null && _o._hasChilds=='y')
	{
		_Menu_MouseUp(_o);
	}
	//...13-Enter
	else if (_code==13)
	{
		_Menu_MouseUp(_o);
	}
}


