AJS.toInit(function($) {
    var hideSidebarCookie = "com.atlassian.confluence.sidebar.hide",
        sidebar = $("#personal-info-sidebar"),
        height = sidebar.height(),
        sidebarControl = $("#personal-info-sidebar .sidebar-collapse"),
        contentContainer = $("#full-height-container"),
        content = $("#content");

    function toggleSidebar() {
        var howHighIsContentContainer = contentContainer.height();
        var howHighIsContent = content.height();
        sidebar.toggleClass("collapsed");
        content.toggleClass("sidebar-collapsed");

        // fixed width theme: force page to stay tall enough to cope with sidebar
        if ($("body").hasClass("theme-fixedwidth") && !sidebar.hasClass("collapsed") && content.hasClass("view") && $("#main").hasClass("has-personal-sidebar")) {
            if (content.hasClass("sidebar-collapsed")) {
                $("#page").height(howHighIsContentContainer);
                content.height(howHighIsContent);
                sidebarControl.height(sidebar.height());
            } else {
                $("#page").height("auto");
                content.height("auto");
            }
        }

    }

    if (getCookie(hideSidebarCookie) == "true") {
        toggleSidebar();
    }

    $(".sidebar-collapse").click(function(e) {
        toggleSidebar();
        setCookie(hideSidebarCookie, sidebar.hasClass("collapsed"));
        return AJS.stopEvent(e);
    }).height(height);
    //sidebar.height(height); // fixes half-px rounding bug in FF but causes overflow bug
});
