function findPos( obj )
{
	var left = 0;
	var top = 0;
	if ( obj.offsetParent )
	{
		left = obj.offsetLeft;
		top = obj.offsetTop;
		while ( obj = obj.offsetParent )
		{
			left += obj.offsetLeft;
			top += obj.offsetTop;
		}
	}
	return [left,top];
}

function setupNavmenu()
{
	var menuLi = $( "#navmenu" ).parent(),
		mPos = menuLi.offset();
		
	if ( mPos != null )
	{
		var lt = mPos.left
			tp = mPos.top
			wd = menuLi.width()
			ht = menuLi.height();

		$( '#navbar' ).css( 'left', ( lt + wd - 151 ) + 'px' );
		$( '#navbar' ).css( 'top', ( tp + ht + 1 ) + 'px' );
		
		$( 'a.more', '#navbar' ).addClass( 'closed' );
		
		$( 'a.more', '#navbar' ).click( 
			function () 
			{ 
				// toggle submenus open/closed
				if ( $(this).attr( 'class' ).indexOf( 'closed' ) > -1 )
				{
					$(this).removeClass( 'closed' );
					$(this).addClass( 'open' );
					if ( $.browser.msie && $.browser.version == '7.0' )
						$(this).next().show( 500 );
					else
						$(this).next().slideToggle( 500 );
				}
				else if ( $(this).attr( 'class' ).indexOf( 'open' ) > -1 )
				{
					$(this).removeClass( 'open' );
					$(this).addClass( 'closed' );
					$(this).next().slideToggle( 500 );
				}
				
				return false;
			}
		);
	}
		
	// IE hacks to overcome no hover style for a-tags with IDs
	if ( $.browser.msie )
	{
		$( '#navmenu' ).hover( 
			function () { $(this).css( "color", "#119955" ); }, 
			function () { $(this).css( "color", "white" ); });
		$( 'a.more', '#navbar' ).hover( 
			function () { $(this).css( "backgroundColor", "white" ); }, 
			function () { $(this).css( 'backgroundColor', "#ccffcc" ); });
	}
	
	$( '#navmenu' ).click( 
		function () 
		{
			var closed = $( '#arrow' ).css( 'background' ).indexOf( 'expand' ) > -1;
			// $( '#navbar' ).slideToggle( 'slow' );
			if ( closed )
			{
				if ( $.browser.mozilla )
					$( '#navbar' ).fadeIn( 'slow' );
				else
					$( '#navbar' ).slideDown( 'slow' );
			}
			else
			{
				if ( $.browser.mozilla )
					$( '#navbar' ).fadeOut( 'slow' );
				else
					$( '#navbar' ).slideUp( 'slow' );
			}

			if ( !closed )
			{
				$( '#navbar ul ul' ).hide();
				$( 'a.more', '#navbar' ).removeClass( 'open' );
				$( 'a.more', '#navbar' ).addClass( 'closed' );
			}

			$( '#arrow' ).css( 'background', 'transparent url(/FileData/IMACSWeb/images/' 
								+ ( closed ? 'collapse' : 'expand-btn' ) 
								+ '.gif) no-repeat 0.2em 50%' );
			return false;
		}
	);
}

$(document).ready( 
	function() 
	{
		if ( !document.getElementById || !document.createTextNode ) {return;} // test to see if user agent supports modern dom

		// fix display of PNG images and background images on IE5.5 and IE6	
		//if ( $.browser.msie && /MSIE\s(5\.5|6\.)/.test(navigator.userAgent) )
		//{
		//		$.ifixpng( '/FileData/IMACSWeb/images/transparentpixel.gif' );
		//		$( 'img[src$=.png], #top, #dl-top, #dl-bottom, #spotlight-top, #bottom' ).ifixpng( 'crop' );
		//		$('#navbar').bgiframe();
		//}

		setTimeout( function() { setupNavmenu() }, 200 );
	}
);
