 if(typeof(MVJS)=="undefined")
{
	MVJS={};
}
   MVJS.ContextMenu=function(ContextMenuContainer,mainMenuClass,StartZindex){
	return new MVJS._ContextMenu(ContextMenuContainer,mainMenuClass,StartZindex);
};
MVJS._ContextMenu= function(ContextMenuContainer,mainMenuClass,StartZindex){
	
	this.MainDivContainer = ContextMenuContainer;
	this.mainMenuClass="mainMenuClass";
	this.MenuListWithIcon=false;
	this.MenuLinkClass="menuLinkClass";
	this.MenuLinkImageClass="menuLinkImageClass";
	this.DividerClass="dividerAbove";
	this.childMenuClass="childMenuClass";
	this.IconHeight=0;
	this.IconWidth=0;
	
	this.StartZindex="1000";
	this.ulMainContainer=document.createElement("UL");
	this.MenuItemsArray=new Array();
	
	this.previousMenu=null;
	
	this.ContianerDiv = document.createElement("div");		
	this.ContianerDiv.style.display='none';
	this.ContianerDiv.style.zIndex=this.StartZindex;
	if(ContextMenuContainer)
	{
		document.getElementById(this.MainDivContainer).appendChild(this.ContianerDiv);
	}
	this.ContianerDiv.appendChild(this.ulMainContainer);
	
	if(mainMenuClass!='' && mainMenuClass!=null)
	this.mainMenuClass= mainMenuClass;
	
	if(StartZindex!='' && StartZindex!=null)
	this.StartZindex= StartZindex;
	
	this.ulMainContainer.className=this.mainMenuClass;
	this.ulMainContainer.style.zIndex=this.StartZindex;
	this.MouseEntered = false;
	this.ChildMouseEntered = false;
	this.FirstEnter = false;
	this.MouseOut = true;
	this.Timeflag = null;
	this.ShowUp= false;
	var _this=this;
	
	
	this.ulMainContainer.onmouseover = function()
	{
		_this.MouseEntered = true;
		_this.FirstEnter = true;
	};
};
MVJS._ContextMenu.prototype.addMenuItem = function(MenuID,ParentMenuID,MenuText,MenuLink,IconPath,LinkTarget) 
{
	/*** Fill Array ***/	

	this.MenuItemsArray[this.MenuItemsArray.length]= new Array();
	this.MenuItemsArray[this.MenuItemsArray.length-1][0]=MenuID;
	this.MenuItemsArray[this.MenuItemsArray.length-1][1]=ParentMenuID;
	this.MenuItemsArray[this.MenuItemsArray.length-1][2]=MenuText;
	this.MenuItemsArray[this.MenuItemsArray.length-1][3]=MenuLink;
	this.MenuItemsArray[this.MenuItemsArray.length-1][4]=IconPath;


	var IconText="";
	if(typeof(IconPath)=="undefined" || IconPath=="" || IconPath==null)
	IconText="";
	else
	IconText="<img src='"+ IconPath +"' border='0' alt=''> &nbsp;";			

	var NewMenu=document.createElement("LI");

	NewMenu.lang=MenuID;
	NewMenu.id="MVJS"+this.MainDivContainer+"_"+MenuID;

	if(MenuText== "-")
	{
		NewMenu.className=this.DividerClass;
		NewMenu.style.height="1px";
		this.ulMainContainer.appendChild(NewMenu);
	}
	else
	{
		MenuText=IconText+MenuText;

		if(MVJS.IsIE()) NewMenu.style.zIndex= (-1)* parseInt(this.StartZindex);

		this.ulMainContainer.appendChild(NewMenu);				
		var NewMenuLink=document.createElement("a");
		NewMenuLink.href=MenuLink;
		NewMenuLink.className=this.MenuLinkClass;
		NewMenuLink.innerHTML=MenuText;
		if(LinkTarget)	NewMenuLink.target = LinkTarget;
		NewMenu.appendChild(NewMenuLink);
	}
	var _this=this;


	NewMenu.onclick = function ()
	{
		_this.MouseEntered = false;				
		_this.HideAll();
	};

	if(ParentMenuID!=0)
	{
		var ParentReference;
		for(var i=0; i<this.MenuItemsArray.length;i++)
		{
			if(this.MenuItemsArray[i][0]==ParentMenuID)
			{
				ParentReference = this.MenuItemsArray[i][5];
				break;
			}
		}

		if(ParentReference.childNodes.length==1)
		{
			var childMainUL=document.createElement("UL");
			childMainUL.className=this.childMenuClass;
			childMainUL.appendChild(NewMenu);	


			if(MVJS.IsIE())					
			NewMenu.style.zIndex = (-1)* (parseInt(ParentReference.parentNode.style.zIndex)+1);

			childMainUL.style.zIndex=parseInt(ParentReference.parentNode.style.zIndex)+1;
			ParentReference.appendChild(childMainUL);
			var _ParentReference=ParentReference;	
			ParentReference.onmouseover=function()
			{	
				if(MVJS.IsIE() && this.style.zIndex <0)
				{
					this.style.zIndex=(-1)*this.style.zIndex;							
				}

				childMainUL.style.display="block";
			};
			ParentReference.onmouseout=function()
			{
				if(MVJS.IsIE() && this.style.zIndex >0)
				{
					this.style.zIndex=(-1)*this.style.zIndex;							
				}
				childMainUL.style.display="none";


			};
		}
		else
		{
			if(MVJS.IsIE())					
			NewMenu.style.zIndex = (-1)* parseInt(ParentReference.childNodes[1].style.zIndex);

			ParentReference.childNodes[1].appendChild(NewMenu);
		}

		if(ParentReference.childNodes[0].className==this.MenuLinkClass)
		ParentReference.childNodes[0].className=this.MenuLinkImageClass;
	}
	this.MenuItemsArray[this.MenuItemsArray.length-1][5]=NewMenu;
};
MVJS._ContextMenu.prototype.ChangeAllNodeLink = function (Replacer,ParentNode)
{
	
	if(ParentNode==null)
	ParentNode =this.ulMainContainer;
	
	for(var i=0;i<ParentNode.childNodes.length;i++)
	{
		if(ParentNode.childNodes[i].nodeName=="LI")
		{
			var AncherChild=null,UlChild=null;
				
			
			for(var j=0;j< ParentNode.childNodes[i].childNodes.length;j++)
			{
				
				if(ParentNode.childNodes[i].childNodes[j].nodeName=="A")
				{
					
					AncherChild = ParentNode.childNodes[i].childNodes[j];
					var index = ParentNode.childNodes[i].lang;
					index = this.getMenuIndex(index);
					var Link = this.MenuItemsArray[index][3];					
					if(Link!="#")
					{					
						AncherChild.href = MVJS.FormatString(Link,Replacer);
					}
				}
				else if(ParentNode.childNodes[i].childNodes[j].nodeName=="UL")
				{
					this.ChangeAllNodeLink(Replacer,ParentNode.childNodes[i].childNodes[j]);
				}
			}
		}
	}
					
};
MVJS._ContextMenu.prototype.getMenuIndex = function(MenuID) {
	for(var i=0;i<this.MenuItemsArray.length;i++)
	{
		if(this.MenuItemsArray[i][0]==MenuID)
		return i;
	}
	return 0;
};
MVJS._ContextMenu.prototype.getChildItems = function(MenuID){
	var ChildItems = new Array();
	for(var i=0;i<this.MenuItemsArray.length;i++)
	{
		if(this.MenuItemsArray[i][1]==MenuID)
		{
			ChildItems[ChildItems.length]=this.MenuItemsArray[i][0];
		}
	}
	return ChildItems;
};
MVJS._ContextMenu.prototype.HideIfMouseOut = function(event)
{
	try
	{
		var Coords;
		if(MVJS.IsIE() && window.event)
		{
			Coords=MVJS.EventHandler().GetMouseCoords(window.event);
		}
		else
		{
			Coords=MVJS.EventHandler().GetMouseCoords(event);
		}
		var ControlRegion=MVJS.CoordinatesHandler.GetControlRegion(this.ulMainContainer);
		if(MVJS.IsIE() && MVJS.Nuggets.TopMenu_Main)
		{
			Coords.Y-=MVJS.Nuggets.TopMenu_Main.TabPageTop;
			ControlRegion.Y1-=MVJS.Nuggets.TopMenu_Main.TabPageTop;
			ControlRegion.Y2-=MVJS.Nuggets.TopMenu_Main.TabPageTop;
		}		
		if(Coords.X < ControlRegion.X1 || Coords.X > ControlRegion.X2 || Coords.Y < ControlRegion.Y1 || Coords.Y > ControlRegion.Y2)			
		{
			this.MouseOut = true;
			if(this.Timeflag) clearTimeout(this.Timeflag);
			var _this = this;
			this.Timeflag = setTimeout(function(){_this.HideAll();},150);			
		}
		else
		{
			this.MouseOut = false;
		}
	}
	catch(e)
	{
	}
};
MVJS._ContextMenu.prototype.onDocumentMouseMove = function(event)
{		
	try
	{
		if(!this.MouseEntered) return;
		if(this.ContianerDiv.style.display!='none')
		{			
			this.HideIfMouseOut(event);
		}
	}
	catch(e)
	{
		MVJS.ReportError(e);
	}
};
MVJS._ContextMenu.prototype.DisplayMenu = function (ReferenceID,id,MouseCords,ScrollValue){
	try
	{	
		if(id!=null)
		this.ChangeAllNodeLink(id);	
		var ControlHeight=0;
		var ShowUp = false;
		this.FirstEnter = false;
		if(this.ContianerDiv)
		{
			this.ContianerDiv.style.display="none";
		}
		else
		{
			return;
		}
		if(ReferenceID==null && MouseCords== null) return;		
		if(MouseCords && (!MouseCords.X || ! MouseCords.Y)) return;
		
		var objCoordinatesHandler= MVJS.CoordinatesHandler;
		var objStyleHandler = MVJS.StyleHandler;
		var ReferenceCords;
		if(ReferenceID!=null)
		{		
			if(typeof(ReferenceID)=="string")
			{			
				ReferenceID=document.getElementById(ReferenceID);
			}
			ReferenceCords=objCoordinatesHandler.GetCoordsByControlID(ReferenceID,"LeftBottom");
			var _this=this;
			ReferenceID.onmouseout = function (event)
			{	
				 _this.HideIfMouseOut(event);
			};
		}
		else 
		{
			ReferenceCords = MouseCords;
		}
		this.ContianerDiv.style.display="block";
		ControlHeight = parseFloat(this.ContianerDiv.childNodes[0].offsetHeight);	
		this.ContianerDiv.style.display="none";
		var ScreenAdj =0;
		if(MVJS.Nuggets.TopMenu_Main)
		ScreenAdj= (MVJS.IsIE())?(parseFloat(MVJS.Nuggets.TopMenu_Main.TabPageTop) - parseFloat(MVJS.Nuggets.TopMenu_Main.ScrollTop)):parseFloat(MVJS.Nuggets.TopMenu_Main.TabPageTop);	
		if(this.ShowUp || (MVJS.Nuggets.TopMenu_Main && (ControlHeight+ parseFloat(ReferenceCords.Y)) > (parseFloat(screen.height)-ScreenAdj)))
		{	ShowUp = true;	
			if(ReferenceID!=null)
			ReferenceCords=objCoordinatesHandler.GetCoordsByControlID(ReferenceID,"LeftTop");
		}
		if(MVJS.IsIE() && MVJS.Nuggets.TopMenu_Main)
		{	
			ReferenceCords.Y-=MVJS.Nuggets.TopMenu_Main.TabPageTop;
		}
		if(ScrollValue)
		{
			ReferenceCords.Y-=parseInt(ScrollValue);
		}
		this.ContianerDiv.style.display="block";
		var Adj=0;
		if(ShowUp)
		{
			Adj = ControlHeight;	
		}
		objCoordinatesHandler.MoveControl(this.ContianerDiv,"LeftTop",ReferenceCords.X,ReferenceCords.Y-Adj,null,false);					
		var _this = this;
		MVJS.EventHandler().AttachEvent(document.body,"mousemove",function (e){_this.onDocumentMouseMove(e);});
		if(MVJS.IsIE() && MVJS.Nuggets.TopMenu_Main)
		{
			this.ContianerDiv.style.zIndex = this.ulMainContainer.style.zIndex;		
			var ObjSelectBuster = document.getElementById('PageSelectBuster');			
			/*this.ContianerDiv.parentNode.appendChild(ObjSelectBuster);		*/
			MVJS.StyleHandler.SetOpacity('PageSelectBuster',0);		
			ObjSelectBuster.style.width = MVJS.StyleHandler.GetWidth(this.ulMainContainer);
			ObjSelectBuster.style.height = MVJS.StyleHandler.GetHeight(this.ulMainContainer);
			ObjSelectBuster.style.top = ReferenceCords.Y-Adj;
			ObjSelectBuster.style.left = ReferenceCords.X;	
			ObjSelectBuster.style.display = "block";		
		}
	}
	catch(e){}
	
				
};				
MVJS._ContextMenu.prototype.RemoveMenuItem = function (MenuID,Permanently) {
	var MenuIndex = this.getMenuIndex (MenuID);
	var ChildItems = this.getChildItems(MenuID);
	for(var i=0;i<ChildItems.length;i++)
	{
		if(Permanently)
		this.DeleteMenuItem(ChildItems[i]);
		else
		this.HideMenuItem (ChildItems[i]);
				this.MenuItemsArray.splice(this.getMenuIndex (ChildItems[i]),1);
	}
	
	if(Permanently)
	{
		this.DeleteMenuItem(MenuID);
		this.MenuItemsArray.splice(MenuIndex,1);
	}
	else
	{
		this.HideMenuItem(MenuID);	
	}
};
MVJS._ContextMenu.prototype.DeleteMenuItem = function (MenuID) {
	var MenuIndex = this.getMenuIndex (MenuID);
	var objLI = document.getElementById("MVJS"+ this.MainDivContainer +"_"+MenuID);			
	if(this.MenuItemsArray[MenuIndex][1]!==0 && objLI.parentNode.childNodes.length==1)
	{
		objLI.parentNode.parentNode.childNodes[0].className= this.MenuLinkClass;
		objLI.parentNode.parentNode.removeChild(objLI.parentNode);
	}
	else
	objLI.parentNode.removeChild(objLI);
};
MVJS._ContextMenu.prototype.HideMenuItem = function (MenuID) {
	var MenuIndex = this.getMenuIndex (MenuID);
	var objLI = document.getElementById("MVJS"+ this.MainDivContainer +"_"+MenuID);
	
	objLI.style.display='none';
	
	if(this.MenuItemsArray[MenuIndex][1]!==0)
	{
		var count=0;
		for(var i=0;i<objLI.parentNode.childNodes.length;i++)
		{
			if(objLI.parentNode.childNodes[i].nodeType=="LI" && objLI.parentNode.childNodes[i].style.display!="none")
			{
				count++;
			}
		}
		if(count==0)
		{
			objLI.parentNode.parentNode.childNodes[0].className= this.MenuLinkClass;
			objLI.parentNode.parentNode.onmouseover = function (){};			
			objLI.parentNode.style.display='none';
			
		}		
	}
	
	
};
MVJS._ContextMenu.prototype.ShowMenuItem = function (MenuID) {
	var MenuIndex = this.getMenuIndex (MenuID);
	var objLI = document.getElementById("MVJS"+ this.MainDivContainer +"_"+MenuID);		
	objLI.style.display='block';
	if(objLI.parentNode.parentNode.childNodes[0].className== this.MenuLinkClass)
	{
		objLI.parentNode.parentNode.childNodes[0].className = this.MenuLinkImageClass;
		objLI.parentNode.parentNode.onmouseover = function (){			
			objLI.parentNode.style.display= "block";
		};	
	}
	
};
MVJS._ContextMenu.prototype.RenameMenuItem = function (MenuID,NewText) {
	var MenuIndex = this.getMenuIndex (MenuID);
	var objLI = document.getElementById("MVJS"+ this.MainDivContainer +"_"+MenuID);		
	if(objLI && objLI.childNodes.length>0)
	{
		objLI.childNodes[0].innerHTML=NewText;
	}
};
MVJS._ContextMenu.prototype.DisableMenuItem = function (MenuID) {
	var MenuIndex = this.getMenuIndex (MenuID);
		var ChildItems = this.getChildItems(MenuID);
	
	this.MenuItemsArray[MenuIndex][3]="#";
	var objLI = document.getElementById("MVJS"+ this.MainDivContainer +"_"+MenuID);
	objLI.childNodes[0].style.color ="#999999";
	objLI.childNodes[0].href='javascript:var x=false;';
	objLI.childNodes[0].onmousedown = function () { return false;};
		for(var i=0;i<ChildItems.length;i++)
		{
			this.DisableMenuItem(ChildItems[i]);			
			this.MenuItemsArray[this.getMenuIndex (ChildItems[i])][3]="#";
		}
						
	
};		
MVJS._ContextMenu.prototype.HideAll = function ()
{	
	if(!this.MouseEntered || this.MouseOut)
	{
		this.ContianerDiv.style.display='none';
		this.MouseEntered = false;
		var _this = this;
		MVJS.EventHandler().RemoveEvent(document,"mousemove",function () { _this.onDocumentMouseMove();});
		if(MVJS.IsIE())
		{			
			if(MVJS.GetById('PageSelectBuster'))
			document.getElementById('PageSelectBuster').style.display= 'none';	
		}
	}
};

