(function($) {
// Takes in an unordered list of (similar sized) images and resizes the images that are contained to fit the browser window
$.fn.maxGrid = function(options) {
  var opts = $.extend({}, $.fn.maxGrid.defaults, options);
  
  var i=0;              // Setup iterater
  var ul = this;
  var originalWidth;
  var originalHeight;
  var ratio;
  var rightSpace;
  var thumbSizes = [];
  
  
    this.find('li:first').each(function() {
      var $this = $(this);
      
      $(window).load(function(){
        o = $.meta ? $.extend({}, opts, $this.data()) : opts;
        
        // Set the original width and height of the image
        originalWidth   = $this.find('img').width();
        originalHeight  = $this.find('img').height();
        
        // Based on the width and height, find the ratio
        ratio = find_ratio(originalWidth,originalHeight);
        
        var liMR    = to_i($this.css('margin-right'));
        var liPR    = to_i($this.css('padding-right'));
        var imgMR   = to_i($this.find('img').css('margin-right'));
        var imgPR   = to_i($this.find('img').css('padding-right'));
        rightSpace  = margin_and_padding_space(liMR,liPR,imgMR,imgPR);
        
        // Get the new width and height
        thumbSizes = get_thumb_sizes(ratio,rightSpace,o);
    
        // And do exactly the same thing if the browser is resized
        $(window).resize(function() {
          thumbSizes = get_thumb_sizes(ratio,rightSpace,o);
        });
      });
    });
    
    this.find('li').each(function(i) {
      var $this = $(this);
      
      $(window).load(function(){
        for(j=1;j<o.numberOfRows;j++){
          if(i==(o.numberOfColumns*j)-1){
            $this.addClass('last');
          }
        }
    
        $this.find('img').width(thumbSizes[0]);
        $this.find('img').height(thumbSizes[1]);
    
        $(window).resize(function() {
          $this.find('img').width(thumbSizes[0]);
          $this.find('img').height(thumbSizes[1]);
        });
      });
    });
  
  
  // private function for debugging
  function get_thumb_sizes(ratio,rightSpace,o){
    var pageWidth = $(window).width() - o.horizontalOffset;
    var pageHeight = $(window).height() - o.verticalOffset;
    var marginPixels = rightSpace * o.numberOfColumns;      // All of the pixels reserved for right paddings and margins
    var availPixels = pageWidth - marginPixels;             // Space available for images
    
    width = Math.abs(availPixels / o.numberOfColumns);      // Takes the available pixels and divides by how many columns we have to get the image width.
    height = Math.abs(width / ratio);
    
    arrayImageSize = new Array(width,height);
    return arrayImageSize;
  }
  
  function find_ratio(width,height) {
    var ratio = width/height;
    return ratio;
  }
  function margin_and_padding_space(liMR,liPR,imgMR,imgPR){
    var space = liMR + liPR + imgMR + imgPR;
    return space;
  }
  
  function to_i(i){
    if(i=="auto"){
      i=0;
    }
    last = parseInt(i);
    return last;
  }

  
  function debug($obj) {
    if (window.console && window.console.log) {
      window.console.log($obj);
    }
  }
};

// default options
$.fn.maxGrid.defaults = {
  numberOfColumns:7,
  numberOfRows:10,
  horizontalOffset:0,
  verticalOffset:0
};

})(jQuery);