
var AutoComplete=Class.create();AutoComplete.START=1;AutoComplete.GLOBAL=2;AutoComplete.WORDS=3;AutoComplete.DefaultOptions={caseSensitive:false,matching:AutoComplete.START,showIfEmpty:false};AutoComplete.prototype={initialize:function(element,dataArray,displayArray,options){element=$(element);if(!element||!element.parentNode||(element.tagName||'').toLowerCase()!='input'||(element.getAttribute('type')||'').toLowerCase()!='text')return;this.options=Object.extend(Object.extend({},AutoComplete.DefaultOptions),options||{});this.dataArray=dataArray||[];this.displayArray=(displayArray&&displayArray.length==this.dataArray.length)?displayArray:this.dataArray;this.displayedIndexes=[];this.displayedIndex=-1;this.element=element;this.visible=false;this.container=document.createElement('div');this.container.style.display='none';this.container.className='autocomplete';this.container.appendChild(document.createElement('div'));document.body.appendChild(this.container);Event.observe(this.element,'keypress',this.onKeyPress.bindAsEventListener(this),false);Event.observe(this.element,'keyup',this.onKeyUp.bindAsEventListener(this),false);Event.observe(document,'click',this.hide.bindAsEventListener(this),false);AutoCompleteManager.register(this);},show:function(){if(!this.visible)this.displayedIndex=-1;this.updateValue();if(this.displayedIndexes.length>0){this.initPosition();this.visible=true;Element.show(this.container);}else if(this.visible){this.hide();}},hide:function(){this.visible=false;Element.hide(this.container);},updateValue:function(){this.valueUpdated=true;var compareVal=this.options.caseSensitive?this.element.value:this.element.value.toLowerCase();var prevIndex=this.dataArrayIndex();this.displayedIndexes=[];this.selectedElem=null;var valuesDiv=document.createElement('div');valuesDiv.className='autocomplete_values';if(compareVal.length>0||this.options.showIfEmpty){for(var i=0;i<this.displayArray.length;i++){var arrayVal=this.displayArray[i]?this.displayArray[i].toString():'';var compareArrayVal=this.options.caseSensitive?arrayVal:arrayVal.toLowerCase();var index=compareArrayVal.indexOf(compareVal);var isMatched=false;switch(this.options.matching){case AutoComplete.START:isMatched=(index==0);break;case AutoComplete.GLOBAL:isMatched=(index!=-1);break;case AutoComplete.WORDS:if(index==0){isMatched=true;break;}
while(index!=-1){var c=arrayVal.charAt(index-1);isMatched=(c==' '||c=='(');if(isMatched)break;index=compareArrayVal.indexOf(compareVal,index+1);}
break;}
if(isMatched){var valueDiv=document.createElement('div');valueDiv.displayedIndex=this.displayedIndexes.length;valueDiv.className=(i==prevIndex)?'autocomplete_value autocomplete_selected':'autocomplete_value';valueDiv.innerHTML=arrayVal.substring(0,index)+'<span class="autocomplete_highlight">'+
arrayVal.substring(index,index+compareVal.length)+'</span>'+
arrayVal.substring(index+compareVal.length);valueDiv.onclick=function(e){this.displayedIndex=Event.element(window.event||e).displayedIndex;this.valueChosen();this.element.focus();}.bind(this);valuesDiv.appendChild(valueDiv);if(i==prevIndex){this.selectedElem=valueDiv;this.displayedIndex=this.displayedIndexes.length;}
this.displayedIndexes.push(i);}}}
this.container.replaceChild(valuesDiv,this.container.firstChild);if(!this.selectedElem)this.displayedIndex=-1;},selectPrevious:function(){if(this.displayedIndex<1)return;this.select(-1);},selectNext:function(){if(this.displayedIndex>=this.displayedIndexes.length-1)return;this.select(1);},dataArrayIndex:function(){return(this.displayedIndex==-1?-1:this.displayedIndexes[this.displayedIndex]);},setDataArray:function(dataArray){this.dataArray=dataArray||[];},onKeyPress:function(event){switch(event.keyCode){case Event.KEY_TAB:case Event.KEY_RETURN:if(this.visible){this.valueChosen();Event.stop(event);return false;}
break;case Event.KEY_ESC:if(this.visible){this.hide();Event.stop(event);}
break;case Event.KEY_UP:this.visible?this.selectPrevious():this.show();Event.stop(event);break;case Event.KEY_DOWN:this.visible?this.selectNext():this.show();Event.stop(event);break;case Event.KEY_LEFT:case Event.KEY_RIGHT:case Event.KEY_HOME:case Event.KEY_END:case Event.KEY_PAGEUP:case Event.KEY_PAGEDOWN:break;default:this.allowUpdateValue=true;}},onKeyUp:function(){if(this.allowUpdateValue){this.allowUpdateValue=false;this.show();}},select:function(incr){this.displayedIndex+=incr;if(this.selectedElem)this.selectedElem.className='autocomplete_value';this.selectedElem=this.container.firstChild.childNodes[this.displayedIndex];this.selectedElem.className='autocomplete_value autocomplete_selected';var scrollTop=this.container.firstChild.scrollTop;var scrollBottom=scrollTop+this.container.firstChild.offsetHeight;var elemTop=this.selectedElem.offsetTop;var elemBottom=elemTop+this.selectedElem.offsetHeight;if(elemTop<scrollTop)this.container.firstChild.scrollTop=elemTop;else if(elemBottom>scrollBottom)this.container.firstChild.scrollTop=elemBottom-this.container.firstChild.offsetHeight;},valueChosen:function(){this.hide();if(this.displayedIndex>-1)
this.element.value=(this.dataArray[this.dataArrayIndex()]||'').toString();},initPosition:function(){if(this.positionInitialized)return;this.positionInitialized=true;this.x=this.getX(this.element);this.y=this.getY(this.element);this.w=this.getW(this.element);this.container.style.position='absolute';this.container.style.left=this.x+'px';this.container.style.top=this.y+'px';this.container.style.width=this.w+'px';},getX:function(elem){return this.left(elem);},getY:function(elem){return this.top(elem)+this.height(elem);},getW:function(elem){return this.width(elem);},left:function(elem){return elem?((Prototype.Browser.IE?elem.clientLeft+elem.offsetLeft:elem.offsetLeft)+this.left(elem.offsetParent)):0;},top:function(elem){return elem?((Prototype.Browser.IE?elem.clientTop+elem.offsetTop:elem.offsetTop)+this.top(elem.offsetParent)):0;},width:function(elem){return elem.offsetWidth;},height:function(elem){return elem.offsetHeight;}};AutoCompleteManager={nextId:1,autoCompletes:{},register:function(autoComplete){var id=null;do{id='autocomplete_'+(autoComplete.element.id||nextId++);}while(this.autoCompletes[id])
autoComplete.id=id;this.autoCompletes[id]=autoComplete;}};