// Awesomplete - Lea Verou - MIT license
(function(){function d(a,b){return a?"string"===typeof a?(b||document).querySelector(a):a:null}function k(a,b){return Array.prototype.slice.call((b||document).querySelectorAll(a))}function h(a){return a.replace(/[-\\^$*+?.()|[\]{}]/g,"\\$&")}d.create=function(a,b){var c=document.createElement(a),e;for(e in b){var f=b[e];"inside"==e?d(f).appendChild(c):"around"==e?(f=d(f),f.parentNode.insertBefore(c,f),c.appendChild(f)):e in c?c[e]=f:c.setAttribute(e,f)}return c};d.bind=function(a,b){if(a)for(var c in b){var e=
b[c];c.split(/\s+/).forEach(function(b){a.addEventListener(b,e)})}};d.fire=function(a,b,c){var e=document.createEvent("HTMLEvents");e.initEvent(b,!0,!0);for(var f in c)e[f]=c[f];a.dispatchEvent(e)};var g=self.Awesomplete=function(a,b){var c=this;b=b||{};this.input=a;a.setAttribute("aria-autocomplete","list");this.minChars=+a.getAttribute("data-minchars")||b.minChars||2;this.maxItems=+a.getAttribute("data-maxitems")||b.maxItems||10;this.list=a.getAttribute("data-list")||b.list||[];this.filter=b.filter||
g.FILTER_CONTAINS;this.sort=b.sort||g.SORT_BYLENGTH;this.autoFirst=a.hasAttribute("data-autofirst")||b.autoFirst||!1;this.item=b.item||function(a,b){return d.create("li",{innerHTML:a.replace(RegExp(h(b.trim()),"gi"),"<mark>$&</mark>"),"aria-selected":"false"})};this.index=-1;this.container=d.create("div",{className:"awesomplete",around:a});this.ul=d.create("ul",{hidden:"",inside:this.container});d.bind(this.input,{input:function(){c.evaluate()},blur:function(){c.close()},keydown:function(a){var b=
a.keyCode;if(13==b&&-1<c.index)a.preventDefault(),c.select();else if(27==b)c.close();else if(38==b||40==b)a.preventDefault(),c[38==b?"previous":"next"]()}});d.bind(this.input.form,{submit:function(a){c.close()}});d.bind(this.ul,{mousedown:function(a){a=a.target;if(a!=this){for(;a&&!/li/i.test(a.nodeName);)a=a.parentNode;a&&c.select(a)}}})};g.prototype={set list(a){Array.isArray(a)?this._list=a:"string"==typeof a&&-1<a.indexOf(",")?this._list=a.split(/\s*,\s*/):(a=d(a))&&a.children&&(this._list=[].slice.apply(a.children).map(function(a){return a.innerHTML.trim()}))},
close:function(){this.ul.setAttribute("hidden","");this.index=-1;d.fire(this.input,"awesomplete-close")},open:function(){this.ul.removeAttribute("hidden");this.autoFirst&&-1==this.index&&this.goto(0);d.fire(this.input,"awesomplete-open")},next:function(){this.goto(this.index<this.ul.children.length-1?this.index+1:-1)},previous:function(){var a=this.ul.children.length;this.goto(-1<this.index?this.index-1:a-1)},goto:function(a){var b=this.ul.children;-1<this.index&&b[this.index].setAttribute("aria-selected",
"false");this.index=a;-1<a&&0<b.length&&b[a].setAttribute("aria-selected","true")},select:function(a){if(a=a||this.ul.children[this.index]){var b;d.fire(this.input,"awesomplete-select",{text:a.textContent,preventDefault:function(){b=!0}});b||(this.input.value=a.textContent,this.close(),d.fire(this.input,"awesomplete-selectcomplete"))}},evaluate:function(){var a=this,b=this.input.value;b.length>=this.minChars&&0<this._list.length?(this.index=-1,this.ul.innerHTML="",this._list.filter(function(c){return a.filter(c,
b)}).sort(this.sort).every(function(c,d){a.ul.appendChild(a.item(c,b));return d<a.maxItems-1}),this.open()):this.close()}};g.FILTER_CONTAINS=function(a,b){return RegExp(h(b.trim()),"i").test(a)};g.FILTER_STARTSWITH=function(a,b){return RegExp("^"+h(b.trim()),"i").test(a)};g.SORT_BYLENGTH=function(a,b){return a.length!=b.length?a.length-b.length:a<b?-1:1};k("input.awesomplete").forEach(function(a){new Awesomplete(a)});g.$=d;g.$$=k})();