//DESCRIBES VARIABLES
//container id to be resize
var div_id = 'container_fullscreen';

var winW = 0;
var winH = 0;
var minW = 1050;
var minH = 560;

var types = [["multimediablock", "multi"], ["galleryblock", "gallery"], ["body_wrap", "text"]];
var type = false;
var bodyHeight = 0;
var scrollHeight = 0;
var scrollPadding = 0;
var maxScrollHeight = 0;

var focusImage = false;
var top = 0;



var galleryBlock = '#body .galleryblock';

$(window).resize(function() {
	checkWindowDimensions();
	processResize();
});

//process resize
function processResize()
{
	processPageType();
	if(type)
	{
		resizeCommon();
		//document.title = type;
		if(type == 'text')
		{
			resizeText();
		}
		else if(type == 'gallery')
		{
			resizeGallery();
		}
		else if(type == 'multi')
		{
			resizeMultiGallery();
		}
		onReadyThumbCarousel();
	}
}

//process what type of page it it
function processPageType()
{
	if(!type)
	{
		for(var i=0; i < types.length; i++)
		{
			var p = $('#body .'+types[i][0]);
			if(p.height() != undefined)
			{
				type = types[i][1];
				break;
			}
		}
	}
}

//common resize
function resizeCommon()
{
	//$('#sticky_gallery_nav').css('overflow', 'hidden');
	$('#body .scroll_wrap').css('position', 'relative');
	$('#body .scroll_wrap').css('left', 0);
	
	if(type == 'text')
	{
		bodyHeight = $('#body .body_wrap').height();
	}
	else if(type == 'gallery')
	{
		bodyHeight = $('.galleryblock_info').height();
	}
	else if(type == 'multi')
	{
		bodyHeight = $('#body .body_wrap').height();
	}
	
	//spefici to layout
	onResizeBodySpecific();

	scrollHeight = actualHeight($('#body .scroll_wrap'));
	$('.scroll_wrap').jScrollPaneRemove();
	$('.scroll_wrap').height(scrollHeight);
	
	top = 0;
	if(scrollHeight > bodyHeight)
	{
		//document.title = maxScrollHeight + " > " + bodyHeight;
		if(maxScrollHeight > 0 && (bodyHeight+scrollPadding) > maxScrollHeight)
		{
			$('.scroll_wrap').height(maxScrollHeight-scrollPadding);
		}
		else
		{
			$('.scroll_wrap').height(bodyHeight);
		}
		$('.scroll_wrap').jScrollPane({showArrows:false, scrollbarWidth: 10, scrollbarMargin:10});
	}
	else
	{
		top = (bodyHeight - scrollHeight)/2;
	}
}

//////////////////////////////
// PAGE TYPE RESIZING
//////////////////////////////

//text page resizing
function resizeText()
{
	$('#body .body_wrap').css('padding-top', top);
}

//gallery resizing
function resizeGallery()
{
	if(!focusImage)
	{
		$('#body .galleryblock').css('visibility', 'hidden');
	}
	else
	{
		resizeGalleryItem(focusImage);
	}
}

//multi media resing
function resizeMultiGallery()
{
	$('#body .body_wrap').css('padding-top', top);
}

//resize focus gallery image, only on gallery pages
function resizeGalleryItem(fimg)
{
	//unhide image
	$('#body .galleryblock').css('visibility', 'visible');
	
	//defined actual height of image before resize
	actualHeight(fimg);
		
	//set gallery body height
	bodyHeight = $('#content_layer_gallery').height();
	
	var skh = $('#sticky_gallery_nav').height();
	if(skh != null && bodyHeight == $('#body').height() && skh > 0)
	{
		bodyHeight = bodyHeight - skh - 9;
	}
	
	//default vertical center
	$(galleryBlock).css('margin-top', 0);
	if(fimg.attr(aHAtt) < bodyHeight)
	{									
		//if actual image height is less than gallery body height
		top = (bodyHeight - fimg.attr(aHAtt))/2;
		
		//document.title = "bodyHeight:"+bodyHeight+", imgHeight:"+fimg.attr(aHAtt)+", margin-top:"+top;
		
		//center image vertically
		$(galleryBlock).css('margin-top', top);
	}
	else
	{
		//if image greater than space available then scale
		fimg.height(bodyHeight);
	}
	
	onResizeGalleryItemSpecific(fimg);
}

//on gallery image loaded
function onFocusImageLoaded()
{
	if(type && type == 'gallery')
	{
		focusImage = $('#body .galleryblock .galleryblock_image a img');
		
		maxScrollHeight = focusImage.height();
		resizeCommon();
		
		resizeGalleryItem(focusImage);
	}
}

//checks for page resize
function checkWindowDimensions()
{
	//actual dimensions
	winW = $(window).width();
	winH = $(window).height();
	if(winW < minW)
	{
		$('#'+div_id).width(minW);
	}
	else
	{
		$('#'+div_id).width("100%");
	}
	if(winH < minH)
	{
		$('#'+div_id).height(minH);
	}
	else
	{
		$('#'+div_id).height("100%");
	}
}
