(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)||1!==a.length||"INPUT"!==a.get(0).tagName.toUpperCase())return window.alert("Invalid parameter for jquery.Autocompleter, jQuery object with one element with INPUT tag expected"),!1;a.attr("autocomplete","off");
this.options="string"===typeof d?{url:d}:d;this.options.minChars=parseInt(this.options.minChars,10);if(isNaN(this.options.minChars)||1>this.options.minChars)this.options.minChars=2;this.options.maxItemsToShow=parseInt(this.options.maxItemsToShow,10);if(isNaN(this.options.maxItemsToShow)||1>this.options.maxItemsToShow)this.options.maxItemsToShow=10;this.options.maxCacheLength=parseInt(this.options.maxCacheLength,10);if(isNaN(this.options.maxCacheLength)||1>this.options.maxCacheLength)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=""+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(void 0!==this.cacheData_[c])return this.cacheData_[c];h++}b++}}return!1};c.Autocompleter.prototype.cacheWrite=function(a,d){return this.options.useCache?(this.cacheLength_>=this.options.maxCacheLength&&this.cacheFlush(),a=""+a,void 0!==this.cacheData_[a]&&
this.cacheLength_++,this.cacheData_[a]=d,this.cacheData_[a]):!1};c.Autocompleter.prototype.cacheFlush=function(){this.cacheData_={};this.cacheLength_=0};c.Autocompleter.prototype.callHook=function(a,d){var b=this.options[a];return b&&c.isFunction(b)?b(d,this):!1};c.Autocompleter.prototype.activate=function(){var a=this,d=parseInt(this.options.delay,10);if(isNaN(d)||0>=d)d=250;this.keyTimeout_&&clearTimeout(this.keyTimeout_);this.keyTimeout_=setTimeout(function(){a.activateNow()},d)};c.Autocompleter.prototype.activateNow=
function(){var a=this.beforeUseConverter(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;!1!==b&&(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)},dataType:"text"})}};c.Autocompleter.prototype.setExtraParam=function(a,d){var b=c.trim(""+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);!1===this.options.queryParamName?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+=-1===b.indexOf("?")?"?":"&",b+=f.join("&"));return b};c.Autocompleter.prototype.makeUrlParam=function(a,d){return[a,encodeURIComponent(d)].join("=")};
c.Autocompleter.prototype.parseRemoteData=function(a){return"json"===this.options.remoteDataType?this.parseRemoteJSON(a):this.parseRemoteText(a)};c.Autocompleter.prototype.parseRemoteText=function(a){for(var d=[],b,c,f,h=(""+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(decodeURIComponent(f[b]));b=c.shift();d.push({value:b,data:c})}return d};c.Autocompleter.prototype.parseRemoteJSON=
function(a){return c.parseJSON(a)};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("string"===i)e=g,f={};else if(c.isArray(g))e=g[0],f=g.slice(1);else if("object"===i)e=g.value,f=g.data;e=""+e;""<e&&("object"!==typeof f&&(f={}),this.options.filterResults?(g=this.matchStringConverter(d),i=this.matchStringConverter(e),this.options.matchCase||
(g=g.toLowerCase(),i=i.toLowerCase()),g=i.indexOf(g),g=this.options.matchInside?-1<g:0===g):g=!0,g&&b.push({value:e,data:f}))}this.options.sortResults&&(b=this.sortResults(b,d));if(0<this.options.maxItemsToShow&&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=""+a.value;d=""+d.value;this.options.matchCase||(a=a.toLowerCase(),d=d.toLowerCase());return a>d?1:a<d?-1:0};c.Autocompleter.prototype.matchStringConverter=function(a,d,b){var e=this.options.matchStringConverter;c.isFunction(e)&&(a=e(a,d,b));return a};c.Autocompleter.prototype.beforeUseConverter=function(a,d,b){var e=this.options.beforeUseConverter;c.isFunction(e)&&(a=e(a,d,b));return a};c.Autocompleter.prototype.enableFinishOnBlur=function(){this.finishOnBlur_=
!0};c.Autocompleter.prototype.disableFinishOnBlur=function(){this.finishOnBlur_=!1};c.Autocompleter.prototype.createItemFromResult=function(a){var d=this,b=c("<li>"+this.showResult(a.value,a.data)+"</li>");b.data("value",a.value);b.data("data",a.data);b.click(function(){d.selectItem(b)}).mousedown(d.disableFinishOnBlur).mouseup(d.enableFinishOnBlur);return b};c.Autocompleter.prototype.showResults=function(a,d){var b=a.length;if(0===b)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=this.createItemFromResult(g),f.append(i),!1===j&&(j=""+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();this.options.autoWidth&&(f=this.dom.$elem.outerWidth()-this.dom.$results.outerWidth()+this.dom.$results.width(),this.dom.$results.css(this.options.autoWidth,f));c("li",this.dom.$results).hover(function(){e.focusItem(this)},function(){});(this.autoFill(j,d)||this.options.selectFirst||
this.options.selectOnly&&1===b)&&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;return this.options.autoFill&&8!==this.lastKeyPressed_&&(b=(""+a).toLowerCase(),c=(""+d).toLowerCase(),f=a.length,h=d.length,b.substr(0,h)===c)?(this.dom.$elem.val(a),this.selectRange(h,f),!0):!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),"number"===typeof a?(a=parseInt(a,10),0>a?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);1===a.length?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),e=this.beforeUseConverter(b);this.lastSelectedValue_=this.lastProcessedValue_=e;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.beforeUseConverter(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){var d;1<arguments.length?(d=a,a=arguments[1],a.url=d):"string"===typeof a&&(a={url:a});var b=c.extend({},c.fn.autocomplete.defaults,
a);return this.each(function(){var a=c(this),d=new c.Autocompleter(a,b);a.data("autocompleter",d)})};c.fn.autocomplete.defaults={inputClass:"acInput",loadingClass:"acLoading",resultsClass:"acResults",selectClass:"acSelect",queryParamName:"q",limitParamName:"limit",extraParams:{},remoteDataType:!1,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,matchStringConverter:null,beforeUseConverter:null,autoWidth:"min-width"}})(jQuery);

