/*
jquery.resizer.js
Rich Reuter
Sogeti
rich.reuter@us.sogeti.com
10/13/2008

This jQuery plugin makes an image fit in a predefined space by shrinking the image to fit 
within a certain dimension, then adding vertical and horizontal padding to make sure that the image will 
correctly fit into the bounding element.

Usage:
jQuery('#image').resizer({width: 200, height: 200});
*/
(function() {  
    jQuery.fn.resizer = function(settings) {
        // define defaults and override with options, if available
        // by extending the default settings, we don't modify the argument
        settings = jQuery.extend({
         width: 450,
         height: 445
        }, settings);

        // Get the original settings
        var orig_height = jQuery(this).height();
        var orig_width = jQuery(this).width();
        
        var height_chg_pct = (settings.height - orig_height) / orig_height;
        var width_chg_pct = (settings.width - orig_width) / orig_width;
        
        var chg_pct;
        if (height_chg_pct == width_chg_pct)
        {
            chg_pct = height_chg_pct; // Doesn't matter
        }
        else if (Math.abs(height_chg_pct) < Math.abs(width_chg_pct))
        {
            chg_pct = width_chg_pct;
        }
        else
        {
            chg_pct = height_chg_pct;
        }
        
        // If either the height or width of the orignal image is greater than
        // the bounding box, then scale the image to fit. 
        if (orig_height > settings.height || orig_width > settings.width)
        {
            var new_height = orig_height + Math.floor(orig_height * chg_pct);
            var new_width = orig_width + Math.floor(orig_width * chg_pct);
            jQuery(this).css('height', new_height + 'px ! important');
            jQuery(this).css('width', new_width + 'px ! important');
        }
        
        // Reset the padding so that the padding for the image can be calculated
        jQuery(this).css('padding', '0px');
        jQuery(this).css('padding-left', '0px');
        jQuery(this).css('padding-right', '0px');
        jQuery(this).css('padding-top', '0px');
        jQuery(this).css('padding-bottom', '0px');
        
        // Set the padding to compensate for difference in the height and width
        if (jQuery(this).height() < settings.height)
        {
            var vertical_padding = settings.height - jQuery(this).height();
            var vert_half = Math.round(vertical_padding / 2) + "px";
            jQuery(this).css('padding-top', vert_half);
            jQuery(this).css('padding-bottom', vert_half);
        }
        
        if (jQuery(this).width() < settings.width)
        {
            var horizontal_padding = settings.width - jQuery(this).width();
            var horizontal_half = Math.round(horizontal_padding / 2) + "px";
            jQuery(this).css('padding-left', horizontal_half);
            jQuery(this).css('padding-right', horizontal_half);
        }  
        
        return jQuery;
    };
})(jQuery);  