$(document).ready(function(){

// get the root-relative path of the current page  
$path = window.location.pathname  
if($path.indexOf(".html") == -1)
    $path = $path + "index.html"
$path = $path.toLowerCase()  // normalize case  

// find the first matching link and apply classes accordingly
$("#fb-nav a").each(function(){	
	
	if($(this).attr("href")) {   
    $url = $(this).attr("href").toLowerCase() // normalize case	
    if($path == $url) // compare string value
    {        
        // active and open are css classes used for styling
        // current and selected are css classes used as flags
        $(this).parent("li").addClass("active").addClass("current")        
        $(this).parent("li").parents("ul").addClass("selected").parents("li").addClass("open").addClass("selected") // set appropriate styles and flags to ancestors
        $(this).parent("li").parents("ul").show() // make sure ancestors are displayed
        $("#fb-nav > ul > li.has-sub.selected").focus() // apply :hover styles on the top-level links
        $("#fb-nav > ul").removeClass("selected") // remove flag from top level so that it does not get hidden on .click
        //return false; // return after the first link is found    
    }
		}
    
});  

// close accordion elements that were opened earlier
$("#fb-nav a").click(function(){
    
    // check if this has been done already
    if($("#fb-nav li.current").length != 0)
    {
        // if the selected link is outside of the manually opened structure
        if($(this).parents("ul.selected").length == 0)
        {       
            $("#fb-nav li.selected").removeClass("selected").removeClass("open").parent("ul.selected").fadeOut(500) // hide elements that were manually displayed        
            $("#fb-nav li.current").removeClass("current").removeClass("active") // remove remaining flags/styles    
            
        }               
    }

})

  
});
