// Author:  Matt Rossi. Apated for ATutor by Vegard A. Johansen
// Links and info: http://atutor.no/2008/09/create-side-menu-toggle-function-cookies-jquery

$(document).ready(function() {
    // set initial state: the main container has enough space, and the side menu shows.
    $("#categories-panel").show();

    // create a variable which is the link to toggle the menu
    // this creates a link to # with the class "togglemenu"
    var a = $("<a>toggle panel</a>").attr('href','#').addClass("togglemenu");

    // .. and place this link within the element with class "toggle-menu"
    $('.toggle-menu').append(a);

    // define a function when you click the link with the class togglemenu
    $(".togglemenu").click(function(){
        // if the menu is visible (initial state), the function allows you to hide it 
        // ..and sets the cookie to hidden after, so the state is remembered
        if ($("#categories-panel").is(":visible")) {
            $("#categories-panel").slideUp("slow");
            $(this).addClass("active");
            $.cookie('categories-panel', 'hiding', {path: "/"});
            return false;
        } else {
        // and if not visible, the function let's you show it
            $("#categories-panel").slideDown("slow");
            $(this).removeClass("active");
            $.cookie('categories-panel', 'showing', {path: "/"});
            return false;
        }
    }); 

// cookie time!
    // creates a variable with the contents of the cookie catpanel
    var catpanel = $.cookie('categories-panel');

    // if cookie says the menu is hiding, keep it hidden!
    if (catpanel == 'hiding') {
            $("#categories-panel").hide();
            $(".togglemenu").addClass("active");
    };
}) // jquery ends
   
;