
eltops = new Array();

function positionFooterOn(element) {
	var footerHeight 	= document.getElementById("footer").offsetHeight;
	var footerOffset 	= 25;
	
	if (document.getElementById("primaryContent"))
		$("primaryContent").style.height = '';

	if (document.getElementById("secondaryContent"))
		$("secondaryContent").style.height = '';

	h = elementHeight(element) + elementTop(element);

	if (document.getElementById("footer"))
		$("footer").style.top = h + "px";

	if (document.getElementById("primaryContent"))
		$("primaryContent").style.height =  (h - elementTop("primaryContent") /*- footerHeight*/) + "px";

	if (document.getElementById("secondaryContent"))
		$("secondaryContent").style.height =  (h - elementTop("secondaryContent")/* - footerHeight*/) + "px";

	if (document.getElementById("sideBar"))
		$("sideBar").style.height =  h + "px";
}

function positionFooter() {
	var footerHeight = document.getElementById("footer").offsetHeight;
	var footerOffset = 25;
	
	if (document.getElementById("primaryContent"))
		$("primaryContent").style.height = '';

	if (document.getElementById("secondaryContent"))
		$("secondaryContent").style.height = '';

	if (document.getElementById("sideBar"))
		$("sideBar").style.height = '';

	if(window.footerpos) {
		h = footerpos;
	} else {
		var bodyHeight = document.body.offsetHeight;
		
		var divs = $A(document.getElementsByTagName('div'));
	
		
		var h = 0;
		
		eh = new Array();
		divs.each(function(e){eh[eh.length] = elementHeight(e);});
		h = eh.max();
		
		h = (h + footerHeight < bodyHeight && bodyHeight - (h+footerHeight) > footerOffset) ? bodyHeight - footerHeight - footerOffset : h;
		//if(navigator.userAgent.indexOf('IE') != -1) h-=footerOffset;
	}

	if (document.getElementById("footer"))
		$("footer").style.top = h + "px";

	if (document.getElementById("hud")) {
		$("hud").style.top = (h + 30) + "px";
		$("hud").style.position = 'absolute';
	}

	if (document.getElementById("primaryContent"))
		$("primaryContent").style.height =  (h - elementTop("primaryContent") /* - footerHeight */) + "px";

	if (document.getElementById("secondaryContent"))
		$("secondaryContent").style.height =  (h - elementTop("secondaryContent") /* - footerHeight*/) + "px";

	if (document.getElementById("sideBar")) {
		var newHeight = 0;

		if (navigator.userAgent.indexOf('IE') != -1)
			newHeight = h - elementTop("sideBar");
		else
			newHeight = h - elementTop("sideBar") - 136;

		$("sideBar").style.height = newHeight + "px";
	}
}

function elementHeight(element) {
	if(!$(element)) return 0;
	if($(element).id == 'footer') return 0;
	el = $(element);
	h = el.offsetHeight
	//if(eltops[element]) return h + eltops[element]; 
	t = el.offsetTop
	while(el.parentElement) {
		el = el.parentElement
		t += el.offsetTop
	} ;
	//eltops[element] = t
	return h + t;
}

function elementTop(element) {
	if(!$(element)) return 0;
	//if(eltops[element]) return eltops[element]; 
	el = $(element);
	h = el.offsetTop
	//if(navigator.userAgent.indexOf('IE') != -1) return h;
	while(el.parentElement) {
		el = el.parentElement
		h += el.offsetTop
	} ;
	//eltops[element] = h; 
	return h;
}

function positionFooterDelay() {
	window.setTimeout("positionFooter()", 1000);
}

Event.observe(window, 'load', positionFooter);
Event.observe(window, 'resize', positionFooter);

