var menuFuncs = new Object();

var menuLineHeight = 0



var CSS_CLASS_SELECTED_MENU = "selectedMenuItem";

var CSS_CLASS_SELECTED_SUBMENU = "selectedSubMenu";

var CSS_CLASS_SUBMENU = "submenu";



menuFuncs.buildsubmenus = function( treeid )

{

	menuFuncs.markMenuOpenToCurrentPage();

	var ultags = document.getElementById( treeid ).getElementsByTagName( "ul" )

	for ( var t=0; t < ultags.length; t++ )

	{

		if ( treeid == ultags[t].parentNode.parentNode.id )

		{

			ultags[t].level1 = 1;

		}



		if ( ultags[t].parentNode.className != null

			&& ultags[t].parentNode.className == CSS_CLASS_SELECTED_MENU )

		{

			ultags[t].parentNode.className = CSS_CLASS_SELECTED_SUBMENU;

		}

		else

		{

			ultags[t].parentNode.className = CSS_CLASS_SUBMENU;

		}



		ultags[t].parentNode.onmouseover=function()

		{

			var that = this.getElementsByTagName("ul")[0];

			that.style.display = "block"

			if ( that.level1 )

			{	// recompute menuLineHeight from first level in case user changes text size

				var numberOfItems = menuFuncs.getChildCount( that );

				var lineHeight = that.offsetHeight;

				if ( numberOfItems != 0 && lineHeight != 0 )

				{

					menuLineHeight = parseInt( lineHeight / numberOfItems );

				}

			}



			var yScrollingOffset = menuFuncs.getScrollXY()[1];

			var windowHeight = menuFuncs.getWindowSize()[1];

			var x = this.clientWidth - 1;

			var numberOfItems = menuFuncs.getChildCount( that );

			var menuHeight = numberOfItems * menuLineHeight;

			var y = parseInt( -numberOfItems / 2 ) * menuLineHeight;	// good for most menus



			var offsetPointer = this;

			var yAbsolute = 0;

			while ( offsetPointer )

			{

				yAbsolute += offsetPointer.offsetTop;

				offsetPointer = offsetPointer.offsetParent;

			}

			var yRelative = yAbsolute - yScrollingOffset;

			if ( yRelative + y + menuHeight > windowHeight )

			{

				//y = parseInt( ( windowHeight - yRelative - menuHeight ) * 1.5 ) -1;

				y = parseInt( ( windowHeight - yRelative - menuHeight ) * 1.1 ) -1;

				if ( yRelative + y + menuHeight + menuLineHeight < 0 )

				{

					y = y + yRelative + y + menuHeight + menuLineHeight

				}

			}

			if ( yRelative + y < 0 )

			{

				y = -yRelative;

			}



			that.style.left = x + "px";

			that.style.top = y + "px";

		}



		ultags[t].parentNode.onmouseout=function()

		{

			this.getElementsByTagName("ul")[0].style.display="none"

			//that.style.zIndex = 0;

		}

	} //end for loop



} //END buildsubmenus





menuFuncs.getChildCount = function( object )

{

	var counter = 0;

	var children =  object.childNodes;



	for ( var i = 0; i < children.length; i++ )

	{

		if ( children[i].nodeType == 1 ) counter++;

	}



	return counter;



} //END getChildCount





menuFuncs.getPageName = function( pageUrl )

{

	if ( pageUrl == null || pageUrl.length == 0 )

		return " ";



	pageUrl = unescape( pageUrl );

	pageUrlParts = pageUrl.split( "/" );

	if ( pageUrlParts.length == 0 )

		return pageUrl;



	pageName = pageUrlParts[ pageUrlParts.length - 1 ];



	if ( pageName.length == 0 && pageUrlParts.length > 1 )

		pageName = pageUrlParts[ pageUrlParts.length - 2 ];



	if ( pageName.indexOf( "index." ) == 0 )

		pageName = pageUrlParts[ pageUrlParts.length - 2 ];

	return pageName;



} //END getPageName





menuFuncs.getScrollXY = function()

{

  var scrOfX = 0, scrOfY = 0;



  if( typeof( window.pageYOffset ) == 'number' )

  {

    //Netscape compliant

    scrOfY = window.pageYOffset;

    scrOfX = window.pageXOffset;

  }

  else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) )

  {

    //DOM compliant

    scrOfY = document.body.scrollTop;

    scrOfX = document.body.scrollLeft;

  }

  else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) )

  {

    //IE6 standards compliant mode

    scrOfY = document.documentElement.scrollTop;

    scrOfX = document.documentElement.scrollLeft;

  }



  return [ scrOfX, scrOfY ];



} //END getScrollXY





menuFuncs.getWindowSize = function()

{

	var myWidth = 0, myHeight = 0;



	if( typeof( window.innerWidth ) == 'number' )

	{

		//Non-IE

		myWidth = window.innerWidth;

		myHeight = window.innerHeight;

	}

	else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) )

	{

		//IE 6+ in 'standards compliant mode'

		myWidth = document.documentElement.clientWidth;

		myHeight = document.documentElement.clientHeight;

	}

	else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) )

	{

		//IE 4 compatible

		myWidth = document.body.clientWidth;

		myHeight = document.body.clientHeight;

	}



	return [ myWidth, myHeight ];



} //END getWindowSize





menuFuncs.markMenuOpenToCurrentPage = function()

{

	var pageName = menuFuncs.getPageName( location.href );

	var menuId = menuUrls[ pageName ];

	if ( menuId != null )

	{

		var menuItem = document.getElementById( menuId );

		if ( menuItem != null )

		{

			menuItem.className = CSS_CLASS_SELECTED_MENU;



			var submenu = menuItem.getElementsByTagName( "ul" )[0];

			if ( submenu == null )

			{

				menuItem.parentNode.setAttribute( "rel", "open" );

			}

			else

			{

				submenu.setAttribute( "rel", "open" );

			}

		}

	}



} //END markMenuOpenToCurrentPage




