ALL.namespace("utils");ALL.utils.autocomplete=function(element,options){var suggest_box,suggestions,autocomplete_node,html,string;var config={delay:400,limit:7,onlyInsertIntoInput:false};if(options){ALL.utils.hashMerge(config,options)}var current=-1;var last_word="",no_response=false,user_input="";var timer=null;var search_f=document.getElementById("search")||document.search_form;var search_sg=document.getElementById("sg");typeof (element)==="string"?string=ALL.$(element):string=element;suggest_box=ALL.$("suggestedWords");suggestions=ALL.$("suggestedWords").getElementsByTagName("a");function hideSB(){suggest_box.innerHTML="";ALL.$("searchSuggest").style.display="none"}function showSB(){ALL.$("searchSuggest").style.display="block"}function searchSuggest(str,force){if(ALL.cookie.get("suggest")!="false"){str=str.replace(/^\s+/g,"").replace(/\s{2,}/g," ");if((last_word==""&&str!="")||(force&&str!="")||!(str==""||(no_response&&str.indexOf(last_word)==0&&str.length>=last_word.length))){ALL.ajax.request("/suggest.php?limit="+config.limit+"&search="+encodeURIComponent(str).replace(" ","+"),{onSuccess:function(xhr){handleSearchSuggest(eval("("+xhr.responseText+")"))}})}else{hideSB()}}}function prepareResponse(xhr_response){var i,j,k,query_splitted,tmp,build_response,response_rest;var query=""+xhr_response.query;var response=new Array().concat(xhr_response.response);query_splitted=query.split(" ");if(query_splitted.length>1){for(i=0;i<response.length;i++){if(response[i].indexOf(query)==-1){build_response=[];tmp=response[i].split(" ");response_rest=tmp;for(k=0;k<query_splitted.length;k++){for(j=0;j<tmp.length;j++){if(k<query_splitted.length-1){if(tmp[j]==query_splitted[k]){build_response.push(tmp[j]);response_rest.splice(j,1);break}}else{if(tmp[j].indexOf(query_splitted[k])!=-1){build_response.push(tmp[j]);response_rest.splice(j,1);break}}}}response[i]=build_response.concat(response_rest).join(" ")}}}for(i=0;i<response.length;i++){response[i]=response[i].replace(query.toLowerCase(),"<strong>"+query.toLowerCase()+"</strong>")}return response}function handleSearchSuggest(received_response){var suggest="",i,j,resp,parsed_response,onclick_action;suggest_box.innerHTML="";resp=received_response.response;if(resp.length>1||(resp.length==1&&resp[0]!=""&&resp[0]!=null)){parsed_response=prepareResponse(received_response);for(i=0;i<resp.length;i++){suggest='<a class="suggest" id="sw'+i+'" href="/search.php?string='+resp[i].replace(" ","+")+'&sg=1">'+parsed_response[i]+"</a>";suggest_box.innerHTML+=suggest}last_word=received_response.query;no_response=false;showSB();for(j=0;j<suggestions.length;j++){suggestions[j].onmouseover=function(){current=Math.floor(this.id.replace(/sw(\d+)/,"$1"));setCurrent(current,false,false)};if(config.onlyInsertIntoInput){suggestions[j].onclick=function(){current=Math.floor(this.id.replace(/sw(\d+)/,"$1"));setCurrent(current);hideSB();return false}}}}else{no_response=true;hideSB()}ALL.$("hide_suggest").onclick=function(){var exp=new Date();ALL.cookie.set("suggest","false",{expires:new Date(exp.setFullYear(exp.getFullYear()+1))});hideSB();current=-1;ALL.$("suggestDown").style.display="block";string.setAttribute("autocomplete","on");removeStatParam();return false}}function keyAction(evt){if(typeof evt==="undefined"){evt=window.event}if(evt){switch(evt.keyCode){case 38:if(suggestions.length>0){(current<0)?current=suggestions.length-1:current--;setCurrent(current)}break;case 40:if(suggestions.length>0){(current==suggestions.length-1)?current=-1:current++;setCurrent(current)}break;case 27:hideSB();current=-1;setCurrent(current);break;case 37:case 39:case 27:case 18:case 17:case 16:break;case 46:case 8:last_word=last_word.slice(0,last_word.length-1);current=-1;setCurrent(current,false);if(typeof timer!=="undefined"){clearTimeout(timer)}timer=setTimeout(function(){searchSuggest(string.value,true)},config.delay);break;case 13:if(config.onlyInsertIntoInput&&current!=-1){setCurrent(current);hideSB();current=-1}break;default:if(typeof timer!=="undefined"){clearTimeout(timer)}timer=setTimeout(function(){searchSuggest(string.value)},config.delay);break}}}function setCurrent(cur,insert_into_input,stat_param){for(i=0;i<suggestions.length;i++){if(i==cur){addClass(suggestions[i],"current")}else{removeClass(suggestions[i],"current")}}if(current!=-1){if(insert_into_input!=false){document.all?string.value=suggestions[cur].innerText:string.value=suggestions[cur].textContent}if(stat_param!=false){addStatParam()}}else{if(insert_into_input!=false){string.value=last_word}removeStatParam()}}function addStatParam(){if(search_sg.value==0){search_sg.value=1}}function removeStatParam(){if(search_sg.value==1){search_sg.value=0}}(function(){removeStatParam();if(ALL.cookie.get("suggest")=="false"){string.setAttribute("autocomplete","on");ALL.$("suggestDown").style.display="block"}else{string.setAttribute("autocomplete","off")}if(ALL.$("view_suggest")){ALL.$("view_suggest").onclick=function(){var exp=new Date();ALL.cookie.set("suggest","true",{expires:new Date(exp.setFullYear(exp.getFullYear()+1))});ALL.$("suggestDown").style.display="none";string.setAttribute("autocomplete","off");searchSuggest(string.value);string.focus();return false}}search_f.onsubmit=function(){if(no_response){removeStatParam()}};string.onkeyup=function(e){keyAction(e)};if(config.onlyInsertIntoInput){string.onkeypress=function(e){if(typeof e==="undefined"){e=window.event}return !(e.keyCode==13&&current!=-1)}}string.onblur=function(){setTimeout(function(){hideSB()},150)};document.onclick=function(){hideSB()}})()};
