/*
bgimage - Fullscreen Background Image jQuery Plugin
By Josh Gerdes altered from Supersized plugin by Sam Dunn
*/
//You need an anonymous function to wrap around your function to avoid conflict
(function($) {
	//Settings list and the default values  
	$.bgimage = {
		defaults: {  
			 startheight: 480,  
			 startwidth: 640,  
			 vertical_center: 0  
		 }
	};
	
 	$.fn.extend({
		bgimage: function(options) {

			var options = $.extend({}, $.bgimage.defaults, options);  
			
			// Iterate over the current set of matched elements
			return this.each(function() {
				var obj = $(this);
				
				$(window).bind('resize', function() {
					obj.resizenow(options);
				});
				
				$(window).bind('load', function() {
					obj.resizenow(options);
				});
			});
		},
		resizenow: function(options) {
			//Define image ratio
			var ratio = options.startheight/options.startwidth;
			
			//Gather browser and current image size
			var imagewidth = $(this).width();
			var imageheight = $(this).height();
			var browserwidth = $(window).width();
			var browserheight = $(window).height();
			var offset;

			//Resize image to proper ratio
			if ((browserheight/browserwidth) > ratio){
				$(this).height(browserheight);
				$(this).width(browserheight / ratio);
				$(this).children().height(browserheight);
				$(this).children().width(browserheight / ratio);
			} else {
				$(this).width(browserwidth);
				$(this).height(browserwidth * ratio);
				$(this).children().width(browserwidth);
				$(this).children().height(browserwidth * ratio);
			}
			if (options.vertical_center == 1){
				$(this).children().css('left', (browserwidth - $(this).width())/2);
				$(this).children().css('top', (browserheight - $(this).height())/2);
			}

			var ie6 = ($.browser.msie && parseInt($.browser.version, 10) <= 6);
			if (ie6) {
				$(this).addClass('ie6fixed');
				$(this).css('height', browserheight);
			} else {
				$(this).css({
					'position' : 'fixed',
					'top' : 0,
					'left' : 0
				});
			}
			
			return false;
		}
	});
})(jQuery);
