var Autocompleter={};Autocompleter.Base=new Class({options:{minLength:1,useSelection:true,markQuery:true,inheritWidth:true,maxChoices:25,injectChoice:null,onSelect:Class.empty,onShow:Class.empty,onHide:Class.empty,customTarget:null,className:"autocompleter-choices",zIndex:42,observerOptions:{},fxOptions:{},overflown:[]},initialize:function(B,A){this.setOptions(A);this.element=$(B);this.build();this.observer=new Observer(this.element,this.prefetch.bind(this),$merge({delay:400},this.options.observerOptions));this.value=this.observer.value;this.queryValue=null},build:function(){if($(this.options.customTarget)){this.choices=this.options.customTarget}else{this.choices=new Element("ul",{"class":this.options.className,styles:{zIndex:this.options.zIndex}}).injectInside(document.body);this.fix=new OverlayFix(this.choices)}this.fx=this.choices.effect("opacity",$merge({wait:false,duration:200},this.options.fxOptions)).addEvent("onStart",function(){if(this.fx.now){return }this.choices.setStyle("display","");this.fix.show()}.bind(this)).addEvent("onComplete",function(){if(this.fx.now){return }this.choices.setStyle("display","none");this.fix.hide()}.bind(this)).set(0);this.element.setProperty("autocomplete","off").addEvent(window.ie?"keydown":"keypress",this.onCommand.bindWithEvent(this)).addEvent("mousedown",this.onCommand.bindWithEvent(this,[true])).addEvent("focus",this.toggleFocus.bind(this,[true])).addEvent("blur",this.toggleFocus.bind(this,[false])).addEvent("trash",this.destroy.bind(this))},destroy:function(){this.choices.remove()},toggleFocus:function(A){this.focussed=A;if(!A){this.hideChoices()}},onCommand:function(B,A){if(A&&this.focussed){this.prefetch()}if(B.key&&!B.shift){switch(B.key){case"enter":if(this.selected&&this.visible){this.choiceSelect(this.selected);B.stop()}return ;case"up":case"down":if(this.observer.value!=(this.value||this.queryValue)){this.prefetch()}else{if(this.queryValue===null){break}else{if(!this.visible){this.showChoices()}else{this.choiceOver((B.key=="up")?this.selected.getPrevious()||this.choices.getLast():this.selected.getNext()||this.choices.getFirst());this.setSelection()}}}B.stop();return ;case"esc":this.hideChoices();return }}this.value=false},setSelection:function(){if(!this.options.useSelection){return }var C=this.queryValue.length;if(this.element.value.indexOf(this.queryValue)!=0){return }var B=this.selected.inputValue.substr(C);if(document.getSelection){this.element.value=this.queryValue+B;this.element.selectionStart=C;this.element.selectionEnd=this.element.value.length}else{if(document.selection){var A=document.selection.createRange();A.text=B;A.move("character",-B.length);A.findText(B);A.select()}}this.value=this.observer.value=this.element.value},hideChoices:function(){if(!this.visible){return }this.visible=this.value=false;this.observer.clear();this.fx.start(0);this.fireEvent("onHide",[this.element,this.choices])},showChoices:function(){if(this.visible||!this.choices.getFirst()){return }this.visible=true;var A=this.element.getCoordinates(this.options.overflown);this.choices.setStyles({left:A.left,top:A.bottom});if(this.options.inheritWidth){this.choices.setStyle("width",A.width)}this.fx.start(1);this.choiceOver(this.choices.getFirst());this.fireEvent("onShow",[this.element,this.choices])},prefetch:function(){if(this.element.value.length<this.options.minLength){this.hideChoices()}else{if(this.element.value==this.queryValue){this.showChoices()}else{this.query()}}},updateChoices:function(A){this.choices.empty();this.selected=null;if(!A||!A.length){return }if(this.options.maxChoices<A.length){A.length=this.options.maxChoices}A.each(this.options.injectChoice||function(B,C){var D=new Element("li").setHTML(this.markQueryValue(B));D.inputValue=B;this.addChoiceEvents(D).injectInside(this.choices)},this);this.showChoices()},choiceOver:function(A){if(this.selected){this.selected.removeClass("autocompleter-selected")}this.selected=A.addClass("autocompleter-selected")},choiceSelect:function(A){this.observer.value=this.element.value=A.inputValue;this.hideChoices();this.fireEvent("onSelect",[this.element],20)},markQueryValue:function(A){return(this.options.markQuery&&this.queryValue)?A.replace(new RegExp("("+this.queryValue.escapeRegExp()+")","i"),'<span class="autocompleter-queried">$1</span>'):A},addChoiceEvents:function(A){return A.addEvents({mouseover:this.choiceOver.bind(this,[A]),mousedown:this.choiceSelect.bind(this,[A])})}});Autocompleter.Base.implement(new Events);Autocompleter.Base.implement(new Options);Autocompleter.Local=Autocompleter.Base.extend({options:{minLength:0,filterTokens:null},initialize:function(B,C,A){this.parent(B,A);this.tokens=C;if(this.options.filterTokens){this.filterTokens=this.options.filterTokens.bind(this)}},query:function(){this.hideChoices();this.queryValue=this.element.value;this.updateChoices(this.filterTokens())},filterTokens:function(A){var B=new RegExp("^"+this.queryValue.escapeRegExp(),"i");return this.tokens.filter(function(C){return B.test(C)})}});Autocompleter.Ajax={};Autocompleter.Ajax.Base=Autocompleter.Base.extend({options:{postVar:"value",postData:{},ajaxOptions:{},onStartRequest:Class.empty,onRequest:Class.empty,onComplete:Class.empty},initialize:function(C,B,A){this.parent(C,A);this.ajax=new Ajax(B,$merge({autoCancel:true},this.options.ajaxOptions));this.ajax.addEvent("onComplete",this.queryResponse.bind(this));this.ajax.addEvent("onFailure",this.queryResponse.bind(this,[false]))},query:function(){this.fireEvent("onStartRequest",[this.element,this.ajax]);var A=$extend({},this.options.postData);A[this.options.postVar]=this.element.value;this.fireEvent("onRequest",[this.element,this.ajax]);this.ajax.request(A)},queryResponse:function(A){this.value=this.queryValue=this.element.value;this.selected=false;this.hideChoices();this.fireEvent(A?"onComplete":"onFailure",[this.element,this.ajax],20)}});Autocompleter.Ajax.Json=Autocompleter.Ajax.Base.extend({queryResponse:function(A){this.parent(A);var B=Json.evaluate(A||false);if(!B||!B.length){return }this.updateChoices(B)}});Autocompleter.Ajax.Xhtml=Autocompleter.Ajax.Base.extend({options:{parseChoices:null},queryResponse:function(A){this.parent(A);if(!A){return }this.choices.setHTML(A).getChildren().each(this.options.parseChoices||this.parseChoices,this);this.showChoices()},parseChoices:function(A){var B=A.innerHTML;A.inputValue=B;A.setHTML(this.markQueryValue(B))}});var OverlayFix=new Class({initialize:function(A){this.element=$(A);if(window.ie){this.element.addEvent("trash",this.destroy.bind(this));this.fix=new Element("iframe",{properties:{frameborder:"0",scrolling:"no",src:"javascript:false;"},styles:{position:"absolute",border:"none",display:"none",filter:"progid:DXImageTransform.Microsoft.Alpha(opacity=0)"}}).injectAfter(this.element)}},show:function(){if(this.fix){this.fix.setStyles($extend(this.element.getCoordinates(),{display:"",zIndex:(this.element.getStyle("zIndex")||1)-1}))}return this},hide:function(){if(this.fix){this.fix.setStyle("display","none")}return this},destroy:function(){this.fix.remove()}});