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