var ow = 0, oh = 0, imgLoaded = false;

function setBg()
{
	ww = jQuery(window).width();
	wh = jQuery(window).height();
	//whd = jQuery('.page_c').height()- 500;
	whd = wh - 150;
	
	if(whd > wh) wh = whd;
	nw = ww;
	
	jQuery('#bg').css('height', wh);
	
	if((imgLoaded)&&(ow!=0))
	{
		nh = oh / ow * nw;
		if(nh < wh)
		{
			ratio = wh / nh;
			nw = Math.ceil(nw * ratio);
			nh = Math.ceil(nh * ratio);
		}
		
		jQuery('#bg img').css('width', nw);
		jQuery('#bg img').css('height', nh);
		
		jQuery('#bg img').css('left', 0);
		if(ww < nw)
		{
			lft = Math.ceil((ww - nw) / 2);
			jQuery('#bg img').css('left', lft);
		}
		if(nw < ww)
		{
			jQuery('#bg img').css('width', ww);
			jQuery('#bg img').css('left', '0px');
		}
	}
	else
	{
		getImageSize();
		setTimeout('setBg()', 250);
		return;
	}
}
var ch = 0;
function observeContentSize()
{
	t = jQuery('.content').height();
	if(t != ch)
	{
		ch = t;
		setBg();
	}
}

function getImageSize()
{
	ow = jQuery('#bg img:first').width();
	oh = jQuery('#bg img:first').height();
	imgLoaded = true;
	setBg();
}

jQuery(document).ready(
	function()
	{
		jQuery('#bg img:first').load(function () { getImageSize(); } );
		
		jQuery(window).resize(function(){ setBg(); });
		
		if(jQuery('.content_mid').length > 0)
			setInterval('observeContentSize()', 250);
	}
);
