/*!
 * jQuery FirstMind plugin: Common Lib
 * version 1.00 (2009-10-19)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 * http://www.firstmind.kr
 */

/**
 * @author Jay Chin (http://zzin.com)
 */

(function($) { 

    $.fn.fm_imgResize = function(options) {
        if(options) {
            return this.each(function() {
                $.fm_imgResize(this, options);
            });
        } else {
            return this;
        }
    };

    $.fm_imgResize = function(container, options) {
        var maxsize = options; // ±âÁØÀÌ µÇ´Â max size
        var imgwidth = $(container).attr('width');  // ÇØ´ç imgÀÇ ¼Ó¼º Áß Æø(width)ÀÇ °ª
        var imgheight = $(container).attr('height');  // ÇØ´ç imgÀÇ ¼Ó¼º Áß ³ôÀÌ(height)ÀÇ °ª
        var ratio = imgwidth/imgheight; // ºñÀ²
        if(imgwidth>imgheight) { // °¡·Î°¡ ±ä ÀÌ¹ÌÁö
            var newWidth = maxsize;
            var newHeight = Math.round(maxsize/ratio);  // »õ ³ôÀÌ´Â max size ÀÇ ¿ø·¡ ºñÀ²¿¡ ¸ÂÃã
        } else {
            var newWidth = Math.round(maxsize*ratio);  // »õ ±æÀÌ´Â´Â max size ÀÇ ¿ø·¡ ºñÀ²¿¡ ¸ÂÃã
            var newHeight = maxsize;
        }
        $(container).width(newWidth).height(newHeight);  // Æø°ú ³ôÀÌ¸¦ »õ·Î ÁöÁ¤

        //alert("w:"+newWidth + " h:"+newHeight);
        return container;
    };

})(jQuery);
