/**
 * Resize Image with keeping aspect Ratio
 */
function resizeImage(sel, img_width, img_height, callback) {
(function($) { 
    $(function(){ 
        resizeImageNow(sel, img_width, img_height, callback);
    });
})(jQuery);
}

function resizeImageNow(sel, img_width, img_height, callback) {

    (function($) {

        var selector = $(sel);

        selector.each(function() {
            var f = function(event, elem) {
                elem = elem || this;
                var imageRatio = jQuery(elem).height() / jQuery(elem).width();
                var img_Ratio = img_height / img_width;
                if (imageRatio > img_Ratio) {
                    if (img_height < jQuery(elem).height()) {
                        jQuery(elem).height(img_height);
                        jQuery(elem).width(img_height / imageRatio);
                    }
                } else {
                    if (img_width < jQuery(elem).width()) {
                        jQuery(elem).width(img_width);
                        jQuery(elem).height(img_width * imageRatio);
                    }
                }
                if (callback) {
                    callback();
                }
            };
            if (jQuery(this).height() > 0 && jQuery(this).width() > 0 && jQuery(this).attr("complete")) {
                f(null, this);
            } else {
                jQuery(this).bind("load", f);
                if (callback) {
                    jQuery(this).bind("error", callback);
                }
            }
        });
    })(jQuery);

}
