var lib= parent.lib;
var mainMenu;
var popupIndex;
var isFirstLevel;
var closedByOpener;
var latestEl;
var latestPopUp;
var showPopUpRight;

function onLoadPopUp()
{
	document.ondragstart= lib.cancelEvent;
	document.onselectstart= lib.cancelEvent;
	document.oncontextmenu= lib.cancelEvent;

	document.onmouseout= onMouseOut;
	document.onmouseover= onMouseOver;
	document.onclick= onClick;
	document.onunload= onUnload;

//	document.focus();
//	document.parentWindow.focus();
//	window.setTimeout("document.focus();", 500);
}

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.closeSubmenu();
			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;
	}

	//PopUp.hide();
	//PopUp.show( leftCorner, 0, Width+ 2, Height+ 4, el);

	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;
	//var Width= 100;
	//var Height= 100;

	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);
		//showPopUpRight = true;
	}

	this.mainMenu.document.parentWindow.focus();
	//PopUp.document.parentWindow.setTimeout("setActive();", 1);
}
