$.fn.bgResize = function(options) {
	
	var defaults = {  
		imageWidth: 800,
		imageHeight: 600
	};
	
	var options = $.extend(defaults, options);
	
	var obj = $(this);
	
	var initHtml = obj.html();
	
	var windowHeight = $(window).height();
	var windowWidth = $(window).width();
		
	obj.height(windowHeight);
	obj.width(windowWidth);
	
	obj.html('<div id="inner-bg">'+initHtml+'</div>');
	
	$('#inner-bg img').each(function(){
		$(this).css({'display' : 'block', 'width' : '100%', 'height' : '100%'})								 
	});
	
	function doResize(){
		
		var screenheight = $(window).height();
		var screenwidth = $(window).width();
		
		var imageheight = options.imageheight;
		var imagewidth = options.imagewidth;
		
		var ratio = imagewidth/imageheight;
		
		var testwidth = screenheight * ratio;
		var testheight = screenwidth / ratio;
		
		obj.height(screenheight);
		obj.width(screenwidth);
		
		if (testheight < screenheight){
			obj.children('#inner-bg').width(testwidth);
			obj.children('#inner-bg').height(testwidth/ratio);
			var finalheight = Math.round(testwidth/ratio);
			var finalwidth = testwidth;
			var topoffset = (finalheight - screenheight)/2;
			var leftoffset = (finalwidth - screenwidth)/2;
		}
		
		else if (testheight > screenheight){
			obj.children('#inner-bg').height(testheight);
			obj.children('#inner-bg').width(testheight * ratio);
			var finalwidth = Math.round(testheight * ratio);
			var finalheight = testheight;
			var topoffset = (finalheight - screenheight)/2;
			var leftoffset = (finalwidth - screenwidth)/2;
		}
		
		else {}
		
		obj.children('#inner-bg').css("top", -topoffset);
		obj.children('#inner-bg').css("left", -leftoffset);
		
		$('#width').html('Width:'+screenwidth);
		$('#height').html('Height:'+screenheight);
	
	}
	
	doResize();
	
	$(window).resize(function(){
		doResize();
	});

	
	return this;

};

