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

var contentScrollRef	= '#content .scroll_wrap';
var containerRef		= '#right';

$(window).resize(function() {onPageResize();});

function onPageResize()
{
	processPage();
}

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

function processPage()
{
	//set defaults
	dimensionsSet = false;
	containerTopCss = 'padding-top';
	contentScrollRef = '#content .scroll_wrap';
	$(containerRef).css('padding-top', '').css('margin-top', '');
	
	//set the heights of content, and parent
	processHeights();
	
	//chekc to see if scrollbar is needed
	isScrollbarNeeded();
}

function processHeights()
{
	if(!dimensionsSet)
	{
		//get container height
		containerHeight = $(containerRef).outerHeight();	
		
		//get content height
		contentHeight = setContentHeight(contentScrollRef);
		
		debug(resizeDebug, "containerRef: "+containerRef+", containerHeight: "+containerHeight+" , contentHeight: "+contentHeight);
	}
}

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

function isScrollbarNeeded()
{
	if(contentHeight != null && containerHeight != null)
	{
		if(contentHeight > containerHeight)
		{
			//scrollbar needed
			toggleScrollBars(true);
		}
		else
		{
			//scrollbar not needed
			toggleScrollBars(false);
			centerContent();
		}
	}
}

function toggleScrollBars(b)
{
	if(b)
	{
		$(contentScrollRef).height(containerHeight);
	
		//add scrollbar
		var scrollConfig = {showArrows:false, verticalGutter:10};
		if($('#setmobile a').size() > 0 )
		{
			scrollConfig.showArrows = true;
		}
		$(contentScrollRef).jScrollPane(scrollConfig);
		//debug(resizeDebug, "add scrollbar");
	}
	else
	{
		if($(contentScrollRef+' .jspContainer') != null && $(contentScrollRef+' .jspContainer').size() > 0 && $(contentScrollRef).data != null && $(contentScrollRef).data('jsp') != null)
		{
			var html = $(contentScrollRef+' .jspPane').html();
			contentHeight = setContentHeight(contentScrollRef);
			$(contentScrollRef).parent().html('<div style="overflow:hidden;" data-ah="'+contentHeight+'" class="scroll_wrap">'+html+'</div>');
			//debug(resizeDebug, "remove scrollbar");
		}
	}
}

function centerContent()
{
	var contentPaddingTop = Math.round((containerHeight - contentHeight)/2);
	$('#content').css('padding-top', '').css('margin-top', '');
	$('#content').css(containerTopCss, contentPaddingTop);
}
