
var Autocompleter={};Autocompleter.Base=new Class({options:{minLength:1,markQuery:true,width:"inherit",maxChoices:10,injectChoice:null,customChoices:null,className:"autocompleter-choices",zIndex:42,delay:400,observerOptions:{},fxOptions:{},onOver:$empty,onSelect:$empty,onSelection:$empty,onShow:$empty,onHide:$empty,onBlur:$empty,onFocus:$empty,autoSubmit:false,overflow:false,overflowMargin:25,selectFirst:false,filter:null,filterCase:false,filterSubset:false,forceSelect:false,selectMode:true,choicesMatch:null,injectAfter:null,multiple:false,separator:", ",separatorSplit:/\s*[,;]\s*/,autoTrim:true,allowDupes:false,cache:true,relative:false},initialize:function(_1,_2){this.element=$(_1);this.setOptions(_2);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 _3=this.options.selectMode;this.typeAhead=(_3=="type-ahead");this.selectMode=(_3===true)?"selection":_3;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.options.relative===true)?this.element.getOffsetParent():$(this.options.relative);}
if(this.options.injectAfter){this.choices.inject($(this.options.injectAfter),"after");this.relative=$(this.options.injectAfter);}
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(_4){this.focussed=_4;if(!_4){this.hideChoices(true);}
this.fireEvent((_4)?"onFocus":"onBlur",[this.element]);},onCommand:function(e){if(!e&&this.focussed){return this.prefetch();}
if(e&&e.key&&!e.shift){switch(e.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 up=(e.key=="up");this.choiceOver((this.selected||this.choices)[(this.selected)?((up)?"getPrevious":"getNext"):((up)?"getLast":"getFirst")](this.options.choicesMatch),true);}
return false;case"esc":case"tab":this.hideChoices(true);break;}}
return true;},setSelection:function(_7){var _8=this.selected.inputValue,value=_8;var _9=this.queryValue.length,end=_8.length;if(_8.substr(0,_9).toLowerCase()!=this.queryValue.toLowerCase()){_9=0;}
if(this.options.multiple){var _a=this.options.separatorSplit;value=this.element.value;_9+=this.queryIndex;end+=this.queryIndex;var _b=value.substr(this.queryIndex).split(_a,1)[0];value=value.substr(0,this.queryIndex)+_8+value.substr(this.queryIndex+_b.length);if(_7){var _c=/[^\s,]+/;var _d=value.split(this.options.separatorSplit).filter(_c.test,_c);if(!this.options.allowDupes){_d=[].combine(_d);}
var _e=this.options.separator;value=_d.join(_e)+_e;end=value.length;}}
this.observer.setValue(value);this.opted=value;if(_7||this.selectMode=="pick"){_9=end;}
this.element.selectRange(_9,end);this.fireEvent("onSelection",[this.element,this.selected,value,_8]);},showChoices:function(){var _f=this.options.choicesMatch,first=this.choices.getFirst(_f);this.selected=this.selectedValue=null;if(this.fix){var pos=this.element.getCoordinates(this.relative),width=this.options.width||"auto";this.choices.setStyles({"left":pos.left,"top":pos.bottom,"width":(width===true||width=="inherit")?pos.width:width});}
if(!first){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||first.inputValue==this.queryValue){this.choiceOver(first,this.typeAhead);}
var _11=this.choices.getChildren(_f),max=this.options.maxChoices;var _12={"overflowY":"hidden","height":""};this.overflown=false;if(_11.length>max){var _13=_11[max-1];_12.overflowY="scroll";_12.height=_13.getCoordinates(this.choices).bottom;this.overflown=true;}
this.choices.setStyles(_12);this.fix.show();},hideChoices:function(_14){if(_14){var _15=this.element.value;if(this.options.forceSelect){_15=this.opted;}
if(this.options.autoTrim){_15=_15.split(this.options.separatorSplit).filter($arguments(0)).join(this.options.separator);}
this.observer.setValue(_15);}
if(!this.visible){return;}
this.visible=false;this.observer.clear();var _16=function(){this.choices.setStyle("display","none");this.fix.hide();}.bind(this);if(this.fx){this.fx.start(0).chain(_16);}else{_16();}
this.fireEvent("onHide",[this.element,this.choices]);},prefetch:function(){var _17=this.element.value,query=_17;if(this.options.multiple){var _18=this.options.separatorSplit;var _19=_17.split(_18);var _1a=this.element.getCaretPosition();var _1b=_17.substr(0,_1a).split(_18);var _1c=_1b.length-1;_1a-=_1b[_1c].length;query=_19[_1c];}
if(query.length<this.options.minLength){this.hideChoices();}else{if(query===this.queryValue||(this.visible&&query==this.selectedValue)){if(this.visible){return false;}
this.showChoices();}else{this.queryValue=query;this.queryIndex=_1a;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(_1d){this.choices.empty();this.cached=_1d;if(!_1d||!_1d.length){this.hideChoices();}else{if(this.options.maxChoices<_1d.length&&!this.options.overflow){_1d.length=this.options.maxChoices;}
_1d.each(this.options.injectChoice||function(_1e){var _1f=new Element("li",{"html":this.markQueryValue(_1e)});_1f.inputValue=_1e;this.addChoiceEvents(_1f).inject(this.choices);},this);this.showChoices();}},choiceOver:function(_20,_21){if(!_20||_20==this.selected){return;}
if(this.selected){this.selected.removeClass("autocompleter-selected");}
this.selected=_20.addClass("autocompleter-selected");this.fireEvent("onSelect",[this.element,this.selected,_21]);if(!_21){return;}
this.selectedValue=this.selected.inputValue;if(this.overflown){var _22=this.selected.getCoordinates(this.choices),margin=this.options.overflowMargin,top=this.choices.scrollTop,height=this.choices.offsetHeight,bottom=top+height;if(_22.top-margin<top&&top){this.choices.scrollTop=Math.max(_22.top-margin,0);}else{if(_22.bottom+margin>bottom){this.choices.scrollTop=Math.min(_22.bottom-height+margin,bottom);}}}
if(this.selectMode){this.setSelection();}},choiceSelect:function(_23){if(_23){this.choiceOver(_23);}
this.setSelection(true);this.queryValue=false;this.hideChoices();},filter:function(_24){var _25=new RegExp(((this.options.filterSubset)?"":"^")+this.queryValue.escapeRegExp(),(this.options.filterCase)?"":"i");return(_24||this.tokens).filter(_25.test,_25);},markQueryValue:function(str){return(!this.options.markQuery||!this.queryValue)?str:str.replace(new RegExp("("+((this.options.filterSubset)?"":"^")+this.queryValue.escapeRegExp()+")",(this.options.filterCase)?"":"i"),"<span class=\"autocompleter-queried\">$1</span>");},addChoiceEvents:function(el){return el.addEvents({"mouseover":this.choiceOver.bind(this,[el]),"click":this.choiceSelect.bind(this,[el])});}});Autocompleter.Base.implement(new Events);Autocompleter.Base.implement(new Options);Autocompleter.Local=new Class({Extends:Autocompleter.Base,options:{minLength:0,delay:200},initialize:function(_28,_29,_2a){this.parent(_28,_2a);this.tokens=_29;},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(_2b,_2c){this.parent(_2b,_2c);var _2d=$(this.options.indicator);if(_2d){this.addEvents({"onRequest":_2d.show.bind(_2d),"onComplete":_2d.hide.bind(_2d)},true);}},query:function(){var _2e=$unlink(this.options.postData);_2e[this.options.postVar]=this.queryValue;this.fireEvent("onRequest",[this.element,this.request,_2e,this.queryValue]);this.request.send({"data":_2e});},queryResponse:function(){this.fireEvent("onComplete",[this.element,this.request,this.response]);}});Autocompleter.Ajax.Json=new Class({Extends:Autocompleter.Ajax.Base,initialize:function(el,url,_31){this.parent(el,_31);this.request=new Request.JSON($merge({"url":url,"link":"cancel"},this.options.ajaxOptions)).addEvent("onComplete",this.queryResponse.bind(this));},queryResponse:function(_32){this.parent();this.update(_32);}});Autocompleter.Ajax.Xhtml=new Class({Extends:Autocompleter.Ajax.Base,initialize:function(el,url,_35){this.parent(el,_35);this.request=new Request.HTML($merge({"url":url,"link":"cancel","update":this.choices},this.options.ajaxOptions)).addEvent("onComplete",this.queryResponse.bind(this));},queryResponse:function(_36,_37){this.parent();if(!_37||!_37.length){this.hideChoices();}else{this.choices.getChildren(this.options.choicesMatch).each(this.options.injectChoice||function(_38){var _39=_38.innerHTML;_38.inputValue=_39;this.addChoiceEvents(_38.set("html",this.markQueryValue(_39)));},this);this.showChoices();}}});var OverlayFix=new Class({initialize:function(el){if(Browser.Engine.trident){this.element=$(el);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 _3b=this.element.getCoordinates(this.relative);delete _3b.right;delete _3b.bottom;this.fix.setStyles($extend(_3b,{"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=this.fix.destroy();}});Element.implement({getOffsetParent:function(){var _3c=this.getDocument().body;if(this==_3c){return null;}
if(!Browser.Engine.trident){return $(this.offsetParent);}
var el=this;while((el=el.parentNode)){if(el==_3c||Element.getComputedStyle(el,"position")!="static"){return $(el);}}
return null;},getCaretPosition:function(){if(!Browser.Engine.trident){return this.selectionStart;}
this.focus();var _3e=document.selection.createRange();var all=this.createTextRange();_3e.setEndPoint("StartToStart",all);return _3e.text.length;},selectRange:function(_40,end){if(Browser.Engine.trident){var _42=this.createTextRange();_42.collapse(true);_42.moveEnd("character",end);_42.moveStart("character",_40);_42.select();}else{this.focus();this.setSelectionRange(_40,end);}
return this;}});
