﻿/* Relies on jquery library being referenced in the host page prior to this script */
$(document).ready(function(){
    // find all first level list items (which have nested UL/third level cat) and attach a click event
    $('div#searchRefinements li > a.expandable').click(function(){
        // current element i.e. the containing li
        var current = $(this).parent();
        // get a reference to the highest level <ul>
        var parentList = current.parent();
        // get a reference to the <ul> that will be expanded
        var childList = current.find('ul');
            
        // if uncollapsed clicked
        if(childList.css('display') == 'none')
        {
            // hide any other (expanded) menus
            if(parentList.find('li ul:visible').length > 0)
            {
                parentList.find('li ul:visible').slideUp('slow',function(){
                    // expand the menu the user clicked on
                    childList.slideDown('slow');
                    current.removeClass('expandable');
                    current.addClass('expanded');                        
                });
                // Close the other menus
                parentList.find('li').removeClass('expanded');
                parentList.find('li').addClass('expandable');
            }
            else
            {
                // Expand the menu
                childList.slideDown('slow');
                current.removeClass('expandable');
                current.addClass('expanded');
            }
        }
        else {
            // Close the menu
            childList.slideUp('slow');
            current.removeClass('expanded');
            current.addClass('expandable');
        }
        // stop the href in the clicked anchor from doing its thing.   
        return false;
    });   
});

