(function(a){a.suggest=function(n,f){var c=a(n).attr("autocomplete","off");var e=a(document.createElement("ul"));var m=false;var d=0;var p=[];var o=0;e.addClass(f.resultsClass).appendTo("body");i();a(window).load(i).resize(i);c.blur(function(){setTimeout(function(){e.hide()},200)});c.data("reset",i);c.keyup(l);function i(){var u=c.offset();e.css({top:(u.top+n.offsetHeight)+"px",left:u.left+"px"})}function l(u){if((/27$|38$|40$/.test(u.keyCode)&&e.is(":visible"))||(/^13$|^9$/.test(u.keyCode)&&s())){if(u.preventDefault){u.preventDefault()}if(u.stopPropagation){u.stopPropagation()}u.cancelBubble=true;u.returnValue=false;switch(u.keyCode){case 38:j();break;case 40:r();break;case 9:case 13:q();break;case 27:e.hide();break}}else{if(c.val().length!=d){if(m){clearTimeout(m)}m=setTimeout(k,f.delay);d=c.val().length;userInput=c.val()}}}function k(){var u=a.trim(c.val());if(u.length>=f.minchars){cached=t(u);if(cached){h(cached.items)}else{a.get(f.source,{q:u},function(v){e.hide();var w=b(v,u);h(w);g(u,w,v.length)})}}else{e.hide()}}function t(v){for(var u=0;u<p.length;u++){if(p[u]["q"]==v){p.unshift(p.splice(u,1)[0]);return p[0]}}return false}function g(x,u,v){while(p.length&&(o+v>f.maxCacheSize)){var w=p.pop();o-=w.size}p.push({q:x,size:v,items:u});o+=v}function h(u){if(!u){return}if(!u.length){e.hide();return}var w="";for(var v=0;v<u.length;v++){w+="<li>"+u[v]+"</li>"}e.html(w).show();e.children("li").mouseover(function(){e.children("li").removeClass(f.selectClass);a(this).addClass(f.selectClass)}).click(function(x){x.preventDefault();x.stopPropagation();q()})}function b(u,y){var v=[];var z=u.split(f.delimiter);for(var x=0;x<z.length;x++){var w=a.trim(z[x]);if(w){w=w.replace(new RegExp(y,"ig"),function(A){return'<span class="'+f.matchClass+'">'+A+"</span>"});v[v.length]=w}}return v}function s(){if(!e.is(":visible")){return false}var u=e.children("li."+f.selectClass);if(!u.length){u=false}return u}function q(){$currentResult=s();if($currentResult){c.val($currentResult.text());e.hide();if(f.onSelect){f.onSelect.apply(c[0])}}}function r(){$currentResult=s();if($currentResult){$currentResult.removeClass(f.selectClass).next().addClass(f.selectClass);c.val($currentResult.next().text()||userInput)}else{e.children("li:first-child").addClass(f.selectClass);c.val(e.children("li:first-child").text())}}function j(){$currentResult=s();if($currentResult){$currentResult.removeClass(f.selectClass).prev().addClass(f.selectClass);c.val($currentResult.prev().text()||userInput)}else{e.children("li:last-child").addClass(f.selectClass);c.val(e.children("li:last-child").text())}}};a.fn.suggest=function(c,b){if(!c){return}b=b||{};b.source=c;b.delay=b.delay||100;b.resultsClass=b.resultsClass||"ac_results";b.selectClass=b.selectClass||"ac_over";b.matchClass=b.matchClass||"ac_match";b.minchars=b.minchars||1;b.delimiter=b.delimiter||"\n";b.onSelect=b.onSelect||false;b.maxCacheSize=b.maxCacheSize||65536;this.each(function(){new a.suggest(this,b)});return this}})(jQuery);
