/**
 * filler.js - Extend and fill the vertical height of the #contents Div to fill
 * the current window height. If the #contents Div has too little content to
 * fill the window, it is expanded until it does. If the current size of #contents
 * overflows the window it is left as is.
 *
 * @version 1.0
 * @author Jeff Furgal
 * @copyright (c) 2008 by Jeff Furgal and Image Pros, Inc.
 *
 * @license No part of this work may be used or reproduced without the written
 * permission of the copyright holder.
 *
 */

var originalWindowHeight = 0;
var previousWindowHeight = 0;
var originalContentsHeight = 0;
var originalWrapperHeight = 0;
var originalWrapperBottom = 0;
var originalFooterBottom = 0;
var minimumContentsSize = 0;
var minimumWrapperSize = 0;
var headerHeight = 0;

function resizeContents() {
//    debugger;
	var windowHeight = $(window).height();
    var wrapper = $("#wrapper");
    var wrapperHeight = wrapper.height();
//    console.log("Initial wrapper height = " + wrapperHeight);
    var wrapperBottom = wrapper.position().top + wrapperHeight;
//    console.log("Initial wrapper bottom = " + wrapperBottom);

    var footer = $("#footer");
    var footerHeight = footer.height();
//    console.log("Initial footer height = " + footerHeight);
//    console.log("Footer offset top = " + footer.position().top);
    var footerBottom = footer.position().top + footerHeight;
//    console.log("Initial footer bottom = " + footerBottom);

    var contentsHeight = $("#contents").height();
//    console.log("Initial contents height = " + contentsHeight);
	var desiredHeight = 0;

	if (originalWindowHeight === 0) {
		originalWindowHeight = windowHeight;
        originalContentsHeight = contentsHeight;
        originalWrapperHeight = wrapperHeight;
        originalWrapperBottom = wrapperBottom;
        originalFooterBottom = footerBottom;
        headerHeight = $("#header").height();
		minimumContentsSize = Math.max($("#navColumn").height(),
								       $("#content").height());
		minimumWrapperSize = headerHeight + minimumContentsSize + footerHeight;
	}

//    console.log("Current window height = " + windowHeight);
//    console.log("Original window height = " + originalWindowHeight);
//    console.log("Original contents height = " + originalContentsHeight);
//    console.log("Original wrapper height = " + originalWrapperHeight);
//    console.log("Original wrapper bottom = " + originalWrapperBottom);
//    console.log("Original footer bottom = " + originalFooterBottom);

	if (windowHeight <= minimumWrapperSize) {
		desiredHeight = minimumContentsSize;
        desiredHeight++; // Expand to reach the footer top.
	} else if (windowHeight < previousWindowHeight) {
		var windowSizeDiff = previousWindowHeight - windowHeight;

        contentsHeight = minimumContentsSize;
        wrapperBottom = windowHeight;
        footerBottom -= windowSizeDiff;
        desiredHeight = Math.max(((footerBottom - footerHeight) - headerHeight),
                                  minimumContentsSize);
    } else if (footerBottom <= wrapperBottom) {
        desiredHeight = contentsHeight + (wrapperBottom - footerBottom);
//        console.log("Desired contents height = " + desiredHeight);
    }

	if (desiredHeight > 0) {
        $("#contents").height(desiredHeight);
	}

	// Save for the next event.
    previousWindowHeight = windowHeight;
//    var contents = $("#contents");
//    contentsHeight = contents.height();
//    console.log("Final contents height = " + contentsHeight);
}
