var MainMenu;
var mTimer, CurrentObj, CurrentElement;

function initMenu()
{
	MainMenu= mainMenu( "indeXML.php?mod=Menu&act=101");
	//MainMenu= mainMenu( "js/menu.xml");
}

function mainMenu( menuUrl)
{
var Obj=
{

menuUrl: null,
oNode: null,
currentNode: null,
div: null,
varname: null,
PopUps: new Array(),
RenderedGroup: null,
latestEl: null,
stopSubmenu: false,

init: function( menuUrl)
{
	this.div= document.all["mainMenu"];
	this.div.varname= "mainMenu";
	this.varname= "mainMenu";

	lib.Collection[ this.varname]= this;

	this.menuUrl= menuUrl;
		
	this.renderHTML();

	this.div.onmouseover= this.onMouseOver;
	this.div.onmouseout= this.onMouseOut;
	this.div.onclick= this.onClick;

	this.div.ondragstart= lib.cancelEvent;
	this.div.onselectstart= lib.cancelEvent;
},

renderHTML: function( Refresh)
{
	this.oNode= lib.loadData( this.menuUrl, null, Refresh);

	this.div.innerHTML= this.getHTML();
},

getHTML: function()
{
	var oDoc= this.oNode;
	if( !oDoc)
	{
		return "";
	}

	if (!this.isMainStyleSheet)
	{
		var oNode= oDoc.selectSingleNode( "//MainMenuStyles");
		if( oNode)
		{
			document.createStyleSheet().cssText= oNode.text;
		}

		this.isMainStyleSheet= true;

		this.submenuStyles = {};
		var oNodes= oDoc.selectNodes( "//SubmenuStyles");
		for (var i= 0; i < oNodes.length; i++)
		{
			var oNode= oNodes[ i];
			var level= oNode.getAttribute("level");
			var strLevel= "defaultStyle";
			if( level)
			{
				strLevel= "level" + level;
			}
			eval( "this.submenuStyles." + strLevel + "= oNode.text;");
		}

		var oNode= oDoc.selectSingleNode( "//MainMenuDefaultDelimiter");
		if( oNode)
		{
			this.MainMenuDefaultDelimiter= oNode.xml;
		}
	}

	var sHTML= "<table cellpadding=0 cellspacing=0><td>&nbsp;</td>";

	var oNode= oDoc.selectSingleNode( "Items");
	if( !oNode)
	{
		return "";
	}

	var latestNode= "";
	for( oNode= oNode.firstChild; oNode; oNode= oNode.nextSibling)
	{
		if( oNode.nodeName== "Item")
		{
			var Hidden= oNode.getAttribute( "Hidden");
			if( Hidden && Hidden== "1")
			{
				continue;
			}
			else
			{
				latestNode= "Item";
			}

			var Text= oNode.getAttribute( "Text");
			var Title= oNode.getAttribute( "Title");
			var Image= oNode.getAttribute( "Image");

			sHTML= sHTML+ "<td noWrap id='menuItem"+ oNode.getAttribute( "sID")+ "'"
			+ " "+ ( oNode.getAttribute( "Hidden") ? "style='display:none'" : "")
			+ ( Title ? " Title='"+ Title+ "'" : "")
			+ "><table class='menuItem' cellpadding=0 cellspacing=0"

			+ ( oNode.firstChild!= null
				? " sSubmenuNode='"+ oNode.getAttribute( "sID")+ "'"
				: ""
			)

			+ ( oNode.getAttribute( "Command")!= null
				? " sCommand=\""+ oNode.getAttribute( "Command")+ "\""
				: ""
			)

			+ " sID='"+ oNode.getAttribute( "sID")+ "'"

			+ ( oNode.firstChild!= null && oNode.getAttribute( "Command")== null
				? " sSubmenuNode='"+ oNode.getAttribute( "sID")+ "'"
				: ""
			)
			+ ">"

			+ ( Image
				? "<td><img align='absmiddle' class='menuItemImage' src='"+ Image+ "' /></td>"
				: ""
			)

			+ ( Text
				? "<td><span class='menuItemText"
				+ ( Image ? " withImage" : "")+ "'>"+ Text+ "</span></td>"
				: ""
			)

			+ "</table></span></td>";

			if (this.MainMenuDefaultDelimiter && oNode.nextSibling)
			{
				sHTML+= "<td>" + this.MainMenuDefaultDelimiter + "</td>";
			}
		}
	}

	sHTML+= "</table>";

	return sHTML;
},

onClick: function( )
{
	var se= event.srcElement;
	var el= lib.findParentElement( se, "menuItem");
	if( !el)
		var el= lib.findParentElement( se, "menuItemSelected");

	if( el)
	{
		var ob= lib.getMe( el, "mainMenu");

		if( el.sCommand!= null)
		{
			ob.currentNode= ob.oNode.selectSingleNode( '//Item[@sID="'+ el.sID+ '"]');
			try
			{
				eval( el.sCommand);
			}
			catch( e)
			{
			}
			return;
		}
	}
},

getCurrentTitle: function( )
{
	var Title= this.currentNode.getAttribute( "Title");

	if( !Title)
	{
		Title= this.currentNode.getAttribute( "Text");
	}

	return Title;
},

onMouseOver: function( )
{
	var el= lib.findParentElement( event.srcElement, "menuItem");

	if( el)
	{
		if( !el.className.match(/Selected$/))
			el.className+= "Selected";

		var ob= lib.getMe( el, "mainMenu");

		try {
			window.clearTimeout(mTimer);
		} catch(e) {}

		CurrentObj = ob;
		CurrentElement = el;

		if( el.sSubmenuNode)
		{
			ob.openSubMenu( el);
			//mTimer = window.setTimeout("CurrentObj.openSubMenu(CurrentElement);", 1);
		}
		else
		{
			ob.closeSubmenu(el);
			//mTimer = window.setTimeout("CurrentObj.closeSubmenu(CurrentElement);", 2);
		}
	}
},

onMouseOut: function( )
{
	//return;
	var el= lib.findParentElement( event.srcElement, "menuItemSelected");
	if (!el)
		el= lib.findParentElement( event.srcElement, "menuItem");

	if( el)
	{
		//el.className= el.className.replace( /Selected$/, "");
		var ob= lib.getMe( el, "mainMenu");
		ob.isOverSubmenuOpener= false;
	}
},

getSubmenuHTML: function( sID)
{
	var oNode= this.oNode.selectSingleNode( "Items");

	oNode= this.oNode.selectSingleNode( '//Item[@sID="'+ sID+ '"]');
	var level= 0;

	for (var oParent= oNode;
		oParent && oParent.nodeName != "Items";
		oParent= oParent.parentNode, level++) ;

	strLevel= "level" + level;

	submenuStyle = eval( "this.submenuStyles." + strLevel + ";");
	if(submenuStyle) ;
	else
		submenuStyle = this.submenuStyles.defaultStyle;


	var sHTML= ""
+ "<html>"
+ "<head>"
+ "<meta http-equiv='content-type' content='text/html; charset=windows-1251'>"
+ "<style>"
+ submenuStyle
+ "</style>"
+ "<script language='JScript'>"
+ "var lib= parent.lib;"
+ "var mainMenu;"
+ "var popupIndex;"
+ "var isFirstLevel;"
+ "var closedByOpener;"
+ "var latestEl;"
+ "var latestPopUp;"
+ "var showPopUpRight;"
+ ""
+ "function onLoadPopUp()"
+ "{"
+ "	if(!document.onunload)"
+ "	{"
+ "		document.ondragstart= lib.cancelEvent;"
+ "		document.onselectstart= lib.cancelEvent;"
+ "		document.oncontextmenu= lib.cancelEvent;"
+ ""
+ "		document.onmouseout= onMouseOut;"
+ "		document.onmouseover= onMouseOver;"
+ "		document.onclick= onClick;"
+ "		document.onunload= onUnload;"
+ "	}"
+ "}"
+ ""
+ "function onUnload()"
+ "{"
+ "	if( !closedByOpener && isFirstLevel && mainMenu && !mainMenu.isOverSubmenuOpener)"
+ "	{"
+ "		mainMenu.closeSubmenu();"
+ "	}"
+ "}"
+ ""
+ "function onMouseOver()"
+ "{"
+ "	if( !lib)"
+ "	{"
+ "		return;"
+ "	}"
+ ""
+ "	var el= lib.findParentElement( event.srcElement, \"menuItem\");"
+ ""
+ "	if (el!= null)"
+ "	{"
+ "		if( !el.className.match(/Selected$/))"
+ "			el.className+= \"Selected\";"
+ "		if( !el.firstChild.firstChild.firstChild.firstChild.firstChild.lastChild.className.match(/Selected$/))"
+ "			el.firstChild.firstChild.firstChild.firstChild.firstChild.lastChild.className+= \"Selected\";"
+ ""
+ "		var sSubmenuNode= el.sSubmenuNode;"
+ ""
+ "		if( sSubmenuNode)"
+ "		{"
+ "			openSubMenu( sSubmenuNode, el);"
+ "		}"
+ "		else"
+ "		{"
+ "			if( latestPopUp)"
+ "			{"
+ "				latestPopUp.hide();"
+ "				latestPopUp= null;"
+ "			}"
+ ""
+ "			if( latestEl && latestEl!= el)"
+ "			{"
+ "				latestEl.className= latestEl.className.replace( /Selected$/, \"\");"
+ "				latestEl.firstChild.firstChild.firstChild.firstChild.firstChild.lastChild.className= latestEl.firstChild.firstChild.firstChild.firstChild.firstChild.lastChild.className.replace( /Selected$/, \"\");"
+ "			}"
+ ""
+ "			latestEl= el;"
+ "		}"
+ "	}"
+ "}"
+ ""
+ "function onMouseOut()"
+ "{"
+ "	return;"
+ ""
+ "	if( !lib)"
+ "	{"
+ "		return;"
+ "	}"
+ ""
+ "	var el= lib.findParentElement( event.srcElement, \"menuItemSelected\");"
+ ""
+ "	if (el!= null)"
+ "	{"
+ "		el.className= el.className.replace( /Selected$/, \"\");"
+ "		el.firstChild.firstChild.firstChild.firstChild.firstChild.lastChild.className= el.firstChild.firstChild.firstChild.firstChild.firstChild.lastChild.className.replace( /Selected$/, \"\");"
+ "	}"
+ "}"
+ ""
+ "function onClick()"
+ "{"
+ "	if( !lib)"
+ "	{"
+ "		return;"
+ "	}"
+ ""
+ "	var el= lib.findParentElement( event.srcElement, \"menuItemSelected\");"
+ ""
+ "	if (el)"
+ "	{"
+ "		if( el.sCommand!= null)"
+ "		{"
+ "			mainMenu.currentNode= mainMenu.oNode.selectSingleNode( '//Item[@sID=\"'+ el.sID+ '\"]');"
+ "			top.setTimeout( el.sCommand, 1);"
+ "			mainMenu.closeAllSubmenu();"
+ "			return;"
+ "		}"
+ ""
+ "		if( !el.className.match(/Selected$/))"
+ "			el.className+= \"Selected\";"
+ "		if( !el.firstChild.firstChild.firstChild.firstChild.firstChild.lastChild.className.match(/Selected$/))"
+ "			el.firstChild.firstChild.firstChild.firstChild.firstChild.lastChild.className+= \"Selected\";"
+ ""
+ "		var sSubmenuNode= el.sSubmenuNode;"
+ ""
+ "		if( sSubmenuNode)"
+ "		{"
+ "			openSubMenu( sSubmenuNode, el);"
+ "		}"
+ "		else"
+ "		{"
+ "			if( latestPopUp)"
+ "			{"
+ "				latestPopUp.hide();"
+ "				latestPopUp= null;"
+ "			}"
+ ""
+ "			if( latestEl && latestEl!= el)"
+ "			{"
+ "				latestEl.className= latestEl.className.replace( /Selected$/, \"\");"
+ "				latestEl.firstChild.firstChild.firstChild.firstChild.firstChild.lastChild.className= latestEl.firstChild.firstChild.firstChild.firstChild.firstChild.lastChild.className.replace( /Selected$/, \"\");"
+ "			}"
+ ""
+ "			latestEl= el;"
+ "		}"
+ "	}"
+ "}"
+ ""
+ "function openSubMenu( sID, el)"
+ "{"
+ "	if( !mainMenu)"
+ "	{"
+ "		return;"
+ "	}"
+ ""
+ "	if( latestEl== el)"
+ "		return;"
+ ""
+ "	if( latestEl)"
+ "	{"
+ "		latestEl.className= latestEl.className.replace( /Selected$/, \"\");"
+ "		latestEl.firstChild.firstChild.firstChild.firstChild.firstChild.lastChild.className= latestEl.firstChild.firstChild.firstChild.firstChild.firstChild.lastChild.className.replace( /Selected$/, \"\");"
+ "	}"
+ ""
+ "	latestEl= el;"
+ ""
+ "	mainMenu.PopUps[ popupIndex+ 1]= { Element: el, PopUp: window.createPopup()};"
+ ""
+ "	var PopUp= mainMenu.PopUps[ popupIndex+ 1].PopUp;"
+ "	latestPopUp = PopUp;"
+ ""
+ "	PopUp.document.open();"
+ "	PopUp.document.write( mainMenu.getSubmenuHTML( sID));"
+ "	PopUp.document.close();"
+ ""
+ "	var leftCorner= el.offsetWidth;"
+ "	for(var oParent= el.parentElement, elP= el; oParent; elP= oParent, oParent= oParent.parentElement)"
+ "	{"
+ "		leftCorner= leftCorner + oParent.offsetWidth - elP.offsetWidth - elP.offsetLeft;"
+ "	}"
+ ""
+ "	leftCorner = window.screenLeft + document.body.offsetWidth;"
+ "	var topCorner = window.screenTop;"
+ "	for(var oParent= el.offsetParent, elP= el; oParent; elP= oParent, oParent= oParent.offsetParent)"
+ "	{"
+ "		topCorner= topCorner + oParent.offsetTop;"
+ "	}"
+ ""
+ "	PopUp.show( leftCorner, topCorner, 1, 1);"
+ "	PopUp.document.parentWindow.mainMenu= this.mainMenu;"
+ "	PopUp.document.parentWindow.popupIndex= popupIndex+ 1;"
+ "	var Width= PopUp.document.all[ \"itemsTable\"].offsetWidth;"
+ "	var Height= PopUp.document.all[ \"itemsTable\"].offsetHeight;"
+ ""
+ "	if (showPopUpRight == null)"
+ "	{"
+ "		PopUp.show( leftCorner, topCorner, Width, Height);"
+ "	}"
+ ""
+ "	if (showPopUpRight || document.parentWindow.screenLeft+ document.body.offsetWidth- 1> PopUp.document.parentWindow.screenLeft)"
+ "	{"
+ "		PopUp.document.parentWindow.showPopUpRight= true;"
+ "		PopUp.show( leftCorner - document.body.offsetWidth - Width, topCorner, Width, Height);"
+ "	}"
+ "}"
+ "</script>"
+ "</head>"

+ "<body onunload='onUnload();' onload='onLoadPopUp();'>"

+ "<div id='divToolbarBarPopUp" + sID + "'>"
+ "<table id='itemsTable' cellpadding='0' cellspacing='0'>";

	var latestNode= "";
	for( oNode= oNode.firstChild; oNode!= null; oNode= oNode.nextSibling)
	{
		switch( oNode.nodeName)
		{
			case "Item":
				if( oNode.getAttribute( "Hidden"))
				{
					continue;
				}

				latestNode= "Item";
				var Text= oNode.getAttribute( "Text");
				var Title= oNode.getAttribute( "Title");
				var Image= oNode.getAttribute( "Image");

				sHTML= sHTML+ "<tr><td nowrap><span width='100%' class='menuItem'"

				+ ( Title ? " Title='"+ Title+ "'" : "")

				+ ( oNode.getAttribute( "Command")!= null
					? " sCommand=\""+ oNode.getAttribute( "Command")+ "\""
					: ""
				)

				+ " sID='"+ oNode.getAttribute( "sID")+ "'"

				+ ( oNode.firstChild!= null
					? " sSubmenuNode='"+ oNode.getAttribute( "sID")+ "'"
					: ""
				)

				+ "><table style='' width='100%' cellpadding=0 cellspacing=0><td><nobr>"

				+ ( Image
					? "<img align='absmiddle' class='menuItemImage' src='"+ Image+ "' />"
					: "<span class='menuItemImage'></span>"
				)

				+ ( Text ? "<span class='menuItemText'>"+ Text+ "&nbsp;</span>" : "")

				//+ "</td><td class='beforeSubmenuOpener'>&nbsp;"
				+ "</td><td width='1'>"

				+ ( oNode.firstChild!= null	? "<IMG class='submenuOpener' src='js/arrowLTR.gif'>" : "&nbsp;")
				+ "</td></table>"
				+ "</span></td></tr>";
				break;
		}
	}

	sHTML+= "</table></div></body></html>";

	return sHTML;
},

closeSubmenu: function(el)
{
	try {
		window.clearTimeout(mTimer);
	} catch(e) {}

	if( el && this.latestEl== el)
		return;

	if( this.latestEl)
	{
		this.latestEl.className= this.latestEl.className.replace( /Selected$/, "");
	}

	if( el)
		this.latestEl= el;
	else
		this.latestEl= null;

	if( this.PopUps.length> 0)
	{
		var PopUp= this.PopUps[ 0].PopUp;
		PopUp.document.parentWindow.closedByOpener= true;
		PopUp.hide();
		this.PopUps= new Array();
	}
},

closeAllSubmenu: function()
{
	try {
		if( this.PopUps.length> 0)
		{
			var PopUp= this.PopUps[ 0].PopUp;
			PopUp.document.parentWindow.closedByOpener= true;
			PopUp.hide();
			this.PopUps= new Array();
		}
	} catch(e) {}
},

openSubMenu: function( el)
{
	if( this.stopSubmenu)
		return;

	this.isOverSubmenuOpener= true;

	if( this.latestEl== el)
	{
		this.isOverSubmenuOpener= false;
		return;
	}

	if( this.latestEl)
	{
		this.latestEl.className= this.latestEl.className.replace( /Selected$/, "");
	}

	this.latestEl= el;
	this.PopUps= new Array();
	this.PopUps[ 0]= { Element: el, PopUp: window.createPopup()};

	var PopUp= this.PopUps[ 0].PopUp;

	PopUp.document.open();
	PopUp.document.write( this.getSubmenuHTML( el.sSubmenuNode));
	PopUp.document.close();

	PopUp.show( 0, el.offsetHeight, 1, 1, el);

	var Width= PopUp.document.all[ "itemsTable"].offsetWidth;
	var Height= PopUp.document.all[ "itemsTable"].offsetHeight;

	//PopUp.hide();
	PopUp.show( 0, el.offsetHeight, Width, Height, el);

	PopUp.document.parentWindow.mainMenu= this;
	PopUp.document.parentWindow.popupIndex= 0;
	PopUp.document.parentWindow.isFirstLevel= true;

	this.isOverSubmenuOpener= false;
},

onStop: function()
{
	MainMenu.stopSubmenu = false;
},

onUnload: function()
{
	MainMenu.stopSubmenu = true;
	MainMenu.closeAllSubmenu();
}
};

Obj.init( menuUrl);
window.attachEvent("onunload", Obj.onUnload);
document.attachEvent("onstop", Obj.onStop);

return Obj;
//	mainMenu object
}
