l&&(n.suggestions=n.suggestions.slice(0,l)),n},getSuggestions:function(t){var n,s,i,o=this,l=o.options,a=l.serviceUrl;if(l.params[l.paramName]=t,s=l.ignoreParams?null:l.params,o.isLocal?n=o.getSuggestionsLocal(t):("function"==typeof a&&(a=a.call(o.element,t)),i=a+"?"+e.param(s||{}),n=o.cachedResponse[i]),n&&Array.isArray(n.suggestions))o.suggestions=n.suggestions,o.suggest();else if(!o.isBadQuery(t)){if(!1===l.onSearchStart.call(o.element,l.params))return;o.currentRequest&&o.currentRequest.abort(),o.currentRequest=e.ajax({url:a,data:s,type:l.type,dataType:l.dataType}).done((function(e){var n;o.currentRequest=null,n=l.transformResult(e),o.processResponse(n,t,i),l.onSearchComplete.call(o.element,t,n.suggestions)})).fail((function(e,n,s){l.onSearchError.call(o.element,t,e,n,s)}))}},isBadQuery:function(e){if(!this.options.preventBadQueries)return!1;for(var t=this.badQueries,n=t.length;n--;)if(0===e.indexOf(t[n]))return!0;return!1},hide:function(){var t=this;t.visible=!1,t.selectedIndex=-1,e(t.suggestionsContainer).hide(),t.signalHint(null)},suggest:function(){if(0!==this.suggestions.length){var t,n=this,s=n.options,i=s.formatResult,o=n.getQuery(n.currentValue),l=n.classes.suggestion,a=n.classes.selected,r=e(n.suggestionsContainer),u=s.beforeRender,c="";s.triggerSelectOnValidInput&&-1!==(t=n.findSuggestionIndex(o))?n.select(t):(e.each(n.suggestions,(function(e,t){c+=''+i(t,o)+"
"})),this.adjustContainerWidth(),r.html(c),s.autoSelectFirst&&(n.selectedIndex=0,r.children().first().addClass(a)),"function"==typeof u&&u.call(n.element,r),r.show(),n.visible=!0,n.findBestHint())}else this.options.showNoSuggestionNotice?this.noSuggestions():this.hide()},noSuggestions:function(){var t=e(this.suggestionsContainer),n="";n+=''+this.options.noSuggestionNotice+"
",this.adjustContainerWidth(),t.html(n),t.show(),this.visible=!0},adjustContainerWidth:function(){var t,n=this,s=n.options,i=e(n.suggestionsContainer);"auto"===s.width&&(t=n.el.outerWidth()-2,i.width(t>0?t:300))},findBestHint:function(){var t=this,n=t.el.val().toLowerCase(),s=null;n&&(e.each(t.suggestions,(function(e,t){var i=0===t.value.toLowerCase().indexOf(n);return i&&(s=t),!i})),t.signalHint(s))},signalHint:function(t){var n="",s=this;t&&(n=s.currentValue+t.value.substr(s.currentValue.length)),s.hintValue!==n&&(s.hintValue=n,s.hint=t,(this.options.onHint||e.noop)(n))},verifySuggestionsFormat:function(t){return t.length&&"string"==typeof t[0]?e.map(t,(function(e){return{value:e,data:null}})):t},processResponse:function(e,t,n){var s=this,i=s.options;e.suggestions=s.verifySuggestionsFormat(e.suggestions),i.noCache||(s.cachedResponse[n]=e,i.preventBadQueries&&0===e.suggestions.length&&s.badQueries.push(t)),t===s.getQuery(s.currentValue)&&(s.suggestions=e.suggestions,s.suggest())},activate:function(t){var n,s=this,i=s.classes.selected,o=e(s.suggestionsContainer),l=o.children();return o.children("."+i).removeClass(i),s.selectedIndex=t,-1!==s.selectedIndex&&l.length>s.selectedIndex?(n=l.get(s.selectedIndex),e(n).addClass(i),n):null},selectHint:function(){var t=this,n=e.inArray(t.hint,t.suggestions);t.select(n)},select:function(e){this.hide(),this.onSelect(e)},moveUp:function(){var t=this;if(-1!==t.selectedIndex)return 0===t.selectedIndex?(e(t.suggestionsContainer).children().first().removeClass(t.classes.selected),t.selectedIndex=-1,t.el.val(t.currentValue),void t.findBestHint()):void t.adjustScroll(t.selectedIndex-1)},moveDown:function(){var e=this;e.selectedIndex!==e.suggestions.length-1&&e.adjustScroll(e.selectedIndex+1)},adjustScroll:function(t){var n,s,i,o=this,l=o.activate(t);l&&(n=l.offsetTop,i=(s=e(o.suggestionsContainer).scrollTop())+o.options.maxHeight-25,ni&&e(o.suggestionsContainer).scrollTop(n-o.options.maxHeight+25),o.el.val(o.getValue(o.suggestions[t].value)),o.signalHint(null))},onSelect:function(e){var t=this,n=t.options.onSelect,s=t.suggestions[e];t.currentValue=t.getValue(s.value),t.currentValue!==t.el.val()&&t.el.val(t.currentValue),t.signalHint(null),t.suggestions=[],t.selection=s,"function"==typeof n&&n.call(t.element,s)},getValue:function(e){var t,n,s=this.options.delimiter;return s?1===(n=(t=this.currentValue).split(s)).length?e:t.substr(0,t.length-n[n.length-1].length)+e:e},dispose:function(){var t=this;t.el.off(".autocomplete").removeData("autocomplete"),t.disableKillerFn(),e(window).off("resize.autocomplete",t.fixPositionCapture),e(t.suggestionsContainer).remove()}},e.fn.yithautocomplete=function(t,n){var s="autocomplete";return 0===arguments.length?this.first().data(s):this.each((function(){var i=e(this),o=i.data(s);"string"==typeof t?o&&"function"==typeof o[t]&&o[t](n):(o&&o.dispose&&o.dispose(),o=new r(this,t),i.data(s,o))}))}}));