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