﻿/**
 * Gets the offset height of an element
 * 
 * See the following URL for more information:
 * http://developer.mozilla.org/en/docs/DOM:element.offsetHeight   
 * 
 * @param   id    Id of the element investigated for offset height.
 * @return        The offset height of the element, or NULL if not found
 */ 

function getOffsetHeight(id) {
	if(document.getElementById(id)!=null) {
		var div = document.getElementById(id);
		return div.offsetHeight;
	}
	else {
		return null;
	}
}

/**
 * Get the height of the viewport
 * First used in http://www.clacs.uiuc.edu/ 
 * 
 * @return        The height of the viewport
 */ 

function getWindowHeight() {
	var windowHeight = 0;
	if (typeof(window.innerHeight) == 'number') {
		windowHeight = window.innerHeight;
	}
	else {
		if (document.documentElement && document.documentElement.clientHeight) {
			windowHeight = document.documentElement.clientHeight;
		}
	else {
		if (document.body && document.body.clientHeight) {
			windowHeight = document.body.clientHeight;
		}
	}
}
return windowHeight;	
}

/**
 * adjusts the layout according to formulas defined below
 * 
 * May be adjusted to the user's liking
 * 
 * @return        void
 */     

function adjustLayout() {
	//Obtain DIVS for height manipulation
	var divContent = document.getElementById("content");
	var divFooter = document.getElementById("footer");
	var divBody = document.getElementById("home");
	
	//Obtain heights for DIV and WINDOW
	var quicklinks = getOffsetHeight("quick-links");
	var title = getOffsetHeight("title-wrapper");
	var nav = getOffsetHeight("nav");
	var content = getOffsetHeight("content");
	var footer = getOffsetHeight("footer");
	var windowHeight = getWindowHeight();
	
	var idealContentHeight = windowHeight - quicklinks - title - nav - footer - 69;
	
	if(idealContentHeight>0) {
		if(content<idealContentHeight) {
			divContent.style.height = idealContentHeight + 'px';
		}
	}
}

//Executes the function when the document is loaded
$(window).resize(function(){
	adjustLayout();
});
$(window).load(function(){
	adjustLayout();
});
