/*
#####################################################
# 
# FullBleed 1.1
# Another full-screen background plugin for jQuery
#
# Hand-crafted by Phenotype (phenotype.net)
# Heavily based upon Supersized by Sam Dunn 
# (buildinternet.com / onemightyroar.com)
#
#####################################################
*/

	/**
	* @param options:Object			(startWidth, startHeight, minSize)
	*/

(function($){

	//Resize image on ready or resize
	$.fn.fullbleed = function(options) {
	
		// Reset any CSS dimensions applied to images
		$(this).css({'width' : 'auto', 'height' : 'auto'});
		
		// Default options
		var defaults = {
			startWidth: $(this).width(),
			startHeight: $(this).height(),
			minSize: .5
		};
		
		// Merge with user-defined options
		$.fn.fullbleed.options = $.extend(defaults, options);
		
		// Get container/image
		var fullbleed = $(this);
		
		$().ready(function() {
			$(fullbleed).fullbleedActivate($.fn.fullbleed.options); 
		});
		
		$(window).bind("resize", function(){
    		$(fullbleed).fullbleedActivate($.fn.fullbleed.options);
		});
	};
	
	//Adjust image size
	$.fn.fullbleedActivate = function(options) {
		
	  	return this.each(function() {
			
			//Define image aspect ratio & minimum dimensions
			var minWidth = options.minSize*(options.startWidth);
			var minHeight = options.minSize*(options.startHeight);
			var aspectRatio = options.startHeight/options.startWidth;
			
			//Gather browser and current image size
			var imageWidth = options.startWidth
			var imageHeight = options.startWidth;
			var browserWidth = $(window).width();
			var browserHeight = $(window).height();
			
			//Check for minimum dimensions
			if ((browserHeight < minHeight) && (browserWidth < minWidth)){
				$(this).height(minHeight);
				$(this).width(minWidth);
			}
			else{	
				//When browser is taller	
				if (browserHeight > browserWidth){
				    imageHeight = browserHeight;
				    $(this).height(browserHeight);
				    imageWidth = browserHeight/aspectRatio;
				    $(this).width(imageWidth);
				    
				    if (browserWidth > imageWidth){
				    	imageWidth = browserWidth;
				    	$(this).width(browserWidth);
				    	imageHeight = browserWidth * aspectRatio;
				    	$(this).height(imageHeight);
				    }
				
				}
				
				//When browser is wider
				if (browserWidth >= browserHeight){
				    imageWidth = browserWidth;
				    $(this).width(browserWidth);
				    imageHeight = browserWidth * aspectRatio;
				    $(this).height(imageHeight);
				    
				    if (browserHeight > imageHeight){
				    	imageHeight = browserHeight;
				    	$(this).height(browserHeight);
				    	imageWidth = browserHeight/aspectRatio;
				    	$(this).width(imageWidth);
				    }
				}
			}
			return false;
		});
	};
	
	// Persistent options object
	$.fn.fullbleed.options = {};
	
})(jQuery); 	