$(document).ready(function () {
    //caching our JQuery objects in variables...
    var mainExpand = $('#main-expand');
    var mainCollapse = $('#main-collapse');
    var toc = $('.toc');
    var classYuiB = $('.yui-b');
    var tocExpand = $('#toc-expand');
    var tocCollapse = $('#toc-collapse');
    var classYuiGmain = $('#yui-g-main');
    var glossyMenu = $('.glossymenu');
    var tocTreeLink = $('.tocTree a:not([class="tocExpandCollapseLink"])');
    var table = $('#yui-g-main>table');
    var navBarDivUpper = $('#navbardiv-upper>table');
    var navBarDivLower = $('#navbardiv-lower table');
    var allCollections = $('div.capitalized>a:first-child');
    var masthead = $('li.masthead-navbar a');
    var hd = $('div#hd a');
    //functions for expanding and collapsing the main content div...
    mainExpand.click(function () {
        mainExpand.fadeOut();
        mainCollapse.fadeIn();
        toc.hide("fast");
        classYuiB.css('margin-left', '0em');
        table.css('width', '800px');            
        navBarDivUpper.css('width', '800px');
        navBarDivLower.css('width', '800px');
        $.cookie('main', 'full');
    });
    mainCollapse.click(function () {
        mainCollapse.fadeOut();
        mainExpand.fadeIn();
        classYuiB.css('margin-left', '15.1em');
        table.removeAttr("style");            
        navBarDivUpper.removeAttr("style");
        navBarDivLower.removeAttr("style");
        toc.css('width', '185px');
        toc.show("slow");
        $.cookie('main', 'normal');
    });
    //functions for expanding and collapsing the toc...
    tocExpand.click(function () {
        tocExpand.fadeOut();
        tocCollapse.fadeIn();
        classYuiGmain.hide(1000);
        toc.css('width', 'auto');
        glossyMenu.css('width', '800px');
        $.cookie('toc', 'full');
        tocTreeLink.click(function () {                            //returning to two-column layout when clicking a link inside the eclipsing nav bar
            tocCollapse.fadeOut();
            tocExpand.fadeIn();
            classYuiGmain.show(1000);
            toc.css("width", "185px");
            glossyMenu.css('width', '185px');
            $.cookie('toc', 'normal');
        });
    });
    tocCollapse.click(function () {
        tocCollapse.fadeOut();
        tocExpand.fadeIn();
        classYuiGmain.show(1000);
        toc.css("width", "185px");
        glossyMenu.css('width', '185px');
        $.cookie('toc', 'normal');
    });
    allCollections.click(function () {    //returning to two-column layout when selecting another collection title.
        $.cookie('main', null);
        $.cookie('toc', null);
    });
    masthead.click(function () {
        $.cookie('main', null);
        $.cookie('toc', null);
    });
    hd.click(function () {
        $.cookie('main', null);
        $.cookie('toc', null);
    });
    
    
    //setting variables and starting to define user preferences...
    var mainContent = $.cookie('main');
    var navBar = $.cookie('toc');
    //the main content cookie is set to full or normal...
    if (mainContent == 'full') {
        mainExpand.fadeOut();
        mainCollapse.fadeIn();
        toc.hide("fast");
        classYuiB.css('margin-left', '0em');
        table.css('width', '800px');          
        navBarDivUpper.css('width', '800px');
        navBarDivLower.css('width', '800px');
    };
    if (mainContent == 'normal') {
        mainCollapse.fadeOut();
        mainExpand.fadeIn();
        classYuiB.css('margin-left', '15.1em');
        table.removeAttr("style");           
        navBarDivUpper.removeAttr("style");
        navBarDivLower.removeAttr("style");
        toc.css('width', '185px');
        toc.show("slow");
    };
    //the nav bar cookie is set to full or normal...
    if (navBar == 'full') {
        tocExpand.fadeOut();
        tocCollapse.fadeIn();
        classYuiGmain.hide(1000);
        toc.css('width', 'auto');
        glossyMenu.css('width', '800px');
        tocTreeLink.click(function () {                            //returning to two-column layout when clicking a link inside the eclipsing nav bar
            tocCollapse.fadeOut();
            tocExpand.fadeIn();
            classYuiGmain.show(1000);
            toc.css("width", "185px");
            glossyMenu.css('width', '185px');
            $.cookie('toc', 'normal');
        });
    };
    if (navBar == 'normal') {
        tocCollapse.fadeOut();
        tocExpand.fadeIn();
        classYuiGmain.show(1000);
        toc.css("width", "185px");
        glossyMenu.css('width', '185px');
    };
});
