
$(document).ready(function(){var attachAC=function(context){context=context||document;var udb="";$("input.autocomplete:not(.autocomplete-processed)",context).each(function(){var uri="http://www.turism.ee/ajax/userdb.php?q=";if(!udb){udb=new userDB(uri);}
var input=$("#input_rcpts").attr("autocomplete","OFF")[0];$(input.form).submit(autocompleteSubmit);new jsAC(input,udb);$(this).addClass('autocomplete-processed');});}
autocompleteSubmit=function(){return $("#autocomplete").each(function(){this.owner.hidePopup();}).size()==0;};jsAC=function(input,db){var u=this;this.input=input;this.db=db;$(this.input).keydown(function(event){return u.onkeydown(this,event);}).keyup(function(event){return u.onkeyup(this,event);}).blur(function(){u.hidePopup();u.db.cancel();});};jsAC.prototype.onkeydown=function(input,e){if(!e){e=window.event;}
switch(e.keyCode){case 40:this.selectDown();return false;case 38:this.selectUp();return false;default:return true;}};jsAC.prototype.onkeyup=function(input,e){if(!e){e=window.event;}
switch(e.keyCode){case 16:case 17:case 18:case 20:case 33:case 34:case 35:case 36:case 37:case 38:case 39:case 40:return true;case 9:case 13:case 27:this.hidePopup(e.keyCode);return true;default:if(input.value.length>0){this.populatePopup();}
else{this.hidePopup(e.keyCode);}
return true;}};jsAC.prototype.select=function(node){this.input.value=node.autocompleteValue;};jsAC.prototype.selectDown=function(){if(this.selected&&this.selected.nextSibling){this.highlight(this.selected.nextSibling);}
else{var lis=$("li",this.popup);if(lis.size()>0){this.highlight(lis.get(0));}}};jsAC.prototype.selectUp=function(){if(this.selected&&this.selected.previousSibling){this.highlight(this.selected.previousSibling);}};jsAC.prototype.highlight=function(node){if(this.selected){$(this.selected).removeClass("selected");}
$(node).addClass("selected");this.selected=node;};jsAC.prototype.unhighlight=function(node){$(node).removeClass("selected");this.selected=false;};jsAC.prototype.hidePopup=function(keycode){if(this.selected&&((keycode&&keycode!=46&&keycode!=8&&keycode!=27)||!keycode)){this.input.value=this.selected.autocompleteValue;}
var popup=this.popup;if(popup){this.popup=null;$(popup).fadeOut("fast",function(){$(popup).remove();});}
this.selected=false;};jsAC.prototype.populatePopup=function(){if(this.popup){$(this.popup).remove();}
this.selected=false;this.popup=document.createElement("div");this.popup.id="autocomplete";this.popup.owner=this;$(this.popup).css({marginTop:this.input.offsetHeight+"px",width:(this.input.offsetWidth-2)+"px",display:"none"});$(this.input).before(this.popup);this.db.owner=this;this.db.search(this.input.value);};jsAC.prototype.found=function(matches){if(!this.input.value.length){return false;}
var ul=document.createElement("ul");var u=this;for(key in matches){var li=document.createElement("li");$(li).html("<div>"+matches[key]+"</div>").mousedown(function(){u.select(this);}).mouseover(function(){u.highlight(this);}).mouseout(function(){u.unhighlight(this);});li.autocompleteValue=key;$(ul).append(li);}
if(this.popup){if(ul.childNodes.length>0){$(this.popup).empty().append(ul).show();}
else{$(this.popup).css({visibility:"hidden"});this.hidePopup();}}};jsAC.prototype.setStatus=function(status){switch(status){case"begin":$(".profile_selects").addClass("loading");break;case"cancel":case"error":case"found":$(".profile_selects").removeClass("loading");break;}};userDB=function(uri){this.uri=uri;this.delay=300;this.cache={};};userDB.prototype.search=function(searchString){var db=this;this.searchString=searchString;if(this.cache[searchString]){return this.owner.found(this.cache[searchString]);}
if(this.timer){clearTimeout(this.timer);}
this.timer=setTimeout(function(){db.owner.setStatus("begin");$.ajax({type:"GET",url:db.uri+searchString,dataType:"json",success:function(matches){if(typeof matches['status']=="undefined"||matches["status"]!=0){db.cache[searchString]=matches;if(db.searchString==searchString){db.owner.found(matches);}
db.owner.setStatus("found");}},error:function(hmlhttp){alert('Viga');}});},this.delay);};userDB.prototype.cancel=function(){if(this.owner)this.owner.setStatus("cancel");if(this.timer)clearTimeout(this.timer);this.searchString="";};attachAC();});