(function($) {
// The maxImages plugin resizes an image dynamically, according to the width of the browser.
$.fn.maxImage = function(options) {
  Settings = {
    maxFollows:           'both',  // Options: width, height, both
    verticalOffset:       0,
    horizontalOffset:     0,
    leftSpace:            0,
    topSpace:             0,
    rightSpace:           0,
    bottomSpace:          0,
    position:             'absolute',
    isBackground:         false,
    zIndex:               -10,
    verticalAlign:        'bottom',
    maxAtOrigImageSize:   false
  }
  
  var opts = $.extend({}, Settings, options);
  
  return this.each(function() {
    var $this = $(this);
	
	$this.hide();
	
  // Support for the Metadata Plugin.
  var o = $.meta ? $.extend({}, opts, $this.data()) : opts;

	$(window).load(function(){
	  var originalWidth = $this.width();
	  var originalHeight = $this.height();
	  
    // debug(originalHeight);
	  
      width_and_height = find_width_and_height(originalWidth,originalHeight,o);
      
      debug(width_and_height);
      
      // Use those figures to set the elements height and width
      $this.width( width_and_height[0] );
      $this.height( width_and_height[1] );
      if(o.isBackground){
        $this.css({
          'position':'fixed',
          'left': 0
        });
      
        // If user wants to align bg image position to the bottom
        if(o.verticalAlign == 'bottom'){
          $this.css({'bottom':0});
        } else {
          $this.css({'top': 0});
        }
      } else if(o.position == 'absolute'){
        $this.css({
          'top':    o.topSpace,
          'right':  o.rightSpace,
          'bottom': o.bottomSpace,
          'left':   o.leftSpace,
          'position': 'absolute'
        });
      } else {
        $this.css({
          'margin-top':     o.topSpace,
          'margin-right':   o.rightSpace,
          'margin-bottom':  o.bottomSpace,
          'margin-left':    o.leftSpace 
        });
      }
    
      $this.css({
        'z-index': o.zIndex
      });
	  
	  $this.show();
	  
	  $(window).resize(function() {
        width_and_height = find_width_and_height(originalWidth,originalHeight,o);
      
        // On window resize, reset height and width
        $this.width( width_and_height[0] );
        $this.height( width_and_height[1] );
      });
	
	});
	
  });
  
  
  // private utilities for this plugin
  function find_width_and_height(originalWidth,originalHeight,o) {
    var pageWidth = $(window).width() - o.horizontalOffset; // from jquery.dimensions.min.js
    var pageHeight = $(window).height() - o.verticalOffset; // from jquery.dimensions.min.js
    var ratio = find_ratio(originalWidth,originalHeight);
    
    if(!o.isBackground){
      if(o.maxFollows=='both'){
        max_follows_width(pageWidth,ratio,o);
      
        if( height > pageHeight ){
          max_follows_height(pageHeight,ratio,o);
        }
      } else if (o.maxFollows == 'width'){
        max_follows_width(pageWidth,ratio,o);
      } else if (o.maxFollows == 'height'){  
        max_follows_height(pageHeight,ratio,o);
      }
    }else{
      width = pageWidth + 40;
      height = width/ratio;
      
      if( height < pageHeight ){
        height = pageHeight - (o.topSpace + o.bottomSpace);
        width = height*ratio;
      }
    }
	
	
	
	// If maxAtRatio == true and your new width is larger than originalWidth, size to originalWidth
	if ( o.maxAtOrigImageSize && width > originalWidth){
	  arrayImageSize = new Array(originalWidth,originalHeight);
	}else{
	  arrayImageSize = new Array(width,height);
  }
    
	return arrayImageSize;
  }
  
  function max_follows_height(pageHeight,ratio,o){
    height = pageHeight - (o.topSpace + o.bottomSpace);  // Page Height minus topSpace and bottomSpace
    width = height*ratio;
  }
  
  function max_follows_width(pageWidth,ratio,o){
    width = pageWidth - (o.leftSpace + o.rightSpace); // Page Width minus leftSpace and rightSpace
    height = width/ratio;
  }
  
  function find_ratio(width,height) {
    var ratio = width/height;
    return ratio;
  }
  
  // private function for debugging
  function debug($obj) {
    if (window.console && window.console.log) {
      window.console.log($obj);
    }
  }
};

// default options
// $.fn.maxImage.defaults = {
//   maxFollows:           'both',  // Options: width, height, both
//   verticalOffset:       0,
//   horizontalOffset:     0,
//   leftSpace:            0,
//   topSpace:             0,
//   rightSpace:           0,
//   bottomSpace:          0,
//   position:             'absolute',
//   isBackground:         false,
//   zIndex:               -10,
//   verticalAlign:        'bottom',
//   maxAtOrigImageSize:   false
// };

})(jQuery);
