function run_autoheight() {
    var windowHeight = $(window).height();
    var globalSubtractHeight = 0; // start with arbitrary 40px
    $('.autoheight_subtract').each(function() {
        globalSubtractHeight += $(this).height();
    });
    $('.autoheight').each(function() {
        var ele = $(this);
        var offsetTop = ele.offset().top;

        if (offsetTop > 10000) {
            // fix for strange IE8 issue where offset().top seems to return eg, "15598" instead of "156"
            offsetTop = Math.round(offsetTop / 100);
        }

        try {
            if (offsetTop == 0) {
                // find a sibling that is visible (since .offset() doesn't work on non-visible elements)
                var siblings = ele.siblings('.autoheight:visible');
                if (siblings[0]) {
                    offsetTop = siblings.offset().top;
                }

                if (!$.browser.msie) { // MSIE doesn't need this.. 
                    // still no offset, try to find first parent with an offset and use that
                    if (offsetTop == 0) {
                        var eleParent = ele.parent();
                        while (eleParent[0] && (offsetTop == 0)) {
                            offsetTop = eleParent.offset().top;
                            eleParent = eleParent.parent();
                        }
                    }
                }
            }
        } catch (ex) {
            offsetTop = 0;
        }

        // find class of "autoheight_subtract_10" or whatever number, and subtract that number
        // also look for "autoheight_minimum_10" or whatever, and make sure that is the minimum
        var subtractHeight = 0;
        var minHeight = 0;
        var classes = $(this).attr('class').split(' ');
        var match;
        for (var i = 0; i < classes.length; i++) {
            if (match = /^autoheight_subtract_(\d+)$/.exec(classes[i])) {
                subtractHeight += parseInt(match[1]);
            }

            if (match = /^autoheight_minimum_(\d+)$/.exec(classes[i])) {
                minHeight = parseInt(match[1]);
            }
        }

        var newHeight = windowHeight /* - globalSubtractHeight */ - subtractHeight - offsetTop;
        if (offsetTop + newHeight >= windowHeight) {
            // prevent from scrolling off the page
            newHeight = windowHeight - offsetTop - 1;
        }

        // enforce minimum height
        if (newHeight < minHeight) newHeight = minHeight;

        // ie fix
        if (newHeight < 0) newHeight = 0;

        ele.css('height', (newHeight) + 'px');
    });
}

//variables used through the script.
var splitterPosition = 205;
var splitterCollapsed = true;
var resize_timer = false;

function doActualResize() {
    run_autoheight();
}


$(document).ready(function() {


    $(window).bind('resize', function() {
        if (resize_timer) clearTimeout(resize_timer);

        resize_timer = setTimeout("doActualResize();", 500);
    });

    doActualResize();


});

/* make sure console.log() is always defined */
if (typeof (console) == "undefined") {
    console = { log: function() { } };
}

