//DESCRIBES VARIABLES
var containerHeight 	= 0;
var contentHeight 		= 0;

var contentScrollRef	= '#body .scroll_wrap';
var dimensionsSet 		= false;
var containerTopCss		= 'padding-top';
var contentScrollMargin	= 0;

//this is currently only for layout 1, 
//where if overflow the image uses the nav space
var imageOverflow 		= false;

//video item ref
var videoPlayerId		= false;

$(window).resize(function() {onPageResize();});

function onPageResize()
{
	processPage();
	onResizeSpecific();
	onReadyThumbCarousel();
}

//when new data is recieved and ready to display
function onPageRefreshed()
{
	videoPlayerId = false;
	removeActualHeight($(contentScrollRef));
	onPageResize();
}

function processPage()
{
	//set defaults
	dimensionsSet = false;
	contentScrollMargin = 0;
	containerTopCss = 'padding-top';
	contentScrollRef = '#body .scroll_wrap';
	$(containerRef).css('padding-top', '').css('margin-top', '');
	
	processBodyCenter();
	
	initialisePage();
	
	//page type resize
	pageTypeResize();
	
	//set the heights of content, and parent
	processHeights();
	
	//chekc to see if scrollbar is needed
	isScrollbarNeeded();
}

//ie copatibility mode
var iecom = false;
function processBodyCenter()
{
	//if body margin is auto
	if($('#body').css('margin-right') == 'auto')
	{
		var bbl = 0;
		var bbo = 0;
		var bo 	= 0;
		
		//get offsets
		if($('#body .body_wrap').size() > 0)
		{
			bbl = pixelToNum($('#body .body_wrap').css('left'));
			bbo = $('#body .body_wrap').offset().left-bbl;
		}
		
		if($('#body').size() > 0)
		{
			bo = $('#body').offset().left;
		}
		
		//if offsets are the same, 
		//therefore the margin:auto is not working
		if(bbo == bo || iecom)
		{
			//therefore its in ie copatibility mode
			if(!iecom)
			{
				iecom = true;
			}
			
			//calculate body wrap margin left
			var bw = $('#body').width();
			var cw = $('#body .body_wrap').width();
			var cp = (bw-cw)/2 - bbl;
			if($('#info').css('margin-left') == 'auto' || iecom == 'both')
			{
				//if info needs to be centered
				iecom = 'both';
				cw = $('#info').width();
				cp = (bw-cw)/2;
				$('#info').css('margin-left', cp);
			}
			
			if($('#body .body_wrap').css('margin-right') == 'auto')
			{
				$('#body .body_wrap').css('margin-left', cp);
			}
			
		}
	}
}

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};
		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);
	$(containerRef).css('padding-top', '').css('margin-top', '');
	$(containerRef).css(containerTopCss, contentPaddingTop);
}

//////////////////////////////
// PAGE TYPE RESIZING
//////////////////////////////

//resize focus gallery image, only on gallery pages
function pageTypeResize()
{
	if(type == "videolist")
	{
		dimensionsSet = true;
		contentScrollRef = '#body .videocontainer';
		containerTopCss	= 'margin-top';
		$(containerRef).css('padding-top', '').css('margin-top', '');
		
		containerHeight = $(containerRef).outerHeight();
		
		resizeVideoGallery($(contentScrollRef+' .video_wrap'));
	}
	else if(type == "gallery")
	{
		resizeImageGallery();
	}
}

function resizeImageGallery()
{
	var img = $('#body .galleryblock .galleryblock_image a img');
	if(img.height() > 0 && img.attr('data-loaded') != undefined)
	{
		//defined actual height and width
		//of image before resize
		var imgh = actualHeight(img);
		var imgw = actualWidth(img);
		
		//determine total dimension
		var cw = $('#body').width();
		var ch = $('#body').height();
		
		//if image not to overflow onto nav 
		if(!imageOverflow)
		{
			//if there is a nav
			var skh = $('#sticky_gallery_nav');
			if(skh.size() > 0)
			{
				ch = ch - (skh.height() + 9);
				//ch = $('#content_layer_wrap').height();
			}
		}
		
		
		var ginfo 	= $('#body .galleryblock_info:visible');
		var ginfoh 	= 0;
		if(ginfo.size() > 0 && ginfo.find('.scroll_wrap').html() != '')
		{
			if(ginfo.css('position') != 'absolute')
			{
				ginfo.css('padding-top', 0).css('padding-bottom', 0);
				ginfoh = $('#body .galleryblock_info:visible .scroll_wrap .textblockinfo').height();
				setActualHeight(ginfo.find('.scroll_wrap'), ginfoh);
				debug(resizeDebug, "info height: "+ginfoh);
				//contentScrollMargin = 40;
				//if info then reduce width allowance
				cw -= (ginfo.width() + 40);
			}
			else
			{
				//else slide out option
				toggleInfo(false);
			}
		}
		
		//determine resized dimensions of image
		var imgd = ratio(cw, ch, imgw, imgh);
		debug(resizeDebug, "container: "+cw+","+ch+", image:"+imgd[0]+","+imgd[1]);
		
		//set dimensions
		img.width(imgd[0]).height(imgd[1]);
		$(containerRef).height(imgd[1]);

		//calculate dimensions
		dimensionsSet 	= true;
		containerTopCss	= 'margin-top';
		contentHeight	= imgd[1];
		containerHeight = ch;
		
		//centers gallery block
		centerContent();
		
		if(ginfo.size() > 0 && ginfo.css('position') != 'absolute')
		{
			//setup dimenison for gallery info
			//this adds scrollbars if needed
			ginfo.css('display', 'block');
			containerRef	= '#body .galleryblock_info';
			containerTopCss	= 'padding-top';
			contentHeight 	= ginfoh;
			containerHeight	= imgd[1]-contentScrollMargin;
		}
		
		//onResizeGalleryItemSpecific(img);
	}
}


