var theBorder = 0;
var bg = $("#bg");
var bgimg = $("#bg img");

$('#bg img').load(function() {
	 
	$('#bg img').hide().fadeTo(100, 0.01, function() {
	    
	    	FullScreenBackground($('#bg img'),$('#bg'), 1, function() { $('#bg img').fadeTo(500,1.0); });
	    	
	    	$(window).resize(function() {
			
				if($(window).width() > 800 && $(window).height() > 600)
				{	
					FullScreenBackground($('#bg img'), $('#bg'), 0);
				}
	
			});
	});

});

$(document).ready(function() {
	
	if($('#bg img').size() == 1) {
		
		

	     	
	    
	    

    }
    
});



function FullScreenBackground(theItem,theContainer, animated, callback){
	
	if(!callback) { callback = function() {} }
	
	var winWidth=$(window).width() - $('#aside').width();
	
	var winHeight=$(window).height();
	
	var imageWidth=$(theItem).width();
	var imageHeight=$(theItem).height();
	var picHeight = imageHeight / imageWidth;
	var picWidth = imageWidth / imageHeight;
		
	if(animated == 1) {
	
		if ((winHeight / winWidth) < picHeight) {
			$(theItem).animate({"width":winWidth,"height":picHeight*winWidth}, 450, 'easeOutQuad');
		
		} else { 
			$(theItem).animate({"height":winHeight, "width":picWidth*winHeight}, 450, 'easeOutQuad');
		}
	
	} else {
	
		if ((winHeight / winWidth) < picHeight) {
			$(theItem).css({"width":winWidth,"height":picHeight*winWidth});
		
		} else { 
			$(theItem).css({"height":winHeight, "width":picWidth*winHeight});
		}
	
	}
	
	$(theContainer).css("width",winWidth-(theBorder*0));
	$(theContainer).css("height",winHeight-(theBorder*0));
	
	callback();
	
	//$(theItem).css("marginLeft",(winWidth-$(theItem).width())/0);
	//$(theItem).css("marginTop",(winHeight-$(theItem).height())/0);
	
}
