!function(e){"use strict";"function"==typeof define&&define.amd?define(["jquery"],e):e(jQuery)}(function(c){"use strict";var n={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}},s=27,i=9,o=13,l=38,a=39,u=40;function r(e,t){var n=function(){},s=this,n={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:n,onSearchComplete:n,onSearchError:n,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"};s.element=e,s.el=c(e),s.suggestions=[],s.badQueries=[],s.selectedIndex=-1,s.currentValue=s.element.value,s.intervalId=0,s.cachedResponse={},s.onChangeInterval=null,s.onChange=null,s.isLocal=!1,s.suggestionsContainer=null,s.options=c.extend({},n,t),s.classes={selected:"autocomplete-selected",suggestion:"autocomplete-suggestion"},s.hint=null,s.hintValue="",s.selection=null,s.initialize(),s.setOptions(t)}r.utils=n,(c.YithAutocomplete=r).formatResult=function(e,t){t="("+n.escapeRegExChars(t)+")";return e.value.replace(new RegExp(t,"gi"),"<strong>$1</strong>")},r.prototype={killerFn:null,initialize:function(){var e,t=this,n="."+t.classes.suggestion,s=t.classes.selected,i=t.options;t.element.setAttribute("autocomplete","off"),t.killerFn=function(e){0===c(e.target).closest("."+t.options.containerClass).length&&(t.killSuggestions(),t.disableKillerFn())},t.suggestionsContainer=r.utils.createNode(i.containerClass),(e=c(t.suggestionsContainer)).appendTo(i.appendTo),"auto"!==i.width&&e.width(i.width),e.on("mouseover.autocomplete",n,function(){t.activate(c(this).data("index"))}),e.on("mouseout.autocomplete",function(){t.selectedIndex=-1,e.children("."+s).removeClass(s)}),e.on("click.autocomplete",n,function(){t.select(c(this).data("index"))}),t.fixPosition(),t.fixPositionCapture=function(){t.visible&&t.fixPosition()},c(window).on("resize.autocomplete",t.fixPositionCapture),t.el.on("keydown.autocomplete",function(e){t.onKeyPress(e)}),t.el.on("keyup.autocomplete",function(e){t.onKeyUp(e)}),t.el.on("blur.autocomplete",function(){t.onBlur()}),t.el.on("focus.autocomplete",function(){t.onFocus()}),t.el.on("change.autocomplete",function(e){t.onKeyUp(e)})},onFocus:function(){var e=this;e.fixPosition(),e.options.minChars<=e.el.val().length&&e.onValueChange()},onBlur:function(){this.enableKillerFn()},setOptions:function(e){var t=this,n=t.options;c.extend(n,e),t.isLocal=Array.isArray(n.lookup),t.isLocal&&(n.lookup=t.verifySuggestionsFormat(n.lookup)),c(t.suggestionsContainer).css({"max-height":n.maxHeight+"px",width:n.width+"px","z-index":n.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 e,t=this;"body"===t.options.appendTo&&(e={top:(e=t.el.offset()).top+t.el.outerHeight()+"px",left:e.left+"px"},"auto"===t.options.width&&(e.width=t.el.outerWidth()-2+"px"),c(t.suggestionsContainer).css(e))},enableKillerFn:function(){c(document).on("click.autocomplete",this.killerFn)},disableKillerFn:function(){c(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=this.el.val().length,t=this.element.selectionStart;return"number"==typeof t?t===e:!document.selection||((t=document.selection.createRange()).moveStart("character",-e),e===t.text.length)},onKeyPress:function(e){var t=this;if(t.disabled||t.visible||e.which!==u||!t.currentValue){if(!t.disabled&&t.visible){switch(e.which){case s:t.el.val(t.currentValue),t.hide();break;case a:if(t.hint&&t.options.onHint&&t.isCursorAtEnd()){t.selectHint();break}return;case i:if(t.hint&&t.options.onHint)return void t.selectHint();case o:if(-1===t.selectedIndex)return void t.hide();if(t.select(t.selectedIndex),e.which===i&&!1===t.options.tabDisabled)return;break;case l:t.moveUp();break;case u: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 l:case u:return}clearInterval(t.onChangeInterval),t.currentValue!==t.el.val()&&(t.findBestHint(),0<t.options.deferRequestBy?t.onChangeInterval=setInterval(function(){t.onValueChange()},t.options.deferRequestBy):t.onValueChange())}},onValueChange:function(){var e,t=this,n=t.options,s=t.el.val(),i=t.getQuery(s);t.selection&&(t.selection=null,(n.onInvalidateSelection||c.noop).call(t.element)),clearInterval(t.onChangeInterval),t.currentValue=s,t.selectedIndex=-1,n.triggerSelectOnValidInput&&-1!==(e=t.findSuggestionIndex(i))?t.select(e):i.length<n.minChars?t.hide():t.getSuggestions(i)},findSuggestionIndex:function(e){var n=-1,s=e.toLowerCase();return c.each(this.suggestions,function(e,t){if(t.value.toLowerCase()===s)return n=e,!1}),n},getQuery:function(e){var t=this.options.delimiter;return t?(t=e.split(t),c.trim(t[t.length-1])):e},getSuggestionsLocal:function(t){var e=this.options,n=t.toLowerCase(),s=e.lookupFilter,i=parseInt(e.lookupLimit,10),e={suggestions:c.grep(e.lookup,function(e){return s(e,t,n)})};return i&&e.suggestions.length>i&&(e.suggestions=e.suggestions.slice(0,i)),e},getSuggestions:function(s){var e,t,n,i=this,o=i.options,l=o.serviceUrl;o.params[o.paramName]=s,t=o.ignoreParams?null:o.params,(e=i.isLocal?i.getSuggestionsLocal(s):("function"==typeof l&&(l=l.call(i.element,s)),n=l+"?"+c.param(t||{}),i.cachedResponse[n]))&&Array.isArray(e.suggestions)?(i.suggestions=e.suggestions,i.suggest()):i.isBadQuery(s)||!1!==o.onSearchStart.call(i.element,o.params)&&(i.currentRequest&&i.currentRequest.abort(),i.currentRequest=c.ajax({url:l,data:t,type:o.type,dataType:o.dataType}).done(function(e){i.currentRequest=null,e=o.transformResult(e),i.processResponse(e,s,n),o.onSearchComplete.call(i.element,s,e.suggestions)}).fail(function(e,t,n){o.onSearchError.call(i.element,s,e,t,n)}))},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(){this.visible=!1,this.selectedIndex=-1,c(this.suggestionsContainer).hide(),this.signalHint(null)},suggest:function(){var e,t,n,s,i,o,l,a,u,r;0!==this.suggestions.length?(t=(e=this).options,n=t.formatResult,s=e.getQuery(e.currentValue),i=e.classes.suggestion,o=e.classes.selected,l=c(e.suggestionsContainer),a=t.beforeRender,u="",t.triggerSelectOnValidInput&&-1!==(r=e.findSuggestionIndex(s))?e.select(r):(c.each(e.suggestions,function(e,t){u+='<div class="'+i+'" data-index="'+e+'">'+n(t,s)+"</div>"}),this.adjustContainerWidth(),l.html(u),t.autoSelectFirst&&(e.selectedIndex=0,l.children().first().addClass(o)),"function"==typeof a&&a.call(e.element,l),l.show(),e.visible=!0,e.findBestHint())):this.options.showNoSuggestionNotice?this.noSuggestions():this.hide()},noSuggestions:function(){var e=c(this.suggestionsContainer),t="";t+='<div class="autocomplete-no-suggestion">'+this.options.noSuggestionNotice+"</div>",this.adjustContainerWidth(),e.html(t),e.show(),this.visible=!0},adjustContainerWidth:function(){var e=this.options,t=c(this.suggestionsContainer);"auto"===e.width&&(e=this.el.outerWidth()-2,t.width(0<e?e:300))},findBestHint:function(){var s=this.el.val().toLowerCase(),i=null;s&&(c.each(this.suggestions,function(e,t){var n=0===t.value.toLowerCase().indexOf(s);return n&&(i=t),!n}),this.signalHint(i))},signalHint:function(e){var t="",n=this;e&&(t=n.currentValue+e.value.substr(n.currentValue.length)),n.hintValue!==t&&(n.hintValue=t,n.hint=e,(this.options.onHint||c.noop)(t))},verifySuggestionsFormat:function(e){return e.length&&"string"==typeof e[0]?c.map(e,function(e){return{value:e,data:null}}):e},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(e){var t=this,n=t.classes.selected,s=c(t.suggestionsContainer),i=s.children();return s.children("."+n).removeClass(n),t.selectedIndex=e,-1!==t.selectedIndex&&i.length>t.selectedIndex?(t=i.get(t.selectedIndex),c(t).addClass(n),t):null},selectHint:function(){var e=c.inArray(this.hint,this.suggestions);this.select(e)},select:function(e){this.hide(),this.onSelect(e)},moveUp:function(){var e=this;if(-1!==e.selectedIndex)return 0===e.selectedIndex?(c(e.suggestionsContainer).children().first().removeClass(e.classes.selected),e.selectedIndex=-1,e.el.val(e.currentValue),void e.findBestHint()):void e.adjustScroll(e.selectedIndex-1)},moveDown:function(){this.selectedIndex!==this.suggestions.length-1&&this.adjustScroll(this.selectedIndex+1)},adjustScroll:function(e){var t,n,s=this,i=s.activate(e);i&&(t=i.offsetTop,i=(n=c(s.suggestionsContainer).scrollTop())+s.options.maxHeight-25,t<n?c(s.suggestionsContainer).scrollTop(t):i<t&&c(s.suggestionsContainer).scrollTop(t-s.options.maxHeight+25),s.el.val(s.getValue(s.suggestions[e].value)),s.signalHint(null))},onSelect:function(e){var t=this,n=t.options.onSelect,e=t.suggestions[e];t.currentValue=t.getValue(e.value),t.currentValue!==t.el.val()&&t.el.val(t.currentValue),t.signalHint(null),t.suggestions=[],t.selection=e,"function"==typeof n&&n.call(t.element,e)},getValue:function(e){var t,n=this.options.delimiter;return!n||1===(n=(t=this.currentValue).split(n)).length?e:t.substr(0,t.length-n[n.length-1].length)+e},dispose:function(){this.el.off(".autocomplete").removeData("autocomplete"),this.disableKillerFn(),c(window).off("resize.autocomplete",this.fixPositionCapture),c(this.suggestionsContainer).remove()}},c.fn.yithautocomplete=function(n,s){var i="autocomplete";return 0===arguments.length?this.first().data(i):this.each(function(){var e=c(this),t=e.data(i);"string"==typeof n?t&&"function"==typeof t[n]&&t[n](s):(t&&t.dispose&&t.dispose(),t=new r(this,n),e.data(i,t))})}});