//************************************************************************************
// CXSims.CxMenuBar and CXSims.CxMenuItem Objects - displays a dropdown menu
// By: CXS Consulting, Inc.
// Date: December 2007
// Version: 1.0
//*************************************************************************************
//
var CSS_MenuBar = "MenuBar";
var CSS_MenuBarBreak = "MenuBarBreak";
var CSS_MenuBarItem = "MenuBarItem";
var CSS_MenuBarOver = "MenuBarMouseOver";
var CSS_Menu = "Menu";
var CSS_MenuItem = "MenuItem";
var CSS_MenuBreak = "MenuBreak";
var CSS_MenuItemOver = "MenuItemMouseOver";
var BodyID = "PageBody"

function CxMenuBar(sParentFolder)
{
	this.objName="CxMenuBar"+CxMenuBar.count++;
	eval(this.objName+"=this");
	if ((sParentFolder != null) && (sParentFolder != "")) 
		this.ParentFolder = sParentFolder;
	else
		this.ParentFolder = "";
	this.TopMenus = new Array();
	this.PageBody = document.body 
	if (this.PageBody == null)
		this.PageBody = document.getElementById(BodyID);	
	this.Error = null;
	
	this.SetHideMenuEvent=function(sElementID)
	{
		if ((sElementID==null) || (sElementID==""))
		{
		 	this.Error = "Element.id is unassigned";
			return false;
		}
			
		var pElement = document.getElementById(sElementID);
		if (pElement == null)
		{
		 	this.Error = "Unable to locate Element["+sElementID+"]";
			return false;
		}
		
		pElement.onmouseover = new Function(this.objName+".HideMenus(); return true");
		return true;
	}
	
	// Call to hide all submenus and to show all top menus normal.
	this.HideMenus=function()
	{
		if ((this.TopMenus == null) || (this.TopMenus.length == 0))
		  return;
		
		for(var i=0;i<this.TopMenus.length;i++)
		{
		   var pMenu = this.TopMenus[i];
		   if ((pMenu != null) && (!pMenu.IsBreak));
		   {
		   	pMenu.HideSubMenu();
		   	pMenu.ShowNormal();
		   }
		}
	}	
	
	// Add a new TopMenu Item with a SubMenu
	// sCaption= menu Caption
	this.AddTopMenu=function(sCaption,sTitle)
	{
		if ((sCaption==null) || (sCaption==""))
		{
		  this.Error = "A menu caption cannot be unassigned";
		  return null;
		}
		 
		var iLength = this.TopMenus.push(new CxMenuItem(this,null,sCaption,sTitle,false,null,null,null));
		var pMenu = this.TopMenus[iLength-1];
		return pMenu;
	}
	
	// Add a new TopMenu Item with 
	// sCaption= menu Caption
	// sHref = the hiperlink reference (must be defined)
	// sFolder = subfolder location reflative to the root. 
	this.AddTopMenuCmd=function(sCaption, sTitle, sHref, sFolder, sTarget)
	{
		if ((sCaption==null) || (sCaption==""))
		{
		 alert("A menu caption cannot be unassigned");
		 return null;
		}

		if ((sHref==null) && (sHref==""))
		{
		  alert("A menu reference cannot be unassigned");
		  return null;
		}
		 
		var iLength = this.TopMenus.push(new CxMenuItem(this,null,sCaption,sTitle,false,sHref,sFolder,sTarget));
		var pMenu = this.TopMenus[iLength-1];
		return pMenu;
	}
	
	// Add a new TopMenu Item with a Script to Run on Click
	this.AddTopActionMenu=function(sCaption,sTitle,sScript)
	{
		if ((sCaption==null) || (sCaption==""))
		{
		  this.Error = "A menu caption cannot be unassigned";
		  return null;
		}
		
		if ((sScript == null) && (sScript == ""))
		{
		  this.Error = "A Action Menu's Action Script cannot be unassigned";
		  return null;
		}
		 
		var iLength = this.TopMenus.push(new CxMenuItem(this,null,sCaption,sTitle,false,sScript,null,null));
		var pMenu = this.TopMenus[iLength-1];
		pMenu.IsActionCmd = true;
		return pMenu;
	}

	//
	// Add a new Top Menu Brreak line
	//
	this.AddTopMenuBreak=function()
	{
		var iLength = this.TopMenus.push(new CxMenuItem(this,null,null,null,true,null,null,null));
		var pMenu = this.TopMenus[iLength-1];
		return pMenu;
	}
	
	// Add a new TopMenu Item with a SubMenu
	// sCaption= menu Caption
	this.AddSubMenu=function(pParent, sCaption, sTitle)
	{
		if (pParent == null)
		{
		  alert("Parent menu cannot be unassigned");
		  return null;
		}
		 
		if ((sCaption==null) || (sCaption==""))
		{
		  alert("A menu caption cannot be unassigned");
		  return null;
		}
		  
		var pMenu = new CxMenuItem(this,pParent ,sCaption,sTitle,false,null,null,null);
		return pMenu;
	}
	
	// Add a new SubMenu Item with 
	// sCaption= menu Caption
	// sHref = the hiperlink reference (must be defined)
	// sFolder = subfolder location reflative to the root. 
	this.AddSubMenuCmd=function(pParent, sCaption, sTitle, sHref, sFolder,sTarget)
	{
		//alert(pParent.Caption);
		if (pParent == null)
		{
		  alert("Parent menu cannot be unassigned");
		  return null;
		 }
		 
		if ((sCaption==null) || (sCaption==""))
		{
		  alert("A menu caption cannot be unassigned");
		  return null;
		}
		  
		if ((sHref==null) || (sHref==""))
		{
		  alert("A menu reference cannot be unassigned");
		  return null;
		}
		 
		var pMenu = new CxMenuItem(this,pParent,sCaption,sTitle,false,sHref,sFolder,sTarget);
		return pMenu;
	}

	//
	// Add a new Sub Menu Brreak line
	//
	this.AddSubMenuBreak=function(pParent)
	{
		if (pParent == null)
		{
		  alert("Parent menu cannot be unassigned");
		  return null;
		}
		 
		var pMenu = new CxMenuItem(this,pParent,null,null,true,null,null,null);
		return pMenu;
	}
	
	// Return the ElementID for the MenuBar
	this.MenuID = function()
	{
		return (this.objName+"_MenuBar");
	}
	
	// Call to add the innerHTML to the document
	this.Output = function()
	{
		if (this.PageBody == null)
		{
			document.write("<p>Unable to locate Element["+BodyID+"]</p>");
			return;
		}
		
		if ((this.TopMenus == null) || (this.TopMenus.length == 0))
			return;
		
		var sOut = "<div id='"+this.MenuID()+"' class='"+CSS_MenuBar+"'>\n";
		var aMenuList = new Array();
		var pMenu = null;
		for(var i=0;i<this.TopMenus.length;i++)
		{
		   pMenu = this.TopMenus[i];
		   if (pMenu == null)
		      continue;
		   
		   //alert("Menu["+pMenu.MenuID()+"].SubMenuCount = "+pMenu.SubMenus.length);
		   
		   sOut += pMenu.Output();
		   sOut += "\n";
		   if (pMenu.SubMenus.length > 0)
		      aMenuList.push(pMenu);
		}		
		sOut += "</div>\n";
		//alert(sOut);

		//alert(aMenuList.length);

		pMenu = aMenuList.shift();
		while (pMenu != null)
		{
			var sSubOut = "";
			if (pMenu.SubMenus.length > 0)
			{
  				var pMenuID = pMenu.MenuID();
  				sSubOut = "<div id='"+ pMenuID  +"' class='"+CSS_Menu+"'>\n";
				 
				for(var j=0;j<pMenu.SubMenus.length;j++)
				{
				   var pSubMenu = pMenu.SubMenus[j];
				   if (pSubMenu != null)
				   {
				   	sSubOut += pSubMenu.Output();
				   	sSubOut += "\n";
		   		  	if (pSubMenu.SubMenus.length > 0)
		      			aMenuList.push(pSubMenu);
		      	   }
				}
				sSubOut += "</div>\n";
				//alert(sSubOut);
			}
			sOut += sSubOut; 
			pMenu = aMenuList.shift();
		}
		//alert(sOut);
  		document.write(sOut);
  		this.HideMenus();		
	}
}
CxMenuBar.count=0;

// CXSims.CxMenuItem Object
function CxMenuItem(pMenuBar, pParent,sCaption,sTitle,bIsBreak,sCmd,sParentFolder,sTarget)
{
	this.objName="CxMenuItem"+CxMenuItem.count++;
	eval(this.objName+"=this");
	this.MenuBar = pMenuBar;
	this.Parent = pParent;
	if (sCaption==null) this.Caption=this.objName; else this.Caption=sCaption;
	if ((sCmd!= null) && (sCmd!= "")) 
		this.Cmd = sCmd;
	else
		this.Cmd = null;
	this.IsCmd = (this.Cmd != null);
	this.IsActionCmd = false;
	if ((this.Cmd != null) && (sParentFolder != null) && (sParentFolder != "")) 
		this.ParentFolder = sParentFolder;
	else
		this.ParentFolder = null;
		
	if ((sTitle!= null) && (sTitle!= "")) 
		this.Title = sTitle;
	else
		this.Title = null;
		
  if (!bIsBreak) this.IsBreak=false; else this.IsBreak=bIsBreak;
  if ((typeof(target) == "undefined") || (target==null)) this.target="_self"; else this.target=target;      
	this.SubMenus = new Array();
	this.Level = (this.Parent == null)? 0: this.Parent.Level+1;
	if (this.Parent != null)
	  this.Parent.AddSubMenu(this);

	// Add a SubMenu to the list.
	this.AddSubMenu=function(pSubMenu)
	{
	  	if ((pSubMenu != null) && (pSubMenu.Parent == this) && (!this.IsCmd))
		   this.SubMenus.push(pSubMenu);
	}
	
	this.MenuID = function()
	{
	   return (this.objName+"_SubMenu");
	}
	
	this.ItemID = function()
	{
	   return (this.objName+"_MenuItem");
	}
	
	// Get the SubMenuCount
	this.SubMenuCount = function()
	{
		return this.SubMenus.length;
	}
	
	this.MenuItemElement = function()
	{
		var pItemID = this.ItemID();
		return document.getElementById(pItemID );
	}
	
	this.SubMenuElement = function()
	{
		var pMenuID = this.MenuID();
		return document.getElementById(pMenuID );
	}
	
/*	this.ParentElement = function()
	{
	   if (this.Parent == null)
	     return null;
	     
	   var pParentID = this.Parent.MenuID;
	   return document.getElementByID(pParentID);
	}
*/	
	// Get the Menu's Class Name
	this.Class = function(bMouseOver)
	{
		var sClass = null;
		if (this.Level == 0)
		{
			if (this.IsBreak)
			   sClass = CSS_MenuBarBreak;
			else if (bMouseOver)
			   sClass = CSS_MenuBarOver;
			else
			   sClass = CSS_MenuBarItem;
		}
		else
		{
			if (this.IsBreak)
			   sClass = CSS_MenuBreak;
			else if (bMouseOver)
			   sClass = CSS_MenuItemOver;
			else
			   sClass = CSS_MenuItem;
		}
		
		return sClass;
	}
	
	// Called by CxMenuBar to add the Menus Definition to the HTML String
	this.Output = function()
	{
	    var sOut  = "";
          if (this.MenuBar == null)
            return sOut;
            
	    sOut += "<div id='"+this.ItemID()+"'";
	    if (this.Class != null) sOut +=" class='"+this.Class(false)+"'";
	    
	    //alert(this.Caption+"; Level = "+this.Level()+"; Class="+this.Class(false));
	    
	    if (this.IsBreak)
	    {
	    	if (this.Level > 0)
	    	  sOut +="><hr class='"+CSS_MenuBreak+"'></div>";
	    	else
	    	  sOut +="></div>";
	    }
	    else
	    {
	   		if (this.Title != null) sOut += " title='"+this.Title+"'";
				sOut += " onMouseOver='"+this.objName+".MouseOver(); return true'";
				sOut += " onMouseOut='"+this.objName+".MouseOut(); return true'";
				if (this.IsActionCmd)
 				   sOut += " onClick='"+this.Cmd+"'";
				else if (this.IsCmd)
 				   sOut += " onClick='"+this.objName+".Do(); return true'";
				
				sOut += ">"+this.Caption;
				if ((this.Level > 0) && (this.SubMenus.length > 0))
					sOut += "&nbsp;&nbsp; <b>>></b>";
				
				sOut += "</div>";
	    }
	    
	    return sOut;
	}
	
	// Eventhandler for Event[MouseOver]
	this.MouseOver= function() 
	{
		if (this.IsBreak)
		  return true;
		  
		var sStatus = "";
		if (this.Level == 0)
		{
		   if (this.MenuBar != null)
		     this.MenuBar.HideMenus();
		}
		else
		{
		   if (this.Parent != null)
		     this.Parent.ShowSubMenu();
		}
		
		var pElement = this.MenuItemElement();
	  if (pElement != null) 
	  {
			this.HighLight();
			if (this.Title != null)	
				sStatus  = this.Title;			
			this.ShowSubMenu();			
		}
		
		window.defaultStatus = sStatus;
		return true;
	}
	
	// Reset the Status Bar
	this.MouseOut=function()
	{
		window.defaultStatus = "";
		return true;
	}
	
	// Call to display the SubMenu (if applicable) and set all the MenuItem to
	// normal display. SubMenus of the SubMenus will be hidden.
	this.ShowSubMenu = function()
	{
		 if ((this.IsBreak) || (this.SubMenus.length == 0))
	       	return;
	       
	       //alert('Has Subs');
		var pMenuElement = this.SubMenuElement();
		if (pMenuElement == null) 
		   return;
		
		for(var i=0;i<this.SubMenus.length;i++)
		{
		  	var pSubMenu = this.SubMenus[i];
		  	if ((pSubMenu != null) && (!pSubMenu.IsBreak))
		      {  	
		      	pSubMenu.ShowNormal();
	  			pSubMenu.HideSubMenu();
		  	}
		}
	      
	      if (pMenuElement.style.display=='block')
	        return;
	      		
		//var pBodyElement = (this.MenuBar == null)? null: this.MenuBar.PageBody;
 		var iTop = 0;
 		var iLeft = 0;
		if (this.Level == 0)
		{
		/*	alert("Parent.OffsetTop = "+pItemElement.offsetParent.offsetTop+"\n"+
			        "Parent.Offsetleft = "+pItemElement.offsetParent.offsetLeft+"\n"+
			        "Item.OffsetTop = "+pItemElement.offsetTop+"\n"+
			        "Item.OffsetHeight = "+pItemElement.offsetHeight +"\n"+
			        "Item.OffsetLeft = "+pItemElement.offsetLeft +"\n"+
			        "Item.OffsetWidth = "+pItemElement.offsetWidth);
		*/	
			iTop = this.AbsBottomOffset() - 2;			
			iLeft = this.AbsLeftOffset();
			
			//iTop =  pItemElement.offsetTop + pItemElement.offsetHeight + pItemElement.offsetParent.offsetTop - 2;
			//iLeft = pItemElement.offsetLeft;  //+ pItemElement.offsetParent.offsetLeft;
		 	//pMenuElement.style.pixelTop =  pItemElement.getBoundingClientRect().bottom + pBodyElement.scrollTop - 2;
		 	//pMenuElement.style.pixelLeft = pItemElement.getBoundingClientRect().left + pBodyElement.scrollLeft;
	 	 }	
		else
		{
			iTop = this.AbsTopOffset();
			iLeft = this.AbsRightOffset() - 5;
		    /*   iTop  =  pItemElement.getBoundingClientRect().top + pBodyElement.scrollTop;
		       iLeft = pItemElement.getBoundingClientRect().right - 5; // + MenuBody.scrollLeft
		       if(pMenuElement.getBoundingClientRect().right > window.screen.availWidth )
		    	     pMenuElement.style.pixelLeft = pItemElement.getBoundingClientRect().left - pMenuElement.offsetWidth + 5; */
		}
		
		//alert("Top = "+iTop+"; Left = "+iLeft);
	 	pMenuElement.style.position = 'absolute';
	 	pMenuElement.style.top = iTop;
	 	pMenuElement.style.left= iLeft;
		
	  	pMenuElement.style.display='block';		
	}
	
	this.AbsTopOffset = function ()
	{
		var pItemElement = this.MenuItemElement();
		if (pItemElement == null)
		  return 0;		
		  
		var iOffset = pItemElement.offsetTop;
		var pParent = pItemElement.offsetParent;
		while (pParent  != null)
		{   
			//alert("Parent["+pParent.tagName+"].offsetTop = "+pParent.offsetTop);
			iOffset += pParent.offsetTop;
			pParent = pParent.offsetParent;
		}
		
		//alert("Element["+pElement+"].AbsBottomOffset = "+iOffset);
		return iOffset;
	}
	
	this.AbsBottomOffset = function ()
	{
		var pItemElement = this.MenuItemElement();
		if (pItemElement == null)
		  return 0;		
		  
		var iOffset = pItemElement.offsetTop + pItemElement.offsetHeight;
		var pParent = pItemElement.offsetParent;
		while (pParent  != null)
		{   
			//alert("Parent["+pParent.tagName+"].offsetTop = "+pParent.offsetTop);
			iOffset += pParent.offsetTop;
			pParent = pParent.offsetParent;
		}
		
		//alert("Element["+pElement+"].AbsBottomOffset = "+iOffset);
		return iOffset;
	}
	
	this.AbsLeftOffset = function()
	{
		var pItemElement = this.MenuItemElement();
		if (pItemElement == null)
		  return 0;		
		  
		var iOffset = pItemElement.offsetLeft;
		var pParent = pItemElement.offsetParent;
		while (pParent  != null)
		{   
			//alert("Parent["+pParent.tagName+"].offsetTop = "+pParent.offsetLeft);
			iOffset += pParent.offsetLeft;
			pParent = pParent.offsetParent;
		}
		
		//alert("Element["+pElement+"].AbsLeftOffset = "+iOffset);
		return iOffset;
	}
	
	this.AbsRightOffset = function()
	{
		var pItemElement = this.MenuItemElement();
		if (pItemElement == null)
		  return 0;		
		  
		var iOffset = pItemElement.offsetLeft + pItemElement.offsetWidth;
		var pParent = pItemElement.offsetParent;
		while (pParent  != null)
		{   
			//alert("Parent["+pParent.tagName+"].offsetTop = "+pParent.offsetLeft);
			iOffset += pParent.offsetLeft;
			pParent = pParent.offsetParent;
		}
		
		//alert("Element["+pElement+"].AbsLeftOffset = "+iOffset);
		return iOffset;
	}
	
	// Set the SubMenu Item's to normal display and hide the SubMenu Element.
	this.HideSubMenu = function()
	{
		 if ((this.IsBreak) || (this.SubMenus.length == 0))
	       	return;
	       
	       //alert('Has Subs');
		var pMenuElement = this.SubMenuElement();
		if ((pMenuElement == null)  || (pMenuElement.style.display=="none"))
		   return;
    		 
		// Clear Hightlights 
		for(var i=0;i<this.SubMenus.length;i++)
		{
		  	var pSubMenu = this.SubMenus[i];
		  	if (pSubMenu != null)
		  	{
		  		pSubMenu.ShowNormal();
		  		pSubMenu.HideSubMenu();
		  	}
		}
		
		pMenuElement.style.display="none";
	}
	
	// Show the MenuItem Highlighted - assign MouseOver class
	this.HighLight = function()
	{
	   var pElement = this.MenuItemElement();
	   var sClass =  this.Class(true);
	   if ((pElement != null) && (pElement.className != sClass))
	   {
	   	pElement.className = sClass;
	   }	   	
	}

	// Show the MenuItem un-Highlighted - assign Normal class
	this.ShowNormal = function()
	{
	   var pElement = this.MenuItemElement();
	   var sClass =  this.Class(false);
	   if  ((pElement != null) && (pElement.className != sClass))
	   	pElement.className = sClass;
	}
	
	
	// Eventhandler for Event[Click]
	this.Do= function(pObj) 
	{
		if (this.Cmd == null)
	      	return false;		  
	
		var sCmd = this.Cmd;
    var pBaseFolder = (this.MenuBar == null)? null: this.MenuBar.ParentFolder;
	  var sRelPath = GetRelativePath(this.ParentFolder, pBaseFolder);
	
		//alert("sRelPath = "+sRelPath)
			
		if ((sRelPath != null) && (sRelPath.length > 0))
	 	     	sCmd = sRelPath +"/"+sCmd 
	 	      	
			//alert("cmd = "+cmd )
			
		window.location.href = sCmd;  
	}
}
CxMenuItem.count=0;

