/*
 * jQuery Backstretch 1.0
 * http://srobbin.com/jquery-plugins/jquery-backstretch/
 *
 * Add a dynamically-resized background image to the page
 *
 * Copyright (c) 2009 Scott Robbin (srobbin.com)
 * Dual licensed under the MIT and GPL licenses.
*/

(function($) {
	$.backstretch = function(src, options, callback) {
		var settings = {
			hideUntilReady: true, // Hide the image until it's finished loading
			speed: 2000 // fadeIn speed for background after image loads (e.g. "fast" or 500)
		},
		imgRatio;
		
		// Extend the settings with those the user has provided
		if (options && typeof options === "object") {
			$.extend(settings, options);
		}
		
		function _adjustBG(callback) {
			var bgWidth = $(window).width(),
				bgHeight = bgWidth / imgRatio;
				
			if(bgHeight < $(window).height()) {
				bgHeight = $(window).height();
				bgWidth = bgHeight * imgRatio;
			}
			
			$("#backstretch img.new").width( bgWidth ).height( bgHeight );
			
			if (typeof callback === "function") {
				callback();
			}
		}
		
		function _init() {
			// Prepend image, wrapped in a DIV, with some positioning and zIndex voodoo
			if (src) {
				var commonCSS = {left: 0, top: 0};
				var wrap, container;
				if ($("#backstretch-wrap").length === 0) {
					wrap = $("<div />").attr("id", "backstretch-wrap").css( $.extend(commonCSS, {position: "absolute", zIndex: -1}) );
					container = $("<div />").attr("id", "backstretch").css( $.extend(commonCSS, {position: "fixed", overflow: "hidden", zIndex: -1}) ).appendTo(wrap);
				} else {
					wrap = $("div#backstretch-wrap");
					container = $("div#backstretch");
				}
				
				// previous image ?
				img = $("div#backstretch > img");
				
				// don't do anything if new image src is the same as the one we're trying to replace
				if (img.length > 0) {
					if (img.attr('src') === src) {
						return;
					} else {
						img.removeClass("new");
					}
				}
				src = src + "?" + new Date().getTime();
				
				newImg = $("<img />").attr("src", src).addClass("new").bind("load", function(e) {
						var self = $(this);
						imgRatio = self.width() / self.height();
						_adjustBG(function() {
							if( settings.hideUntilReady ) {
								if (img.length > 0) {
									// if (!Modernizr.csstransitions) {
										img.fadeOut(settings.speed, function() {
											self.fadeIn(settings.speed, function(){
												img.remove();
												// Callback, if necessary
												if (typeof callback === "function") {
													callback();
												}
											});
										});
									// } else {
									// 	img.bind('webkitTransitionEnd transitionend OTransitionEnd', function() {
									// 		img.css('display', 'none');
									// 		self.css('opacity', 1);
									// 		if (typeof callback === "function") {
									// 			callback();
									// 		}
									// 		img.remove();
									// 	});
									// 	img.css('opacity', 0);
									// }
								} else {
									// if (!Modernizr.csstransitions) {
										self.fadeIn(settings.speed, function(){
											// Callback, if necessary
											if (typeof callback === "function") {
												callback();
											}
										});
									// } else {
									// 	self.css('opacity', 1);
									// 	self.bind('webkitTransitionEnd transitionend OTransitionEnd', function() {
									// 		if (typeof callback === "function") {
									// 			callback();
									// 		}
									// 	});
									// }
								}
							}
						});
					}
				);
				
				if (settings.hideUntilReady) {
				// 	if (!Modernizr.csstransitions) {
						newImg.hide();
				// 	} else {
				// 		newImg.css('opacity', 0);
				// 	}
				}
				newImg.appendTo(container);
				
				$("body").prepend(wrap);
				
				// Adjust the background size when the window is resized
				$(window).resize(_adjustBG);
			}
		}
		
		// Initialize
		$(document).ready(_init);
		
		// For chaining
		return this;
		

	};
})(jQuery);
