/**
 * jQuery.fullBg Copyright (c) 2010 c.bavota - http://bavotasan.com Dual
 * licensed under MIT and GPL. Date: 02/23/2010
 */

var imgwidth, imgheight;
var winwidth, winheight;
var widthratio, heightratio;
var widthdiff, heightdiff;
var positiontop, positionleft;

(function(win, $, undef) {
	
	$.fn.fullBg = function() {
		
		var bgImg = $(this);

		function resizeImg() {
			
			//if (imgwidth == undef && imgheight == undef) {
				imgwidth = bgImg.width();
				imgheight = bgImg.height();
			//}

			winwidth = $(win).width();
			winheight = $(win).height();

			widthratio = winwidth / imgwidth;
			heightratio = winheight / imgheight;

			widthdiff = heightratio * imgwidth;
			heightdiff = widthratio * imgheight;

			positiontop = 0;
			positionleft = 0;

			positiontop = (winheight - heightdiff) / 2;
			positionleft = (winwidth - widthdiff) / 2;

			if (heightdiff >= winheight) {
				
				bgImg.css({
					width : winwidth + 'px',
					height : heightdiff + 'px',
					top : positiontop + 'px',
					left : '0px'
				});
				ratio = 'height';
			} else {
				
				bgImg.css({
					width : widthdiff + 'px',
					height : winheight + 'px',
					left : positionleft + 'px',
					top : '0px'
				});
				ratio = 'width';
			}
		}
		
		resizeImg();
		$(window).resize(function() {
			resizeImg();
		}).resize();
	};
	
})(window, jQuery);
