﻿var Texpand=(function(){var g=Class.create();g.Version='0.9.5';g.FeatureTests={PRE_WRAP_FORMATTING_IS_KEPT:(function(){var a=false;var c=i();if(c=='pre')a=true;else{var d=document.documentElement,b=document.createElement('div');b.style.whiteSpace=c;d.appendChild(b);b.appendChild(document.createTextNode('aa'));var f=b.offsetHeight;b.firstChild.nodeValue='a\na';a=(b.offsetHeight===0)||(f==b.offsetHeight);d.removeChild(b);b=f=null}return!a})(),SUPPORTED_PRE_WRAP_STYLE:i()};g.options={increment:5,autoShrink:false,expandOnLoad:false,expandOnFocus:false,shrinkOnBlur:false,tabSize:4,onExpand:Prototype.emptyFunction};g.prototype={initialize:function(a,c){this.options={};Object.extend(this.options,g.options);Object.extend(this.options,c||{});this.element=$(a);if(this.element.tagName.toLowerCase()!='textarea'){throw(new Error('Texpand: can only be initialized with a <textarea> but got <'+this.element.tagName.toLowerCase()+'>'));}if(typeof Prototype=='undefined'||(parseFloat(Prototype.Version.split(".")[0]+"."+Prototype.Version.split(".")[1])<1.6)){throw(new Error('Texpand: requires Prototype 1.6.0+'));}if(typeof Effect=='undefined'){throw(new Error('Textpand: requires Script.aculo.us, specifically Effects'));}this.element.value=this.element.value.replace(/\t/g,' '.times(this.options.tabSize));this.element.insert({after:'<div id="texpand-mimic-'+this.element.identify()+'Parent"><div id="texpand-mimic-'+this.element.identify()+'">'+this.element.value+'</div></div>'}).setStyle({resize:'none',overflow:'hidden'});this.mimic=this.element.next().down();this._1();this._2();this.initialHeight=parseInt(this.options.initialHeight||this.element.getHeight(),10);this.element.observe("keyup",this._0.bind(this));if(this.options.expandOnFocus)this.element.observe("focus",this._0.bind(this));if(this.options.shrinkOnBlur)this.element.observe("blur",this._4.bind(this));if(this.options.expandOnLoad)this._0();return this.element},_2:function(){if(!this.elementStyles&&!this.mimicStyles){this.elementStyles={};this.mimicStyles={};var c=this.element.getStyle('fontSize');this.elementStyles.fontSize=c;if(c.search(/em/)>=0){var d=parseFloat(c.replace(/em/,''))*10;this.elementStyles.fontSize=d+'px'}var b=$w('borderBottomColor borderBottomStyle borderBottomWidth borderTopColor borderTopStyle borderTopWidth borderRightColor borderRightStyle borderRightWidth borderLeftColor borderLeftStyle borderLeftWidth fontSize fontFamily fontWeight letterSpacing lineHeight marginTop marginRight marginBottom marginLeft paddingTop paddingRight paddingBottom paddingLeft textAlign textIndent width wordSpacing');for(var f=0,k=b.length;f<k;f++){this.mimicStyles[b[f]]=this.element.getStyle(b[f])}this.mimicStyles.display='block';this.mimicStyles.position='absolute';this.mimicStyles.left='-9999px';this.mimicStyles.top='-9999px';if(g.FeatureTests.PRE_WRAP_FORMATTING_IS_KEPT)this.mimicStyles.whiteSpace=g.FeatureTests.SUPPORTED_PRE_WRAP_STYLE;var h=this.element.getWidth();if(h!=this.mimic.getWidth()){var j;$w('marginLeft marginRight borderLeftWidth borderRightWidth paddingLeft paddingRight').each(function(a){h-=isNaN(j=parseInt(this.element.getStyle(a),10))?0:j}.bind(this));h+='px';this.mimicStyles.width=h}}this.mimic.setStyle(this.mimicStyles);this.element.setStyle(this.elementStyles);this.element.setStyle({fontFamily:this.mimic.getStyle('fontFamily')})},_1:function(){if(g.FeatureTests.PRE_WRAP_FORMATTING_IS_KEPT){(this.mimic.firstChild?this.mimic.firstChild:this.mimic.appendChild(document.createTextNode(''))).nodeValue=this.element.value.replace(/\r\n/g,"\n")+"\n"}else{var a=this.mimic.up();a.update('<pre id="texpand-mimic-'+this.element.identify()+'">'+this.element.value+"\n</pre>");this.mimic=a.down();this._2();this.mimic.setStyle({wordWrap:'break-word'})}},_3:function(c){var d=Effect.Queues.get('texpand'+this.element.identify());d.each(function(a){a.cancel()});this.element.morph('height: '+c+'px;',{duration:0.05,queue:{position:'end',scope:'texpand'+this.element.identify(),limit:2}})},_4:function(a){this._3(this.initialHeight)},_0:function(a){if(a){clearTimeout(this.keypressDelay);this.keypressDelay=this.doExpand.bind(this).delay(0.1,a)}else this.doExpand()},doExpand:function(a){this._1();var c=this.mimic.getHeight();var d=this.element.getHeight();var b=d-c;var f;if(this.element.value==='')f=this.initialHeight;else f=d+(this.options.increment-b);if((this.options.autoShrink&&(b>this.options.increment)||this.element.value==='')||b<this.options.increment){this._3(f);this.options.onExpand.call(a)}},appendText:function(a){this.element.value+=a;this._0()},setValue:function(a){this.element.value=a;this._0()}};function i(){var c,d=document.documentElement,b=new Element('div');d.appendChild(b);$w('pre-wrap -moz-pre-wrap -pre-wrap -o-pre-wrap pre').each(function(a){try{b.setStyle({'whiteSpace':a});if(b.getStyle('whiteSpace')!=a)throw(new Error('pre-wrap type error'));}catch(e){return}c=a;throw $break;});d.removeChild(b);b=null;return c}return g})();