/**
 * Author: Megan Plummer
 * Created: Dec 19, 2010 11:27:35 AM
 *
 * Adds expand/collapse functionality to the public site's main menu.
 */

function toggleNav(){
    var parentLi = $(this).closest('li');
    var handle = parentLi.find('span:first');

    //if the parent menu li includes a child list
    if(parentLi.find('ul').size() > 0){

        //flip the collapsed/expanded class
        if(handle.hasClass('expanded')){
            handle.removeClass('expanded').addClass('collapsed');
        } else {
            handle.removeClass('collapsed').addClass('expanded');
        }

        //toggle the child menu list
        parentLi.children('ul').toggle();

        //don't go anywhere
        return false;
    }
}

//initialize the collapsible left nav
$(document).ready(function() {

    //loop through all parent menu elements
    $("#mainMenu > ul").children().each(function(){
        var thisLi = $(this);
        var topLink = thisLi.find('a:first');

        //add space for the expand/collapse symbol
        topLink.prepend('<span class="sign"></span>');

        //if the parent menu li includes a child list
        if($(this).find('ul').size() > 0){

            //if the parent doesn't start out expanded, hide the child menu
            if(!thisLi.find('span:first').hasClass('expanded')){
                thisLi.find('span:first').addClass('collapsed');
                thisLi.children('ul').toggle();
            }
        }
    });

    $("#mainMenu ul > li").delegate('span > a', 'click', toggleNav);

});

