!function(e){"use strict";"function"==typeof define&&define.amd?define(["jquery"],e):e(jQuery)}((function(e){"use strict";var t={escapeRegExChars:function(e){return e.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g,"\\$&")},createNode:function(e){var t=document.createElement("div");return t.className=e,t.style.position="absolute",t.style.display="none",t}},n=27,s=9,i=13,o=38,l=39,a=40;function r(t,n){var s=function(){},i=this,o={autoSelectFirst:!1,appendTo:"body",serviceUrl:null,lookup:null,onSelect:null,width:"auto",minChars:1,maxHeight:300,deferRequestBy:0,params:{},formatResult:r.formatResult,delimiter:null,zIndex:9999,type:"GET",noCache:!1,onSearchStart:s,onSearchComplete:s,onSearchError:s,containerClass:"autocomplete-suggestions",tabDisabled:!1,dataType:"text",currentRequest:null,triggerSelectOnValidInput:!0,preventBadQueries:!0,lookupFilter:function(e,t,n){return-1!==e.value.toLowerCase().indexOf(n)},paramName:"query",transformResult:function(e){return"string"==typeof e?JSON.parse(e):e},showNoSuggestionNotice:!1,noSuggestionNotice:"No results"};i.element=t,i.el=e(t),i.suggestions=[],i.badQueries=[],i.selectedIndex=-1,i.currentValue=i.element.value,i.intervalId=0,i.cachedResponse={},i.onChangeInterval=null,i.onChange=null,i.isLocal=!1,i.suggestionsContainer=null,i.options=e.extend({},o,n),i.classes={selected:"autocomplete-selected",suggestion:"autocomplete-suggestion"},i.hint=null,i.hintValue="",i.selection=null,i.initialize(),i.setOptions(n)}r.utils=t,e.YithAutocomplete=r,r.formatResult=function(e,n){var s="("+t.escapeRegExChars(n)+")";return e.value.replace(new RegExp(s,"gi"),"$1")},r.prototype={killerFn:null,initialize:function(){var t,n=this,s="."+n.classes.suggestion,i=n.classes.selected,o=n.options;n.element.setAttribute("autocomplete","off"),n.killerFn=function(t){0===e(t.target).closest("."+n.options.containerClass).length&&(n.killSuggestions(),n.disableKillerFn())},n.suggestionsContainer=r.utils.createNode(o.containerClass),(t=e(n.suggestionsContainer)).appendTo(o.appendTo),"auto"!==o.width&&t.width(o.width),t.on("mouseover.autocomplete",s,(function(){n.activate(e(this).data("index"))})),t.on("mouseout.autocomplete",(function(){n.selectedIndex=-1,t.children("."+i).removeClass(i)})),t.on("click.autocomplete",s,(function(){n.select(e(this).data("index"))})),n.fixPosition(),n.fixPositionCapture=function(){n.visible&&n.fixPosition()},e(window).on("resize.autocomplete",n.fixPositionCapture),n.el.on("keydown.autocomplete",(function(e){n.onKeyPress(e)})),n.el.on("keyup.autocomplete",(function(e){n.onKeyUp(e)})),n.el.on("blur.autocomplete",(function(){n.onBlur()})),n.el.on("focus.autocomplete",(function(){n.onFocus()})),n.el.on("change.autocomplete",(function(e){n.onKeyUp(e)}))},onFocus:function(){var e=this;e.fixPosition(),e.options.minChars<=e.el.val().length&&e.onValueChange()},onBlur:function(){this.enableKillerFn()},setOptions:function(t){var n=this,s=n.options;e.extend(s,t),n.isLocal=Array.isArray(s.lookup),n.isLocal&&(s.lookup=n.verifySuggestionsFormat(s.lookup)),e(n.suggestionsContainer).css({"max-height":s.maxHeight+"px",width:s.width+"px","z-index":s.zIndex})},clearCache:function(){this.cachedResponse={},this.badQueries=[]},clear:function(){this.clearCache(),this.currentValue="",this.suggestions=[]},disable:function(){var e=this;e.disabled=!0,e.currentRequest&&e.currentRequest.abort()},enable:function(){this.disabled=!1},fixPosition:function(){var t,n,s=this;"body"===s.options.appendTo&&(n={top:(t=s.el.offset()).top+s.el.outerHeight()+"px",left:t.left+"px"},"auto"===s.options.width&&(n.width=s.el.outerWidth()-2+"px"),e(s.suggestionsContainer).css(n))},enableKillerFn:function(){e(document).on("click.autocomplete",this.killerFn)},disableKillerFn:function(){e(document).off("click.autocomplete",this.killerFn)},killSuggestions:function(){var e=this;e.stopKillSuggestions(),e.intervalId=window.setInterval((function(){e.hide(),e.stopKillSuggestions()}),50)},stopKillSuggestions:function(){window.clearInterval(this.intervalId)},isCursorAtEnd:function(){var e,t=this.el.val().length,n=this.element.selectionStart;return"number"==typeof n?n===t:!document.selection||((e=document.selection.createRange()).moveStart("character",-t),t===e.text.length)},onKeyPress:function(e){var t=this;if(t.disabled||t.visible||e.which!==a||!t.currentValue){if(!t.disabled&&t.visible){switch(e.which){case n:t.el.val(t.currentValue),t.hide();break;case l:if(t.hint&&t.options.onHint&&t.isCursorAtEnd()){t.selectHint();break}return;case s:if(t.hint&&t.options.onHint)return void t.selectHint();case i:if(-1===t.selectedIndex)return void t.hide();if(t.select(t.selectedIndex),e.which===s&&!1===t.options.tabDisabled)return;break;case o:t.moveUp();break;case a:t.moveDown();break;default:return}e.stopImmediatePropagation(),e.preventDefault()}}else t.suggest()},onKeyUp:function(e){var t=this;if(!t.disabled){switch(e.which){case o:case a:return}clearInterval(t.onChangeInterval),t.currentValue!==t.el.val()&&(t.findBestHint(),t.options.deferRequestBy>0?t.onChangeInterval=setInterval((function(){t.onValueChange()}),t.options.deferRequestBy):t.onValueChange())}},onValueChange:function(){var t,n=this,s=n.options,i=n.el.val(),o=n.getQuery(i);n.selection&&(n.selection=null,(s.onInvalidateSelection||e.noop).call(n.element)),clearInterval(n.onChangeInterval),n.currentValue=i,n.selectedIndex=-1,s.triggerSelectOnValidInput&&-1!==(t=n.findSuggestionIndex(o))?n.select(t):o.lengthl&&(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))}))}}));