(function(factory){"use strict";if(typeof define==="function"&&define.amd){define(["jquery"],factory)}else if(typeof exports==="object"&&typeof require==="function"){factory(require("jquery"))}else{factory(jQuery)}})(function($){"use strict";var utils=function(){return{escapeRegExChars:function(value){return value.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g,"\\$&")},createNode:function(containerClass){var div=document.createElement("div");div.className=containerClass;div.style.position="absolute";div.style.display="none";return div}}}(),keys={ESC:27,TAB:9,RETURN:13,LEFT:37,UP:38,RIGHT:39,DOWN:40};function Autocomplete(el,options){var noop=function(){},that=this,defaults={ajaxSettings:{},autoSelectFirst:false,appendTo:document.body,serviceUrl:null,lookup:null,onSelect:null,width:"auto",minChars:1,maxHeight:300,deferRequestBy:0,params:{},formatResult:Autocomplete.formatResult,delimiter:null,zIndex:9999,type:"GET",noCache:false,onSearchStart:noop,onSearchComplete:noop,onSearchError:noop,preserveInput:false,containerClass:"autocomplete-suggestions",tabDisabled:false,dataType:"text",currentRequest:null,triggerSelectOnValidInput:true,preventBadQueries:true,lookupFilter:function(suggestion,originalQuery,queryLowerCase){return suggestion.value.toLowerCase().indexOf(queryLowerCase)!==-1},paramName:"query",transformResult:function(response){return typeof response==="string"?$.parseJSON(response):response},showNoSuggestionNotice:false,noSuggestionNotice:"No results",orientation:"bottom",forceFixPosition:false};that.element=el;that.el=$(el);that.suggestions=[];that.badQueries=[];that.selectedIndex=-1;that.currentValue=that.element.value;that.intervalId=0;that.cachedResponse={};that.onChangeInterval=null;that.onChange=null;that.isLocal=false;that.suggestionsContainer=null;that.noSuggestionsContainer=null;that.options=$.extend({},defaults,options);that.classes={selected:"autocomplete-selected",suggestion:"autocomplete-suggestion"};that.hint=null;that.hintValue="";that.selection=null;that.initialize();that.setOptions(options)}Autocomplete.utils=utils;$.Autocomplete=Autocomplete;Autocomplete.formatResult=function(suggestion,currentValue){var pattern="("+utils.escapeRegExChars(currentValue)+")";return suggestion.value.replace(new RegExp(pattern,"gi"),"$1").replace(/'/g,"'").replace(/&/g,"&").replace(//g,">").replace(/"/g,""").replace(/<(\/?strong)>/g,"<$1>")};Autocomplete.prototype={killerFn:null,initialize:function(){var that=this,suggestionSelector="."+that.classes.suggestion,selected=that.classes.selected,options=that.options,container;that.element.setAttribute("autocomplete","off");that.killerFn=function(e){if($(e.target).closest("."+that.options.containerClass).length===0){that.killSuggestions();that.disableKillerFn()}};that.noSuggestionsContainer=$('
').html(this.options.noSuggestionNotice).get(0);that.suggestionsContainer=Autocomplete.utils.createNode(options.containerClass);container=$(that.suggestionsContainer);container.appendTo(options.appendTo);if(options.width!=="auto"){container.width(options.width)}container.on("mouseover.autocomplete",suggestionSelector,function(){that.activate($(this).data("index"))});container.on("mouseout.autocomplete",function(){that.selectedIndex=-1;container.children("."+selected).removeClass(selected)});container.on("click.autocomplete",suggestionSelector,function(){that.select($(this).data("index"))});that.fixPositionCapture=function(){if(that.visible){that.fixPosition()}};$(window).on("resize.autocomplete",that.fixPositionCapture);that.el.on("keydown.autocomplete",function(e){that.onKeyPress(e)});that.el.on("keyup.autocomplete",function(e){that.onKeyUp(e)});that.el.on("blur.autocomplete",function(){that.onBlur()});that.el.on("focus.autocomplete",function(){that.onFocus()});that.el.on("change.autocomplete",function(e){that.onKeyUp(e)});that.el.on("input.autocomplete",function(e){that.onKeyUp(e)})},onFocus:function(){var that=this;that.fixPosition();if(that.options.minChars===0&&that.el.val().length===0){that.onValueChange()}},onBlur:function(){this.enableKillerFn()},abortAjax:function(){var that=this;if(that.currentRequest){that.currentRequest.abort();that.currentRequest=null}},setOptions:function(suppliedOptions){var that=this,options=that.options;$.extend(options,suppliedOptions);that.isLocal=$.isArray(options.lookup);if(that.isLocal){options.lookup=that.verifySuggestionsFormat(options.lookup)}options.orientation=that.validateOrientation(options.orientation,"bottom");$(that.suggestionsContainer).css({"max-height":options.maxHeight+"px",width:options.width+"px","z-index":options.zIndex})},clearCache:function(){this.cachedResponse={};this.badQueries=[]},clear:function(){this.clearCache();this.currentValue="";this.suggestions=[]},disable:function(){var that=this;that.disabled=true;clearInterval(that.onChangeInterval);that.abortAjax()},enable:function(){this.disabled=false},fixPosition:function(){var that=this,$container=$(that.suggestionsContainer),containerParent=$container.parent().get(0);if(containerParent!==document.body&&!that.options.forceFixPosition){return}var orientation=that.options.orientation,containerHeight=$container.outerHeight(),height=that.el.outerHeight(),offset=that.el.offset(),styles={top:offset.top,left:offset.left};if(orientation==="auto"){var viewPortHeight=$(window).height(),scrollTop=$(window).scrollTop(),topOverflow=-scrollTop+offset.top-containerHeight,bottomOverflow=scrollTop+viewPortHeight-(offset.top+height+containerHeight);orientation=Math.max(topOverflow,bottomOverflow)===topOverflow?"top":"bottom"}if(orientation==="top"){styles.top+=-containerHeight}else{styles.top+=height}if(containerParent!==document.body){var opacity=$container.css("opacity"),parentOffsetDiff;if(!that.visible){$container.css("opacity",0).show()}parentOffsetDiff=$container.offsetParent().offset();styles.top-=parentOffsetDiff.top;styles.left-=parentOffsetDiff.left;if(!that.visible){$container.css("opacity",opacity).hide()}}if(that.options.width==="auto"){styles.width=that.el.outerWidth()-2+"px"}$container.css(styles)},enableKillerFn:function(){var that=this;$(document).on("click.autocomplete",that.killerFn)},disableKillerFn:function(){var that=this;$(document).off("click.autocomplete",that.killerFn)},killSuggestions:function(){var that=this;that.stopKillSuggestions();that.intervalId=window.setInterval(function(){that.hide();that.stopKillSuggestions()},50)},stopKillSuggestions:function(){window.clearInterval(this.intervalId)},isCursorAtEnd:function(){var that=this,valLength=that.el.val().length,selectionStart=that.element.selectionStart,range;if(typeof selectionStart==="number"){return selectionStart===valLength}if(document.selection){range=document.selection.createRange();range.moveStart("character",-valLength);return valLength===range.text.length}return true},onKeyPress:function(e){var that=this;if(!that.disabled&&!that.visible&&e.which===keys.DOWN&&that.currentValue){that.suggest();return}if(that.disabled||!that.visible){return}switch(e.which){case keys.ESC:that.el.val(that.currentValue);that.hide();break;case keys.RIGHT:if(that.hint&&that.options.onHint&&that.isCursorAtEnd()){that.selectHint();break}return;case keys.TAB:if(that.hint&&that.options.onHint){that.selectHint();return}if(that.selectedIndex===-1){that.hide();return}that.select(that.selectedIndex);if(that.options.tabDisabled===false){return}break;case keys.RETURN:if(that.selectedIndex===-1){that.hide();return}that.select(that.selectedIndex);break;case keys.UP:that.moveUp();break;case keys.DOWN:that.moveDown();break;default:return}e.stopImmediatePropagation();e.preventDefault()},onKeyUp:function(e){var that=this;if(that.disabled){return}switch(e.which){case keys.UP:case keys.DOWN:return}clearInterval(that.onChangeInterval);if(that.currentValue!==that.el.val()){that.findBestHint();if(that.options.deferRequestBy>0){that.onChangeInterval=setInterval(function(){that.onValueChange()},that.options.deferRequestBy)}else{that.onValueChange()}}},onValueChange:function(){var that=this,options=that.options,value=that.el.val(),query=that.getQuery(value);if(that.selection&&that.currentValue!==query){that.selection=null;(options.onInvalidateSelection||$.noop).call(that.element)}clearInterval(that.onChangeInterval);that.currentValue=value;that.selectedIndex=-1;if(options.triggerSelectOnValidInput&&that.isExactMatch(query)){that.select(0);return}if(query.length