(function($){
	$.fn.ImageAutoSize = function(){
		var _o = this;
		_o.options = $.extend({
				w : 120, h : 90,
				callback : null
		},arguments[0]);
		_o.width = _o.options.w;
		_o.height= _o.options.h;
		return this.each(function(){
			var _tmp = $(this).hide();
			var loadImage = new Image();
			loadImage.onload = function(){
				_o.getWidth = _tmp.width();
				_o.getHeight= _tmp.height();
				_o.Percent  = _o.getHeight/_o.getWidth;
				_o.setWidth = _o.getWidth;
				_o.setHeight= _o.getHeight;
				if(_o.getWidth>_o.width){
					_o.setWidth = _o.width;
					_o.setHeight=_o.width*_o.Percent;
					if(_o.setHeight>_o.height){
						_o.setWidth = _o.height/_o.Percent;
						_o.setHeight = _o.height;
					}
				}else if(_o.getHeight>_o.height){
					_o.setWidth = _o.height/_o.Percent;
					_o.setHeight= _o.height;
					if(_o.setWidth>_o.width){
						_o.setWidth = _o.width;
						_o.setHeight = _o.width*_o.Percent;
					}
				}
				_tmp.width(_o.setWidth);
				_tmp.height(_o.setHeight);
				if($.isFunction(_o.options.callback))
					_o.options.callback.apply(_tmp,[{left:_o.width/2-_o.setWidth/2,top:_o.height/2-_o.setHeight/2}]);
				if(_tmp.is(':hidden')) _tmp.show();
				loadImage = null;
			}
			loadImage.src = _tmp.attr('src');
		});
	}
})(jQuery);

