(function(factory){if(typeof define==='function'&&define.amd){define(['jquery'],factory);}
else{factory(jQuery);}}(function($){var Expanding=function($textarea,opts){Expanding._registry.push(this);this.$textarea=$textarea;this.$textCopy=$("<span/>");this.$clone=$("<pre class='expanding-clone'><br/></pre>").prepend(this.$textCopy);$textarea.wrap($("<div class='expanding-wrapper' style='position:relative'/>")).after(this.$clone);this.attach();this.setStyles();this.update();if(opts.update)$textarea.bind("update.expanding",opts.update);};Expanding._registry=[];Expanding.getExpandingInstance=function(textarea){var $textareas=$.map(Expanding._registry,function(instance){return instance.$textarea[0];}),index=$.inArray(textarea,$textareas);return index>-1?Expanding._registry[index]:null;};var ieVersion=(function(){var v=-1;if(navigator.appName==="Microsoft Internet Explorer"){var ua=navigator.userAgent;var re=new RegExp("MSIE ([0-9]{1,}[\\.0-9]{0,})");if(re.exec(ua)!==null)v=parseFloat(RegExp.$1);}
return v;})();var inputSupported="oninput"in document.createElement("input")&&ieVersion!==9;Expanding.prototype={attach:function(){var events='input.expanding change.expanding',_this=this;if(!inputSupported)events+=' keyup.expanding';this.$textarea.bind(events,function(){_this.update();});},update:function(){this.$textCopy.text(this.$textarea.val().replace(/\r\n/g,"\n"));this.$textarea.triggerHandler("update.expanding");},destroy:function(){this.$clone.remove();this.$textarea.unwrap().attr('style',this._oldTextareaStyles||'');delete this._oldTextareaStyles;var index=$.inArray(this,Expanding._registry);if(index>-1)Expanding._registry.splice(index,1);this.$textarea.unbind('input.expanding change.expanding keyup.expanding update.expanding');},setStyles:function(){this._resetStyles();this._setCloneStyles();this._setTextareaStyles();},_resetStyles:function(){this._oldTextareaStyles=this.$textarea.attr('style');this.$textarea.add(this.$clone).css({margin:0,webkitBoxSizing:"border-box",mozBoxSizing:"border-box",boxSizing:"border-box",width:"100%"});},_setCloneStyles:function(){var css={display:'block',border:'0 solid',visibility:'hidden',minHeight:this.$textarea.outerHeight()};if(this.$textarea.attr("wrap")==="off")css.overflowX="scroll";else css.whiteSpace="pre-wrap";this.$clone.css(css);this._copyTextareaStylesToClone();},_copyTextareaStylesToClone:function(){var _this=this,properties=['lineHeight','textDecoration','letterSpacing','fontSize','fontFamily','fontStyle','fontWeight','textTransform','textAlign','direction','wordSpacing','fontSizeAdjust','wordWrap','word-break','borderLeftWidth','borderRightWidth','borderTopWidth','borderBottomWidth','paddingLeft','paddingRight','paddingTop','paddingBottom','maxHeight'];$.each(properties,function(i,property){var val=_this.$textarea.css(property);if(_this.$clone.css(property)!==val){_this.$clone.css(property,val);if(property==='maxHeight'&&val!=='none'){_this.$clone.css('overflow','hidden');}}});},_setTextareaStyles:function(){this.$textarea.css({position:"absolute",top:0,left:0,height:"100%",resize:"none",overflow:"auto"});}};$.expanding=$.extend({autoInitialize:true,initialSelector:"textarea.expanding",opts:{update:function(){}}},$.expanding||{});$.fn.expanding=function(o){if(o==="destroy"){this.each(function(){var instance=Expanding.getExpandingInstance(this);if(instance)instance.destroy();});return this;}
if(o==="active"){return!!this.filter(function(){return!!Expanding.getExpandingInstance(this);}).length;}
if(o==="refresh"){this.each(function(){var instance=Expanding.getExpandingInstance(this);if(instance)instance.setStyles();});return this;}
var opts=$.extend({},$.expanding.opts,o);this.filter("textarea").each(function(){var visible=this.offsetWidth>0||this.offsetHeight>0,initialized=Expanding.getExpandingInstance(this);if(visible&&!initialized)new Expanding($(this),opts);else{if(!visible)_warn("ExpandingTextareas: attempt to initialize an invisible textarea. Call expanding() again once it has been inserted into the page and/or is visible.");if(initialized)_warn("ExpandingTextareas: attempt to initialize a textarea that has already been initialized. Subsequent calls are ignored.");}});return this;};function _warn(text){if(window.console&&console.warn)console.warn(text);}
$(function(){if($.expanding.autoInitialize){$($.expanding.initialSelector).expanding();}});}));