(function($){
	$.support = $.support || {};
	$.support.backgroundSize =  document.body.style['KhtmlBackgroundSize']  !== undefined
	                         || document.body.style['MozBackgroundSize']    !== undefined
	                         || document.body.style['OBackgroundSize']      !== undefined
	                         || document.body.style['WebkitBackgroundSize'] !== undefined
	                         || document.body.style['BackgroundSize']       !== undefined
	                         || document.body.style['backgroundSize']       !== undefined;
	
	$.fn.backgroundSize = function(init) {
		if(!$.support.backgroundSize) {
			var $this = $(this);
			if (init) {
				$('<img />')
					.attr(
						'src',
						$this.css('background-image').replace(/url\(|\)|"|'/g,"")
					)
					.load(function(){
						$this
							.append($(this))
							.css('background', 'none');
							
						width  = $(this).width();
						height = $(this).height();
						
						$this.backgroundSize(false);
					});
			} else {
				var maxW  = $this.width();
				var maxH  = $this.height();
				
				var ratio = Math.max(maxW/width, maxH/height);
				
				$this.find('img').css({
					'width'       : Math.ceil(width*ratio) + 'px',
					'height'      : Math.ceil(height*ratio) + 'px',
					'margin-left' : '-' + (((width*ratio)-maxW)/2) + 'px',
					'margin-top'  : '-' + (((height*ratio)-maxH)/2) + 'px'
				});
			}
		}
	}
})(jQuery);

/*$(document).ready(function() {
	if($.support.backgroundSize) {
		alert('background-size supported');
	} else {
		alert('background-size not supported');
	}
	$('#background').backgroundSize(true);
});*/

$(window).resize(function() {
	$('#background').backgroundSize(false);
});
