﻿// by Paul@YellowPencil.com and Scott@YellowPencil.com
// see also: http://www.paulbellows.com/getsmart/balance_columns/

function setTall() {
	if (document.getElementById) {
		// the divs array contains references to each column's div element
		if(document.getElementById('thirdColumn'))
		{
    		var divs = new Array(document.getElementById('firstColumn'), document.getElementById('secondColumn'), document.getElementById('thirdColumn'));
		}
		else
		{
    		var divs = new Array(document.getElementById('firstColumn'), document.getElementById('secondColumn'));
		}
		
		// determine the maximum height out of all columns specified
		var maxHeight = 0;
		for (var i = 0; i < divs.length; i++)
		{
			if (divs[i].offsetHeight > maxHeight) maxHeight = divs[i].offsetHeight;
		}
		if(document.documentElement.offsetHeight > maxHeight)
		{
		    maxHeight = document.documentElement.offsetHeight;
		}
		
		// set all columns to maximum height
		for (var i = 0; i < divs.length; i++)
		{
			divs[i].style.height = maxHeight + 'px';

			// if the browser's working in standards-compliant mode, the height property
			// sets the height excluding padding, so we figure the padding out by subtracting the
			// old maxHeight from the new offsetHeight, and compensate!
			if (divs[i].offsetHeight > maxHeight) {
				divs[i].style.height = (maxHeight - (divs[i].offsetHeight - maxHeight)) + 'px';
			}
		}

		if(document.getElementById('footerArea'))
		{
		    var _footer = document.getElementById('footerArea');
            var _footerTop = maxHeight;
            // document.documentElement.scrollHeight is better!
            if(document.documentElement.scrollHeight > _footerTop)
            {
                _footerTop = document.documentElement.scrollHeight;
            }
            var _offsetHeight = document.documentElement.offsetHeight - 10; // minus footerHeight;
            if(_offsetHeight > _footerTop)
            {
                _footerTop = _offsetHeight;
            }
            _footer.style.top = _footerTop + 'px';
            if(document.all) // does NOT work for Firefox (yet).
            {
                _footer.style.display = "block";
            }
		}
	}
}

window.onload = function() {
	setTall();
}

window.onresize = function() {
	setTall();
}

