(function(a){a.fn.extend({autocomplete:function(d,c){var b=typeof d=="string";c=a.extend({},a.Autocompleter.defaults,{url:b?d:null,data:b?null:d,delay:b?a.Autocompleter.defaults.delay:10,max:c&&!c.scroll?10:150},c);c.highlight=c.highlight||function(e){return e};c.formatMatch=c.formatMatch||c.formatItem;return this.each(function(){new a.Autocompleter(this,c)})},result:function(b){return this.bind("result",b)},search:function(b){return this.trigger("search",[b])},flushCache:function(){return this.trigger("flushCache")},setOptions:function(b){return this.trigger("setOptions",[b])},unautocomplete:function(){return this.trigger("unautocomplete")}});a.Autocompleter=function(f,q){var p={UP:38,DOWN:40,DEL:46,TAB:9,RETURN:13,ESC:27,COMMA:188,PAGEUP:33,PAGEDOWN:34,BACKSPACE:8};var o=a(f).attr("autocomplete","off").addClass(q.inputClass);var m;var k="";var i=a.Autocompleter.Cache(q);var n=0;var l;var j={mouseDownOnSelect:false};var h=a.Autocompleter.Select(q,f,g,j);var d;a.browser.opera&&a(f.form).bind("submit.autocomplete",function(){if(d){d=false;return false}});o.bind((a.browser.opera?"keypress":"keydown")+".autocomplete",function(y){l=y.keyCode;switch(y.keyCode){case p.UP:y.preventDefault();if(h.visible()){h.prev()}else{c(0,true)}break;case p.DOWN:y.preventDefault();if(h.visible()){h.next()}else{c(0,true)}break;case p.PAGEUP:y.preventDefault();if(h.visible()){h.pageUp()}else{c(0,true)}break;case p.PAGEDOWN:y.preventDefault();if(h.visible()){h.pageDown()}else{c(0,true)}break;case q.multiple&&a.trim(q.multipleSeparator)==","&&p.COMMA:case p.TAB:case p.RETURN:if(g()){y.preventDefault();d=true;return false}break;case p.ESC:h.hide();break;default:clearTimeout(m);m=setTimeout(c,q.delay);break}}).focus(function(){n++}).blur(function(){n=0;if(!j.mouseDownOnSelect){b()}}).click(function(){if(n++>1&&!h.visible()){c(0,true)}}).bind("search",function(){var z=(arguments.length>1)?arguments[1]:null;function y(D,B){var A;if(B&&B.length){for(var C=0;C<B.length;C++){if(B[C].result.toLowerCase()==D.toLowerCase()){A=B[C];break}}}if(typeof z=="function"){z(A)}else{o.trigger("result",A&&[A.data,A.value])}}a.each(trimWords(o.val()),function(B,A){w(A,y,y)})}).bind("flushCache",function(){i.flush()}).bind("setOptions",function(){a.extend(q,arguments[1]);if("data" in arguments[1]){i.populate()}}).bind("unautocomplete",function(){h.unbind();o.unbind();a(f.form).unbind(".autocomplete")});function g(){var A=h.selected();if(!A){return false}var y=A.result;k=y;if(q.multiple){var z=s(o.val());if(z.length>1){y=z.slice(0,z.length-1).join(q.multipleSeparator)+q.multipleSeparator+y}y+=q.multipleSeparator}o.val(y);r();o.trigger("result",[A.data,A.value]);return true}function c(A,z){if(l==p.DEL){h.hide();return}var y=o.val();if(!z&&y==k){return}k=y;y=v(y);if(y.length>=q.minChars){o.addClass(q.loadingClass);if(!q.matchCase){y=y.toLowerCase()}w(y,u,r)}else{t();h.hide()}}function s(A){if(!A){return[""]}var z=A.split(q.multipleSeparator);var y=[];a.each(z,function(C,B){if(a.trim(B)){y[C]=a.trim(B)}});return y}function v(z){if(!q.multiple){return z}var y=s(z);return y[y.length-1]}function x(y,z){if(q.autoFill&&(v(o.val()).toLowerCase()==y.toLowerCase())&&l!=p.BACKSPACE){o.val(o.val()+z.substring(v(k).length));a.Autocompleter.Selection(f,k.length,k.length+z.length)}}function b(){clearTimeout(m);m=setTimeout(r,200)}function r(){var y=h.visible();h.hide();clearTimeout(m);t();if(q.mustMatch){o.search(function(A){if(!A){if(q.multiple){var z=s(o.val()).slice(0,-1);o.val(z.join(q.multipleSeparator)+(z.length?q.multipleSeparator:""))}else{o.val("")}}})}if(y){a.Autocompleter.Selection(f,f.value.length,f.value.length)}}function u(y,z){if(z&&z.length&&n){t();h.display(z,y);x(y,z[0].value);h.show()}else{r()}}function w(C,B,A){if(!q.matchCase){C=C.toLowerCase()}var y=i.load(C);if(y&&y.length){B(C,y)}else{if((typeof q.url=="string")&&(q.url.length>0)){var z={timestamp:+new Date()};a.each(q.extraParams,function(E,D){z[E]=typeof D=="function"?D():D});a.ajax({mode:"abort",port:"autocomplete"+f.name,dataType:q.dataType,url:q.url,data:a.extend({q:v(C),limit:q.max},z),success:function(E){var D=q.parse&&q.parse(E)||e(E);i.add(C,D);B(C,D)}})}else{h.emptyList();A(C)}}}function e(C){var B=[];var z=C.split("\n");for(var y=0;y<z.length;y++){var A=a.trim(z[y]);if(A){A=A.split("|");B[B.length]={data:A,value:A[0],result:q.formatResult&&q.formatResult(A,A[0])||A[0]}}}return B}function t(){o.removeClass(q.loadingClass)}};a.Autocompleter.defaults={inputClass:"ac_input",resultsClass:"ac_results",loadingClass:"ac_loading",minChars:1,delay:400,matchCase:false,matchSubset:true,matchContains:false,cacheLength:10,max:100,mustMatch:false,extraParams:{},selectFirst:true,formatItem:function(b){return b[0]},formatMatch:null,autoFill:false,width:0,multiple:false,multipleSeparator:", ",highlight:function(c,b){return c.replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)("+b.replace(/([\^\$\(\)\[\]\{\}\*\.\+\?\|\\])/gi,"\\$1")+")(?![^<>]*>)(?![^&;]+;)","gi"),"<strong>$1</strong>")},scroll:true,scrollHeight:180};a.Autocompleter.Cache=function(e){var c={};var d=0;function b(l,k){if(!e.matchCase){l=l.toLowerCase()}var j=l.indexOf(k);if(e.matchContains=="word"){j=l.toLowerCase().search("\\b"+k.toLowerCase())}if(j==-1){return false}return j==0||e.matchContains}function f(j,i){if(d>e.cacheLength){h()}if(!c[j]){d++}c[j]=i}function g(){if(!e.data){return false}var k={},j=0;if(!e.url){e.cacheLength=1}k[""]=[];for(var m=0,l=e.data.length;m<l;m++){var q=e.data[m];q=(typeof q=="string")?[q]:q;var n=e.formatMatch(q,m+1,e.data.length);if(n===false){continue}var p=n.charAt(0).toLowerCase();if(!k[p]){k[p]=[]}var o={value:n,data:q,result:e.formatResult&&e.formatResult(q)||n};k[p].push(o);if(j++<e.max){k[""].push(o)}}a.each(k,function(r,s){e.cacheLength++;f(r,s)})}setTimeout(g,25);function h(){c={};d=0}return{flush:h,add:f,populate:g,load:function(n){if(!e.cacheLength||!d){return null}if(!e.url&&e.matchContains){var m=[];for(var j in c){if(j.length>0){var o=c[j];a.each(o,function(p,k){if(b(k.value,n)){m.push(k)}})}}return m}else{if(c[n]){return c[n]}else{if(e.matchSubset){for(var l=n.length-1;l>=e.minChars;l--){var o=c[n.substr(0,l)];if(o){var m=[];a.each(o,function(p,k){if(b(k.value,n)){m[m.length]=k}});return m}}}}}return null}}};a.Autocompleter.Select=function(s,r,q,m){var l={ACTIVE:"ac_over"};var k,j=-1,h,g="",f=true,d,c;function b(){if(!f){return}d=a("<div/>").hide().addClass(s.resultsClass).css("position","absolute").appendTo(document.body);c=a("<ul/>").appendTo(d).mouseover(function(t){if(target(t).nodeName&&target(t).nodeName.toUpperCase()=="LI"){j=a("li",c).removeClass(l.ACTIVE).index(target(t));a(target(t)).addClass(l.ACTIVE)}}).click(function(t){a(target(t)).addClass(l.ACTIVE);q();r.focus();return false}).mousedown(function(){m.mouseDownOnSelect=true}).mouseup(function(){m.mouseDownOnSelect=false});if(s.width>0){d.css("width",s.width)}f=false}function i(u){var t=u.target;while(t&&t.tagName!="LI"){t=t.parentNode}if(!t){return[]}return t}function e(u){k.slice(j,j+1).removeClass(l.ACTIVE);p(u);var t=k.slice(j,j+1).addClass(l.ACTIVE);if(s.scroll){var v=0;k.slice(0,j).each(function(){v+=this.offsetHeight});if((v+t[0].offsetHeight-c.scrollTop())>c[0].clientHeight){c.scrollTop(v+t[0].offsetHeight-c.innerHeight())}else{if(v<c.scrollTop()){c.scrollTop(v)}}}}function p(t){j+=t;if(j<0){j=k.size()-1}else{if(j>=k.size()){j=0}}}function o(t){return s.max&&s.max<t?s.max:t}function n(){c.empty();var u=o(h.length);for(var v=0;v<u;v++){if(!h[v]){continue}var w=s.formatItem(h[v].data,v+1,u,h[v].value,g);if(w===false){continue}var t=a("<li/>").html(s.highlight(w,g)).addClass(v%2==0?"ac_even":"ac_odd").appendTo(c)[0];a.data(t,"ac_data",h[v])}k=c.find("li");if(s.selectFirst){k.slice(0,1).addClass(l.ACTIVE);j=0}if(a.fn.bgiframe){c.bgiframe()}}return{display:function(u,t){b();h=u;g=t;n()},next:function(){e(1)},prev:function(){e(-1)},pageUp:function(){if(j!=0&&j-8<0){e(-j)}else{e(-8)}},pageDown:function(){if(j!=k.size()-1&&j+8>k.size()){e(k.size()-1-j)}else{e(8)}},hide:function(){d&&d.hide();k&&k.removeClass(l.ACTIVE);j=-1},visible:function(){return d&&d.is(":visible")},current:function(){return this.visible()&&(k.filter("."+l.ACTIVE)[0]||s.selectFirst&&k[0])},show:function(){var v=a(r).offset();d.css({width:typeof s.width=="string"||s.width>0?s.width:a(r).width(),top:v.top+r.offsetHeight,left:v.left}).show();if(s.scroll){c.scrollTop(0);c.css({maxHeight:s.scrollHeight,overflow:"auto"});if(a.browser.msie&&typeof document.body.style.maxHeight==="undefined"){var t=0;k.each(function(){t+=this.offsetHeight});var u=t>s.scrollHeight;c.css("height",u?s.scrollHeight:t);if(!u){k.width(c.width()-parseInt(k.css("padding-left"))-parseInt(k.css("padding-right")))}}}},selected:function(){var t=k&&k.filter("."+l.ACTIVE).removeClass(l.ACTIVE);document.getElementById("searchbutton").style.backgroundColor="red";document.getElementById("searchbutton").value="Go!";return t&&t.length&&a.data(t[0],"ac_data")},emptyList:function(){c&&c.empty()},unbind:function(){d&&d.remove()}}};a.Autocompleter.Selection=function(d,c,b){if(d.createTextRange){var e=d.createTextRange();e.collapse(true);e.moveStart("character",c);e.moveEnd("character",b);e.select()}else{if(d.setSelectionRange){d.setSelectionRange(c,b)}else{if(d.selectionStart){d.selectionStart=c;d.selectionEnd=b}}}d.focus()}})(jQuery);