(function($){

	var img_prop;
	var imageArray = [];
	var defaults = {};
	
	$.fn.extend({ 
		npFullBgImg: function(imgPath, options) {
			defaults = {
				 fadeInSpeed: 1000,
   				 center: false
			};
			
			var options = $.extend(defaults, options);
			var targetContainer = $(this); 
			//create image
			var img  = new Image();
			//add to array
 	 		imageArray.unshift(img);

	        $(img).load(function () {
	            //add image to container
	            $(targetContainer).append(img)
	            //resize image
	            $(img).css({'display': 'none', left:0, top: 0, position: 'absolute', 'z-index': -100})
				resizeImg($(window).width(), $(window).height(), $(img).width(), $(img).height())
	            
	            $(img).fadeIn(defaults.fadeInSpeed, function () {
		            if(imageArray.length > 1) {
		            	imageArray.pop()
		            	$(targetContainer).children().eq(0).remove()     	
		            }
		            
		          	if( typeof options.callback == 'function' ){
						options.callback.call(this, targetContainer, options);
					}

	            })
	        }).error(function () {
	            // got an error
	            alert('image not loaded')
	        }).attr('src', imgPath + '?random=' + (new Date()).getTime());
	    	}
		});
	
	$(window).bind("resize", function(){
			resizeImg($(window).width(), $(window).height(), $(imageArray[0]).width(), $(imageArray[0]).height())		  			
	});

  	function resizeImg(sw, sh, imgw, imgh, targetContainer){
			if ((sh / sw) > (imgh / imgw)) {
					img_prop = imgw/imgh;
					destHeight = sh;
					destWidth = sh * img_prop;
				} else {
					img_prop = imgh/imgw;
					destWidth = sw;
					destHeight = sw * img_prop;
				}

			$(imageArray[0]).attr({
				width: destWidth,
				height: destHeight
			});
			
			if(defaults.center) {
				var xVal = sw * .5 - $(imageArray[0]).width() * .5
				var yVal = sh * .5 - $(imageArray[0]).height() * .5
				$(imageArray[0]).css({left:xVal, top: yVal})
			}
	}

	
})(jQuery);

