//DESCRIBES VARIABLES
var containerHeight 	= 0;
var contentHeight 		= 0;

var containerMargin		= 0;
var contentPadding		= 0;

var contentRef			= '#content';
var contentScrollRef	= '#content';

var jsScroll			= false;

function onPageResize()
{
	processPage();
}

//when new data is recieved and ready to display
function onPageRefreshed()
{
	removeActualHeight($(contentRef));
	onPageResize();
}

function processPage()
{
	//set defaults
	dimensionsSet = false;
	
	contentScrollRef = '#content .text';
	if($('#content .right').size() > 0)
	{
		contentScrollRef = '#content .right';
	}
	else if($('#content .full').size() > 0)
	{
		contentScrollRef = '#content .full .listholder';
	}
	
	//set the heights of content, and parent
	processHeights();
	
	//chekc to see if scrollbar is needed
	isScrollbarNeeded();
	
	processBackgroundsPosition();
}

function processHeights()
{
	if(!dimensionsSet)
	{
		containerMargin 	= pixelToNum($('#sitemap').css('margin-bottom'))*2 + $('#sitemap').height() + $('#footer').height();
		contentPadding 		= pixelToNum($('#box').css('padding-bottom')) + pixelToNum($('#box').css('padding-top'));
		
		//get container height
		containerHeight 	= $(window).height();
		if(containerHeight < 810)
		{
			containerHeight = 810;
		}
		
		//get content height
		contentHeight 		= setContentHeight(contentRef);
	}
}

function setContentHeight(selector)
{
	var item = $(selector);
	ch = getActualHeight(item);
	if(!ch)
	{
		ch = item.height();
		setActualHeight(item, ch);
	}
	return ch;
}

function isScrollbarNeeded()
{
	//if(!$('body').hasClass('mobile'))
	{
		if(contentHeight != null && containerHeight != null)
		{
			debug(resizeDebug, "contentHeight: "+contentHeight+" , containerHeight: "+(containerHeight-containerMargin));
			//console.log("contentHeight: "+contentHeight+" , containerHeight: "+(containerHeight)+", containerMargin:"+containerMargin);
			
			var chm = 0;
			if(contentHeight > 450)
			{
				chm = containerMargin;
			}
			
			if(contentHeight > containerHeight-chm)
			{
				//scrollbar needed
				//console.log("scrollbars");
				toggleScrollBars(true);
			}
			else
			{
				//scrollbar not needed
				//console.log("no scrollbars");
				toggleScrollBars(false);
			}
			
			centerContent();
		}
	}
}

function toggleScrollBars(b)
{
	if(b)
	{
		contentHeight = containerHeight-contentPadding;
		$(contentScrollRef).height(containerHeight-contentPadding-containerMargin);
	
		//add scrollbar
		var scrollConfig = {showArrows:true, verticalGutter:10};
		jsScroll = $(contentScrollRef).jScrollPane(scrollConfig);
		//debug(resizeDebug, "add scrollbar");
	}
	else if($(contentScrollRef+' .jspPane').size() > 0 && jsScroll)
	{
		jsScroll.data().jsp.destroy();
		$(contentScrollRef).attr('style', '');
		jsScroll = false;
	}
}

function centerContent()
{
	var contentMarginTop = Math.round(((containerHeight - contentHeight)/2) - $('#sitemap').height());
	$(contentRef).css('margin-top', '');
	$(contentRef).css('margin-top', contentMarginTop);
}
