(function($){$.fn.extend({elastic:function(){var mimics=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 lineHeight=parseInt($textarea.css('lineHeight'),10)||parseInt($textarea.css('fontSize'),'10');var minheight=parseInt($textarea.css('height'),10)||lineHeight*3;var maxheight=parseInt($textarea.css('max-height'),10)||Number.MAX_VALUE;var goalheight=0;var twin=null;var first=true;if(!twin){$twin=$('<div />').css({'position':'absolute','display':'none'}).appendTo($textarea.parent());$.each(mimics,function(){$twin.css(this.toString(),$textarea.css(this.toString()))})};function setHeight(height,overflow){curratedHeight=Math.floor(parseInt(height,10));if($textarea.height()!=curratedHeight){$textarea.css({'height':curratedHeight+'px','overflow':overflow})}}function update(){var textareaContent=$textarea.val().replace(/<|>/g,' ').replace(/\n/g,'<br />').replace(/&/g,"&amp;");var twinContent=$twin.html();if(textareaContent+'&nbsp;'!=twinContent){$twin.html(textareaContent+'&nbsp;');if(Math.abs($twin.height()+lineHeight-$textarea.height())>3){var goalheight=$twin.height()+lineHeight;if(goalheight>=maxheight)setHeight(maxheight,'auto');else if(goalheight<=minheight)setHeight(minheight,'hidden');else setHeight(goalheight,'hidden');if(first){temp=$textarea.val();$textarea.val('');setTimeout(function(){$textarea.val(temp)},1);first=false}}}}$textarea.css({'overflow':'hidden'}).bind('focus',function(){self.periodicalUpdater=window.setInterval(function(){update()},50)}).bind('blur',function(){clearInterval(self.periodicalUpdater)});update()})}})})(jQuery);