/*
UvumiTools TextArea v1.1.0 http://tools.uvumi.com/textarea.html

Copyright (c) 2008 Uvumi LLC

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/

var UvumiTextarea=Class({Implements:Options,options:{selector:'textarea',maxChar:1000,resizeDuration:250,minSize:false,catchTab:true,classPrefix:'tb'},initialize:function(a){this.setOptions(a);this.tbDummies=[];this.tbCounters=[];this.tbProgress=[];this.tbProgressBar=[];window.addEvent('domready',this.domReady.bind(this))},domReady:function(){if($(this.options.selector)){this.options.selector=$(this.options.selector)}this.textareas=$$(this.options.selector);this.textareas.each(this.buildProgress,this);if(this.options.maxChar){this.tbProgressEffects=new Fx.Elements(this.tbProgressBar,{duration:'short',link:'cancel'})}this.tbEffects=new Fx.Elements(this.textareas,{duration:this.options.resizeDuration,link:'cancel'});this.textareas.each(function(a,i){var b=a.get('value');this.previousLength=b.length;if(this.options.maxChar){if(this.previousLength>this.options.maxChar){b=b.substring(0,this.options.maxChar);this.previousLength=b.length;a.set('value',b)}var c=this.options.maxChar-this.previousLength;var d=(c*this.tbProgress[i].getSize().x/this.options.maxChar).toInt();this.tbProgressBar[i].setStyle('width',d);if(!c){var e='No character left'}else if(c==1){var e='1 character left'}else{var e=c+' characters left'}this.tbCounters[i].set('text',e)}this.tbDummies[i].set('value',b);var f=(this.tbDummies[i].getScrollSize().y>this.options.minSize?this.tbDummies[i].getScrollSize().y:this.options.minSize);if(this.tbDummies[i].retrieve('height')!=f){this.tbDummies[i].store('height',f);a.setStyle('height',f)}},this)},buildProgress:function(a,i){a.setStyle('overflow','hidden');if(!this.options.minSize){this.options.minSize=a.getSize().y}this.tbDummies[i]=a.clone().setStyles({'width':a.getStyle('width').toInt(),'position':'absolute','top':0,'height':this.options.minSize,'left':-3000}).store('height',0).inject($(document.body));a.addEvents({'keydown':this.onKeyPress.bindWithEvent(this,[i,this.options.catchTab]),'keyup':this.onKeyPress.bindWithEvent(this,i),'focus':this.startObserver.bind(this,i),'blur':this.stopObserver.bind(this)});if(this.options.maxChar){this.tbProgress[i]=new Element('div',{'class':this.options.classPrefix+'Progress','styles':{'position':'relative','overflow':'hidden','display':'block','position':'relative','width':a.getSize().x-1,'margin':'5px 0 5px '+a.getPosition(a.getParent()).x+'px'}}).inject(a,'after');this.tbProgressBar[i]=new Element('div',{'class':this.options.classPrefix+'ProgressBar','styles':{'position':'absolute','top':0,'left':0,'height':'100%','width':'100%'}}).inject(this.tbProgress[i]);this.tbCounters[i]=new Element('div',{'class':this.options.classPrefix+'Counter','styles':{'position':'absolute','top':0,'left':0,'height':'100%','width':'100%','text-align':'center'}}).inject(this.tbProgress[i]);this.update=this.updateCounter}else{this.update=this.updateNoCounter}},onKeyPress:function(a,i,b){if(b&&a.key=="tab"){a.preventDefault();this.insertTab(i)}if(!a.shift&&!a.control&&!a.alt&&!a.meta){this.update(i)}this.startObserver(i)},startObserver:function(i){$clear(this.observer);this.observer=this.observe.periodical(500,this,i)},stopObserver:function(){$clear(this.observer)},observe:function(i){if(this.textareas[i].get('value').length!=this.previousLength){this.previousLength=this.textareas[i].get('value').length;this.update(i)}},updateCounter:function(i){var a=this.textareas[i].get('value');if(a.length>this.options.maxChar){a=a.substring(0,this.options.maxChar);this.textareas[i].set('value',a)}this.previousLength=a.length;var b=this.options.maxChar-this.previousLength;var c=(b*this.tbProgress[i].getSize().x/this.options.maxChar).toInt();var d={};d[i]={'width':c};this.tbProgressEffects.start(d);if(b==0){var e='No character left';this.tbProgress[i].highlight("#f66")}else if(b==1){var e='1 character left'}else{var e=b+' characters left'}this.tbCounters[i].set('text',e);this.updateHeight(i,a)},updateNoCounter:function(i){var a=this.textareas[i].get('value');this.previousLength=a.length;this.updateHeight(i,a)},updateHeight:function(i,a){this.tbDummies[i].set('value',a);var b=(this.tbDummies[i].getScrollSize().y>this.options.minSize?this.tbDummies[i].getScrollSize().y:this.options.minSize);if(this.tbDummies[i].retrieve('height')!=b){this.tbDummies[i].store('height',b);effect={};effect[i]={'height':b};this.tbEffects.start(effect)}},insertTab:function(i){if(Browser.Engine.trident){var a=document.selection.createRange();a.text="\t"}else{var b=this.textareas[i].selectionStart;var c=this.textareas[i].selectionEnd;var d=this.textareas[i].get('value');this.textareas[i].set('value',d.substring(0,b)+"\t"+d.substring(c,d.length));b++;this.textareas[i].setSelectionRange(b,b)}}});