(function(c){c.Autocompleter=function(a,d){this.cacheData_={};this.cacheLength_=0;this.selectClass_="jquery-autocomplete-selected-item";this.lastSelectedValue_=this.lastProcessedValue_=this.lastKeyPressed_=this.keyTimeout_=null;this.active_=!1;this.finishOnBlur_=!0;if(!a||!(a instanceof jQuery)||a.length!==1||a.get(0).tagName.toUpperCase()!=="INPUT")alert("Invalid parameter for jquery.Autocompleter, jQuery object with one element with INPUT tag expected");else{a.attr("autocomplete","off");this.options=
typeof d==="string"?{url:d}:d;this.options.minChars=parseInt(this.options.minChars,10);if(isNaN(this.options.minChars)||this.options.minChars<1)this.options.minChars=2;this.options.maxItemsToShow=parseInt(this.options.maxItemsToShow,10);if(isNaN(this.options.maxItemsToShow)||this.options.maxItemsToShow<1)this.options.maxItemsToShow=10;this.options.maxCacheLength=parseInt(this.options.maxCacheLength,10);if(isNaN(this.options.maxCacheLength)||this.options.maxCacheLength<1)this.options.maxCacheLength=
10;this.dom={};this.dom.$elem=a;this.options.inputClass&&this.dom.$elem.addClass(this.options.inputClass);this.dom.$results=c("<div></div>").hide();this.options.resultsClass&&this.dom.$results.addClass(this.options.resultsClass);this.dom.$results.css({position:"absolute"});c("body").append(this.dom.$results);var b=this;a.keydown(function(a){b.lastKeyPressed_=a.keyCode;switch(b.lastKeyPressed_){case 38:return a.preventDefault(),b.active_?b.focusPrev():b.activate(),!1;case 40:return a.preventDefault(),
b.active_?b.focusNext():b.activate(),!1;case 9:if(b.active_&&(b.selectCurrent(),b.options.preventDefaultTab))return a.preventDefault(),!1;break;case 13:if(b.active_&&(b.selectCurrent(),b.options.preventDefaultReturn))return a.preventDefault(),!1;break;case 27:if(b.active_)return a.preventDefault(),b.finish(),!1;break;default:b.activate()}});a.blur(function(){b.finishOnBlur_&&setTimeout(function(){b.finish()},200)})}};c.Autocompleter.prototype.position=function(){var a=this.dom.$elem.offset();this.dom.$results.css({top:a.top+
this.dom.$elem.outerHeight(),left:a.left})};c.Autocompleter.prototype.cacheRead=function(a){var d,b,c,f,h;if(this.options.useCache){a=String(a);d=a.length;for(b=this.options.matchSubset?1:d;b<=d;){f=this.options.matchInside?d-b:0;for(h=0;h<=f;){c=a.substr(0,b);if(this.cacheData_[c]!==void 0)return this.cacheData_[c];h++}b++}}return!1};c.Autocompleter.prototype.cacheWrite=function(a,d){if(this.options.useCache)return this.cacheLength_>=this.options.maxCacheLength&&this.cacheFlush(),a=String(a),this.cacheData_[a]!==
void 0&&this.cacheLength_++,this.cacheData_[a]=d,this.cacheData_[a];return!1};c.Autocompleter.prototype.cacheFlush=function(){this.cacheData_={};this.cacheLength_=0};c.Autocompleter.prototype.callHook=function(a,d){var b=this.options[a];if(b&&c.isFunction(b))return b(d,this);return!1};c.Autocompleter.prototype.activate=function(){var a=this,d=parseInt(this.options.delay,10);if(isNaN(d)||d<=0)d=250;this.keyTimeout_&&clearTimeout(this.keyTimeout_);this.keyTimeout_=setTimeout(function(){a.activateNow()},
d)};c.Autocompleter.prototype.activateNow=function(){var a=this.dom.$elem.val();if(a!==this.lastProcessedValue_&&a!==this.lastSelectedValue_&&a.length>=this.options.minChars)this.lastProcessedValue_=a,this.fetchData(a)};c.Autocompleter.prototype.fetchData=function(a){if(this.options.data)this.filterAndShowResults(this.options.data,a);else{var d=this;this.fetchRemoteData(a,function(b){d.filterAndShowResults(b,a)})}};c.Autocompleter.prototype.fetchRemoteData=function(a,d){var b=this.cacheRead(a);if(b)d(b);
else{var e=this;this.dom.$elem.addClass(this.options.loadingClass);var f=function(b){var c=!1;b!==!1&&(c=e.parseRemoteData(b),e.cacheWrite(a,c));e.dom.$elem.removeClass(e.options.loadingClass);d(c)};c.ajax({url:this.makeUrl(a),success:f,error:function(){f(!1)}})}};c.Autocompleter.prototype.setExtraParam=function(a,d){var b=c.trim(String(a));if(b){if(!this.options.extraParams)this.options.extraParams={};this.options.extraParams[b]!==d&&(this.options.extraParams[b]=d,this.cacheFlush())}};c.Autocompleter.prototype.makeUrl=
function(a){var d=this,b=this.options.url,e=c.extend({},this.options.extraParams);this.options.queryParamName===!1?b+=encodeURIComponent(a):e[this.options.queryParamName]=a;if(this.options.limitParamName&&this.options.maxItemsToShow)e[this.options.limitParamName]=this.options.maxItemsToShow;var f=[];c.each(e,function(a,b){f.push(d.makeUrlParam(a,b))});f.length&&(b+=b.indexOf("?")===-1?"?":"&",b+=f.join("&"));return b};c.Autocompleter.prototype.makeUrlParam=function(a,d){return[a,encodeURIComponent(d)].join("=")};
c.Autocompleter.prototype.parseRemoteData=function(a){for(var d=[],b,c,f,h=String(a).replace("\r\n",this.options.lineSeparator).split(this.options.lineSeparator),a=0;a<h.length;a++){f=h[a].split(this.options.cellSeparator);c=[];for(b=0;b<f.length;b++)c.push(unescape(f[b]));b=c.shift();d.push({value:unescape(b),data:c})}return d};c.Autocompleter.prototype.filterAndShowResults=function(a,d){this.showResults(this.filterResults(a,d),d)};c.Autocompleter.prototype.filterResults=function(a,d){var b=[],e,
f,h,g,i;for(h=0;h<a.length;h++){g=a[h];i=typeof g;if(i==="string")e=g,f={};else if(c.isArray(g))e=g[0],f=g.slice(1);else if(i==="object")e=g.value,f=g.data;e=String(e);e>""&&(typeof f!=="object"&&(f={}),this.options.filterResults?(g=this.matchStringConvertor(d),i=this.matchStringConvertor(e),this.options.matchCase||(g=g.toLowerCase(),i=i.toLowerCase()),g=i.indexOf(g),g=this.options.matchInside?g>-1:g===0):g=!0,g&&b.push({value:e,data:f}))}this.options.sortResults&&(b=this.sortResults(b,d));if(this.options.maxItemsToShow>
0&&this.options.maxItemsToShow<b.length)b.length=this.options.maxItemsToShow;return b};c.Autocompleter.prototype.sortResults=function(a,d){var b=this,e=this.options.sortFunction;c.isFunction(e)||(e=function(a,d,c){return b.sortValueAlpha(a,d,c)});a.sort(function(a,b){return e(a,b,d)});return a};c.Autocompleter.prototype.sortValueAlpha=function(a,d){a=String(a.value);d=String(d.value);this.options.matchCase||(a=a.toLowerCase(),d=d.toLowerCase());if(a>d)return 1;if(a<d)return-1;return 0};c.Autocompleter.prototype.matchStringConvertor=
function(a,d,b){var e=this.options.matchStringConvertor;c.isFunction(e)&&(a=e(a,d,b));return a};c.Autocompleter.prototype.showResults=function(a,d){var b=a.length;if(b===0)return this.finish();var e=this,f=c("<ul></ul>"),h,g,i,j=!1,k=!1;for(h=0;h<b;h++)g=a[h],i=c("<li>"+this.showResult(g.value,g.data)+"</li>"),i.data("value",g.value),i.data("data",g.data),i.click(function(){var a=c(this);e.selectItem(a)}).mousedown(function(){e.finishOnBlur_=!1}).mouseup(function(){e.finishOnBlur_=!0}),f.append(i),
j===!1&&(j=String(g.value),k=i,i.addClass(this.options.firstItemClass)),h===b-1&&i.addClass(this.options.lastItemClass);this.position();this.dom.$results.html(f).show();f=this.dom.$results.outerWidth()-this.dom.$results.width();this.dom.$results.width(this.dom.$elem.outerWidth()-f);c("li",this.dom.$results).hover(function(){e.focusItem(this)},function(){});(this.autoFill(j,d)||this.options.selectFirst||this.options.selectOnly&&b==1)&&this.focusItem(k);this.active_=!0};c.Autocompleter.prototype.showResult=
function(a,d){return c.isFunction(this.options.showResult)?this.options.showResult(a,d):a};c.Autocompleter.prototype.autoFill=function(a,d){var b,c,f,h;if(this.options.autoFill&&this.lastKeyPressed_!==8&&(b=String(a).toLowerCase(),c=String(d).toLowerCase(),f=a.length,h=d.length,b.substr(0,h)===c))return this.dom.$elem.val(a),this.selectRange(h,f),!0;return!1};c.Autocompleter.prototype.focusNext=function(){this.focusMove(1)};c.Autocompleter.prototype.focusPrev=function(){this.focusMove(-1)};c.Autocompleter.prototype.focusMove=
function(a){for(var d=c("li",this.dom.$results),a=parseInt(a,10),b=0;b<d.length;b++)if(c(d[b]).hasClass(this.selectClass_)){this.focusItem(b+a);return}this.focusItem(0)};c.Autocompleter.prototype.focusItem=function(a){var d=c("li",this.dom.$results);d.length&&(d.removeClass(this.selectClass_).removeClass(this.options.selectClass),typeof a==="number"?(a=parseInt(a,10),a<0?a=0:a>=d.length&&(a=d.length-1),a=c(d[a])):a=c(a),a&&a.addClass(this.selectClass_).addClass(this.options.selectClass))};c.Autocompleter.prototype.selectCurrent=
function(){var a=c("li."+this.selectClass_,this.dom.$results);a.length===1?this.selectItem(a):this.finish()};c.Autocompleter.prototype.selectItem=function(a){var c=a.data("value"),a=a.data("data"),b=this.displayValue(c,a);this.lastSelectedValue_=this.lastProcessedValue_=b;this.dom.$elem.val(b).focus();this.setCaret(b.length);this.callHook("onItemSelect",{value:c,data:a});this.finish()};c.Autocompleter.prototype.displayValue=function(a,d){return c.isFunction(this.options.displayValue)?this.options.displayValue(a,
d):a};c.Autocompleter.prototype.finish=function(){this.keyTimeout_&&clearTimeout(this.keyTimeout_);this.dom.$elem.val()!==this.lastSelectedValue_&&(this.options.mustMatch&&this.dom.$elem.val(""),this.callHook("onNoMatch"));this.dom.$results.hide();this.lastProcessedValue_=this.lastKeyPressed_=null;this.active_&&this.callHook("onFinish");this.active_=!1};c.Autocompleter.prototype.selectRange=function(a,c){var b=this.dom.$elem.get(0);b.setSelectionRange?(b.focus(),b.setSelectionRange(a,c)):this.createTextRange&&
(b=this.createTextRange(),b.collapse(!0),b.moveEnd("character",c),b.moveStart("character",a),b.select())};c.Autocompleter.prototype.setCaret=function(a){this.selectRange(a,a)};c.fn.autocomplete=function(a){typeof a==="string"&&(a={url:a});var d=c.extend({},c.fn.autocomplete.defaults,a);return this.each(function(){var a=c(this),e=new c.Autocompleter(a,d);a.data("autocompleter",e)})};c.fn.autocomplete.defaults={inputClass:"acInput",loadingClass:"acLoading",resultsClass:"acResults",selectClass:"acSelect",
queryParamName:"q",limitParamName:"limit",extraParams:{},lineSeparator:"\n",cellSeparator:"|",minChars:2,maxItemsToShow:10,delay:400,useCache:!0,maxCacheLength:10,matchSubset:!0,matchCase:!1,matchInside:!0,mustMatch:!1,selectFirst:!1,selectOnly:!1,showResult:null,preventDefaultReturn:!0,preventDefaultTab:!1,autoFill:!1,filterResults:!0,sortResults:!0,sortFunction:null,onItemSelect:null,onNoMatch:null,onFinish:null,matchStringConvertor:null}})(jQuery);
