var webendians = {};

webendians.initialContentHeight = -1;

webendians.handleResize = function()
{
	var viewportWidth = $(window).width(), viewportHeight = $(window).height();
	var widthCorrect = 0, heightCorrect = 40;
	var contentWidth = 1200, contentHeight = 0;
	var headerHeight = 126, footerHeight = 84;
	var lr_width = 0, tb_height = 0, minWidth = 900; 
	
	lr_width = parseInt((viewportWidth - contentWidth - widthCorrect) / 2);
	lr_width = (lr_width < 0) ? 0 : lr_width;	
			
	$('#bg_header_left').width(lr_width);
	$('#bg_header_right').width(lr_width);	
	$('#bg_content_left').width(lr_width);
	$('#bg_content_right').width(lr_width);	
	$('#bg_footer_left').width(lr_width);
	$('#bg_footer_right').width(lr_width);	
		
	if (viewportWidth < 1200)
	{	
		if (viewportWidth > minWidth) minWidth = viewportWidth;
		$('#bg_header_center').width(minWidth);
		$('#bg_content_center').width(minWidth);
		$('#bg_footer_center').width(minWidth);
	}
	
	if (webendians.initialContentHeight == -1)
	{
		//webendians.initialContentHeight = $('.content_html').height() + headerHeight + footerHeight;
		webendians.initialContentHeight = $('div.content_html > div.page').height() + headerHeight + footerHeight;
		webendians.initialContentHeight = (webendians.initialContentHeight < (650-heightCorrect)) ? (650-heightCorrect) : webendians.initialContentHeight;
	}	
	
	contentHeight = webendians.initialContentHeight;	
	
	if (contentHeight < viewportHeight)
	{					
		$('.content_html').height(viewportHeight - (headerHeight + footerHeight) - heightCorrect);
		tb_height = parseInt((viewportHeight - (650 - heightCorrect) - heightCorrect) / 2);	
		tb_height = (tb_height < 0) ? 0 : tb_height;
		$('#overlay_info').html( "P1<br>" );		
	} else {
		$('.content_html').height(contentHeight - (headerHeight + footerHeight));
		tb_height = parseInt(((650 - heightCorrect) - contentHeight) / 2);	
		tb_height = Math.abs(tb_height);
		$('#overlay_info').html( "P2<br>" );
	}	
	
	$('#bg_header').height(tb_height);
	$('#bg_header_left').height(tb_height);
	$('#bg_header_center').height(tb_height);
	$('#bg_header_right').height(tb_height);
	
	$('#bg_footer').height(tb_height);
	$('#bg_footer_left').height(tb_height);
	$('#bg_footer_center').height(tb_height);
	$('#bg_footer_right').height(tb_height);
	
	//$('#overlay_info').html($('#overlay_info').html() + viewportHeight +" "+ contentHeight +" "+ heightCorrect +" "+ tb_height);	
};

//handle window resize event
$(window).resize(function(){webendians.handleResize();});

// initial resize
$(document).ready(function(){webendians.handleResize();});
