(function(e){"use strict";e.fn.autocomplete=function(t){var n;arguments.length>1?(n=t,t=arguments[1],t.url=n):typeof t=="string"&&(n=t,t={url:n});var r=e.extend({},e.fn.autocomplete.defaults,t);return this.each(function(){var t=e(this);t.data("autocompleter",new e.Autocompleter(t,e.meta?e.extend({},r,t.data()):r))})},e.fn.autocomplete.defaults={inputClass:"acInput",loadingClass:"acLoading",resultsClass:"acResults",selectClass:"acSelect",queryParamName:"q",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:1,preventDefaultTab:0,autoFill:!1,filterResults:!0,sortResults:!0,sortFunction:null,onItemSelect:null,onNoMatch:null,onFinish:null,matchStringConverter:null,beforeUseConverter:null,autoWidth:"min-width",useDelimiter:!1,delimiterChar:",",delimiterKeyCode:188,processData:null,onError:null};var t=function(t){var n,r,i=typeof t;return i==="string"?(n=t,r={}):e.isArray(t)?(n=t[0],r=t.slice(1)):i==="object"&&(n=t.value,r=t.data),n=String(n),typeof r!="object"&&(r={}),{value:n,data:r}},n=function(e,t,n){var r=parseInt(e,10);n=n||{};if(isNaN(r)||n.min&&rt?1:e").hide().addClass(this.options.resultsClass).css({position:"absolute"}),e("body").append(this.dom.$results),t.keydown(function(e){i.lastKeyPressed_=e.keyCode;switch(i.lastKeyPressed_){case i.options.delimiterKeyCode:i.options.useDelimiter&&i.active_&&i.selectCurrent();break;case 35:case 36:case 16:case 17:case 18:case 37:case 39:break;case 38:return e.preventDefault(),i.active_?i.focusPrev():i.activate(),!1;case 40:return e.preventDefault(),i.active_?i.focusNext():i.activate(),!1;case 9:if(i.active_){i.selectCurrent();if(i.options.preventDefaultTab)return e.preventDefault(),!1}if(i.options.preventDefaultTab===2)return e.preventDefault(),!1;break;case 13:if(i.active_){i.selectCurrent();if(i.options.preventDefaultReturn)return e.preventDefault(),!1}if(i.options.preventDefaultReturn===2)return e.preventDefault(),!1;break;case 27:if(i.active_)return e.preventDefault(),i.deactivate(!0),!1;break;default:i.activate()}});var s=function(){i.deactivate(!0)};t.blur(function(){i.finishOnBlur_&&(i.finishTimeout_=setTimeout(s,200))}),t.parents("form").on("submit",s)},e.Autocompleter.prototype.position=function(){var t=this.dom.$elem.offset(),n=this.dom.$results.outerHeight(),r=e(window).outerHeight(),i=t.top+this.dom.$elem.outerHeight(),s=i+n,o={top:i,left:t.left};if(s>r){var u=t.top-n;u>=0&&(o.top=u)}this.dom.$results.css(o)},e.Autocompleter.prototype.cacheRead=function(e){var t,n,r,i,s;if(this.options.useCache){e=String(e),t=e.length,this.options.matchSubset?n=1:n=t;while(n<=t){this.options.matchInside?i=t-n:i=0,s=0;while(s<=i){r=e.substr(0,n);if(this.cacheData_[r]!==undefined)return this.cacheData_[r];s++}n++}}return!1},e.Autocompleter.prototype.cacheWrite=function(e,t){return this.options.useCache?(this.cacheLength_>=this.options.maxCacheLength&&this.cacheFlush(),e=String(e),this.cacheData_[e]!==undefined&&this.cacheLength_++,this.cacheData_[e]=t,this.cacheData_[e]):!1},e.Autocompleter.prototype.cacheFlush=function(){this.cacheData_={},this.cacheLength_=0},e.Autocompleter.prototype.callHook=function(t,n){var r=this.options[t];return r&&e.isFunction(r)?r(n,this):!1},e.Autocompleter.prototype.activate=function(){var e=this;this.keyTimeout_&&clearTimeout(this.keyTimeout_),this.keyTimeout_=setTimeout(function(){e.activateNow()},this.options.delay)},e.Autocompleter.prototype.activateNow=function(){var e=this.beforeUseConverter(this.dom.$elem.val());e!==this.lastProcessedValue_&&e!==this.lastSelectedValue_&&this.fetchData(e)},e.Autocompleter.prototype.fetchData=function(e){var t=this,n=function(e,n){t.options.processData&&(e=t.options.processData(e)),t.showResults(t.filterResults(e,n),n)};this.lastProcessedValue_=e,e.length-1:i===0}return!0},e.Autocompleter.prototype.filterResults=function(e,n){var r=[],i,s;for(i=0;i0&&this.options.maxItemsToShow"+this.showResult(t.value,t.data)+"");return r.data({value:t.value,data:t.data}).click(function(){n.selectItem(r)}).mousedown(n.disableFinishOnBlur).mouseup(n.enableFinishOnBlur),r},e.Autocompleter.prototype.getItems=function(){return e(">ul>li",this.dom.$results)},e.Autocompleter.prototype.showResults=function(t,n){var r=t.length,i=this,s=e("
    "),o,u,a,f,l=!1,c=!1;if(r){for(o=0;o=r.length&&(t=r.length-1),n=e(r[t])):n=e(t),n&&n.addClass(this.selectClass_).addClass(this.options.selectClass))},e.Autocompleter.prototype.selectCurrent=function(){var t=e("li."+this.selectClass_,this.dom.$results);t.length===1?this.selectItem(t):this.deactivate(!1)},e.Autocompleter.prototype.selectItem=function(e){var t=e.data("value"),n=e.data("data"),r=this.displayValue(t,n),i=this.beforeUseConverter(r);this.lastProcessedValue_=i,this.lastSelectedValue_=i;var s=this.getDelimiterOffsets(),o=this.options.delimiterChar,u=this.dom.$elem,a=0;this.options.useDelimiter&&(u.val().substring(s.start-1,s.start)==o&&o!=" "&&(r=" "+r),u.val().substring(s.end,s.end+1)!=o&&this.lastKeyPressed_!=this.options.delimiterKeyCode?r+=o:a=1),this.setValue(r),this.setCaret(s.start+r.length+a),this.callHook("onItemSelect",{value:t,data:n}),this.deactivate(!0),u.focus()},e.Autocompleter.prototype.displayValue=function(t,n){return e.isFunction(this.options.displayValue)?this.options.displayValue(t,n):t},e.Autocompleter.prototype.hideResults=function(){this.dom.$results.hide()},e.Autocompleter.prototype.deactivate=function(e){this.finishTimeout_&&clearTimeout(this.finishTimeout_),this.keyTimeout_&&clearTimeout(this.keyTimeout_),e&&(this.lastProcessedValue_!==this.lastSelectedValue_&&(this.options.mustMatch&&this.setValue(""),this.callHook("onNoMatch")),this.active_&&this.callHook("onFinish"),this.lastKeyPressed_=null,this.lastProcessedValue_=null,this.lastSelectedValue_=null,this.active_=!1),this.hideResults()},e.Autocompleter.prototype.selectRange=function(e,t){var n=this.dom.$elem.get(0);if(n.setSelectionRange)n.focus(),n.setSelectionRange(e,t);else if(n.createTextRange){var r=n.createTextRange();r.collapse(!0),r.moveEnd("character",t),r.moveStart("character",e),r.select()}},e.Autocompleter.prototype.setCaret=function(e){this.selectRange(e,e)},e.Autocompleter.prototype.getCaret=function(){var t=this.dom.$elem;if(e.browser.msie){var n=document.selection;if(t[0].tagName.toLowerCase()!="textarea"){var r=t.val(),i=n.createRange().duplicate();i.moveEnd("character",r.length);var s=i.text==""?r.length:r.lastIndexOf(i.text);i=n.createRange().duplicate(),i.moveStart("character",-r.length);var o=i.text.length}else{var i=n.createRange(),u=i.duplicate();u.moveToElementText(t[0]),u.setEndPoint("EndToEnd",i);var s=u.text.length-i.text.length,o=s+i.text.length}}else var s=t[0].selectionStart,o=t[0].selectionEnd;return{start:s,end:o}},e.Autocompleter.prototype.setValue=function(e){if(this.options.useDelimiter){var t=this.dom.$elem.val(),n=this.getDelimiterOffsets(),r=t.substring(0,n.start),i=t.substring(n.end);e=r+e+i}this.dom.$elem.val(e)},e.Autocompleter.prototype.getValue=function(){var e=this.dom.$elem.val();if(this.options.useDelimiter){var t=this.getDelimiterOffsets();return e.substring(t.start,t.end).trim()}return e},e.Autocompleter.prototype.getDelimiterOffsets=function(){var e=this.dom.$elem.val();if(this.options.useDelimiter){var t=e.substring(0,this.getCaret().start),n=t.lastIndexOf(this.options.delimiterChar)+1,r=e.substring(this.getCaret().start),i=r.indexOf(this.options.delimiterChar);i==-1&&(i=e.length),i+=this.getCaret().start}else n=0,i=e.length;return{start:n,end:i}}})(jQuery);