(function(f){var r=function(c){c=c||{};var b=null,d=null,e=-1,g=!1,l="",n=0,d=egg(".BA-shlist").get(0);c.form=c.form||f("#bhAcgSearch").get(0);var p=function(){clearTimeout(n);n=setTimeout(t,500)},t=function(){if(!b||!b.value)d.style.display="none",d.innerHTML="";else if(b.value!=l){l=b.value;var a="kw="+encodeURIComponent(b.value);c.type&&(a+="&t="+parseInt(c.type,10)||"0");c.machine&&(a+="&m="+parseInt(c.machine,10)||"0");f.get("/ajax/typeahead.php",a,function(a){if(a){var c=document.createElement("ol");
300<b.offsetWidth&&(c.style.width=b.offsetWidth+"px");a=a.split("<SPLIT>",10);var h,g=document.createDocumentFragment();for(h=0;h<a.length;h++){var k=document.createElement("li");k.title=a[h];k.innerHTML=a[h];f(k).attr("data-index",h).mouseover(m).mousedown(u);g.appendChild(k)}c.appendChild(g);d.innerHTML="";d.appendChild(c);d.style.display="block";e=-1}else d.style.display="none",d.innerHTML=""})}},m=function(a){var c=f(".BA-shlist > ol").get(0).childNodes,d=c.length;b.focus();c[e]&&(c[e].className=
"");"number"==typeof a?(e+=a,-1>e?e=d-1:e>=d&&(e=-1),(a=c[e])?(a.className="cur",b.value=a.innerHTML):b.value=l):(a=a||window.event,a=a.target||a.srcElement,a.className="cur",e=parseInt(f(a).attr("data-index"),10))},u=function(a){a=a||window.event;a=a.target||a.srcElement;a.className="cur";b.value=a.innerHTML;c.form&&c.form.submit()},v=function(a){a=a||window.event;var b={38:function(){m(-1)},40:function(){m(1)}};g=!0;a.keyCode in b&&(g=!1,"none"==d.style.display?d.hasChildNodes()&&(d.style.display=
"block"):(b[a.keyCode](),a.preventDefault&&a.preventDefault(),a.returnValue=!1))},w=function(a){d.style.display="none";!c.placeholder||"placeholder"in b||b.value||(b.value=c.placeholder)},x=function(a){!c.placeholder||"placeholder"in b||b.value!=c.placeholder||(b.value="")},q=function(){!c.placeholder||"placeholder"in b||b.value!=c.placeholder||(b.value="");b.value&&c.form.submit()};this.bind=function(){b=c.inputElement||f(".bh-typeahead").get(0);"oninput"in b?b.oninput=p:b.onpropertychange=function(){"value"==
window.event.propertyName&&g&&p()};c.placeholder&&("placeholder"in b?b.placeholder=c.placeholder:b.value=c.placeholder);f(b).keyup(v).focus(x).blur(w);f().jquery?f(b).offset():f(b).pos()};this.search=function(){var a="http://acg.gamer.com.tw/search.php";b.value&&(b&&b.value&&(a+="?kw="+encodeURIComponent(b.value)),c.machine?a+="&sp=m"+parseInt(c.machine,10):c.type&&(a+="&sp=t"+parseInt(c.type,10)),location.href=a)};this.submit=q;r.submit=q};window.AcgTypeahead=r})(egg);