﻿/* This script is used to strech the height of layout divs to their parent container's height */
; (function($) {

    $.equalize = function(what) { equalize(what); };
    $.stretch = function(what) { stretch(what); };
    $.stretchSub = function(which, sub) { stretchSub(which, sub); };

    function equalize(what) {
        tallest = 0;
        $(this).each(function() {
            what.each(function() {
                thisHeight = $(this).height();
                if (thisHeight > tallest) {
                    tallest = thisHeight;
                }
            });
            //what.height(tallest);
            what.css({ 'min-height': tallest });
        });
    }
    function stretch(what) {
        what.each(function() {
            var offset = 0;
            container = $(this).parent();
            //alert('container id = ' + container.attr('id'));
            offset = $(this).attr('offsetTop');
            //alert('offset = ' + offset);
            containerHeight = container.height();
            //alert('containerHeight = ' + containerHeight);
            //alert('element id = ' + $(this).attr('id'));
            curHeight = $(this).height();
            //alert('curHeight = ' + curHeight);
            newHeight = containerHeight - offset;
            /*alert('Function: stretch() \n\n' + 'container id = ' + container.attr('id')
            + '\n offset = ' + offset
            + '\n containerHeight = ' + containerHeight
            + '\n element id = ' + $(this).attr('id')
            + '\n curHeight = ' + curHeight
            + '\n newHeight = ' + newHeight);*/
            if (curHeight < containerHeight) {
                $(this).css({ 'min-height': newHeight });
            }
        });
    }
    function stretchSub(which, sub) {
        //alert("which = " + which.attr('id') + "\n sub = " + sub.attr('id'));
        var childrenHeight = 0;
        var count = which.children("div").size() - 1;
        var pad = count * 5;
        which.children("div").each((function() {
            childHeight = $(this).height();
            //alert("element " + $(this).attr('id') + " height  = " + childHeight);
            childrenHeight += childHeight;
            //alert("childrenHeight = " + childrenHeight);
        }));
        parentHeight = which.height();
        currentHeight = sub.height();
        diff = parentHeight - childrenHeight - pad;
        newHeight = currentHeight + diff;
        /*alert('Function: stretchSub() \n\n' + 'element id = ' + sub.attr('id')
        + '\n currentHeight = ' + currentHeight
        + '\n parent id = ' + which.attr('id')
        + '\n count = ' + count
        + '\n parentHeight = ' + parentHeight
        + '\n childrenHeight = ' + childrenHeight
        + '\n pad = ' + pad
        + '\n diff = ' + diff
        + '\n newHeight = ' + newHeight);*/
        sub.css({ 'min-height': newHeight });
    }

})(jQuery);