﻿/* 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#categories > ul > li:has(ul) > a').click(function(){
            // get a reference to the highest level <ul>
            var parentList = $(this).parent().parent();
            // get a reference to the <ul> containing the link that was just clicked
            var childList = $(this).parent().find('ul');
            
            // only do something if a collapsed list was 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');
                        childList.parent().addClass('expanded');
                        });
                        parentList.find('li').removeClass('expanded');
                }
                else
                {
                    childList.slideDown('slow');
                    $(this).parent().addClass('expanded');
                }
            }
            // stop the href in the clicked anchor from doing its thing.   
            return false;
        });
});

