/**
 * Find the height of the window. Should work for all modern browsers.
 * Code adpated from http://www.alistapart.com/articles/footers.
 * 
 * @package Javascripts
 * @author M Gleeson
 * @return void
 */
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;
}

/**
 * SetFooter - reposition the footer to sit at the bottom of the window.
 * Code adpated from http://www.alistapart.com/articles/footers.
 * 
 * @package Javascripts
 * @author M Gleeson
 * @return void
 */
function setFooter() {
	
	var totalHeight = 0; // contains the total height of the page content (main and footer divs)
	
	if (document.getElementById) {
		
		// get the height of the window
		var windowHeight = getWindowHeight();
		
		if (windowHeight > 0) {
			
			// get the height of the main div which contains the header and page content
			var mainElement = document.getElementById('main');
			var mainHeight = mainElement.offsetHeight;
			
			// get the height of the footer div
			var footerElement = document.getElementById('footer');
			var footerHeight = footerElement.offsetHeight;
			
			// get the top and bottom margins of the body
			var marginTop = getBodyTopMargin();
			var marginBottom = getBodyBottomMargin();
			marginTop = parseInt(marginTop.replace('px', ''));
			marginBottom = parseInt(marginBottom.replace('px', ''));
					
			// check if the footer is positioned within or outside the main div
			var isMainParent = getParenID(footerElement) == 'main';
			
			// calculate total height of page content
			if (isMainParent){
				
				totalHeight = mainHeight;
				
				// find the total height of the main div + any margins for body, used to check if it
				// is greater than the window height.	
				var totalHeightNeeded = mainHeight + marginTop + marginBottom;
							
			}else{
				
				totalHeight = mainHeight + footerHeight;
				
				// find the total height of the main div + any margins for body, used to check if it
				// is greater than the window height.
				var totalHeightNeeded = totalHeight + marginTop + marginBottom;
			}
			
			/** 
			 * if the footer and main div is smaller than the window reposition the
			 * footer so that it sits at the bottom of the window. If the footer and
			 * main div is greater than the height of the window, don't do anthing as
			 * the footer will naturally flow after the main div.
			 */
			if (windowHeight - totalHeightNeeded >= 0) {
				
				var topPosition = (windowHeight - totalHeight);
				
				/* 
				 * If the footer is within the parent, then need to resize the height 
				 * of the main div so that it still encapsulates the footer div and then
				 * re-position the footer by giving it a top margin.
				 */
				if(isMainParent){
					
					var newHeight = (windowHeight - marginTop - marginBottom);
					var mainHeightDiff = (newHeight-mainHeight);

					/* 
					 * Find the new top pos of the footer based on the old top pos and add the difference 
					 * in height between the new main div and the old div. 
					 */
			
					var footerTopMarg = parseInt(mainHeightDiff);
					
					// change the footer position so it sits at the bottom of the new re-drawn main div
					footerElement.style.position = 'relative';
				
					// if IE, need to add extra 39px to the margin-top as it treats the 
					// margin-top size differently to other browsers (i havent found the reason why yet)
					var browser = navigator.userAgent.toString();
					var isMSIE = ( browser != null && browser.indexOf( "MSIE" ) != -1 );
					
					if(isMSIE)
					{
						footerElement.style.marginTop = (footerTopMarg+39)+'px';
					}
					else
					{
						footerElement.style.marginTop = footerTopMarg+'px';
					}
					
				}else{
				
					footerElement.style.position = 'relative';
					footerElement.style.top = topPosition+'px';
				}
			}
		}
	}
}

// set the footer to site at bottom of the page when loading
//window.onload = function() {
//  setFooter();
//}

// function to get the id of the parent div of a given div
function getParenID(ref){
	
	ref = ref.parentNode;
	parentID = null;
	
	if (ref.nodeType==1){ //check that the node is a tag, not text (type=3)
		
		if (String(ref.nodeName)=="DIV"){
		
			parentID = ref.id;
		}
	}
	
	return parentID;
}

// get the margin-top css property of the body element
function getBodyTopMargin(){
	
	var marginTop = null;
	
	// IE method of getting margin-top property of body element
	if (document.body.currentStyle){
		
		marginTop = document.body.currentStyle.marginTop;
	}
	
	// web-standards method of getting margin-top property of body element
	else if (document.defaultView){
	
		marginTop = document.defaultView.getComputedStyle(document.body,"").marginTop;
	}
	
	return marginTop;
}

// get the margin-bottom css property of the body element
function getBodyBottomMargin(){
	
	var marginBottom = null;
	
	// IE method of getting margin-bottom property of body element
	if (document.body.currentStyle){
		
		marginBottom = document.body.currentStyle.marginBottom;
	}
	
	// web-standards method of getting margin-bottom property of body element
	else if (document.defaultView){
	
		marginBottom = document.defaultView.getComputedStyle(document.body,"").marginBottom;
	}
	
	return marginBottom;
}