var Autocompleter={}; Autocompleter.Base=new Class({options:{targetElement:null,minLength:3,markQuery:true,width:"400px",maxChoices:10,injectChoice:null,customChoices:null,className:"autocompleter-choices",zIndex:42,delay:250,observerOptions:{},fxOptions:{},onOver:$empty,onSelect:$empty,onSelection:$empty,onShow:$empty,onHide:$empty,onBlur:$empty,onFocus:$empty,autoSubmit:false,autoSubmitFormName:"",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:true,allowDupes:false,cache:true,relative:false},initialize:function(a,c){this.element=$(a);this.setOptions(c);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 b=this.options.selectMode;this.typeAhead=b=="type-ahead";this.selectMode= b===true?"selection":b;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(){this.fix&&this.fix.destroy();this.choices=this.selected=this.choices.destroy()},toggleFocus:function(a){(this.focussed=a)||this.hideChoices(true);this.fireEvent(a?"onFocus":"onBlur",[this.element])},onCommand:function(a){if(!a&&this.focussed)return this.prefetch();if(a&&a.key&&!a.shift)switch(a.key){case "enter":this.setSelection();if(this.element.value!=this.opted)break;if(this.selected&&this.visible){this.choiceSelect(this.selected); return!!this.options.autoSubmit}break;case "up":case "down":if(!this.prefetch()&&this.queryValue!==null){a=a.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)}return true},setSelection:function(a){var c=this.selected.inputValue,b=c,d=this.queryValue.length,e=c.length;if(c.substr(0,d).toLowerCase()!=this.queryValue.toLowerCase())d=0;if(this.options.multiple){var f= this.options.separatorSplit;b=this.element.value;d+=this.queryIndex;e+=this.queryIndex;f=b.substr(this.queryIndex).split(f,1)[0];b=b.substr(0,this.queryIndex)+c+b.substr(this.queryIndex+f.length);if(a){e=/[^\s,]+/;b=b.split(this.options.separatorSplit).filter(e.test,e);this.options.allowDupes||(b=[].combine(b));e=this.options.separator;b=b.join(e)+e;e=b.length}}this.observer.setValue(b);this.opted=b;if(a||this.selectMode=="pick")d=e;this.element.selectRange(d,e);this.fireEvent("onSelection",[this.element, this.selected,b,c]);this.options.autoSubmit&&this.options.autoSubmitFormName!=""&&document.forms[this.options.autoSubmitFormName].submit()},showChoices:function(){var a=this.options.choicesMatch,c=this.choices.getFirst(a);this.selected=this.selectedValue=null;if(this.fix){var b;b=this.options.targetElement?this.options.targetElement.getCoordinates(this.relative):this.element.getCoordinates(this.relative);width=this.options.width||"auto";this.choices.setStyles({left:b.left-365,top:b.bottom,width:width=== true||width=="inherit"?b.width:width})}if(c){if(!this.visible){this.visible=true;this.choices.setStyle("display","");this.fx&&this.fx.start(1);this.fireEvent("onShow",[this.element,this.choices])}if(this.options.selectFirst||this.typeAhead||c.inputValue==this.queryValue)this.choiceOver(c,this.typeAhead);c=this.choices.getChildren(a);b=this.options.maxChoices;a={overflowY:"hidden",height:""};this.overflown=false;if(c.length>b){c=c[b-1];a.overflowY="scroll";a.height=c.getCoordinates(this.choices).bottom; this.overflown=true}this.choices.setStyles(a);this.fix.show()}},hideChoices:function(a){if(a){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){this.visible=false;this.observer.clear();a=function(){this.choices.setStyle("display","none");this.fix.hide()}.bind(this);this.fx?this.fx.start(0).chain(a):a();this.fireEvent("onHide",[this.element, this.choices])}},prefetch:function(){var a=this.element.value,c=a;if(this.options.multiple){var b=this.options.separatorSplit;c=a.split(b);var d=this.element.getCaretPosition();a=a.substr(0,d).split(b);b=a.length-1;d-=a[b].length;c=c[b]}if(c.length<this.options.minLength)this.hideChoices();else if(c===this.queryValue||this.visible&&c==this.selectedValue){if(this.visible)return false;this.showChoices()}else{this.queryValue=c;this.queryIndex=d;this.fetchCached()||this.query()}return true},fetchCached:function(){return false}, markQueryValue:function(a){return!this.options.markQuery||!this.queryValue?a:a.replace(RegExp("("+(this.options.filterSubset?"":"^")+this.queryValue.escapeRegExp()+")",this.options.filterCase?"":"i"),'<span class="autocompleter-queried">$1</span>')},update:function(a){this.choices.empty();this.cached=a;if(!a||!a.length)this.hideChoices();else{if(this.options.maxChoices<a.length&&!this.options.overflow)a.length=this.options.maxChoices;a.each(this.options.injectChoice||function(c){var b=new Element("li", {html:c.short_name});b.inputValue=c.name;this.addChoiceEvents(b).inject(this.choices)},this);this.showChoices()}},choiceOver:function(a,c){if(!(!a||a==this.selected)){this.selected&&this.selected.removeClass("autocompleter-selected");if(this.queryValue!=a.inputValue){this.selected=a.addClass("autocompleter-selected");this.fireEvent("onSelect",[this.element,this.selected,c]);if(c){this.selectedValue=this.selected.inputValue;if(this.overflown){var b=this.selected.getCoordinates(this.choices),d=this.options.overflowMargin, e=this.choices.scrollTop,f=this.choices.offsetHeight,g=e+f;if(b.top-d<e&&e)this.choices.scrollTop=Math.max(b.top-d,0);else if(b.bottom+d>g)this.choices.scrollTop=Math.min(b.bottom-f+d,g)}}}else this.selected=a}},choiceSelect:function(a){a&&this.choiceOver(a);this.setSelection(true);this.queryValue=false;this.hideChoices()},filter:function(a){var c=RegExp((this.options.filterSubset?"":"^")+this.queryValue.escapeRegExp(),this.options.filterCase?"":"i");return(a||this.tokens).filter(c.test,c)},addChoiceEvents:function(a){return a.addEvents({mouseover:this.choiceOver.bind(this, [a]),click:this.choiceSelect.bind(this,[a])})}});Autocompleter.Base.implement(new Events);Autocompleter.Base.implement(new Options);Autocompleter.Local=new Class({Extends:Autocompleter.Base,options:{minLength:0,delay:200},initialize:function(a,c,b){this.parent(a,b);this.tokens=c},query:function(){this.update(this.filter())}});Autocompleter.Ajax={}; Autocompleter.Ajax.Base=new Class({Extends:Autocompleter.Base,options:{postVar:"value",postData:{},ajaxOptions:{},onRequest:$empty,onComplete:$empty},initialize:function(a,c){this.parent(a,c);var b=$(this.options.indicator);b&&this.addEvents({onRequest:b.show.bind(b),onComplete:b.hide.bind(b)},true)},query:function(){var a=$unlink(this.options.postData);a[this.options.postVar]=this.queryValue;this.fireEvent("onRequest",[this.element,this.request,a,this.queryValue]);this.request.send({data:a})},queryResponse:function(){this.fireEvent("onComplete", [this.element,this.request,this.response])}});Autocompleter.Ajax.Json=new Class({Extends:Autocompleter.Ajax.Base,initialize:function(a,c,b){this.parent(a,b);this.request=(new Request.JSON($merge({url:c,link:"cancel"},this.options.ajaxOptions))).addEvent("onComplete",this.queryResponse.bind(this))},queryResponse:function(a){this.parent();this.update(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(){this.fix&&this.fix.setStyle("display","none");return this},destroy:function(){this.fix=this.fix.destroy()}}); Element.implement({getOffsetParent:function(){var a=this.getDocument().body;if(this==a)return null;if(!Browser.Engine.trident)return $(this.offsetParent);for(var c=this;c=c.parentNode;)if(c==a||Element.getComputedStyle(c,"position")!="static")return $(c);return null},getCaretPosition:function(){if(!Browser.Engine.trident)return this.selectionStart;this.focus();var a=document.selection.createRange(),c=this.createTextRange();a.setEndPoint("StartToStart",c);return a.text.length},selectRange:function(a, c){if(Browser.Engine.trident){var b=this.createTextRange();b.collapse(true);b.moveEnd("character",c);b.moveStart("character",a);b.select()}else{this.focus();this.setSelectionRange(a,c)}return this}});
