var Autocompleter=new Class({Implements:[Options,Events],options:{minLength:1,markQuery:true,width:"inherit",maxChoices:10,injectChoice:null,customChoices:null,emptyChoices:null,visibleChoices:true,className:"autocompleter-choices",zIndex:42,delay:400,observerOptions:{},fxOptions:{},autoSubmit:false,overflow:false,overflowMargin:25,selectFirst:false,filter:null,filterCase:false,filterSubset:false,forceSelect:false,selectMode:true,choicesMatch:null,multiple:false,separator:", ",separatorSplit:/\s*[,;]\s*/,autoTrim:false,allowDupes:false,cache:true,relative:false},initialize:function(A,B){this.element=$(A);this.setOptions(B);this.build();this.observer=new Observer(this.element,this.prefetch.bind(this),$merge({"delay":this.options.delay},this.options.observerOptions));this.queryValue=null;if(this.options.filter){this.filter=this.options.filter.bind(this)}var C=this.options.selectMode;this.typeAhead=(C=="type-ahead");this.selectMode=(C===true)?"selection":C;this.cached=[]},build:function(){if($(this.options.customChoices)){this.choices=this.options.customChoices}else{this.choices=new Element("ul",{"class":this.options.className,"styles":{"zIndex":this.options.zIndex}}).inject(document.body);this.relative=false;if(this.options.relative){this.choices.inject(this.element,"after");this.relative=this.element.getOffsetParent()}this.fix=new OverlayFix(this.choices)}if(!this.options.separator.test(this.options.separatorSplit)){this.options.separatorSplit=this.options.separator}this.fx=(!this.options.fxOptions)?null:new Fx.Tween(this.choices,$merge({"property":"opacity","link":"cancel","duration":200},this.options.fxOptions)).addEvent("onStart",Chain.prototype.clearChain).set(0);this.element.setProperty("autocomplete","off").addEvent((Browser.Engine.trident||Browser.Engine.webkit)?"keydown":"keypress",this.onCommand.bind(this)).addEvent("click",this.onCommand.bind(this,[false])).addEvent("focus",this.toggleFocus.create({bind:this,arguments:true,delay:100})).addEvent("blur",this.toggleFocus.create({bind:this,arguments:false,delay:100}))},destroy:function(){if(this.fix){this.fix.destroy()}this.choices=this.selected=this.choices.destroy()},toggleFocus:function(A){this.focussed=A;if(!A){this.hideChoices(true)}this.fireEvent((A)?"onFocus":"onBlur",[this.element])},onCommand:function(B){if(!B&&this.focussed){return this.prefetch()}if(B&&B.key&&!B.shift){switch(B.key){case"enter":if(this.element.value!=this.opted){return true}if(this.selected&&this.visible){this.choiceSelect(this.selected);return !!(this.options.autoSubmit)}break;case"up":case"down":if(!this.prefetch()&&this.queryValue!==null){var A=(B.key=="up");this.choiceOver((this.selected||this.choices)[(this.selected)?((A)?"getPrevious":"getNext"):((A)?"getLast":"getFirst")](this.options.choicesMatch),true)}return false;case"esc":case"tab":this.hideChoices(true);break}}return true},setSelection:function(H){var G=this.selected.inputValue,C=G;var I=this.queryValue.length,B=G.length;if(G.substr(0,I).toLowerCase()!=this.queryValue.toLowerCase()){I=0}if(this.options.multiple){var D=this.options.separatorSplit;C=this.element.value;I+=this.queryIndex;B+=this.queryIndex;var A=C.substr(this.queryIndex).split(D,1)[0];C=C.substr(0,this.queryIndex)+G+C.substr(this.queryIndex+A.length);if(H){var E=C.split(this.options.separatorSplit).filter(function(J){return this.test(J)},/[^\s,]+/);if(!this.options.allowDupes){E=[].combine(E)}var F=this.options.separator;C=E.join(F)+F;B=C.length}}this.observer.setValue(C);this.opted=C;if(H||this.selectMode=="pick"){I=B}this.element.selectRange(I,B);this.fireEvent("onSelection",[this.element,this.selected,C,G])},showChoices:function(){var I=this.options.choicesMatch,H=this.choices.getFirst(I);this.selected=this.selectedValue=null;if(this.fix){var A=this.element.getCoordinates(this.relative),B=this.options.width||"auto";this.choices.setStyles({"left":A.left,"top":A.bottom,"width":(B===true||B=="inherit")?A.width:B})}if(!H){return}if(!this.visible){this.visible=true;this.choices.setStyle("display","");if(this.fx){this.fx.start(1)}this.fireEvent("onShow",[this.element,this.choices])}if(this.options.selectFirst||this.typeAhead||H.inputValue==this.queryValue){this.choiceOver(H,this.typeAhead)}var C=this.choices.getChildren(I),F=this.options.maxChoices;var E={"overflowY":"hidden","height":""};this.overflown=false;if(C.length>F){var D=C[F-1];E.overflowY="scroll";E.height=D.getCoordinates(this.choices).bottom;this.overflown=true}this.choices.setStyles(E);this.fix.show();if(this.options.visibleChoices){var K=document.getScroll(),J=document.getSize(),G=this.choices.getCoordinates();if(G.right>K.x+J.x){K.x=G.right-J.x}if(G.bottom>K.y+J.y){K.y=G.bottom-J.y}window.scrollTo(Math.min(K.x,G.left),Math.min(K.y,G.top))}},hideChoices:function(B){if(B){var A=this.element.value;if(this.options.forceSelect){A=this.opted}if(this.options.autoTrim){A=A.split(this.options.separatorSplit).filter($arguments(0)).join(this.options.separator)}this.observer.setValue(A)}if(!this.visible){return}this.visible=false;if(this.selected){this.selected.removeClass("autocompleter-selected")}this.observer.clear();var C=function(){this.choices.setStyle("display","none");this.fix.hide()}.bind(this);if(this.fx){this.fx.start(0).chain(C)}else{C()}this.fireEvent("onHide",[this.element,this.choices])},prefetch:function(){var A=this.element.value,E=A;if(this.options.multiple){var D=this.options.separatorSplit;var F=A.split(D);var C=this.element.getSelectedRange().start;var G=A.substr(0,C).split(D);var B=G.length-1;C-=G[B].length;E=F[B]}if(E.length<this.options.minLength){this.hideChoices()}else{if(E===this.queryValue||(this.visible&&E==this.selectedValue)){if(this.visible){return false}this.showChoices()}else{this.queryValue=E;this.queryIndex=C;if(!this.fetchCached()){this.query()}}}return true},fetchCached:function(){return false;if(!this.options.cache||!this.cached||!this.cached.length||this.cached.length>=this.options.maxChoices||this.queryValue){return false}this.update(this.filter(this.cached));return true},update:function(B){this.choices.empty();this.cached=B;var A=B&&$type(B);if(!A||(A=="array"&&!B.length)||(A=="hash"&&!B.getLength())){(this.options.emptyChoices||this.hideChoices).call(this)}else{if(this.options.maxChoices<B.length&&!this.options.overflow){B.length=this.options.maxChoices}B.each(this.options.injectChoice||function(D){var C=new Element("li",{"html":this.markQueryValue(D)});C.inputValue=D;this.addChoiceEvents(C).inject(this.choices)},this);this.showChoices()}},choiceOver:function(A,D){if(!A||A==this.selected){return}if(this.selected){this.selected.removeClass("autocompleter-selected")}this.selected=A.addClass("autocompleter-selected");this.fireEvent("onSelect",[this.element,this.selected,D]);if(!this.selectMode){this.opted=this.element.value}if(!D){return}this.selectedValue=this.selected.inputValue;if(this.overflown){var C=this.selected.getCoordinates(this.choices),G=this.options.overflowMargin,E=this.choices.scrollTop,B=this.choices.offsetHeight,F=E+B;if(C.top-G<E&&E){this.choices.scrollTop=Math.max(C.top-G,0)}else{if(C.bottom+G>F){this.choices.scrollTop=Math.min(C.bottom-B+G,F)}}}if(this.selectMode){this.setSelection()}},choiceSelect:function(A){if(A){this.choiceOver(A)}this.setSelection(true);this.queryValue=false;this.hideChoices()},filter:function(A){return(A||this.tokens).filter(function(B){return this.test(B)},new RegExp(((this.options.filterSubset)?"":"^")+this.queryValue.escapeRegExp(),(this.options.filterCase)?"":"i"))},markQueryValue:function(A){return(!this.options.markQuery||!this.queryValue)?A:A.replace(new RegExp("("+((this.options.filterSubset)?"":"^")+this.queryValue.escapeRegExp()+")",(this.options.filterCase)?"":"i"),'<span class="autocompleter-queried">$1</span>')},addChoiceEvents:function(A){return A.addEvents({"mouseover":this.choiceOver.bind(this,[A]),"click":this.choiceSelect.bind(this,[A])})}});var OverlayFix=new Class({initialize:function(A){if(Browser.Engine.trident){this.element=$(A);this.relative=this.element.getOffsetParent();this.fix=new Element("iframe",{"frameborder":"0","scrolling":"no","src":"javascript:false;","styles":{"position":"absolute","border":"none","display":"none","filter":"progid:DXImageTransform.Microsoft.Alpha(opacity=0)"}}).inject(this.element,"after")}},show:function(){if(this.fix){var A=this.element.getCoordinates(this.relative);delete A.right;delete A.bottom;this.fix.setStyles($extend(A,{"display":"","zIndex":(this.element.getStyle("zIndex")||1)-1}))}return this},hide:function(){if(this.fix){this.fix.setStyle("display","none")}return this},destroy:function(){if(this.fix){this.fix=this.fix.destroy()}}});Element.implement({getSelectedRange:function(){if(!Browser.Engine.trident){return{start:this.selectionStart,end:this.selectionEnd}}var C={start:0,end:0};var A=this.getDocument().selection.createRange();if(!A||A.parentElement()!=this){return C}var D=A.duplicate();if(this.type=="text"){C.start=0-D.moveStart("character",-100000);C.end=C.start+A.text.length}else{var B=this.value;var E=B.length-B.match(/[\n\r]*$/)[0].length;D.moveToElementText(this);D.setEndPoint("StartToEnd",A);C.end=E-D.text.length;D.setEndPoint("StartToStart",A);C.start=E-D.text.length}return C},selectRange:function(B,C){if(Browser.Engine.trident){var D=this.value.substr(B,C-B).replace(/\r/g,"").length;B=this.value.substr(0,B).replace(/\r/g,"").length;var A=this.createTextRange();A.collapse(true);A.moveEnd("character",B+D);A.moveStart("character",B);A.select()}else{this.focus();this.setSelectionRange(B,C)}return this}});Autocompleter.Base=Autocompleter;