//*****************************
// PageNameFromHref
// Given an href such as ../../path/path/pagename.php, returns just pagename.php portion
//*****************************
function PageNameFromHref(sHref)
{
	var arrSplit = sHref.split("/");
	var nLen = arrSplit.length;
	
	if (nLen < 1)
	{
		return sHref;
	}
	
	sHref = arrSplit[nLen-1];
	return sHref;

}
//*****************************
// IsSubNavItem
// Given an item's class name, determines if it is a subnav item (will have class of "indent"
//*****************************
function IsSubNavItem(obj)
{
	return (obj.hasClass("indent"));
}
//*****************************
// IsFirstLI
// Given an item's class name, determines if it is the first item (will have class of pos_first)
//*****************************
function IsFirstLI(obj)
{
	var sClassname = obj.attr("class");
	if (sClassname.length < 1)
	{
		return false;
	}
	
	//return (sClassname.indexOf("pos_first")>-1);
	return (obj.hasClass("pos_first"));
}
//*****************************
// IsLastLI
// Given an item's class name, determines if it is the first item (will have class of pos_last)
//*****************************
function IsLastLI(obj)
{
	var sClassname = obj.attr("class");

	if (sClassname.length < 1)
	{
		return false;
	}
	
	//return (sClassname.indexOf("pos_last")>-1);
	return (obj.hasClass("pos_last"));
}
//*****************************
// LIHasChildren
// Given an item's class name, determines if an LI has no children. In this case it would have been assigned
// a class of no_children in the initialization
//*****************************
function LIHasChildren(obj)
{
	var sClassname = obj.attr("class");
	
	if (sClassname.length < 1)
	{
		return true;
	}
	
	//return (sClassname.indexOf("no_children") == -1);
	return (!obj.hasClass("no_children"));
}
//*****************************
// ProcessRightNav
// Sets up the right nav to show / hide the appropriate items
//*****************************
function ProcessRightNav(sCallerPageName)
{
	//$('#rightnav_instructions').addClass("hideme");
	//alert($.browser.version);
	
	// MAC SAFARI 2.x JQUERY INCOMPATIBILITY
	// WARNING: Safari 3.0 is build 523 and higher, so on mac, check to ensure the user has 3.0
	if ( (browser.isMac) && ($.browser.safari) && (parseInt($.browser.version) < 523) )
	{
		//alert("Mac Safari 2.x detected.");
		return;
	}

	/*
	Set the subnav item visibility. 
	the main nav items are just simple <li> elements whose href points to their page
	the subnav items are also <li> elements with href, but with a class of "indent" assigned to the <li>
	loop through each of the <li> items and examine their href. 
	
	if an <li> does not have the 'indent' class assigned to it, then it is a top level item so
	assign an onclick handler to it that will expand or collapse all of the subitems under it
	assuming there are any subitems under it. If there are no subitems under it, do not alter the 
	item at all.
	
	if an <li indent> represents the current page, then leave that <li> alone. 
	if an <li> does not point to the current page, examine all the 
	*/
	var nCount=0;
	var nCurrentPageLIIndex=-1;
	
	var objDiv = $('#subnav_div');
	var objUL = $('#subnav_ul');
	
	nLICount= $('#subnav_ul li a').length;
	
	var arrLI = $('#subnav_ul li');
	nLICount= arrLI.length;

	sCallerPageName = sCallerPageName.toLowerCase();
	
	// init
	// for each A within the LI in the nav list
	$('#subnav_ul li a').each(
		  function(nIndex)
		  {
			//***************************************************************************************
			// mfindlay 9/24/08: if the <a> has the class name "indent", then remove it and instead
			//                   assign it to the owning <li>. This to help in case Contribute
			//                   or Dreamweaver accidently assigns the "indent" class to the <a>
			//                   instead of the intended <li>
			//mfindlay 09/24/08 if ($(this).attr("class")=="indent")
			if (IsSubNavItem($(this)))
			{
				$(this).removeClass("indent");   		// remove the "indent" class from from the <a>
				$(this).parent().addClass("indent");    // add the "indent" class to the owning <li>
			}
			//***************************************************************************************
						
		  } // function(nIndex)
    ); // $('#subnav_ul li a').each(	
	
	// for each A within the LI in the nav list
	$('#subnav_ul li a').each(
	      function(nIndex)
	      {
			// fetch the page this <a> points to.
			var sHref = $(this).attr("href").toLowerCase();
			// strip to just the actual page name
			sHref = PageNameFromHref(sHref);
			
			//***************************************************************************************
			// mfindlay 9/24/08: if the <a> has the class name "indent", then remove it and instead
			//                   assign it to the owning <li>. This to help in case Contribute
			//                   or Dreamweaver accidently assigns the "indent" class to the <a>
			//                   instead of the intended <li>
			//mfindlay 09/24/08 if ($(this).attr("class")=="indent")
			/*if (IsSubNavItem($(this)))
			{
				$(this).removeClass("indent");   		// remove the "indent" class from from the <a>
				$(this).parent().addClass("indent");    // add the "indent" class to the owning <li>
			}*/
			//***************************************************************************************
			
			// fetch the <li> parent of the <a>
			var objLI = $(this).parent();
			
			// CURRENT PAGE?
			// is this item the current page?
			if (sHref==sCallerPageName)
			{
				// set the zero-based index of the current page's LI 
				nCurrentPageLIIndex = nIndex;
				
				// set a class to this item to represent the CURRENT PAGE
				objLI.addClass("rightnav_currentpage");
				
				// remove the hyperlink for this item so the user can't click it.
				var sLinkText = $(this).html();
				objLI.html(sLinkText);
			}
			
			// helper: set these class names on the li to indicate the first and last items
			if (nIndex==0)
			{
				objLI.addClass("pos_first");
			}
			if (nIndex==(nLICount-1))
			{
				objLI.addClass("pos_last");
			}
			
			// fetch the classname of the current <li>
			//var sClassname = objLI.attr("class");
			
			// determine if this item has no children
			//mfindlay 09/24/08 if (!IsSubNavItem(sClassname))
			if (!IsSubNavItem(objLI))
			{		
				// if it's the last LI, then it has no children
				if (IsLastLI(objLI))
				{
					//alert("Is LAST LI. Adding class no_children to Item " + objLI.text());
					objLI.addClass("no_children");
				}
				else
				{
					// test 1 passed. It is not a subnav item. So examine the next item's class name
					//mfindlay 09/24/08 if (!IsSubNavItem(objLI.next().attr("class")))
					if (!IsSubNavItem(objLI.next()))
					{
						//alert("Next item (" + objLI.next().text() + ") not a subnav. Adding class no_children to Item " + objLI.text());
						objLI.addClass("no_children");
					}
				}
			}
			
			// if the LI is a subnav item, make it initially invisible. We will 
			// go through each one and make just the appropriate section subnavs
			// visible further down.
			//mfindlay 09/24/08 if (IsSubNavItem(sClassname))
			if (IsSubNavItem(objLI))
			{
				objLI.toggleClass('indent_hide');
			}
			
			// if the classname is empty, then it is a toplevel LI. So assign an onclick handler 
			// that will allow us to toggle the underlying classes of that element
			//mfindlay 09/24/08 if (!IsSubNavItem(sClassname))
			if (!IsSubNavItem(objLI))
			{
				// ONCLICK
				// assign the following function to the "click" of the LI
				objLI.bind('click', function(objEvent) 
				{
					/* starting the NEXT LI, if the LI has the "indent" class, then toggle its display. 
					   as soon as an LI with an empty class is encountered, or when the end of the list 
					   is encountered, stop.
					   
					   nIndex represents the overall zero-based index into the entire LI list
					   nLICount represents the 1-based total number of elements in the LI list
					   arrLI represents the array of LI objects
					*/
					//alert(objEvent.type);
				
					// Toggle this parent's LI arrow 
					if (LIHasChildren(objLI))
					{
						objLI.toggleClass('li_down');  
					}
					
					// is this the last LI in the list? then no need to toggle anything else
					if (IsLastLI(objLI))
					{
						return;
					} 
					
					// start the loop by fetching the NEXT LI
					var objLIWrk = objLI.next();
					
					// starting with the NEXT <li> in the list.
					while (true)
					{
						// fetch the class name. 
						//var sClassWrk = objLIWrk.attr("class");
						
						// if no class name assigned to the LI, then it is a top level LI and we're done
						//if (!IsSubNavItem(sClassWrk))
						if (!IsSubNavItem(objLIWrk))
						{
							break;
						}
						
						// this is a child LI, so toggle its display
						objLIWrk.toggleClass('indent_hide');
						
						// was this the last one?
						if (IsLastLI(objLIWrk))
						{
							break;
						}
						
						// fetch the next LI
						objLIWrk = objLIWrk.next();
						
					} // while (true)
				}); // objLI.bind('click', function() 
			} // if (!IsSubNavItem(sClassname))
						
			
		  } // function(nIndex)
		); // $('#subnav_ul li a').each(

		// if none of the top level sections are represented by this page, then exit. This could be the case if
		// the user is navigating to a top section landing page, or if the site administrator misquoted a link name
		// or if the page does not use the right nav.
		if (nCurrentPageLIIndex == -1)
		{
			return;
		}

		// expand all of the subnav items for this section. the selected LI may be a subnav
		// item or it may be a top level item.
		
		// make any subnav items visible within this section
		
		// Start by selecting the item representing the current page.
		var objLISelected = arrLI.eq(nCurrentPageLIIndex);
		
		// if this item is a subnav item, move up the list of LIs until the parent is found. We will 
		// process our list from that starting point working down
		//mfindlay 09/24/08 while ( (!IsFirstLI(objLISelected.attr("class"))) && (IsSubNavItem(objLISelected.attr("class"))) )
		while ( (!IsFirstLI(objLISelected)) && (IsSubNavItem(objLISelected)) )
		{
			objLISelected = objLISelected.prev();
		}
		  
		// we now have the parent LI. Set its array to DOWN if it has children
		objLISelected.toggleClass('li_down'); 
		
		// last item?
		if (IsLastLI(objLISelected))
		{
			return;
		}

		// We now have the parent LI. So for each child LI, set it to visible
		// start with the first NEXT item
		if (nLICount > 1)
		{
			objLISelected = objLISelected.next();
		}
		
		// only go until the first non "child" item is found
		// only go until the first non "child" item is found
		//mfindlay 09/24/08 while (IsSubNavItem(objLISelected.attr("class")))
		while (IsSubNavItem(objLISelected))
		{
			objLISelected.toggleClass('indent_hide'); 
			
			// last item?
			if (IsLastLI(objLISelected))
			{
				break;
			}
			
			objLISelected = objLISelected.next();
		}
}
