/*
 *      imgauto 1.0 - jQuery plugin
 *      Author - ΑυΛΙ
 *      http://www.k88.cn
*/

(function($){
    $.fn.imgauto = function(options){
        
        var defaults = {
            width: "auto",
            height: "auto",
            middle: false
        };
        
        var options = $.extend(defaults, options);
        
        return this.each(function(){
            var image = $(this);
            var width = options.width == "auto" ? image.parent().width() : options.width;
            var height = options.height == "auto" ? image.parent().height() : options.height;
            if(image.width()>width){
                image.height(width/image.width()*image.height());
                image.width(width);
            }
            if(image.height()>height){
                image.width(height/image.height()*image.width());
                image.height(height);
            }
            if(options.middle){
                image.css("margin-top",(height-image.height())/2+"px");
            }
        });
    };
})(jQuery);

