/*
$prog= 'js/404.js';
*/
jQuery(document).ready(function(){
	if (jQuery.browser.msie && jQuery.browser.version < 9){
		var $windowWidth = jQuery(window).width()+17;
		var $windowHeight = jQuery(window).height()+17;
	} else {
		var $windowWidth = window.innerWidth;
		var $windowHeight = window.innerHeight;
	}
	
	var $image = jQuery('.bgImage img');
	var img = new Image();
	
	img.src = jQuery($image).attr('src');
	
	var intervalId = setInterval(function(){
		if (img.complete){
			var $imgWidth = img.width;
			var $imgHeight = img.height;
			var $widthFactor = parseFloat($imgWidth/$windowWidth);
			var $heightFactor = parseFloat($imgHeight/$windowHeight);
			
			if ($widthFactor < $heightFactor){
				jQuery($image).css({width:$windowWidth+'px', height:parseInt($imgHeight/$widthFactor)+'px', top:'-'+parseInt((parseInt($imgHeight/$widthFactor)-$windowHeight)/2)+'px', left:0});
			} else {
				jQuery($image).css({width:parseInt($imgWidth/$heightFactor)+'px', height:$windowHeight+'px', top:0, left:'-'+parseInt((parseInt($imgWidth/$heightFactor)-$windowWidth)/2)+'px'});
			}
			
			clearInterval(intervalId);
			LoadImageStart();
		}
	}, 50);
	
	function LoadImageStart(){
		$image.parent().fadeIn(500, function(){
			jQuery(this).css({display:'block'});
		});
	}
	
	jQuery(window).resize(function(){
		if (jQuery.browser.msie && jQuery.browser.version < 9){
			var $windowWidth = jQuery(window).width()+17;
			var $windowHeight = jQuery(window).height();
		} else {
			var $windowWidth = window.innerWidth;
			var $windowHeight = window.innerHeight;
		}
		
		var $imgWidth = img.width;
		var $imgHeight = img.height;
		var $widthFactor = parseFloat($imgWidth/$windowWidth);
		var $heightFactor = parseFloat($imgHeight/$windowHeight);
		
		if ($widthFactor < $heightFactor){
			jQuery($image).css({width:$windowWidth+'px', height:parseInt($imgHeight/$widthFactor)+'px', top:'-'+parseInt((parseInt($imgHeight/$widthFactor)-$windowHeight)/2)+'px', left:0});
		} else {
			jQuery($image).css({width:parseInt($imgWidth/$heightFactor)+'px', height:$windowHeight+'px', top:0, left:'-'+parseInt((parseInt($imgWidth/$heightFactor)-$windowWidth)/2)+'px'});
		}
	});
});

