$(document).ready(function(){

	$(window).load(function(){

		setFullscreen($('img.fullscreen'), true);
		
		$(window).resize(function(){
			setFullscreen($('img.fullscreen'), false);
		});
	
	});
	
});

function setFullscreen(imgObject, fadeIn) {

	if( fadeIn ) imgObject.hide();
	
	var imgWidth	= imgObject.width();
	var imgHeight	= imgObject.height();
	var imgRatio	= imgWidth/imgHeight;

	var screenWidth		= $(window).width();
	var screenHeight	= $(window).height();
	var screenRatio		= screenWidth/screenHeight;

	if(screenRatio>imgRatio){
		var newHeight 	= screenWidth/imgRatio;
		var marginTop 	= -(newHeight/2);
		var marginLeft	= -(screenWidth/2);
		imgObject.width(screenWidth).height(newHeight).css({'marginLeft':marginLeft, 'marginTop':marginTop});
	}
	else if(screenRatio<imgRatio) {
		var newWidth 	= screenHeight*imgRatio;
		var marginTop 	= -(screenHeight/2);
		var marginLeft	= -(newWidth/2);	
		imgObject.height(screenHeight).width(newWidth).css({'marginLeft':marginLeft, 'marginTop':marginTop});
	}
	
	if( fadeIn ) imgObject.fadeIn(300);
	else imgObject.show();
		
}
