//
// OmniSTAR Website
// /js/menu.js
//
// Paul Dragicevich, 26/08/2005
//


var MenuLinkInfo_RegexURL = /^([^\/]+):\/\/([^\/]+)\/?(.*)$/;

//
// MenuLinkInfo
// This is an object that parses a URL and provides information about it
// that is pertinent to the menu show/hide/highlight code below.
//
function MenuLinkInfo ( p_URL )
{
	// Constructor logic here
	this.url = null;
	this.scheme = null;
	this.server = null;
	this.path = null;
	this.section = [];
	
	var matches = MenuLinkInfo_RegexURL.exec(p_URL);
	if ( matches.index > -1 )
	{
		this.url = p_URL;
		this.scheme = matches[1];
		this.server = matches[2];
		this.path = matches[3];
		this.section = this.path.split("/");
	}
	// Constructor finished.
	

	//
	// getSection
	// Returns the section name.
	//
	this.getSection = function ()
	{
		var returnSection = null;
		if ( this.section.length > 0 )
		{
			returnSection = this.section[0];
		}
		return returnSection;
	}

	//
	// getSubsection
	// Returns the sub-section name.
	//
	this.getSubsection = function ()
	{
		var returnSubsection = null;
		if ( this.section.length > 1 )
		{
			returnSubsection = this.section[1];
		}
		return returnSubsection;
	}

	//
	// getFilename
	// Returns the page filename.
	//
	this.getFilename = function ()
	{
		var returnFilename = null;
		if ( this.section.length > 0 )
		{
			returnFilename = this.section[this.section.length-1];
		}
		return returnFilename;
	}
	
	//
	// isSubsection
	// Returns true if this URL is a subsection link.
	//
	this.isSubsection = function ()
	{
		return ( this.section.length > 2 );
	}
	
	//
	// isSection
	// Returns true if this URL is a section link.
	//
	this.isSection = function ()
	{
		return ( this.section.length === 2 );
	}
	
	//
	// isToplevel
	// Returns true if this URL is a section link.
	//
	this.isToplevel = function ()
	{
		return ( this.section.length < 2 );
	}
	

}








//
// MenuAddLoadEvent
// Wire up an onload event.
//
function MenuAddLoadEvent ( p_OnLoadFunction ) 
{
   var oldonload = window.onload;
   if ( typeof(window.onload) != 'function') 
   {
      window.onload = p_OnLoadFunction;
   } 
   else 
   {
      window.onload = function() 
	   {
         oldonload();
         p_OnLoadFunction();
      }
   }
}



//
// MenuActivate
// Show/hide/toggle display styles for menu links.
//
function MenuActivate ()
{
	if ( !document.getElementById )
		return;
	
	var pageInfo = new MenuLinkInfo ( window.location.href );
	if ( !pageInfo.url )
		return;
		
	MenuActivateTop(pageInfo);
	MenuActivateMid(pageInfo);
	MenuActivateSide(pageInfo);
}

//
// MenuActivateTop
// Top menu activation.
//
function MenuActivateTop ( p_PageInfo )
{
	var topmenu = document.getElementById("topmenu");
	if ( !topmenu )
		return;
		
	var links = topmenu.getElementsByTagName("a");
	if ( links.length < 1 )
		return;

	var td;
	var a;
	var aInfo;
	for ( var linkIndex = 0 ; linkIndex < links.length ; linkIndex++ )
	{
		a = links[linkIndex];
		td = a.parentNode;
		if ( IsTag(td,"td") )
		{
			aInfo = new MenuLinkInfo(a.href);
			if ( aInfo.url )
			{
				if ( (aInfo.path === "index.html" && ( p_PageInfo.path === "" || p_PageInfo.path === "index.html" )) ||
					  (aInfo.url === p_PageInfo.url) )
				{
					td.className = "midmenuSelected";
            }
			}
		}
	}	
}

//
// MenuActivateMid
// Mid menu activation.
//
function MenuActivateMid ( p_PageInfo )
{
	var midmenu = document.getElementById("midmenu");
	if ( !midmenu )
		return;
		
	var links = midmenu.getElementsByTagName("a");
	if ( links.length < 1 )
		return;
	
	var td;
	var a;
	var aInfo;
	for ( var linkIndex = 0 ; linkIndex < links.length ; linkIndex++ )
	{
		a = links[linkIndex];
		td = a.parentNode;
		if ( IsTag(td,"td") )
		{
			aInfo = new MenuLinkInfo(a.href);
			if ( aInfo.url )
			{
				if ( aInfo.getSection() === p_PageInfo.getSection() )
				{
					td.className = "midmenuSelected";
            }
			}
		}
	}	
}

//
// MenuActivateSide
// Side menu activation
//
function MenuActivateSide ( p_PageInfo )
{
	var sidemenu = document.getElementById("sidemenu");
	if ( !sidemenu )
		return;
		
	var links = sidemenu.getElementsByTagName("a");
	if ( links.length < 1 )
		return;
	
	var td;
	var tr;
	var a;
	var aInfo;
	for ( var linkIndex = 0 ; linkIndex < links.length ; linkIndex++ )
	{
		a = links[linkIndex];
		td = a.parentNode;
		if ( IsTag(td,"td") )
		{
			tr = td.parentNode;
			if ( IsTag(tr,"tr") )
			{
				a.onmouseover = "return false;"; //MenuSideMouseOver;
				a.onmouseout = "return false;";
				td.onmouseover = "return false;";
				td.onmouseout = "return false;";
				tr.onmouseover = MenuSideMouseOver;
				tr.onmouseout = MenuSideMouseOut;

				aInfo = new MenuLinkInfo(a.href);
				if ( aInfo.url )
				{
					if ( aInfo.path === p_PageInfo.path )
					{
						tr.className += "Selected";
					}
					if ( aInfo.isSubsection() && aInfo.getSubsection() === p_PageInfo.getSubsection() )
						tr.style.display = "block";
				}
			}
		}
	}	
}


//
// MenuSideMouseOver
// Mouse over for sidemenu row.
//
function MenuSideMouseOver ( p_Event )
{
	tr = MenuSideGetParentTR(p_Event);
	if ( tr.className.indexOf("Selected") == -1 )
		MenuSideToggleHover(tr);
}


//
// MenuSideGetParentTR
// Get the ancestor of this which is a tr.
//
function MenuSideGetParentTR ( p_Event )
{
	var ew = new EventWrapper(p_Event);
	var tag = ew.getSourceElement();
	var tagName = ew.getSourceTagName();
	var tr = null;
	if ( tagName === "tr" )
	{
		tr = tag;
	}
	else if ( tagName === "td" && IsTag(tag.parentNode,"tr"))
	{
		tr = tag.parentNode;
	}
	else if ( tagName === "a" && IsTag(tag.parentNode.parentNode,"tr") )
	{
		tr = tag.parentNode.parentNode;
	}
	return tr;
}

//
// MenuSideMouseOut
// Mouseout for sidemenu row.
//
function MenuSideMouseOut ( p_Event )
{
	tr = MenuSideGetParentTR(p_Event);
	if ( tr.className.indexOf("Selected") == -1 )
		MenuSideToggleHover(tr);
}

function MenuSideToggleHover ( p_Tag )
{
	var className = p_Tag.className;
	var hoverPos = className.indexOf("Hover");
	if ( hoverPos == -1 )
		p_Tag.className += "Hover";
	else
		p_Tag.className = className.substr(0,hoverPos);
}



//
// IsTag
// Returns true if p_Node is a Tag of type p_TagName
//
function IsTag ( p_Node , p_TagName )
{
   var isTag = false;
   if ( p_Node && p_Node.tagName && (p_Node.tagName.toLowerCase() === p_TagName.toLowerCase()) )
   {
      isTag = true;
   }
   return isTag;
}

//
// MenuTweakStyles
// document.writeln some style information.  
// The stylesheet is set up so that the absence of this info will display all links 
// (for users who have javascript switched off).  If the user has JS on, but the 
// browser doesn't support DOM, this code doesn't execute so again the user sees all links.
//
function MenuTweakStyles ()
{
	if ( document.getElementById )
	{
		document.writeln("<style type=\"text/css\">\n");
		document.writeln(".sidemenuSub, .sidemenuSubSelected\n");
		document.writeln("{\n");
		document.writeln("   display: none;\n");
		document.writeln("}\n");
		document.writeln("</style>\n");	
	}
}

MenuAddLoadEvent ( MenuActivate );
MenuTweakStyles();
