/* 
 * jBackground - jQuery plugin adds a smooth-scaling, page-filling background
 * @author Ben Plum <benjaminplum@gmail.com>
 * @version 1.9.2
 *
 * Last update: 8/9/2010
 * Compatible Browsers: Firefox, Safari, Chrome, Opera, IE6, IE7 (all modern browsers)
 *
 * Copyright (c) 2009 Ben Plum <ben@benjaminplum.com>
 * Released under the MIT License <http://www.opensource.org/licenses/mit-license.php>
 *
 * Change Log:
 * - Rewritten to match jQuery plugin structure.
 * - Use of .extend() options to keep JS name space clean.
 * - Removed [more] redundancies.
 * - Added Min Document Width Option
 * - Added default animation speed
 * - Added default easing option
 * - Multiple Fitting Options:
 *		- "document" : image fills entire document; scrolls with page 
 *		- "window" : image fills visible window; fixed, centered (Thanks Zach Waugh <zwaugh@gmail.com>)
 * - Fixed horizontal scrolling bug (correctly positions horizontally when "window" fitting on)
 */

(function($) {
	$.fn.jBackground = function(customOptions) {
		
		var options = {
			sourceFile: "",
			minWidth: 0,
			fitting: "document",
			speed: "500",
			easing: "linear",
			imageHeight: 0,
			imageWidth: 0
		};
		$.extend(options, customOptions);
		
		return this.each(function()
		{
			$("body").wrapInner('<div id="jb_content" style="overflow: hidden"></div>');
			$("body").append('<div id="background_image"></div>').bind("jbackground.updateimage", updateImage);
			$(window).resize(resizeImage); //.scroll(resizeImage);
			
			$("#background_image").css({overflow: "hidden", minWidth: options.minWidth, top: 0, width: "100%", zIndex: -1});
			if(options.fitting == "document")
			{
				$("#background_image").css({position: "absolute"});
			}
			else
			{
				$("#background_image").css({height: "100%", position: "fixed"});
			}
			
			loadImage(options.sourceFile);
		});
		
		
		function loadImage(src)
		{
			$("<img />").load(function(){
				var img = $(this);
				
				options.imageWidth = img[0].width;
				options.imageHeight = img[0].height;
				
				img.css({left: "0", marginLeft: "50%", position: "absolute", top: "0"});
				
				if ($("#background_image img").length == 0)
				{
					img.appendTo($("#background_image"));
				}
				else
				{
					img.css({opacity: 0}).appendTo($("#background_image")).animate({opacity: 1}, options.speed, options.easing, function() {
						$("#background_image img").not(":last").remove();
					});
				}
				
				resizeImage();
			}).attr("src", src);
		}
		
		
		function updateImage(event, image)
		{
			loadImage(image);
		}
		
		function resizeImage()
		{
			var size = computeSize(options.imageWidth, options.imageHeight);
			
			$("#background_image > img").attr({height: size.height, width: size.width}).css({left: -size.offsetHorizontal});
			
			if(options.fitting == "window")
			{
				$("#background_image").css({height: $(window).height()});
				$("#background_image > img").css({top: -size.offsetVertical});
			}
			else
			{
				var newHeight = ($(document).height() > $("#jb_content").outerHeight(true)) ? $("#jb_content").outerHeight(true) : $(document).height();
				newHeight = (newHeight < $(window).height()) ? $(window).height() : newHeight;
				$("#background_image").css({height: newHeight});
			}
		}
		
		function computeSize(width, height)
		{
			var windowWidth = $(window).width(); 
			var windowHeight = (options.fitting == "document") ? $(document).height() : $(window).height();
			var windowRatio = windowWidth / windowHeight;
			var imgRatio = width / height;
			var imgWidth = width
			var imgHeight = height;
			var offsetHorizontal = 0;
			var offsetVertical = 0;
			
			imgHeight = windowHeight;
			imgWidth = imgHeight * imgRatio;
			
			if(imgWidth < windowWidth) {
				imgWidth = windowWidth;
				imgHeight = imgWidth / imgRatio;
			}
			
			if(imgWidth < options.minWidth) {
				imgWidth = options.minWidth;
				imgHeight = imgWidth / imgRatio;
			}
			
			offsetHorizontal = (windowWidth / 2) + (imgWidth - windowWidth) / 2;
			offsetVertical = (imgHeight - windowHeight) / 2;
			
			if(options.fitting == "window" && $(document).scrollLeft() > 0)
			{
				offsetHorizontal += $(document).scrollLeft();
			}
			
			return { width: parseInt(imgWidth), height: parseInt(imgHeight), offsetHorizontal: offsetHorizontal, offsetVertical: offsetVertical};
		}
	};
})(jQuery);
