(function($){$.fn.extend({elastic:function(){var j=new Array('paddingTop','paddingRight','paddingBottom','paddingLeft','fontSize','lineHeight','fontFamily','width','fontWeight');return this.each(function(){if(this.type!='textarea')return false;var $textarea=$(this);var d=parseInt($textarea.css('lineHeight'),10)||parseInt($textarea.css('fontSize'),'10');var e=parseInt($textarea.css('height'),10)||d*3;var f=parseInt($textarea.css('max-height'),10)||Number.MAX_VALUE;var g=0;var h=null;var i=true;if(!h){$twin=$('<div />').css({'position':'absolute','display':'none'}).appendTo($textarea.parent());$.each(j,function(){$twin.css(this.toString(),$textarea.css(this.toString()))})};function setHeight(a,b){curratedHeight=Math.floor(parseInt(a,10));if($textarea.height()!=curratedHeight){$textarea.css({'height':curratedHeight+'px','overflow':b})}}function update(){var a=$textarea.val().replace(/<|>/g,' ').replace(/\n/g,'<br />').replace(/&/g,"&amp;");var b=$twin.html();if(a+'&nbsp;'!=b){$twin.html(a+'&nbsp;');if(Math.abs($twin.height()+d-$textarea.height())>3){var c=$twin.height()+d;if(c>=f)setHeight(f,'auto');else if(c<=e)setHeight(e,'hidden');else setHeight(c,'hidden');if(i){temp=$textarea.val();$textarea.val('');setTimeout(function(){$textarea.val(temp)},1);i=false}}}}$textarea.css({'overflow':'hidden'}).bind('focus',function(){self.periodicalUpdater=window.setInterval(function(){update()},50)}).bind('blur',function(){clearInterval(self.periodicalUpdater)});update()})}})})(jQuery);
