$(document).ready(function(){

    function setDimensions() {
        var windowHeight = $(window).height();
        var windowWidth = $(window).width();
        var headerHeight = $('#header').outerHeight();
        var navWidth = $('#nav').outerWidth();
        var footerHeight = $('#footer').outerHeight();
        
        $('#nav').add('#content').height(windowHeight-headerHeight-footerHeight);
        $('#content').width(windowWidth-navWidth);
    }
    
    setDimensions();
    $(window).resize(function(){setDimensions();});

    $('#toc li').filter(':has(li)').addClass('tocParent');
    var categories = $('#toc > ul > li').filter(':has(li)');
    $(categories).addClass('tocCategory');
    $(categories).each(function(index){
        $(this).attr('id','tocCategory' + index);
    });
    var navState = $.cookie('ReedCUSHelpNav');
    if(navState) { $(navState).addClass('tocActive'); }
    $(categories).children('a').click(function(event) {
        event.preventDefault();
        if ($(this).parent().hasClass('tocActive')){
            $(categories).removeClass('tocActive'); // Collapse all panels
        }
        else {
            $(categories).removeClass('tocActive'); // Collapse all panels
            $(this).parent().addClass('tocActive'); // Open the desired panel
        }

        // Set cookie so nav state persists
        navState = $(this).parent().attr('id');
        $.cookie('ReedCUSHelpNav' , '#' + navState , { expires: 1, path: '/', domain: 'reed.edu' });

    });
});
