/*
	Show Fieldsets Script
  Written by: Peter Crackenberg
  Last Changed: 8/23/2010
  
  This script is for use on the George Fox University A-Z site index,
  employee directory, and other situations that require dynamic changing of
  fieldset content.
  
  1/31/06 - This script was changed to add in the correct DOM Event registration
  model, so that the script would play nice with other things that also used
  onload events in the window.
  
  7/10/06 - Script changed to not rely on the "content-right" element that is
  used on the main GFU site.
  
  8/10/2010 - Written to facilitate the change to jquery. - Joel Kelley
  
*/

$(document).ready(function()
{
    $("fieldset").hide();
    
     var showFirst = readCookie(window.location.pathname);
     
     if(showFirst == undefined)
     {
         $("fieldset:first").show();
     }
     else if(showFirst == "show all")
     {
         $("fieldset").show();
     }
     else
     {          
        $("fieldset").hide();
         
         var toShow = getFieldset(showFirst);
         $(toShow).show();
         
     }//End of handle first show
    
    
    $(document).delegate("#nav_buttons a", "click", function()
    {
        var toShow = $(this).text().toLowerCase();
        var cookieName = window.location.pathname;
        
        if(toShow == "show all")
        {
            $("fieldset").show();
            createCookie(cookieName, toShow, 360);
        }
        else
        {
            createCookie(cookieName, toShow, 360);
            
            $("fieldset").hide();
            
            var fieldset = getFieldset(toShow);
            $(fieldset).show();
            
        }
   
    });
});

/**
 * Needed because the ids for the fieldsets violate the specs for ids by using spaces.  
 * This means we can use a jquery id selector, however we can still pull and compare the
 * attribute strings.
 *
 * @param string id - base id of fieldset to return will be lowercased and suffixed 
 *	              with _field
 *
 * @return element the field of the matching id, or null if none matched.
 */
function getFieldset(id)
{
	var fieldset = null;
	
	id = id.toLowerCase() + '_field';
	
	$('fieldset').each(function()
	{
		
		if( $(this).attr('id') == id )
		{
			fieldset = this;
		}
	
	});
	
	return fieldset;
}

