Element.collectTextNodesIgnoreClass=function(d,e){var c=$(d).childNodes;var f="";var b=new RegExp("^([^ ]+ )*"+e+"( [^ ]+)*$","i");for(var a=0;a<c.length;a++){if(c[a].nodeType==3){f+=c[a].nodeValue}else{if((!c[a].className.match(b))&&c[a].hasChildNodes()){f+=Element.collectTextNodesIgnoreClass(c[a],e)}}}return f};var Autocompleter={};Autocompleter.Base=function(){};Autocompleter.Base.prototype={base_initialize:function(b,c,a){this.element=$(b);this.update=$(c);this.has_focus=false;this.changed=false;this.active=false;this.index=0;this.entry_count=0;if(this.setOptions){this.setOptions(a)}else{this.options=a||{}}this.options.tokens=this.options.tokens||new Array();this.options.frequency=this.options.frequency||0.4;this.options.min_chars=this.options.min_chars||1;this.options.onShow=this.options.onShow||function(d,f){if(!f.style.position||f.style.position=="absolute"){f.style.position="absolute";var e=Position.cumulativeOffset(d);f.style.left=e[0]+"px";f.style.top=(e[1]+d.offsetHeight)+"px";f.style.width=d.offsetWidth+"px"}new Effect.Appear(f,{duration:0.15})};this.options.onHide=this.options.onHide||function(d,e){new Effect.Fade(e,{duration:0.15})};if(this.options.indicator){this.indicator=$(this.options.indicator)}if(typeof(this.options.tokens)=="string"){this.options.tokens=new Array(this.options.tokens)}this.observer=null;Element.hide(this.update);Event.observe(this.element,"blur",this.onBlur.bindAsEventListener(this));Event.observe(this.element,"keypress",this.onKeyPress.bindAsEventListener(this))},show:function(){if(this.update.style.display=="none"){this.options.onShow(this.element,this.update)}if(!this.iefix&&(navigator.appVersion.indexOf("MSIE")>0)&&this.update.style.position=="absolute"){new Insertion.After(this.update,'<iframe id="'+this.update.id+'_iefix" style="display:none;filter:progid:DXImageTransform.Microsoft.Alpha(opacity=0);" src="javascript:false;" frameborder="0" scrolling="no"></iframe>');this.iefix=$(this.update.id+"_iefix")}if(this.iefix){Position.clone(this.update,this.iefix);this.iefix.style.zIndex=1;this.update.style.zIndex=2;Element.show(this.iefix)}},hide:function(){if(this.update.style.display==""){this.options.onHide(this.element,this.update)}if(this.iefix){Element.hide(this.iefix)}},startIndicator:function(){if(this.indicator){Element.show(this.indicator)}},stopIndicator:function(){if(this.indicator){Element.hide(this.indicator)}},onKeyPress:function(a){if(this.active){switch(a.keyCode){case Event.KEY_TAB:case Event.KEY_RETURN:this.select_entry();Event.stop(a);case Event.KEY_ESC:this.hide();this.active=false;return;case Event.KEY_LEFT:case Event.KEY_RIGHT:return;case Event.KEY_UP:this.mark_previous();this.render();if(navigator.appVersion.indexOf("AppleWebKit")>0){Event.stop(a)}return;case Event.KEY_DOWN:this.mark_next();this.render();if(navigator.appVersion.indexOf("AppleWebKit")>0){Event.stop(a)}return}}else{if(a.keyCode==Event.KEY_TAB||a.keyCode==Event.KEY_RETURN){return}}this.changed=true;this.has_focus=true;if(this.observer){clearTimeout(this.observer)}this.observer=setTimeout(this.onObserverEvent.bind(this),this.options.frequency*1000)},onHover:function(b){var a=Event.findElement(b,"LI");if(this.index!=a.autocompleteIndex){this.index=a.autocompleteIndex;this.render()}Event.stop(b)},onClick:function(b){var a=Event.findElement(b,"LI");this.index=a.autocompleteIndex;this.select_entry();Event.stop(b)},onBlur:function(a){setTimeout(this.hide.bind(this),250);this.has_focus=false;this.active=false},render:function(){if(this.entry_count>0){for(var a=0;a<this.entry_count;a++){this.index==a?Element.addClassName(this.get_entry(a),"selected"):Element.removeClassName(this.get_entry(a),"selected")}if(this.has_focus){if(this.get_current_entry().scrollIntoView){this.get_current_entry().scrollIntoView(false)}this.show();this.active=true}}else{this.hide()}},mark_previous:function(){if(this.index>0){this.index--}else{this.index=this.entry_count-1}},mark_next:function(){if(this.index<this.entry_count-1){this.index++}else{this.index=0}},get_entry:function(a){return this.update.firstChild.childNodes[a]},get_current_entry:function(){return this.get_entry(this.index)},select_entry:function(){this.active=false;value=Element.collectTextNodesIgnoreClass(this.get_current_entry(),"informal").unescapeHTML();this.updateElement(value);this.element.focus()},updateElement:function(d){var c=this.findLastToken();if(c!=-1){var b=this.element.value.substr(0,c+1);var a=this.element.value.substr(c+1).match(/^\s+/);if(a){b+=a[0]}this.element.value=b+d}else{this.element.value=d}},updateChoices:function(b){if(!this.changed&&this.has_focus){this.update.innerHTML=b;Element.cleanWhitespace(this.update);Element.cleanWhitespace(this.update.firstChild);if(this.update.firstChild&&this.update.firstChild.childNodes){this.entry_count=this.update.firstChild.childNodes.length;for(var a=0;a<this.entry_count;a++){entry=this.get_entry(a);entry.autocompleteIndex=a;this.addObservers(entry)}}else{this.entry_count=0}this.stopIndicator();this.index=0;this.render()}},addObservers:function(a){Event.observe(a,"mouseover",this.onHover.bindAsEventListener(this));Event.observe(a,"click",this.onClick.bindAsEventListener(this))},onObserverEvent:function(){this.changed=false;if(this.getEntry().length>=this.options.min_chars){this.startIndicator();this.getUpdatedChoices()}else{this.active=false;this.hide()}},getEntry:function(){var b=this.findLastToken();if(b!=-1){var a=this.element.value.substr(b+1).replace(/^\s+/,"").replace(/\s+$/,"")}else{var a=this.element.value}return/\n/.test(a)?"":a},findLastToken:function(){var c=-1;for(var b=0;b<this.options.tokens.length;b++){var a=this.element.value.lastIndexOf(this.options.tokens[b]);if(a>c){c=a}}return c}};Ajax.Autocompleter=Class.create();Object.extend(Object.extend(Ajax.Autocompleter.prototype,Autocompleter.Base.prototype),{initialize:function(c,d,b,a){this.base_initialize(c,d,a);this.options.asynchronous=true;this.options.onComplete=this.onComplete.bind(this);this.options.method="post";this.options.defaultParams=this.options.parameters||null;this.url=b},getUpdatedChoices:function(){entry=encodeURIComponent(this.element.name)+"="+encodeURIComponent(this.getEntry());this.options.parameters=this.options.callback?this.options.callback(this.element,entry):entry;if(this.options.defaultParams){this.options.parameters+="&"+this.options.defaultParams}new Ajax.Request(this.url,this.options)},onComplete:function(a){this.updateChoices(a.responseText)}});Autocompleter.Local=Class.create();Autocompleter.Local.prototype=Object.extend(new Autocompleter.Base(),{initialize:function(b,d,c,a){this.base_initialize(b,d,a);this.options.array=c},getUpdatedChoices:function(){this.updateChoices(this.options.selector(this))},setOptions:function(a){this.options=Object.extend({choices:10,partial_search:true,partial_chars:2,ignore_case:true,full_search:false,selector:function(b){var d=new Array();var c=new Array();var h=b.getEntry();var g=0;for(var e=0;e<b.options.array.length&&d.length<b.options.choices;e++){var f=b.options.array[e];var j=b.options.ignore_case?f.toLowerCase().indexOf(h.toLowerCase()):f.indexOf(h);while(j!=-1){if(j==0&&f.length!=h.length){d.push("<li><strong>"+f.substr(0,h.length)+"</strong>"+f.substr(h.length)+"</li>");break}else{if(h.length>=b.options.partial_chars&&b.options.partial_search&&j!=-1){if(b.options.full_search||/\s/.test(f.substr(j-1,1))){c.push("<li>"+f.substr(0,j)+"<strong>"+f.substr(j,h.length)+"</strong>"+f.substr(j+h.length)+"</li>");break}}}j=b.options.ignore_case?f.toLowerCase().indexOf(h.toLowerCase(),j+1):f.indexOf(h,j+1)}}if(c.length){d=d.concat(c.slice(0,b.options.choices-d.length))}return"<ul>"+d.join("")+"</ul>"}},a||{})}});