/* JS
* Copyright 2009 noponies.
* Version 0.1 Beta - Intial Release to the wild - comments, yes please!
*/
(function($){

	var img_prop;
	var imageArray = [];
	var defaults = {};
	var firstLoad = true;
	var targetContainer;
	$.fn.extend({ 
		npFullBgImg: function(imgPath, options) {
			defaults = {
				 fadeInSpeed: 1000,
   				 center: false
			};
			
			var opts = $.extend(defaults, options);
			targetContainer = $(this); 
			//create image
			var img  = new Image();
			//add to array
 	 		imageArray.unshift(img);
 	 		
 	 		if(firstLoad === true) {
 	 			$(targetContainer).fadeTo(10, 0)
 	 		}
			
	        $(img).load(function () {
	        	//this is a hack to stop a flash of the image sometimes
  				$(img).fadeOut(10, 0);
	         	$(img).css({display: 'none', left:0, top: 0, 'z-index': -100});
	            //add image to container
	            $(targetContainer).append(img);
	            //resize image
				resizeImg($(window).width(), $(window).height(),targetContainer);
				
				if(firstLoad === true) {
 	 				$(targetContainer).fadeTo(10, 1)
 	 				firstLoad = false;
 	 			}
				if (defaults.fadeInSpeed==0) {
					$(img).show();
					if(imageArray.length > 1) {
		            	imageArray.pop();
		            	$(targetContainer).children().eq(0).remove();   	
		            }
		            
		          	if( typeof opts.callback == 'function' ){
						opts.callback.call(this, targetContainer, options);
					}
				} else {
					$(img).fadeIn(defaults.fadeInSpeed, function () {
		            	if(imageArray.length > 1) {
		            		imageArray.pop();
		            		$(targetContainer).children().eq(0).remove();   	
		            	}
		          		if( typeof opts.callback == 'function' ){
							opts.callback.call(this, targetContainer, options);
						}
	            	});
				
				}
				
	            
	        }).error(function () {
	            // got an error
	            alert('image not loaded');
	        }).attr('src', imgPath + '?random=' + (new Date()).getTime());
	    	},
	    	
	    	
	    npFullBgImgInline: function(img, options) {
	    	defaults = {
				 fadeInSpeed: 1000,
   				 center: false
			};
	    	var opts = $.extend(defaults, options);
			targetContainer = $(this); 
			
			//targetContainer.find("img").hide();
			
			targetContainer.append(img);
			if (defaults.fadeInSpeed>0) {
				$(img).fadeOut(10, 0);
			}	
			resizeImg($(window).width(), $(window).height(),targetContainer);
			
			targetContainer.find("img").css({'z-index': -100})
			
			$(img).css({display: 'none', 'z-index': -99});
			if (defaults.fadeInSpeed>0) {
				
				
				$(img).fadeIn(defaults.fadeInSpeed);
			} else {
			
				$(img).show();
				
			}
			if( typeof opts.callback == 'function' ){
					opts.callback.call(this, targetContainer, options);
			}
			
			
			
	    }
	    	
		});
	
	$(window).bind("resize", function(){
			var height = window.innerHeight ? window.innerHeight : $(window).height() 
			var width = window.innerWidth ? window.innerWidth : $(window).width() 
			if (mobile) {
				
				
				height = $(document).height(); 
				
				//$("h1").text($(window).height()+"   " +window.innerHeight+"   "+$(document).height());
				
			}
			
			
			resizeImg(width, height,targetContainer);		  			
	});

  	function resizeImg(sw, sh, targetContainer){
			targetContainer.find("img").each(function() {
					var imgw = 1024;
					var imgh = 768;
					
					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;
					}
					
					$(this).attr({
						width: destWidth,
						height: destHeight
					});
					
					if(defaults.center) {

						var xVal = sw * .5 - $(this).width() * .5;
						var yVal = sh * .5 - $(this).height() * .5;
						$(this).css({left:xVal, top: yVal});

					}
					

			})
			
			
			
	}

	
})(jQuery);


