/* Copyright: Guillermo Rauch <http://devthought.com/> - Distributed under MIT - Keep this message! */

Element.implement({getCaretPosition:function(){if(this.createTextRange){var r=document.selection.createRange().duplicate();r.moveEnd("character",this.value.length);if(r.text===""){return this.value.length;}return this.value.lastIndexOf(r.text);}else{return this.selectionStart;}}});var ResizableTextbox=new Class({Implements:Options,options:{min:5,max:500,step:7},initialize:function(_2,_3){var _4=this;this.setOptions(_3);this.el=$(_2);this.el.$attributes.first=this.offsetWidth;this.el.addEvents({"keydown":function(){this.$attributes.last=this.value.length;},"keyup":function(){var _5=_4.options.step*this.value.length;if(_5<=_4.options.min){_5=this.$attributes.first;}if(this.value.length==this.$attributes.last||_5<=_4.options.min||_5>=_4.options.max){return;}this.setStyle("width",_5);}});}});var TextboxList=new Class({Implements:[Events,Options],options:{resizable:{},className:"bit",separator:"###",extrainputs:true,startinput:true,hideempty:true},initialize:function(_6,_7){this.setOptions(_7);this.element=$(_6).setStyle("display","none");this.bits=new Hash;this.events=new Hash;this.count=0;this.current=false;this.maininput=this.createInput({"class":"maininput"});this.holder=new Element("ul",{"class":"holder","events":{"click":function(e){e=new Event(e).stop();this.focus(this.maininput);}.bind(this)}}).inject(this.element,"before").adopt(this.maininput);this.makeResizable(this.maininput);document.addEvent(Browser.Engine.trident?"keydown":"keypress",function(e){if(!this.current){return;}e=new Event(e);if(this.current.$attributes.$type=="box"&&e.code==Event.Keys.backspace){e.stop();}}.bind(this));document.addEvents({"keyup":function(e){e=new Event(e).stop();if(!this.current){return;}switch(e.code){case Event.Keys.left:return this.move("left");case Event.Keys.right:return this.move("right");case Event.Keys.backspace:return this.moveDispose();}}.bind(this),"click":function(){this.fireEvent("onBlur").blur();}.bind(this)});},update:function(){this.element.set("value",this.bits.getValues().join(this.options.separator));return this;},add:function(_b,_c){var _d=this;var id=this.options.className+"-"+this.count++;var el=this.createBox($pick(_c,_b),{"id":id}).inject(this.current||this.maininput,"before").addEvent("click",function(e){e=new Event(e).stop();_d.focus(this);});this.bits.set(id,_b);if(this.options.extrainputs&&(this.options.startinput||el.getPrevious())){this.addSmallInput(el,"before");}return el;},addSmallInput:function(el,_12){var _13=this.createInput({"class":"smallinput"}).inject(el,_12);_13.$attributes.$small=true;this.makeResizable(_13);if(this.options.hideempty){_13.setStyle("display","none");}return _13;},dispose:function(el){this.bits.remove(el.id);if(el.getPrevious().$attributes.$small){el.getPrevious().dispose();}if(this.current==el){this.focus(el.getNext());}if(el.$attributes.$type=="box"){this.fireEvent("onBoxDispose",el);}el.dispose();return this;},focus:function(el,_16){if(!this.current){this.fireEvent("onFocus",el);}else{if(this.current==el){return this;}}this.blur();el.addClass(this.options.className+"-"+el.$attributes.$type+"-focus");if(el.$attributes.$small){el.setStyle("display","block");}if(el.$attributes.$input){if(!_16){this.callEvent(el.$attributes.$input,"focus");}if(this.current!=el){this.fireEvent("onInputFocus",el);}}else{this.fireEvent("onBoxFocus",el);}this.current=el;return this;},blur:function(_17){if(!this.current){return;}if(this.current.$attributes.$input){if(!_17){this.callEvent(this.current.$attributes.$input,"blur");}this.fireEvent("onInputBlur",this.current.$attributes.$input);}else{this.fireEvent("onBoxBlur",this.current);}if(this.current.$attributes.$small&&!this.current.$attributes.$input.value&&this.options.hideempty){this.current.setStyle("display","none");}this.current.removeClass(this.options.className+"-"+this.current.$attributes.$type+"-focus");this.current=false;return this;},createBox:function(_18,_19){var li=new Element("li",$extend(_19||{},{"class":this.options.className+"-box"})).set("html",_18);li.$attributes.$type="box";return li;},createInput:function(_1b){var li=new Element("li",{"class":this.options.className+"-input"});var el=new Element("input",$extend(_1b,{"type":"text","events":{"click":function(e){e=new Event(e).stop();},"focus":function(e){if(this.isSelfEvent("focus")){return;}this.focus(li,true);}.bind(this),"blur":function(){if(this.isSelfEvent("blur")){return;}this.blur(true);}.bind(this),"keydown":function(e){this.$attributes.$lastvalue=this.value;this.$attributes.$lastcaret=this.getCaretPosition();}}}));li.$attributes.$type="input";li.$attributes.$input=el;return li.adopt(el);},callEvent:function(el,_22){this.events.set(_22,el);el[_22]();},isSelfEvent:function(_23){return (this.events.get(_23))?!!this.events.remove(_23):false;},makeResizable:function(li){var el=li.$attributes.$input;el.$attributes.$resizable=new ResizableTextbox(el,$extend(this.options.resizable,{min:el.offsetWidth,max:this.element.getStyle("width").toInt()}));return this;},checkInput:function(){var _26=this.current.$attributes.$input;return (!_26.$attributes.$lastvalue||(_26.getCaretPosition()===0&&_26.$attributes.$lastcaret===0));},move:function(_27){var el=this.current["get"+(_27=="left"?"Previous":"Next")]();if(el&&(!this.current.$attributes.$input||((this.checkInput()||_27=="right")&&(el.$attributes.$type!=this.current.$attributes.$type)))){this.focus(el);}return this;},moveDispose:function(){if(this.current.$attributes.$type=="box"){return this.dispose(this.current);}else{if(this.checkInput()&&this.bits.getKeys().length&&this.current.getPrevious()){this.focus(this.current.getPrevious());}}}});
