var bCheckLoad = false;
var bWidth = false;
jQuery.noConflict();
jQuery(document).ready(function(){
	jQuery(document).pngFix(); 
	jQuery("#background-image").load(function(){
		resizeBackground();
	});
	setTimeout("checkImageLoad()",500);
});
jQuery(window).resize(function(){
	resizeBackground();
});

function checkImageLoad()
{
	var eBGImg = document.getElementById("background-image");
	bgWidth = eBGImg.naturalWidth || eBGImg.width;
	if(bgWidth > 0)
	{
		resizeBackground();
	}
	else
	{
		setTimeout("checkImageLoad()",500);
	}
}

function resizeBackground()
{
	var sImgWidth, sImgHeight;

	var eBGImg = document.getElementById("background-image");

	var sRatioImageWidth = eBGImg.naturalWidth || eBGImg.width;
	var sRatioImageHeight = eBGImg.naturalHeight || eBGImg.height;
	var sImageAspectRatio = sRatioImageWidth / sRatioImageHeight;
	var sWidth = jQuery(window).width();
	var sHeight = jQuery(window).height();
	var sAspectRatio = sWidth / sHeight;
	
	if(sAspectRatio >= sImageAspectRatio)
	{
		if(bWidth)
		{
			sImgWidth = sWidth;
			sImgHeight = Math.ceil(sWidth / sImageAspectRatio);			
		}
		else
		{
			sImgWidth = Math.ceil(sHeight * sImageAspectRatio);
			sImgHeight = sHeight;
		}
	}
	else
	{
		if(bWidth)
		{
			sImgWidth = Math.ceil(sHeight * sImageAspectRatio);
			sImgHeight = sHeight;				
		}
		else
		{
			sImgWidth = sWidth;
			sImgHeight = Math.ceil(sWidth / sImageAspectRatio);
		}
	}
	
	if(sWidth > 1000)
	{
		sLeft = Math.ceil((sWidth - 1000)/2);
	}
	else
	{
		sLeft = 0;
	}
	
	// top space = 20px; top menu = 80px; bottom bar = 30px;
	
	iScrollStart = 100;
	iScrollHeight = sHeight - 130;
	iBGHeight = sHeight - 30;
	
	jQuery('DIV.bgCon').css({visibility:"visible", display:"block", height: sHeight, width: sWidth});
	jQuery('IMG.bgImg').css({height: sImgHeight, width: sImgWidth});
	jQuery('div.contentwhole').css({top: iScrollStart, width: sWidth, height: iScrollHeight});
	if(document.getElementById("contentwidebg")){
		jQuery('div.contentwidebg').css({top: 0, height: iBGHeight});
	}
	if(document.getElementById("contentbg")){
		jQuery('div.contentbg').css({top: 0, height: iBGHeight});
	}
	jQuery('div.contentwide').css({top: 0, height: sHeight});
	bCheckLoad = true;
}
