﻿
(function($){
    $.fn.resizeImage = function(options) {
        var defaults = {
            maxWidth: 100,
            maxHeight: 100,
            ratio: 0             
        };
        
        var options = $.extend(defaults, options);
        
        return this.each(function() {
            //From: http://thejudens.com/eric/2009/07/jquery-image-resize/
            var width = $(this).width();    // Current image width
            var height = $(this).height();  // Current image height

            // Check if the current width is larger than the max
            if (width > options.maxWidth) {
                options.ratio = options.maxWidth / width;   // get ratio for scaling image
                $(this).css("width", options.maxWidth); // Set new width
                $(this).css("height", height * options.ratio);  // Scale height based on ratio
                height = height * options.ratio;    // Reset height to match scaled image
                width = width * options.ratio;    // Reset width to match scaled image
            }

            // Check if current height is larger than max
            if (height > options.maxHeight) {
                ratio = options.maxHeight / height; // get ratio for scaling image
                $(this).css("height", options.maxHeight);   // Set new height
                $(this).css("width", width * options.ratio);    // Scale width based on ratio
                width = width * options.ratio;    // Reset width to match scaled image
            }
        });
    }
})(jQuery);
