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