

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