$(document).ready(function() {
	//adjust_main_height();
	adjust_wrapper_height();
	// equalise main column heights, then amend appropriate styles
	// this function must run *after* any other resizing function, insertion of dynamic content, etc.
	$("#nav, #main, #sidebar").equalizeCols();
});

$(window).resize(function() {
	//adjust_main_height();
	adjust_wrapper_height();
	// equalise main column heights, then amend appropriate styles
	// this function must run *after* any other resizing function, insertion of dynamic content, etc.
	$("#nav, #main, #sidebar").equalizeCols();
});

// #main div must stretch to fill container height when either sidebar is taller
function adjust_main_height() {
	var $mainheight		= $("#main").height();
	var $navheight			= $("#nav").height();
	var $sideheight		= $("#sidebar").height();
	if (($mainheight > $navheight) && ($mainheight > $sideheight)) {	return; }
	var $sidebarheight	= ($navheight > $sideheight) ? $navheight : $sideheight;
	$("#main").css("height", $sidebarheight + "px");
}

function adjust_wrapper_height() {
	var $viewportheight		= $(window).height();
	var $heightadjustment	= 41;

	var $translationheight	= $('#translation').height();
	var $mastheadheight		= $('#masthead').height();
	var $bodywrapheight		= $('#bodywrap').height();
	var $footerheight			= 40;
	//var $footerheight			= $('#footer').height();
	var $contentheight		= $translationheight + $mastheadheight + $bodywrapheight + $footerheight + $heightadjustment;

	// debug
	var $msg					  = '<strong>viewportheight: '		+ $viewportheight + 'px</strong><br />';
	$msg						  += 'translationheight: '	+ $translationheight + 'px<br />';
	$msg						  += 'mastheadheight: '		+ $mastheadheight + 'px<br />';
	$msg						  += 'bodywrapheight: '		+ $bodywrapheight + 'px<br />';
	$msg						  += 'footerheight: '		+ $footerheight + 'px<br />';
	$msg						  += '<strong>contentheight: '		+ $contentheight + 'px</strong><br />';

	if ($contentheight > $viewportheight) {
		$msg	+= '<div style="color: red"><strong>no adjustment</strong></div>';
		display_debug($msg);
		return;
	}
	//	var $bodyheight	= $('#translation').height() + $('#masthead').height() + $('#bodywrap').height();
	// alert($translationheight + ', ' + $mastheadheight + ', ' + $bodywrapheight);
	//$("#outerwrap").css("height", $viewportheight + $heightadjustment);

	$("#footer").css("height", $viewportheight - $translationheight - $mastheadheight - $bodywrapheight - $heightadjustment);
	$msg	+= '<div style="color: red"><strong>footer resized to ' + $('#footer').height() + 'px</strong></div>';
	display_debug($msg);
}

function display_debug($message) {
	$("#jquery-debug").remove();
	//$('<div id="jquery-debug" style="position: absolute; top: 0; left: 0; z-index: 1000; background-color: #ffa; width: 250px; padding: 4px; text-align: left; font: 11px/1.1 \'Trebuchet MS\';">' + $message  + '</div>').prependTo('body');
}

