(function(c){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);a.data("autocompleter",new c.Autocompleter(a,c.meta?c.extend({},b,a.data()):b))})};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"};var i=function(a,d,b){a=parseInt(a,10);b=b||{};if(isNaN(a)||b.min&&a<b.min)a=d;return a},
l=function(a,d){var b=[];c.each(d,function(a,d){b.push([a,encodeURIComponent(d)].join("="))});b.length&&(a+=-1===a.indexOf("?")?"?":"&",a+=b.join("&"));return a};c.Autocompleter=function(a,d){if(!a||!(a instanceof c)||1!==a.length||"INPUT"!==a.get(0).tagName.toUpperCase())throw Error("Invalid parameter for jquery.Autocompleter, jQuery object with one element with INPUT tag expected.");var b=this;this.options=d;this.cacheData_={};this.cacheLength_=0;this.selectClass_="jquery-autocomplete-selected-item";
this.lastSelectedValue_=this.lastProcessedValue_=this.lastKeyPressed_=this.finishTimeout_=this.keyTimeout_=null;this.active_=!1;this.finishOnBlur_=!0;this.options.minChars=i(this.options.minChars,c.fn.autocomplete.defaults.minChars,{min:1});this.options.maxItemsToShow=i(this.options.maxItemsToShow,c.fn.autocomplete.defaults.maxItemsToShow,{min:1});this.options.maxCacheLength=i(this.options.maxCacheLength,c.fn.autocomplete.defaults.maxCacheLength,{min:1});this.options.delay=i(this.options.delay,c.fn.autocomplete.defaults.delay,
{min:0});this.dom={};this.dom.$elem=a;this.dom.$elem.attr("autocomplete","off").addClass(this.options.inputClass);this.dom.$results=c("<div></div>").hide().addClass(this.options.resultsClass).css({position:"absolute"});c("body").append(this.dom.$results);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(){if(b.finishOnBlur_)b.finishTimeout_=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,g;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(g=0;g<=f;){c=a.substr(0,b);if(void 0!==this.cacheData_[c])return this.cacheData_[c];g++}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;this.keyTimeout_&&clearTimeout(this.keyTimeout_);this.keyTimeout_=setTimeout(function(){a.activateNow()},this.options.delay)};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){var d=this,b=function(a,b){d.showResults(d.filterResults(a,b),b)};this.options.data?b(this.options.data,a):this.fetchRemoteData(a,function(d){b(d,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.options.url,b=c.extend({},
this.options.extraParams);!1===this.options.queryParamName?d+=encodeURIComponent(a):b[this.options.queryParamName]=a;if(this.options.limitParamName&&this.options.maxItemsToShow)b[this.options.limitParamName]=this.options.maxItemsToShow;return l(d,b)};c.Autocompleter.prototype.parseRemoteData=function(a){var d;if("json"===this.options.remoteDataType)d=c.parseJSON(a);else{var b=this.options.lineSeparator;d=this.options.cellSeparator;var e=[],f,g,h;h=(""+a).replace("\r\n","\n").split(b);for(a=0;a<h.length;a++){g=
h[a].split(d);b=[];for(f=0;f<g.length;f++)b.push(decodeURIComponent(g[f]));f=b.shift();e.push({value:f,data:b})}d=e}return d};c.Autocompleter.prototype.filterResult=function(a,d){if(!a.value)return!1;if(this.options.filterResults){var b=this.matchStringConverter(d),c=this.matchStringConverter(a.value);this.options.matchCase||(b=b.toLowerCase(),c=c.toLowerCase());b=c.indexOf(b);return this.options.matchInside?-1<b:0===b}return!0};c.Autocompleter.prototype.filterResults=function(a,d){var b=[],e,f;for(e=
0;e<a.length;e++){f=a[e];var g=void 0,h=void 0,j=typeof f;if("string"===j)g=f,h={};else if(c.isArray(f))g=f[0],h=f.slice(1);else if("object"===j)g=f.value,h=f.data;g=""+g;"object"!==typeof h&&(h={});f={value:g,data:h};this.filterResult(f,d)&&b.push(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,c){var d;var e;d=""+a.value;e=""+c.value;b.options.matchCase||(d=d.toLowerCase(),e=e.toLowerCase());return d>e?1:d<e?-1:0});a.sort(function(a,c){return e(a,c,d,b.options)});return a};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,data:a.data}).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>"),
g,h,j,i=!1,k=!1;for(g=0;g<b;g++)h=a[g],j=this.createItemFromResult(h),f.append(j),!1===i&&(i=""+h.value,k=j,j.addClass(this.options.firstItemClass)),g===b-1&&j.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(i,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,c){var b,e,f,g;return this.options.autoFill&&8!==this.lastKeyPressed_&&(b=(""+a).toLowerCase(),e=(""+c).toLowerCase(),f=a.length,g=c.length,b.substr(0,g)===e)?(this.dom.$elem.val(a),this.selectRange(g,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){var d=c("li",this.dom.$results);if(a=i(a,0))for(var 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?(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.finishTimeout_&&clearTimeout(this.finishTimeout_);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)}})(jQuery);

