jQuery(document).ready(function($) {
	$('body').prepend('<div id="bkgd-wrap"></div>');

	function centerImage() {
		if( $('div#bkgd-wrap').width() > $(img).width() ) {
			$(img).css({
				'width' : '100%',
				'height' : 'auto'
			});
		} else if (  $('div#bkgd-wrap').height() > $(img).height()  )  {
			$(img).css({
				'width' : 'auto',
				'height' : '100%'
			});
		}
		var imgCenterWidth = ( $('div#bkgd-wrap').width() - $(img).width() ) / 2;
		var imgCenterHeight = ( $('div#bkgd-wrap').height() - $(img).height() ) / 2;
		$(img)
			.stop(true,true)
			.animate({
				'left' : imgCenterWidth,
				'top' : imgCenterHeight
			}, 300);
	}

	var img = new Image();	
	var startImgLoadTime = new Date();
	$(img)
		.load(function() {
			var endImgLoadTime = new Date();
			$(this).hide();
			$('div#bkgd-wrap').append(this);
			centerImage();
			if( endImgLoadTime.getTime() > (startImgLoadTime.getTime() + 500)) {
				$('div#wrapper').queue(function() {
					$(img).fadeIn(2000);
					$(this).dequeue();
				});
			} else {
				$('div#wrapper').queue(function() {
					$(img).fadeIn(500);
					$(this).dequeue();
				});
			}
		})
		.error(function(){
			alert("Failed to load image.");
		})
		.attr('src', '/wp-content/themes/chdirector/img/mosaic_bkgd_wide.gif');
		
		
		$(window).bind('resize', function() {
			centerImage();
		});
});


