/**
 * The page design has three or four columns.
 * The last two columns must always span the entire height of the page.
 * The footer of the page is positioned under the left two columns and
 * vertically aligned with the bottom of the page.
 * 
 * This function makes sure the right most two columns always reach
 * the bottom of the page and positions the footer on the bottom.
 * 
 * The function is called a number of times over a timespan of 5 seconds.
 * This is so we're able to account for asynchronous loading of images;
 * the window.onload event is fired before images are loaded/rendered.
 */
var ajustHeightsTimer = null,
	ajustHeightsStartTime = 0,
	ajustHeightsBodyHeight = 0;

function calcHeights() {
	
	// Only start the timer if it is not already running
	if ( ajustHeightsTimer == null ) {
		
		ajustHeightsStartTime = new Date();
		ajustHeightsStartTime = ajustHeightsStartTime.getTime();
		
		ajustHeightsTimer = setInterval( "ajustHeights()", 25 );
	}
}

function ajustHeights() {
	
	// If the timer is active; check if we're allowed to execute
	if ( ajustHeightsTimer != null ) {
		
		var now = new Date();
		now = now.getTime();
		if ( now > ajustHeightsStartTime + 5000 ) {
			
			clearTimeout( ajustHeightsTimer );
			ajustHeightsTimer = null;
			return;
		}
	}
	
	// Stop if the height of the body hasn't changed from the last time we checked
	if ( document.body.offsetHeight == ajustHeightsBodyHeight ) {
		
		return;
	}
	else {
		
		ajustHeightsBodyHeight = document.body.offsetHeight;
	}
	
	
	var bgImageHeight = 900,		// The height of the background image (min page height)
		footerMarginBottom = 10;	// The bottom margin for the footer
		
	var k1 = document.getElementById( "kolom1" ),
		k2 = document.getElementById( "kolom2" ),
		k12 = document.getElementById( "kolom1-2" ),
		k3 = document.getElementById( "kolom3" ),
		k4 = document.getElementById( "kolom4" ),
		foot = document.getElementById( "footer" );
	
	var k12Height = 0,
		minHeight = 0,
		colHeight = 0;
	
	// Get the minimum height; the height of the background image minus the bottom of the menu
	minHeight = bgImageHeight - k3.offsetTop;
	
	// Get the height of columns 1 or 2 (the highest) or col12 when it spans both 1 and 2
	if ( k12 == undefined ) {
		
		k12Height = Math.max( k1.offsetHeight, k2.offsetHeight );
	}
	else {
		
		k12Height = k12.offsetHeight;
	}
	// Allow for room to fit the footer
	k12Height += foot.offsetHeight + footerMarginBottom * 2;
	
	// Get the height of the columns
	colHeight = Math.max( k12Height, k3.offsetHeight, k4.offsetHeight, minHeight );
	
	// Make sure both columns are set to the max height
	k3.style.height = colHeight + "px";
	k4.style.height = colHeight + "px";
	
	// Align the bottom of the footer with the bottom of the page
	foot.style.top = k3.offsetTop + k3.offsetHeight - foot.offsetHeight - footerMarginBottom + "px";
}

