function getElement(elementName)
{
	return document.getElementById ? document.getElementById(elementName) : document.all ? document.all[elementName] : null;
}

function getWindowHeight()
{
	// Calculate window height
	var windowHeight = 0;
	if (typeof(window.innerHeight) == 'number')
		{
		//Non-IE
		windowHeight = window.innerHeight;
		}
	else if (document.documentElement && document.documentElement.offsetHeight)
		{
		//IE 6+ in 'standards compliant mode'
		windowHeight = document.documentElement.offsetHeight;
		}
	else if (document.body && document.body.offsetHeight)
		{
		//IE 4 compatible
		windowHeight = document.body.offsetHeight;
		}
	
	return windowHeight;
}

function getPageHeight()
{
	var windowHeight = getWindowHeight();
	var htmlHeight = 0;
	var scrollHeight = document.body.scrollHeight;
	
	var offsetHeight = document.body.offsetHeight;
	if (scrollHeight > offsetHeight)
		htmlHeight = scrollHeight;
	else
		htmlHeight = offsetHeight;
		
	if (htmlHeight < windowHeight)
		return windowHeight;
	else
		return htmlHeight;
}

function getElementHeight(elementName)
{
	var height = 0;
	var element = getElement(elementName);
	if (element)
		height = element.offsetHeight;
	return height;
}

function onInitCurve()
{
	// Set up resize handling
	window.onresize = onResizeCurve;

	// id of element to check for and insert control
	TextResizeDetector.TARGET_ELEMENT_ID = 'page_content';
	// function to call once TextResizeDetector has init'd
	TextResizeDetector.USER_INIT_FUNC = onInitTRD;

	// Temporarily fade content out
	var contentBody = getElement("content_body");
	if (contentBody)
		fadeElement(contentBody, 0, 0, -1);
	
	// Handle initial window size
	onResizeCurve();
	
	// Fade content in
	if (contentBody)
		fadeElement(contentBody, 0, 100, 1);
}

function fadeElement(element, currentAlpha, finalAlpha, direction)
{
	if ((direction > 0 && currentAlpha <= finalAlpha) ||
		(direction < 0 && currentAlpha >= finalAlpha))
		{
		element.style.opacity = (currentAlpha / 100);
		element.style.MozOpacity = (currentAlpha / 100);
		element.style.KhtmlOpacity = (currentAlpha / 100);

		currentAlpha += direction * 20;
		
		setTimeout(function() { fadeElement(element, currentAlpha, finalAlpha, direction);}, 25);
		}
}


function onInitTRD()
{
	TextResizeDetector.addEventListener(onCalcCurve, null);
}

function onResizeCurve()
{
	// Execute twice to allow curve resizing to be taken into account
	// when repositioning footer
	onCalcCurve();
	onCalcCurve();
}

function onCalcCurve()
{
	var footer = getElement("footer");
	if (footer)
		footer.style.marginTop = "0px";
	var contentBody = getElement("content_body");
	if (contentBody)
		contentBody.style.marginTop = "0px";

	// Get element height measurements
	var headerHeight	=	getElementHeight("header");
	var contBodyHeight	=	getElementHeight("content_body");
	var footerHeight	=	getElementHeight("footer");
	var elemsHeight		=	headerHeight + contBodyHeight + footerHeight;

	// 535 = Combined height of top and bottom curves
	var contentHeight =	Math.max(elemsHeight + 150, Math.max(getWindowHeight(), 535))

	var midCurve = getElement("midcurve");
	if (midCurve)
		midCurve.style.height = Math.max(contentHeight - 535, 0) + "px";

	var menuContent = getElement("menu_content");
	if (menuContent)
		menuContent.style.height = contentHeight+ "px";

	var menu = getElement("menu");
	if (menu)
		menu.style.top = (0 - (menu.offsetHeight / 2)) + "px";

	// Pad margin so main body content is vertically centered and footer is pushed to bottom of page
	if (footer && contentBody)
		{
		if (contentHeight > elemsHeight)
			{
			if (contentBody)
				{
				var marginHeight = Math.round((contentHeight - elemsHeight) / 2) - 1;
				contentBody.style.marginTop = marginHeight + "px";
				footer.style.marginTop = marginHeight + "px";
				}
			else
				footer.style.marginTop = (contentHeight - elemsHeight) + "px";
			}
		}
}
