/**
 * Walk through images in a wrapper and make sure that they are not wider than 
 * the wrapper itself
 */
jQuery.fn.scaleBigImages = function() {
  return this.each(function() {
    var wrapper = $(this);
    var wrapper_width = wrapper.width();
    
    wrapper.find('img').each(function() {
      var image = $(this);
      
      // Cache?
      if(image.width() > 0) {
        var width = image.width();
      
        if(width > wrapper_width) {
          if (image.parents('a').length == 0) {
            var link = $('<a></a>')
              .attr('href', image.attr('src'))
              .attr('title', 'Open Full Size (' + image.width() +'x' + image.height()  + 'px)')
              .click(function() {
                window.open($(this).attr('href'), 'Full Size Image');
                return false;
              });
              
            image.after(link).appendTo(link);
          } // if
            
          var scale = wrapper_width / width;
            
          image.css('height', Math.round(image.height() * scale));
          image.css('width', wrapper_width);
        } // if
      
      // Needs to be loaded
      } else {
        image.load(function () {
          var width = image.width();
          
          if(width > wrapper_width) {
            if (image.parents('a').length == 0) {
              var link = $('<a></a>')
                .attr('href', image.attr('src'))
                .attr('title', 'Open Full Size (' + image.width() +'x' + image.height()  + 'px)')
                .click(function() {
                  window.open($(this).attr('href'), 'Full Size Image');
                  return false;
                });
              
              image.after(link).appendTo(link);
            } // if
            
            var scale = wrapper_width / width;
            
            image.css('height', Math.round(image.height() * scale));
            image.css('width', wrapper_width);
          } // if
        });
      } // if
    });
  });
};