﻿///<reference path="S:/SupportFiles/jquery-1.2.6-vsdoc.js" />

var heightmanager =
{
    /// properties ///
    enabled: true,

    overruled: false,

    heights: {
        documenttype: null,
        menu: null,
        searchbar: null,
        leftmenubottom: null,
        rightmargin: null
    },

    // to disable resizing of certain elements
    // set their value to false
    elements: {
        documenttype: true,
        menu: true,
        searchbar: true,
        leftmenubottom: true,
        rightmargin: true
    },
    /// callbacks ///

    onbeforesetheight: null,

    aftersetheight: null,

    /// functions ///

    setHeights: function() {
        if (typeof (heightmanager.onbeforesetheight) == "function")
            heightmanager.onbeforesetheight.call();

        /* if popup, no resize, otherwise the contentcontainer resizes to 0 */
        if (heightmanager.enabled && $('#popup').length == 0) {

            var maxHeight = heightmanager.getMaxHeightInfo(["menu", "documenttype"]);

            if (maxHeight.element == "documenttype") {
                $('#menu').height(maxHeight.height);
                $('#documenttype').height(maxHeight.height);
            } else {
                /*var searchBarHeight = $('#searchbar').height();*/
                $('#documenttype').height(maxHeight.height);
                $('#menu').height(maxHeight.height);
            }

            /* Fix menu after resize */
            $('#leftmenubottom').height(maxHeight.height - ($('#leftmenutop').height() + $('#mijnpagina').height()));

            /* resize to the height of the documenttype - the searchbar */
            var newHeightContent = maxHeight.height - $('#searchbar').height();
            $('#rightmargin').height(newHeightContent);
            $('#contentcontainer').height(newHeightContent);
        }

        if (typeof (heightmanager.aftersetheight) == "function")
            heightmanager.aftersetheight.call();
    },

    setHeightsAfterResize: function() {
        $('#menu').height("auto");
        $('#documenttype').height("auto");
        $('#leftmenubottom').height("auto");
        $('#rightmargin').height("auto");
        $('#contentcontainer').height("auto");
        heightmanager.setHeights();
    },

    // call the function:fn before setheight
    // a.t.m. only one callback is possible
    registerOnBeforeSetHeightCallBack: function(fn) {
        if (typeof (fn) == "function") {
            heightmanager.overruled = true;
            heightmanager.onbeforesetheight = fn;
        }
    },

    // call the function:fn after setheight
    // a.t.m. only one callback is possible
    registerAfterSetHeightCallBack: function(fn) {
        if (typeof (fn) == "function") {
            heightmanager.overruled = true;
            heightmanager.aftersetheight = fn;
        }
    },

    getMaxHeightInfo: function(elementIds) {
        ///	<summary>
        ///		Gets the maximal height of an array with object, returns a object with height and element (name of element)
        ///	</summary>
        ///	<param name="elementIds" type="array">
        ///		An array with elements of values
        ///	</param>
        ///	<returns type="Object" />
        var maxHeight = 0;
        var elementId = null;

        $(elementIds).each(function() {
            var totalHeight = 0;
            if (isNaN(this)) {
                totalHeight = heightmanager.getElementHeight($("#" + this));
            } else {
                totalHeight = this;
            }
            if (totalHeight > maxHeight) {
                maxHeight = totalHeight;
                elementId = this;
            }
        });

        return {
            height: maxHeight,
            element: elementId
        };
    },

    getElementHeight: function(element) {
        ///	<summary>
        ///		Gets the height of an element. If the element height = 0, than we take the height of the children (recursive)
        ///	</summary>
        ///	<param name="element" type="jQuery">
        ///		The element
        ///	</param>
        ///	<returns type="Integer" />
        var elementHeight = $(element).height();
        if (elementHeight <= 0) {
            elementHeight = 0;
            $(element).children().each(function() {
                elementHeight += getElementHeight(this);
            });
        }
        return elementHeight;
    }
}

$(document).ready(function() {
    $(window).load(function() {
        /* remove border from searchbox, when there is no rightmargin */
        if ($('#rightmargin').length > 0) {
            $('#searchbox').css('border-left', 'solid 1px #CCCCCC');
        }
        heightmanager.setHeights();
        siteIframes.registerAfterResizeEvents(heightmanager.setHeightsAfterResize);
    });
});

