/**
 * @name Supersize
 * @desc Full Screen Background/Slideshow jQuery Plugin
 * @date February 2009
 * @author Sam Dunn - www.buildinternet.com / www.onemightyroar.com
 * @author Trevor Morris - www.trovster.com
 * @version 1.0 by Sam Dunn, modified by Trevor Morris
 */
if(typeof jQuery != 'undefined') {
	(function($) {
		$.fn.extend({
			supersize: function(options) {
				var s, o, $$;
				
				$$ = $(this);
				s = $.extend({}, $.fn.supersize.defaults, options);
				o = $.metadata ? $.extend({}, s, $$.metadata()) : s;
				
				if(o.height === null || typeof o.height === 'NaN') {
					o.height = $$.children().height();
				}
				if(o.width === null || typeof o.width === 'NaN') {
					o.width = $$.children().width();
				}
				if(o.height == 0 || typeof o.height === 'NaN') {
					o.height = 1000;
				}
				if(o.width == 0 || typeof o.width === 'NaN') {
					o.width = 1600;
				}
			
				$().ready(function() {
					$$.children().superresize(o); 
				});
				$(window).bind('resize.supersize', function() {
					$$.children().superresize(o);
				});
			}
		});
		
		$.fn.extend({
			superresize: function(options) {
				return this.each(function() {
					var $$, minimum, image, browser, margin, ratio, o;
					o = $.extend({}, $.fn.supersize.defaults, options);
					
					if(o.height == 0 || o.height === null || typeof o.height === 'NaN') {
						o.height = 1000;
					}
					if(o.width == 0 || o.width === null || typeof o.width === 'NaN') {
						o.width = 1600;
					}
					
					$$ = $(this);
					minimum = {
						height  : o.size * (o.height),
						width	: o.size * (o.width)
					};
					image = {
						height  : o.height,
						width	: o.width
					};
					browser = {
						height  : $(window).height(),
						width	: $(window).width()
					};
					margin = {
						top		: 0,
						left	: 0
					};
					ratio = o.height / o.width;
					
					// Check for minimum dimensions
					if((browser.height < minimum.height) && (browser.width < minimum.width)) {
						$$.height(minimum.height);
						$$.width(minimum.width);
					}
					else {	
						// When browser is taller	
						if(browser.height > browser.width) {
							image.height = browser.height;
							image.width = browser.height / ratio;
							
							if(browser.width > image.width) {
								image.width = browser.width;							
								image.height = browser.width * ratio;
							}
						}
						
						// When browser is wider
						if(browser.width >= browser.height) {
							image.width = browser.width;
							image.height = browser.width * ratio;
							
							if(browser.height > image.height) {
								image.height = browser.height;
								image.width = browser.height / ratio;
							}
						}
						
						// Set the new image values
						image.height = Math.round(image.height);
						image.width = Math.round(image.width);
						$$.height(image.height).attr('height', o.height);
						$$.width(image.width).attr('width', o.width);

						// Optionally center the image
						$$.css({top: 0});
						if(o.center === true) {
							margin.left = Math.round((browser.width - image.width) / 2);
							margin.top = Math.round((browser.height - image.height) / 2);
							$$.css({marginLeft: margin.left, marginTop: margin.top});
						}
					}
					
					return false;
				});
			}
		});
		
		// Defaults…
		$.fn.supersize.defaults = {
			width	: null,  
			height 	: null,
			size	: 0.5,
			center	: true
		};
	})(jQuery);
}