/**
 * Handles the height of the personal sidebar and the page content. Remember the sidebar can grow after being
 * opened so it has to be reset whenever it's toggled. JS is designed to collapse whitespace where possible;
 * so when the sidebar's collapsed elements are set back to auto height. IE7 gets extra hand-holding.
 */
AJS.toInit(function($) {
    var sidebar = $("#personal-info-sidebar");

    if(sidebar.length) {
        var page = $("#page"),
            content = $("#content"),
            sidebarControl = $("#personal-info-sidebar .sidebar-collapse"),
            contentContainer = $("#full-height-container"),
            wikiContent = $("#content .wiki-content:first"),
            contentContainerHeight = contentContainer.height(),
            contentHeight = content.height();

        var ie7detected = (jQuery.browser.msie && parseInt(jQuery.browser.version) == 7) ? true : false;

        // ie7: set content height at load if the sidebar's open
        if (ie7detected && !content.hasClass("sidebar-collapsed")) {
            wikiContent.css("min-height",sidebar.height());
        }

        // toggled trigger set in personal-sidebar.js (in core). Fires when sidebar opened or closed.
        sidebar.bind("toggled", function() {
            // force page to stay tall enough to cope with sidebar
            if ($("#main").hasClass("has-personal-sidebar")) {
                if (content.hasClass("sidebar-collapsed")) {
                    page.height(contentContainerHeight);
                    content.height(contentHeight);
                    sidebarControl.height(sidebar.height());
                    if (ie7detected) {
                        wikiContent.css("min-height","auto"); // ie7 needs a nudge
                    }
                } else {
                    //page.height("auto");
                    content.height("auto");
                    if (ie7detected) {
                        wikiContent.css("min-height",sidebar.height()); // ie7 needs a nudge
                    }
                }
            }
        });
    }
});
