(function($) {
	
	jQuery.fn.bkgdImage = function(options) {
		
		var resizeMsgDefaults = {
			show: false,
			location: 'before',
			message: 'Original: ([w]w x [h]h)' 
		};
		
		
		// var opts = $.extend({}, $.fn.maxImage.defaults, options);
		var opts = jQuery.extend({
			offset_vertical:	0,
			offset_horizontal:	0,
			space_Left:			0,
			sapce_Right:		0,
			space_Top:			0,
			space_Bottom:		0,
			overflow:			'hidden',
			position:			'absolute',
			zIndex:				-10,
			align_Vertical:		'bottom',
			align_Horizontal:	'left',
			image_min_height: 	null,
			image_min_width: 	null,
			maxAtOrigImageSize:	false,
			resizeMsg:			resizeMsgDefaults
		}, options);
		
		
		// var resizeDefaults = {show: false, location: 'before', message: '(resized)'};
		opts.resizeMsg = jQuery.extend(resizeMsgDefaults, options.resizeMsg);
		
		
		// Cache jQuery object
		var jQueryMatchedObj = this;
		
		
		function _initialize() {
			_start(this, jQueryMatchedObj);
			return false;
		}
		
		function _start(image, jQueryMatchedObj) {
			Background._setup_background(image);
		}
		
		Background = {
			_setup_background: function(image) {
				$this = $(image);
				$this.hide();
				Background._configure_css(image);
				$(window).load(function() {
					_get_orig_data($this);
					_size_image($this);
					$this.show();
					$(window).resize(function() {
						_size_image($this);
					});
				});
			},
			
			_configure_css: function(image) {
				$(image).css({
					'z-index':  opts.zIndex
				});
				
				if(opts.position == 'absolute') {
				//	$('html').css({'overflow-y':opts.overflow});				/* metal@flat.com */
					$(image).css({
						'overflow':   'hidden',
						'left':       opts.space_Left,
						'top':        opts.space_Top,
						'position':   'absolute'
					});
					if(opts.align_Vertical == 'bottom') {
						$(image).css({'bottom':opts.space_Bottom});
					}
					if(opts.align_Horizontal == 'right') {
						$(image).css({'right':opts.sapce_Right});
					}
				} else {
					$(image).css({
						'margin-top':     opts.space_Top,
						'margin-right':   opts.sapce_Right,
						'margin-bottom':  opts.space_Bottom,
						'margin-left':    opts.space_Left,
						'position':       'relative'
					});
				}
				
				if(opts.image_min_height != null) {								/* metal@flat.com */
					$(image).css({'min-height':opts.image_min_height});
				}
				if(opts.image_min_width != null) {
					$(image).css({'min-width':opts.image_min_width});
				}
			}
			
		} // END Background
		
		
		
		// BROADER SUB-FUNCTIONS
		function _get_orig_data(image) {
			$this = image;
			$this.attr('origWidth', $this.width());
			$this.attr('origHeight', $this.height());
			$this.attr('ratio', _find_ratio($this.width(),$this.height()));
		}
		
		
		function _size_image(image) {
			$this = image;
			var originalWidth = parseInt($this.attr('origWidth'));
			var originalHeight = parseInt($this.attr('origHeight'));
			
			var ratio = $this.attr('ratio');
			
			if(originalWidth == 0 || originalHeight == 0) {
				setTimeout(function() {
					_get_orig_data(image);
					_size_image(image);
				}, 100);
				return;
			}
			
			var width_and_height = [];
			width_and_height = _find_width_and_height(originalWidth,originalHeight,ratio);
			
			$this.width( width_and_height[0] );
			$this.height( width_and_height[1] );
			
			_show_resize_message(originalWidth,originalHeight,image);
		}
		
		
		function _show_resize_message(originalWidth,originalHeight,image) {
			if( (parseInt($this.width()) != originalWidth || parseInt($this.height()) != originalHeight) && opts.resizeMsg.show) {
				$(".maximage_resized").remove();
				
				// Replace [w] and [h] with their respective width or height
				opts.resizeMsg.message = opts.resizeMsg.message.replace('[w]',originalWidth).replace('[h]',originalHeight);
				
				var insertStr = '<div class="maximage_resized">' + opts.resizeMsg.message + '</div>';
				if(opts.resizeMsg.location.toLowerCase() == "before") {
					$this.before(insertStr);
				} else {
					$this.after(insertStr);
				}
			}
		}
		
		
		function _find_width_and_height(originalWidth,originalHeight,ratio) {
			var pageWidth = $(window).width() - opts.offset_horizontal;
			var pageHeight = $(window).height() - opts.offset_vertical;
			
			width = pageWidth + 40;
			height = width/ratio;
			
			if( height < pageHeight ) {
				height = pageHeight - (opts.space_Top + opts.space_Bottom);
				width = height*ratio;
			}
			
			
			// If maxAtRatio == true and your new width is larger than originalWidth, size to originalWidth
			if ( opts.maxAtOrigImageSize && width > originalWidth) {
				arrayImageSize = new Array(originalWidth,originalHeight);
			} else {
				arrayImageSize = new Array(width,height);
			}
			return arrayImageSize;
		}
		
		
		// Not used
		function max_follows_height(pageHeight,ratio) {
			height = pageHeight - (opts.space_Top + opts.space_Bottom);  // Page Height minus space_Top and space_Bottom
			width = height*ratio;
		}
		
		
		// Not used
		function max_follows_width(pageWidth,ratio) {
			width = pageWidth - (opts.space_Left + opts.sapce_Right); // Page Width minus space_Left and sapce_Right
			height = width/ratio;
		}
		
		
		function _find_ratio(width,height) {
			width = parseInt(width);
			height = parseInt(height);
			var ratio = width/height;
			ratio = ratio.toFixed(2);
			return ratio;
		}
		
		
		// private function for debugging
		function debug($obj) {
			if (window.console && window.console.log) {
				window.console.log($obj);
			}
		}
		
		return this.each(_initialize);
	};

})(jQuery);
