var Observer=new Class({Implements:[Options,Events],options:{periodical:!1,delay:1E3},initialize:function(a,b,c){this.element=$(a)||$$(a);this.addEvent("onFired",b);this.setOptions(c);this.bound=this.changed.bind(this);this.resume()},changed:function(){var a=this.element.get("value");$equals(this.value,a)||(this.clear(),this.value=a,this.timeout=this.onFired.delay(this.options.delay,this))},setValue:function(a){this.value=a;this.element.set("value",a);return this.clear()},onFired:function(){this.fireEvent("onFired",
[this.value,this.element])},clear:function(){$clear(this.timeout||null);return this},pause:function(){this.timer?$clear(this.timer):this.element.removeEvent("keyup",this.bound);return this.clear()},resume:function(){this.value=this.element.get("value");this.options.periodical?this.timer=this.changed.periodical(this.options.periodical,this):this.element.addEvent("keyup",this.bound);return this}}),$equals=function(a,b){return a==b||JSON.encode(a)==JSON.encode(b)},Autocompleter=new Class({Implements:[Options,
Events],options:{minLength:1,markQuery:!0,width:"inherit",maxChoices:10,injectChoice:null,customChoices:null,emptyChoices:null,visibleChoices:!0,className:"autocompleter-choices",zIndex:65535,delay:1E3,observerOptions:{},fxOptions:{},autoSubmit:!1,overflow:!1,overflowMargin:25,selectFirst:!0,filter:null,filterCase:!1,filterSubset:!1,forceSelect:!1,selectMode:!0,choicesMatch:null,multiple:!1,separator:", ",separatorSplit:/\s*[,;]\s*/,autoTrim:!1,allowDupes:!1,cache:!0,relative:!0},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;this.options.filter&&(this.filter=this.options.filter.bind(this));var c=this.options.selectMode;this.typeAhead="type-ahead"==c;this.selectMode=!0===c?"selection":c;this.cached=[]},build:function(){$(this.options.customChoices)?this.choices=this.options.customChoices:(this.choices=(new Element("ul",
{"class":this.options.className,styles:{display:"none",zIndex:this.options.zIndex}})).inject(document.body),this.relative=!1,this.options.relative&&(this.choices.inject(this.element,"after"),this.relative=this.element.getOffsetParent()),this.fix=new OverlayFix(this.choices));this.options.separator.test(this.options.separatorSplit)||(this.options.separatorSplit=this.options.separator);this.fx=this.options.fxOptions?(new Fx.Tween(this.choices,$merge({property:"opacity",link:"cancel",duration:200},this.options.fxOptions))).addEvent("onStart",
Chain.prototype.clearChain).set(0):null;this.choices.addEvents({mouseover:function(a){this.store("focussed",!0)},mouseout:function(a){this.store("focussed",!1)},mousedown:function(){this.store("focussed",!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,[!1])).addEvent("focus",this.toggleFocus.create({bind:this,arguments:!0,delay:100})).addEvent("blur",
this.toggleFocus.create({bind:this,arguments:!1,delay:100}))},destroy:function(){this.fix&&this.fix.destroy();this.choices=this.selected=this.choices.destroy()},toggleFocus:function(a){(this.focussed=a)||!this.choices||this.choices.retrieve("focussed")||this.hideChoices(!0);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":if(this.element.value!=this.opted)break;if(this.selected&&
this.visible)return this.choiceSelect(this.selected),!!this.options.autoSubmit;break;case "up":case "down":return this.prefetch()||null===this.queryValue||(a="up"==a.key,this.choiceOver((this.selected||this.choices)[this.selected?a?"getPrevious":"getNext":a?"getLast":"getFirst"](this.options.choicesMatch),!0)),!1;case "esc":case "tab":this.hideChoices(!0)}return!0},setSelection:function(a){var b=this.selected.inputValue,c=b,d=this.queryValue.length,e=b.length;b.substr(0,d).toLowerCase()!=this.queryValue.toLowerCase()&&
(d=0);if(this.options.multiple){var f=this.options.separatorSplit,c=this.element.value,d=d+this.queryIndex,e=e+this.queryIndex,f=c.substr(this.queryIndex).split(f,1)[0],c=c.substr(0,this.queryIndex)+b+c.substr(this.queryIndex+f.length);a&&(c=c.split(this.options.separatorSplit).filter(function(a){return this.test(a)},/[^\s,]+/),this.options.allowDupes||(c=[].combine(c)),e=this.options.separator,c=c.join(e)+e,e=c.length)}this.observer.setValue(c);this.opted=c;if(a||"pick"==this.selectMode)d=e;this.element.selectRange(d,
e);this.fireEvent("onSelection",[this.element,this.selected,c,b])},showChoices:function(){var a=this.options.choicesMatch,b=this.choices.getFirst(a);this.selected=this.selectedValue=null;if(this.fix){var c=this.element.getCoordinates(this.relative),d=this.options.width||"auto";this.choices.setStyles({left:c.left,top:c.bottom,width:!0===d||"inherit"==d?c.width:d})}b&&(this.visible||(this.visible=!0,this.choices.setStyle("display",""),this.fx&&this.fx.start(1),this.fireEvent("onShow",[this.element,
this.choices])),this.options.selectFirst&&(this.typeAhead=!0),(this.options.selectFirst||this.typeAhead||b.inputValue==this.queryValue)&&this.choiceOver(b,this.typeAhead),b=this.choices.getChildren(a),c=this.options.maxChoices,a={overflowY:"hidden",height:""},this.overflown=!1,b.length>c&&(b=b[c-1],a.overflowY="scroll",a.height=b.getCoordinates(this.choices).bottom,this.overflown=!0),this.choices.setStyles(a),this.fix.show(),this.options.visibleChoices&&(a=document.getScroll(),b=document.getSize(),
c=this.choices.getCoordinates(),c.right>a.x+b.x&&(a.x=c.right-b.x),c.bottom>a.y+b.y&&(a.y=c.bottom-b.y),window.scrollTo(Math.min(a.x,c.left),Math.min(a.y,c.top))))},hideChoices:function(a){a&&(a=this.element.value,this.options.forceSelect&&(a=this.opted),this.options.autoTrim&&(a=a.split(this.options.separatorSplit).filter($arguments(0)).join(this.options.separator)),this.observer.setValue(a));this.visible&&(this.visible=!1,this.selected&&this.selected.removeClass("autocompleter-selected"),this.observer.clear(),
a=function(){this.choices.hide();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,b=a;if(this.options.multiple)var c=this.options.separatorSplit,b=a.split(c),d=this.element.getSelectedRange().start,a=a.substr(0,d).split(c),c=a.length-1,d=d-a[c].length,b=b[c];if(b.length<this.options.minLength)this.hideChoices();else if(b===this.queryValue||this.visible&&b==this.selectedValue){if(this.visible)return!1;
this.showChoices()}else this.queryValue=b,this.queryIndex=d,this.fetchCached()||this.query();return!0},fetchCached:function(){return!1},update:function(a){this.choices.empty();var b=(this.cached=a)&&$type(a);!b||"array"==b&&!a.length||"hash"==b&&!a.getLength()?(this.options.emptyChoices||this.hideChoices).call(this):(this.options.maxChoices<a.length&&!this.options.overflow&&(a.length=this.options.maxChoices),a.each(this.options.injectChoice||function(a){var b=new Element("li",{html:this.markQueryValue(a)});
b.inputValue=a;this.addChoiceEvents(b).inject(this.choices)},this),this.showChoices())},choiceOver:function(a,b){if(a&&a!=this.selected&&(this.selected&&this.selected.removeClass("autocompleter-selected"),this.selected=a.addClass("autocompleter-selected"),this.fireEvent("onSelect",[this.element,this.selected,b]),this.selectMode||(this.opted=this.element.value),b)){this.selectedValue=this.selected.inputValue;if(this.overflown){var c=this.selected.getCoordinates(this.choices),d=this.options.overflowMargin,
e=this.choices.scrollTop,f=this.choices.offsetHeight,g=e+f;c.top-d<e&&e?this.choices.scrollTop=Math.max(c.top-d,0):c.bottom+d>g&&(this.choices.scrollTop=Math.min(c.bottom-f+d,g))}this.selectMode&&this.setSelection()}},choiceSelect:function(a){a&&this.choiceOver(a);this.setSelection(!0);this.queryValue=!1;this.fireEvent("submit");this.hideChoices()},filter:function(a){return(a||this.tokens).filter(function(a){return this.test(a)},RegExp((this.options.filterSubset?"":"^")+this.queryValue.escapeRegExp(),
this.options.filterCase?"":"i"))},markQueryValue:function(a){return this.options.markQuery&&this.queryValue?a.replace(RegExp("("+(this.options.filterSubset?"":"^")+this.queryValue.escapeRegExp()+")",this.options.filterCase?"":"i"),'<span class="autocompleter-queried">$1</span>'):a},addChoiceEvents:function(a){return a.addEvents({mouseover:this.choiceOver.bind(this,[a]),click:this.choiceSelect.bind(this,[a])})}}),OverlayFix=new Class({initialize:function(a){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=this.fix.destroy())}});
Autocompleter.script=new Class({Extends:Autocompleter,options:{getData:{},callJSON:$empty,getVar:"value"},initialize:function(a,b,c){this.parent(a,c);this.url=b},query:function(){this.fireEvent("onLoad");var a=$unlink(this.options.getData)||{};a[this.options.getVar]=this.queryValue;var b=$(this.options.indicator);b&&b.setStyle("display","");(b=this.options.indicatorClass)&&this.element.addClass(b);this.fireEvent("onRequest",[this.element,a,this.queryValue]);this.requestScript(a)},requestScript:function(a){var b=
this.options.script||"aotuScript";$(b)&&$(b).destroy();a=this.url+"&"+$H(a).toQueryString();var c=this.queryResponse.bind(this);this.script=(new Element("script",{src:a,id:b})).inject(this.element,"after");this.script.addEvents({load:c,readystatechange:function(){["loaded","complete"].contains(this.readyState)&&c()}})},queryResponse:function(){this.script.removeEvents();var a=$(this.options.indicator);a&&a.hide();(a=this.options.indicatorClass)&&this.element.removeClass(a);a=this.options.callJSON();
this.update(a);this.fireEvent("onComplete",[this.element])}});