text: !function(e,t){"function"==typeof define&&define.amd?define(["jquery","sifter","microplugin"],t):"object"==typeof exports?module.exports=t(require("jquery"),require("sifter"),require("microplugin")):e.Selectize=t(e.jQuery,e.Sifter,e.MicroPlugin)}(this,function(e,t,n){"use strict";var i=function(e,t){if("string"!=typeof t||t.length){var n="string"==typeof t?new RegExp(t,"i"):t,i=function(e){var t=0;if(3===e.nodeType){var o=e.data.search(n);if(o>=0&&e.data.length>0){var s=e.data.match(n),r=document.createElement("span");r.className="highlight";var a=e.splitText(o),l=(a.splitText(s[0].length),a.cloneNode(!0));r.appendChild(l),a.parentNode.replaceChild(r,a),t=1}}else if(1===e.nodeType&&e.childNodes&&!/(script|style)/i.test(e.tagName))for(var d=0;d<e.childNodes.length;++d)d+=i(e.childNodes[d]);return t};return e.each(function(){i(this)})}};e.fn.removeHighlight=function(){return this.find("span.highlight").each(function(){this.parentNode.firstChild.nodeName;var e=this.parentNode;e.replaceChild(this.firstChild,this),e.normalize()}).end()};var o=function(){};o.prototype={on:function(e,t){this._events=this._events||{},this._events[e]=this._events[e]||[],this._events[e].push(t)},off:function(e,t){var n=arguments.length;return 0===n?delete this._events:1===n?delete this._events[e]:(this._events=this._events||{},void(e in this._events!=0&&this._events[e].splice(this._events[e].indexOf(t),1)))},trigger:function(e){if(this._events=this._events||{},e in this._events!=0)for(var t=0;t<this._events[e].length;t++)this._events[e][t].apply(this,Array.prototype.slice.call(arguments,1))}},o.mixin=function(e){for(var t=["on","off","trigger"],n=0;n<t.length;n++)e.prototype[t[n]]=o.prototype[t[n]]};var s=/Mac/.test(navigator.userAgent),r=65,a=13,l=27,d=37,p=38,c=80,u=39,h=40,g=78,f=8,v=46,m=16,y=s?91:17,w=s?18:17,O=9,C=1,b=2,x=!/android/i.test(window.navigator.userAgent)&&!!document.createElement("input").validity,I=function(e){return void 0!==e},S=function(e){return void 0===e||null===e?null:"boolean"==typeof e?e?"1":"0":e+""},_=function(e){return(e+"").replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;")},D={};D.before=function(e,t,n){var i=e[t];e[t]=function(){return n.apply(e,arguments),i.apply(e,arguments)}},D.after=function(e,t,n){var i=e[t];e[t]=function(){var t=i.apply(e,arguments);return n.apply(e,arguments),t}};var F=function(e){var t=!1;return function(){t||(t=!0,e.apply(this,arguments))}},k=function(e,t){var n;return function(){var i=this,o=arguments;window.clearTimeout(n),n=window.setTimeout(function(){e.apply(i,o)},t)}},A=function(e,t,n){var i,o=e.trigger,s={};e.trigger=function(){var n=arguments[0];return-1===t.indexOf(n)?o.apply(e,arguments):void(s[n]=arguments)},n.apply(e,[]),e.trigger=o;for(i in s)s.hasOwnProperty(i)&&o.apply(e,s[i])},P=function(e,t,n,i){e.on(t,n,function(t){for(var n=t.target;n&&n.parentNode!==e[0];)n=n.parentNode;return t.currentTarget=n,i.apply(this,[t])})},T=function(e){var t={};if("selectionStart"in e)t.start=e.selectionStart,t.length=e.selectionEnd-t.start;else if(document.selection){e.focus();var n=document.selection.createRange(),i=document.selection.createRange().text.length;n.moveStart("character",-e.value.length),t.start=n.text.length-i,t.length=i}return t},z=function(e,t,n){var i,o,s={};if(n)for(i=0,o=n.length;i<o;i++)s[n[i]]=e.css(n[i]);else s=e.css();t.css(s)},N=function(t,n){if(!t)return 0;var i=e("<test>").css({position:"absolute",top:-99999,left:-99999,width:"auto",padding:0,whiteSpace:"pre"}).text(t).appendTo("body");z(n,i,["letterSpacing","fontSize","fontFamily","fontWeight","textTransform"]);var o=i.width();return i.remove(),o},H=function(e){var t=null,n=function(n,i){var o,s,r,a,l,d,p,c;n=n||window.event||{},i=i||{},n.metaKey||n.altKey||(i.force||!1!==e.data("grow"))&&(o=e.val(),n.type&&"keydown"===n.type.toLowerCase()&&(s=n.keyCode,r=s>=97&&s<=122||s>=65&&s<=90||s>=48&&s<=57||32===s,46===s||8===s?(c=T(e[0]),c.length?o=o.substring(0,c.start)+o.substring(c.start+c.length):8===s&&c.start?o=o.substring(0,c.start-1)+o.substring(c.start+1):46===s&&void 0!==c.start&&(o=o.substring(0,c.start)+o.substring(c.start+1))):r&&(d=n.shiftKey,p=String.fromCharCode(n.keyCode),p=d?p.toUpperCase():p.toLowerCase(),o+=p)),a=e.attr("placeholder"),!o&&a&&(o=a),(l=N(o,e)+4)!==t&&(t=l,e.width(l),e.triggerHandler("resize")))};e.on("keydown keyup update blur",n),n()},V=function(e){var t=document.createElement("div");return t.appendChild(e.cloneNode(!0)),t.innerHTML},q=function(e,t){t||(t={});var n="Selectize";console.error("Selectize: "+e),t.explanation&&(console.group&&console.group(),console.error(t.explanation),console.group&&console.groupEnd())},L=function(n,i){var o,s,r,a,l=this;a=n[0],a.selectize=l;var d=window.getComputedStyle&&window.getComputedStyle(a,null);if(r=d?d.getPropertyValue("direction"):a.currentStyle&&a.currentStyle.direction,r=r||n.parents("[dir]:first").attr("dir")||"",e.extend(l,{order:0,settings:i,$input:n,tabIndex:n.attr("tabindex")||"",tagType:"select"===a.tagName.toLowerCase()?1:2,rtl:/rtl/i.test(r),eventNS:".selectize"+ ++L.count,highlightedValue:null,isOpen:!1,isDisabled:!1,isRequired:n.is("[required]"),isInvalid:!1,isLocked:!1,isFocused:!1,isInputHidden:!1,isSetup:!1,isShiftDown:!1,isCmdDown:!1,isCtrlDown:!1,ignoreFocus:!1,ignoreBlur:!1,ignoreHover:!1,hasOptions:!1,currentResults:null,lastValue:"",caretPos:0,loading:0,loadedSearches:{},$activeOption:null,$activeItems:[],optgroups:{},options:{},userOptions:{},items:[],renderCache:{},onSearchChange:null===i.loadThrottle?l.onSearchChange:k(l.onSearchChange,i.loadThrottle)}),l.sifter=new t(this.options,{diacritics:i.diacritics}),l.settings.options){for(o=0,s=l.settings.options.length;o<s;o++)l.registerOption(l.settings.options[o]);delete l.settings.options}if(l.settings.optgroups){for(o=0,s=l.settings.optgroups.length;o<s;o++)l.registerOptionGroup(l.settings.optgroups[o]);delete l.settings.optgroups}l.settings.mode=l.settings.mode||(1===l.settings.maxItems?"single":"multi"),"boolean"!=typeof l.settings.hideSelected&&(l.settings.hideSelected="multi"===l.settings.mode),l.initializePlugins(l.settings.plugins),l.setupCallbacks(),l.setupTemplates(),l.setup()};return o.mixin(L),void 0!==n?n.mixin(L):q("Dependency MicroPlugin is missing",{explanation:'Make sure you either: (1) are using the "standalone" version of Selectize, or (2) require MicroPlugin before you load Selectize.'}),e.extend(L.prototype,{setup:function(){var t,n,i,o,r,a,l,d,p,c,u=this,h=u.settings,g=u.eventNS,f=e(window),v=e(document),m=u.$input;if(l=u.settings.mode,d=m.attr("class")||"",t=e("<div>").addClass(h.wrapperClass).addClass(d).addClass(l),n=e("<div>").addClass(h.inputClass).addClass("items").appendTo(t),i=e('<input type="text" autocomplete="off" />').appendTo(n).attr("tabindex",m.is(":disabled")?"-1":u.tabIndex),a=e(h.dropdownParent||t),o=e("<div>").addClass(h.dropdownClass).addClass(l).hide().appendTo(a),r=e("<div>").addClass(h.dropdownContentClass).appendTo(o),(c=m.attr("id"))&&(i.attr("id",c+"-selectized"),e("label[for='"+c+"']").attr("for",c+"-selectized")),u.settings.copyClassesToDropdown&&o.addClass(d),t.css({width:m[0].style.width}),u.plugins.names.length&&(p="plugin-"+u.plugins.names.join(" plugin-"),t.addClass(p),o.addClass(p)),(null===h.maxItems||h.maxItems>1)&&1===u.tagType&&m.attr("multiple","multiple"),u.settings.placeholder&&i.attr("placeholder",h.placeholder),!u.settings.splitOn&&u.settings.delimiter){var O=u.settings.delimiter.replace(/[-\/\\^$*+?.()|[\]{}]/g,"\\$&");u.settings.splitOn=new RegExp("\\s*"+O+"+\\s*")}m.attr("autocorrect")&&i.attr("autocorrect",m.attr("autocorrect")),m.attr("autocapitalize")&&i.attr("autocapitalize",m.attr("autocapitalize")),u.$wrapper=t,u.$control=n,u.$control_input=i,u.$dropdown=o,u.$dropdown_content=r,o.on("mouseenter","[data-selectable]",function(){return u.onOptionHover.apply(u,arguments)}),o.on("mousedown click","[data-selectable]",function(){return u.onOptionSelect.apply(u,arguments)}),P(n,"mousedown","*:not(input)",function(){return u.onItemSelect.apply(u,arguments)}),H(i),n.on({mousedown:function(){return u.onMouseDown.apply(u,arguments)},click:function(){return u.onClick.apply(u,arguments)}}),i.on({mousedown:function(e){e.stopPropagation()},keydown:function(){return u.onKeyDown.apply(u,arguments)},keyup:function(){return u.onKeyUp.apply(u,arguments)},keypress:function(){return u.onKeyPress.apply(u,arguments)},resize:function(){u.positionDropdown.apply(u,[])},blur:function(){return u.onBlur.apply(u,arguments)},focus:function(){return u.ignoreBlur=!1,u.onFocus.apply(u,arguments)},paste:function(){return u.onPaste.apply(u,arguments)}}),v.on("keydown"+g,function(e){u.isCmdDown=e[s?"metaKey":"ctrlKey"],u.isCtrlDown=e[s?"altKey":"ctrlKey"],u.isShiftDown=e.shiftKey}),v.on("keyup"+g,function(e){e.keyCode===w&&(u.isCtrlDown=!1),16===e.keyCode&&(u.isShiftDown=!1),e.keyCode===y&&(u.isCmdDown=!1)}),v.on("mousedown"+g,function(e){if(u.isFocused){if(e.target===u.$dropdown[0]||e.target.parentNode===u.$dropdown[0])return!1;u.$control.has(e.target).length||e.target===u.$control[0]||u.blur(e.target)}}),f.on(["scroll"+g,"resize"+g].join(" "),function(){u.isOpen&&u.positionDropdown.apply(u,arguments)}),f.on("mousemove"+g,function(){u.ignoreHover=!1}),this.revertSettings={$children:m.children().detach(),tabindex:m.attr("tabindex")},m.attr("tabindex",-1).hide().after(u.$wrapper),e.isArray(h.items)&&(u.setValue(h.items),delete h.items),x&&m.on("invalid"+g,function(e){e.preventDefault(),u.isInvalid=!0,u.refreshState()}),u.updateOriginalInput(),u.refreshItems(),u.refreshState(),u.updatePlaceholder(),u.isSetup=!0,m.is(":disabled")&&u.disable(),u.on("change",this.onChange),m.data("selectize",u),m.addClass("selectized"),u.trigger("initialize"),!0===h.preload&&u.onSearchChange("")},setupTemplates:function(){var t=this,n=t.settings.labelField,i=t.settings.optgroupLabelField,o={optgroup:function(e){return'<div class="optgroup">'+e.html+"</div>"},optgroup_header:function(e,t){return'<div class="optgroup-header">'+t(e[i])+"</div>"},option:function(e,t){return'<div class="option">'+t(e[n])+"</div>"},item:function(e,t){return'<div class="item">'+t(e[n])+"</div>"},option_create:function(e,t){return'<div class="create">Add <strong>'+t(e.input)+"</strong>&hellip;</div>"}};t.settings.render=e.extend({},o,t.settings.render)},setupCallbacks:function(){var e,t,n={initialize:"onInitialize",change:"onChange",item_add:"onItemAdd",item_remove:"onItemRemove",clear:"onClear",option_add:"onOptionAdd",option_remove:"onOptionRemove",option_clear:"onOptionClear",optgroup_add:"onOptionGroupAdd",optgroup_remove:"onOptionGroupRemove",optgroup_clear:"onOptionGroupClear",dropdown_open:"onDropdownOpen",dropdown_close:"onDropdownClose",type:"onType",load:"onLoad",focus:"onFocus",blur:"onBlur"};for(e in n)n.hasOwnProperty(e)&&(t=this.settings[n[e]])&&this.on(e,t)},onClick:function(e){var t=this;t.isFocused||(t.focus(),e.preventDefault())},onMouseDown:function(t){var n=this,i=t.isDefaultPrevented();if(e(t.target),n.isFocused){if(t.target!==n.$control_input[0])return"single"===n.settings.mode?n.isOpen?n.close():n.open():i||n.setActiveItem(null),!1}else i||window.setTimeout(function(){n.focus()},0)},onChange:function(){this.$input.trigger("change")},onPaste:function(t){var n=this;return n.isFull()||n.isInputHidden||n.isLocked?void t.preventDefault():void(n.settings.splitOn&&setTimeout(function(){var t=n.$control_input.val();if(t.match(n.settings.splitOn))for(var i=e.trim(t).split(n.settings.splitOn),o=0,s=i.length;o<s;o++)n.createItem(i[o])},0))},onKeyPress:function(e){if(this.isLocked)return e&&e.preventDefault();var t=String.fromCharCode(e.keyCode||e.which);return this.settings.create&&"multi"===this.settings.mode&&t===this.settings.delimiter?(this.createItem(),e.preventDefault(),!1):void 0},onKeyDown:function(e){var t=(e.target,this.$control_input[0],this);if(t.isLocked)return void(9!==e.keyCode&&e.preventDefault());switch(e.keyCode){case 65:if(t.isCmdDown)return void t.selectAll();break;case 27:return void(t.isOpen&&(e.preventDefault(),e.stopPropagation(),t.close()));case 78:if(!e.ctrlKey||e.altKey)break;case 40:if(!t.isOpen&&t.hasOptions)t.open();else if(t.$activeOption){t.ignoreHover=!0;var n=t.getAdjacentOption(t.$activeOption,1);n.length&&t.setActiveOption(n,!0,!0)}return void e.preventDefault();case 80:if(!e.ctrlKey||e.altKey)break;case 38:if(t.$activeOption){t.ignoreHover=!0;var i=t.getAdjacentOption(t.$activeOption,-1);i.length&&t.setActiveOption(i,!0,!0)}return void e.preventDefault();case 13:return void(t.isOpen&&t.$activeOption&&(t.onOptionSelect({currentTarget:t.$activeOption}),e.preventDefault()));case 37:return void t.advanceSelection(-1,e);case 39:return void t.advanceSelection(1,e);case 9:return t.settings.selectOnTab&&t.isOpen&&t.$activeOption&&(t.onOptionSelect({currentTarget:t.$activeOption}),t.isFull()||e.preventDefault()),void(t.settings.create&&t.createItem()&&e.preventDefault());case 8:case 46:return void t.deleteSelection(e)}return!t.isFull()&&!t.isInputHidden||(s?e.metaKey:e.ctrlKey)?void 0:void e.preventDefault()},onKeyUp:function(e){var t=this;if(t.isLocked)return e&&e.preventDefault();var n=t.$control_input.val()||"";t.lastValue!==n&&(t.lastValue=n,t.onSearchChange(n),t.refreshOptions(),t.trigger("type",n))},onSearchChange:function(e){var t=this,n=t.settings.load;n&&(t.loadedSearches.hasOwnProperty(e)||(t.loadedSearches[e]=!0,t.load(function(i){n.apply(t,[e,i])})))},onFocus:function(e){var t=this,n=t.isFocused;return t.isDisabled?(t.blur(),e&&e.preventDefault(),!1):void(t.ignoreFocus||(t.isFocused=!0,"focus"===t.settings.preload&&t.onSearchChange(""),n||t.trigger("focus"),t.$activeItems.length||(t.showInput(),t.setActiveItem(null),t.refreshOptions(!!t.settings.openOnFocus)),t.refreshState()))},onBlur:function(e,t){var n=this;if(n.isFocused&&(n.isFocused=!1,!n.ignoreFocus)){if(!n.ignoreBlur&&document.activeElement===n.$dropdown_content[0])return n.ignoreBlur=!0,void n.onFocus(e);var i=function(){n.close(),n.setTextboxValue(""),n.setActiveItem(null),n.setActiveOption(null),n.setCaret(n.items.length),n.refreshState(),t&&t.focus&&t.focus(),n.ignoreFocus=!1,n.trigger("blur")};n.ignoreFocus=!0,n.settings.create&&n.settings.createOnBlur?n.createItem(null,!1,i):i()}},onOptionHover:function(e){this.ignoreHover||this.setActiveOption(e.currentTarget,!1)},onOptionSelect:function(t){var n,i,o=this;t.preventDefault&&(t.preventDefault(),t.stopPropagation()),i=e(t.currentTarget),i.hasClass("create")?o.createItem(null,function(){o.settings.closeAfterSelect&&o.close()}):void 0!==(n=i.attr("data-value"))&&(o.lastQuery=null,o.setTextboxValue(""),o.addItem(n),o.settings.closeAfterSelect?o.close():!o.settings.hideSelected&&t.type&&/mouse/.test(t.type)&&o.setActiveOption(o.getOption(n)))},onItemSelect:function(e){var t=this;t.isLocked||"multi"===t.settings.mode&&(e.preventDefault(),t.setActiveItem(e.currentTarget,e))},load:function(e){var t=this,n=t.$wrapper.addClass(t.settings.loadingClass);t.loading++,e.apply(t,[function(e){t.loading=Math.max(t.loading-1,0),e&&e.length&&(t.addOption(e),t.refreshOptions(t.isFocused&&!t.isInputHidden)),t.loading||n.removeClass(t.settings.loadingClass),t.trigger("load",e)}])},setTextboxValue:function(e){var t=this.$control_input;t.val()!==e&&(t.val(e).triggerHandler("update"),this.lastValue=e)},getValue:function(){return 1===this.tagType&&this.$input.attr("multiple")?this.items:this.items.join(this.settings.delimiter)},setValue:function(e,t){A(this,t?[]:["change"],function(){this.clear(t),this.addItems(e,t)})},setActiveItem:function(t,n){var i,o,s,r,a,l,d,p,c=this;if("single"!==c.settings.mode){if(t=e(t),!t.length)return e(c.$activeItems).removeClass("active"),c.$activeItems=[],void(c.isFocused&&c.showInput());if("mousedown"===(i=n&&n.type.toLowerCase())&&c.isShiftDown&&c.$activeItems.length){for(p=c.$control.children(".active:last"),r=Array.prototype.indexOf.apply(c.$control[0].childNodes,[p[0]]),a=Array.prototype.indexOf.apply(c.$control[0].childNodes,[t[0]]),r>a&&(d=r,r=a,a=d),o=r;o<=a;o++)l=c.$control[0].childNodes[o],-1===c.$activeItems.indexOf(l)&&(e(l).addClass("active"),c.$activeItems.push(l));n.preventDefault()}else"mousedown"===i&&c.isCtrlDown||"keydown"===i&&this.isShiftDown?t.hasClass("active")?(s=c.$activeItems.indexOf(t[0]),c.$activeItems.splice(s,1),t.removeClass("active")):c.$activeItems.push(t.addClass("active")[0]):(e(c.$activeItems).removeClass("active"),c.$activeItems=[t.addClass("active")[0]]);c.hideInput(),this.isFocused||c.focus()}},setActiveOption:function(t,n,i){var o,s,r,a,l,d=this;d.$activeOption&&d.$activeOption.removeClass("active"),d.$activeOption=null,t=e(t),t.length&&(d.$activeOption=t.addClass("active"),!n&&I(n)||(o=d.$dropdown_content.height(),s=d.$activeOption.outerHeight(!0),n=d.$dropdown_content.scrollTop()||0,r=d.$activeOption.offset().top-d.$dropdown_content.offset().top+n,a=r,l=r-o+s,r+s>o+n?d.$dropdown_content.stop().animate({scrollTop:l},i?d.settings.scrollDuration:0):r<n&&d.$dropdown_content.stop().animate({scrollTop:a},i?d.settings.scrollDuration:0)))},selectAll:function(){var e=this;"single"!==e.settings.mode&&(e.$activeItems=Array.prototype.slice.apply(e.$control.children(":not(input)").addClass("active")),e.$activeItems.length&&(e.hideInput(),e.close()),e.focus())},hideInput:function(){var e=this;e.setTextboxValue(""),e.$control_input.css({opacity:0,position:"absolute",left:e.rtl?1e4:-1e4}),e.isInputHidden=!0},showInput:function(){this.$control_input.css({opacity:1,position:"relative",left:0}),this.isInputHidden=!1},focus:function(){var e=this;e.isDisabled||(e.ignoreFocus=!0,e.$control_input[0].focus(),window.setTimeout(function(){e.ignoreFocus=!1,e.onFocus()},0))},blur:function(e){this.$control_input[0].blur(),this.onBlur(null,e)},getScoreFunction:function(e){return this.sifter.getScoreFunction(e,this.getSearchOptions())},getSearchOptions:function(){var e=this.settings,t=e.sortField;return"string"==typeof t&&(t=[{field:t}]),{fields:e.searchField,conjunction:e.searchConjunction,sort:t}},search:function(t){var n,i,o,s=this,r=s.settings,a=this.getSearchOptions();if(r.score&&"function"!=typeof(o=s.settings.score.apply(this,[t])))throw new Error('Selectize "score" setting must be a function that returns a function');if(t!==s.lastQuery?(s.lastQuery=t,i=s.sifter.search(t,e.extend(a,{score:o})),s.currentResults=i):i=e.extend(!0,{},s.currentResults),r.hideSelected)for(n=i.items.length-1;n>=0;n--)-1!==s.items.indexOf(S(i.items[n].id))&&i.items.splice(n,1);return i},refreshOptions:function(t){var n,o,s,r,a,l,d,p,c,u,h,g,f,v,m,y;void 0===t&&(t=!0);var w=this,O=e.trim(w.$control_input.val()),C=w.search(O),b=w.$dropdown_content,x=w.$activeOption&&S(w.$activeOption.attr("data-value"));for(r=C.items.length,"number"==typeof w.settings.maxOptions&&(r=Math.min(r,w.settings.maxOptions)),a={},l=[],n=0;n<r;n++)for(d=w.options[C.items[n].id],p=w.render("option",d),c=d[w.settings.optgroupField]||"",u=e.isArray(c)?c:[c],o=0,s=u&&u.length;o<s;o++)c=u[o],w.optgroups.hasOwnProperty(c)||(c=""),a.hasOwnProperty(c)||(a[c]=document.createDocumentFragment(),l.push(c)),a[c].appendChild(p);for(this.settings.lockOptgroupOrder&&l.sort(function(e,t){return(w.optgroups[e].$order||0)-(w.optgroups[t].$order||0)}),h=document.createDocumentFragment(),n=0,r=l.length;n<r;n++)c=l[n],w.optgroups.hasOwnProperty(c)&&a[c].childNodes.length?(g=document.createDocumentFragment(),g.appendChild(w.render("optgroup_header",w.optgroups[c])),g.appendChild(a[c]),h.appendChild(w.render("optgroup",e.extend({},w.optgroups[c],{html:V(g),dom:g})))):h.appendChild(a[c]);if(b.html(h),w.settings.highlight&&C.query.length&&C.tokens.length)for(b.removeHighlight(),n=0,r=C.tokens.length;n<r;n++)i(b,C.tokens[n].regex);if(!w.settings.hideSelected)for(n=0,r=w.items.length;n<r;n++)w.getOption(w.items[n]).addClass("selected");f=w.canCreate(O),f&&(b.prepend(w.render("option_create",{input:O})),y=e(b[0].childNodes[0])),w.hasOptions=C.items.length>0||f,w.hasOptions?(C.items.length>0?(m=x&&w.getOption(x),m&&m.length?v=m:"single"===w.settings.mode&&w.items.length&&(v=w.getOption(w.items[0])),v&&v.length||(v=y&&!w.settings.addPrecedence?w.getAdjacentOption(y,1):b.find("[data-selectable]:first"))):v=y,w.setActiveOption(v),t&&!w.isOpen&&w.open()):(w.setActiveOption(null),t&&w.isOpen&&w.close())},addOption:function(t){var n,i,o,s=this;if(e.isArray(t))for(n=0,i=t.length;n<i;n++)s.addOption(t[n]);else(o=s.registerOption(t))&&(s.userOptions[o]=!0,s.lastQuery=null,s.trigger("option_add",o,t))},registerOption:function(e){var t=S(e[this.settings.valueField]);return void 0!==t&&null!==t&&!this.options.hasOwnProperty(t)&&(e.$order=e.$order||++this.order,this.options[t]=e,t)},registerOptionGroup:function(e){var t=S(e[this.settings.optgroupValueField]);return!!t&&(e.$order=e.$order||++this.order,this.optgroups[t]=e,t)},addOptionGroup:function(e,t){t[this.settings.optgroupValueField]=e,(e=this.registerOptionGroup(t))&&this.trigger("optgroup_add",e,t)},removeOptionGroup:function(e){this.optgroups.hasOwnProperty(e)&&(delete this.optgroups[e],this.renderCache={},this.trigger("optgroup_remove",e))},clearOptionGroups:function(){this.optgroups={},this.renderCache={},this.trigger("optgroup_clear")},updateOption:function(t,n){var i,o,s,r,a,l,d,p=this;if(t=S(t),s=S(n[p.settings.valueField]),null!==t&&p.options.hasOwnProperty(t)){if("string"!=typeof s)throw new Error("Value must be set in option data");d=p.options[t].$order,s!==t&&(delete p.options[t],-1!==(r=p.items.indexOf(t))&&p.items.splice(r,1,s)),n.$order=n.$order||d,p.options[s]=n,a=p.renderCache.item,l=p.renderCache.option,a&&(delete a[t],delete a[s]),l&&(delete l[t],delete l[s]),-1!==p.items.indexOf(s)&&(i=p.getItem(t),o=e(p.render("item",n)),i.hasClass("active")&&o.addClass("active"),i.replaceWith(o)),p.lastQuery=null,p.isOpen&&p.refreshOptions(!1)}},removeOption:function(e,t){var n=this;e=S(e);var i=n.renderCache.item,o=n.renderCache.option;i&&delete i[e],o&&delete o[e],delete n.userOptions[e],delete n.options[e],n.lastQuery=null,n.trigger("option_remove",e),n.removeItem(e,t)},clearOptions:function(){var e=this;e.loadedSearches={},e.userOptions={},e.renderCache={},e.options=e.sifter.items={},e.lastQuery=null,e.trigger("option_clear"),e.clear()},getOption:function(e){return this.getElementWithValue(e,this.$dropdown_content.find("[data-selectable]"))},getAdjacentOption:function(t,n){var i=this.$dropdown.find("[data-selectable]"),o=i.index(t)+n;return o>=0&&o<i.length?i.eq(o):e()},getElementWithValue:function(t,n){if(void 0!==(t=S(t))&&null!==t)for(var i=0,o=n.length;i<o;i++)if(n[i].getAttribute("data-value")===t)return e(n[i]);return e()},getItem:function(e){return this.getElementWithValue(e,this.$control.children())},addItems:function(t,n){for(var i=e.isArray(t)?t:[t],o=0,s=i.length;o<s;o++)this.isPending=o<s-1,this.addItem(i[o],n)},addItem:function(t,n){A(this,n?[]:["change"],function(){var i,o,s,r,a,l=this,d=l.settings.mode;return t=S(t),-1!==l.items.indexOf(t)?void("single"===d&&l.close()):void(l.options.hasOwnProperty(t)&&("single"===d&&l.clear(n),"multi"===d&&l.isFull()||(i=e(l.render("item",l.options[t])),a=l.isFull(),l.items.splice(l.caretPos,0,t),l.insertAtCaret(i),(!l.isPending||!a&&l.isFull())&&l.refreshState(),l.isSetup&&(s=l.$dropdown_content.find("[data-selectable]"),l.isPending||(o=l.getOption(t),r=l.getAdjacentOption(o,1).attr("data-value"),l.refreshOptions(l.isFocused&&"single"!==d),r&&l.setActiveOption(l.getOption(r))),!s.length||l.isFull()?l.close():l.positionDropdown(),l.updatePlaceholder(),l.trigger("item_add",t,i),l.updateOriginalInput({silent:n})))))})},removeItem:function(t,n){var i,o,s,r=this;i=t instanceof e?t:r.getItem(t),t=S(i.attr("data-value")),-1!==(o=r.items.indexOf(t))&&(i.remove(),i.hasClass("active")&&(s=r.$activeItems.indexOf(i[0]),r.$activeItems.splice(s,1)),r.items.splice(o,1),r.lastQuery=null,!r.settings.persist&&r.userOptions.hasOwnProperty(t)&&r.removeOption(t,n),o<r.caretPos&&r.setCaret(r.caretPos-1),r.refreshState(),r.updatePlaceholder(),r.updateOriginalInput({silent:n}),r.positionDropdown(),r.trigger("item_remove",t,i))},createItem:function(t,n){var i=this,o=i.caretPos;t=t||e.trim(i.$control_input.val()||"");var s=arguments[arguments.length-1];if("function"!=typeof s&&(s=function(){}),"boolean"!=typeof n&&(n=!0),!i.canCreate(t))return s(),!1;i.lock();var r="function"==typeof i.settings.create?this.settings.create:function(e){var t={};return t[i.settings.labelField]=e,t[i.settings.valueField]=e,t},a=F(function(e){if(i.unlock(),!e||"object"!=typeof e)return s();var t=S(e[i.settings.valueField]);return"string"!=typeof t?s():(i.setTextboxValue(""),i.addOption(e),i.setCaret(o),i.addItem(t),i.refreshOptions(n&&"single"!==i.settings.mode),void s(e))}),l=r.apply(this,[t,a]);return void 0!==l&&a(l),!0},refreshItems:function(){this.lastQuery=null,this.isSetup&&this.addItem(this.items),this.refreshState(),this.updateOriginalInput()},refreshState:function(){this.refreshValidityState(),this.refreshClasses()},refreshValidityState:function(){if(!this.isRequired)return!1;var e=!this.items.length;this.isInvalid=e,this.$control_input.prop("required",e),this.$input.prop("required",!e)},refreshClasses:function(){var t=this,n=t.isFull(),i=t.isLocked;t.$wrapper.toggleClass("rtl",t.rtl),t.$control.toggleClass("focus",t.isFocused).toggleClass("disabled",t.isDisabled).toggleClass("required",t.isRequired).toggleClass("invalid",t.isInvalid).toggleClass("locked",i).toggleClass("full",n).toggleClass("not-full",!n).toggleClass("input-active",t.isFocused&&!t.isInputHidden).toggleClass("dropdown-active",t.isOpen).toggleClass("has-options",!e.isEmptyObject(t.options)).toggleClass("has-items",t.items.length>0),t.$control_input.data("grow",!n&&!i)},isFull:function(){return null!==this.settings.maxItems&&this.items.length>=this.settings.maxItems},updateOriginalInput:function(e){var t,n,i,o,s=this;if(e=e||{},1===s.tagType){for(i=[],t=0,n=s.items.length;t<n;t++)o=s.options[s.items[t]][s.settings.labelField]||"",i.push('<option value="'+_(s.items[t])+'" selected="selected">'+_(o)+"</option>");i.length||this.$input.attr("multiple")||i.push('<option value="" selected="selected"></option>'),s.$input.html(i.join(""))}else s.$input.val(s.getValue()),s.$input.attr("value",s.$input.val());s.isSetup&&(e.silent||s.trigger("change",s.$input.val()))},updatePlaceholder:function(){if(this.settings.placeholder){var e=this.$control_input;this.items.length?e.removeAttr("placeholder"):e.attr("placeholder",this.settings.placeholder),e.triggerHandler("update",{force:!0})}},open:function(){var e=this;e.isLocked||e.isOpen||"multi"===e.settings.mode&&e.isFull()||(e.focus(),e.isOpen=!0,e.refreshState(),e.$dropdown.css({visibility:"hidden",display:"block"}),e.positionDropdown(),e.$dropdown.css({visibility:"visible"}),e.trigger("dropdown_open",e.$dropdown))},close:function(){var e=this,t=e.isOpen;"single"===e.settings.mode&&e.items.length&&(e.hideInput(),e.$control_input.blur()),e.isOpen=!1,e.$dropdown.hide(),e.setActiveOption(null),e.refreshState(),t&&e.trigger("dropdown_close",e.$dropdown)},positionDropdown:function(){var e=this.$control,t="body"===this.settings.dropdownParent?e.offset():e.position();t.top+=e.outerHeight(!0),this.$dropdown.css({width:e.outerWidth(),top:t.top,left:t.left})},clear:function(e){var t=this;t.items.length&&(t.$control.children(":not(input)").remove(),t.items=[],t.lastQuery=null,t.setCaret(0),t.setActiveItem(null),t.updatePlaceholder(),t.updateOriginalInput({silent:e}),t.refreshState(),t.showInput(),t.trigger("clear"))},insertAtCaret:function(t){var n=Math.min(this.caretPos,this.items.length);0===n?this.$control.prepend(t):e(this.$control[0].childNodes[n]).before(t),this.setCaret(n+1)},deleteSelection:function(t){var n,i,o,s,r,a,l,d,p,c=this;if(o=t&&8===t.keyCode?-1:1,s=T(c.$control_input[0]),c.$activeOption&&!c.settings.hideSelected&&(l=c.getAdjacentOption(c.$activeOption,-1).attr("data-value")),r=[],c.$activeItems.length){for(p=c.$control.children(".active:"+(o>0?"last":"first")),a=c.$control.children(":not(input)").index(p),o>0&&a++,n=0,i=c.$activeItems.length;n<i;n++)r.push(e(c.$activeItems[n]).attr("data-value"));t&&(t.preventDefault(),t.stopPropagation())}else(c.isFocused||"single"===c.settings.mode)&&c.items.length&&(o<0&&0===s.start&&0===s.length?r.push(c.items[c.caretPos-1]):o>0&&s.start===c.$control_input.val().length&&r.push(c.items[c.caretPos]));if(!r.length||"function"==typeof c.settings.onDelete&&!1===c.settings.onDelete.apply(c,[r]))return!1;for(void 0!==a&&c.setCaret(a);r.length;)c.removeItem(r.pop());return c.showInput(),c.positionDropdown(),c.refreshOptions(!0),l&&(d=c.getOption(l),d.length&&c.setActiveOption(d)),!0},advanceSelection:function(e,t){var n,i,o,s,r,a,l=this;0!==e&&(l.rtl&&(e*=-1),n=e>0?"last":"first",i=T(l.$control_input[0]),l.isFocused&&!l.isInputHidden?(s=l.$control_input.val().length,(r=e<0?0===i.start&&0===i.length:i.start===s)&&!s&&l.advanceCaret(e,t)):(a=l.$control.children(".active:"+n),a.length&&(o=l.$control.children(":not(input)").index(a),l.setActiveItem(null),l.setCaret(e>0?o+1:o))))},advanceCaret:function(e,t){var n,i,o=this;0!==e&&(n=e>0?"next":"prev",o.isShiftDown?(i=o.$control_input[n](),i.length&&(o.hideInput(),o.setActiveItem(i),t&&t.preventDefault())):o.setCaret(o.caretPos+e))},setCaret:function(t){var n=this;if(t="single"===n.settings.mode?n.items.length:Math.max(0,Math.min(n.items.length,t)),!n.isPending){var i,o,s,r;for(s=n.$control.children(":not(input)"),i=0,o=s.length;i<o;i++)r=e(s[i]).detach(),i<t?n.$control_input.before(r):n.$control.append(r)}n.caretPos=t},lock:function(){this.close(),this.isLocked=!0,this.refreshState()},unlock:function(){this.isLocked=!1,this.refreshState()},disable:function(){var e=this;e.$input.prop("disabled",!0),e.$control_input.prop("disabled",!0).prop("tabindex",-1),e.isDisabled=!0,e.lock()},enable:function(){var e=this;e.$input.prop("disabled",!1),e.$control_input.prop("disabled",!1).prop("tabindex",e.tabIndex),e.isDisabled=!1,e.unlock()},destroy:function(){var t=this,n=t.eventNS,i=t.revertSettings;t.trigger("destroy"),t.off(),t.$wrapper.remove(),t.$dropdown.remove(),t.$input.html("").append(i.$children).removeAttr("tabindex").removeClass("selectized").attr({tabindex:i.tabindex}).show(),t.$control_input.removeData("grow"),t.$input.removeData("selectize"),e(window).off(n),e(document).off(n),e(document.body).off(n),delete t.$input[0].selectize},render:function(t,n){var i,o,s="",r=!1,a=this;return"option"!==t&&"item"!==t||(i=S(n[a.settings.valueField]),r=!!i),r&&(I(a.renderCache[t])||(a.renderCache[t]={}),a.renderCache[t].hasOwnProperty(i))?a.renderCache[t][i]:(s=e(a.settings.render[t].apply(this,[n,_])),"option"===t||"option_create"===t?s.attr("data-selectable",""):"optgroup"===t&&(o=n[a.settings.optgroupValueField]||"",s.attr("data-group",o)),"option"!==t&&"item"!==t||s.attr("data-value",i||""),r&&(a.renderCache[t][i]=s[0]),s[0])},clearCache:function(e){var t=this;void 0===e?t.renderCache={}:delete t.renderCache[e]},canCreate:function(e){var t=this;if(!t.settings.create)return!1;var n=t.settings.createFilter;return e.length&&("function"!=typeof n||n.apply(t,[e]))&&("string"!=typeof n||new RegExp(n).test(e))&&(!(n instanceof RegExp)||n.test(e))}}),L.count=0,L.defaults={options:[],optgroups:[],plugins:[],delimiter:",",splitOn:null,persist:!0,diacritics:!0,create:!1,createOnBlur:!1,createFilter:null,highlight:!0,openOnFocus:!0,maxOptions:1e3,maxItems:null,hideSelected:null,addPrecedence:!1,selectOnTab:!1,preload:!1,allowEmptyOption:!1,closeAfterSelect:!1,scrollDuration:60,loadThrottle:300,loadingClass:"loading",dataAttr:"data-data",optgroupField:"optgroup",valueField:"value",labelField:"text",optgroupLabelField:"label",optgroupValueField:"value",lockOptgroupOrder:!1,sortField:"$order",searchField:["text"],searchConjunction:"and",mode:null,wrapperClass:"selectize-control",inputClass:"selectize-input",dropdownClass:"selectize-dropdown",dropdownContentClass:"selectize-dropdown-content",dropdownParent:null,copyClassesToDropdown:!0,render:{}},e.fn.selectize=function(t){
  var n=e.fn.selectize.defaults,i=e.extend({},n,t),o=i.dataAttr,s=i.labelField,r=i.valueField,a=i.optgroupField,l=i.optgroupLabelField,d=i.optgroupValueField,p=function(t,n){var a,l,d,p,c=t.attr(o);if(c)for(n.options=JSON.parse(c),a=0,l=n.options.length;a<l;a++)n.items.push(n.options[a][r]);else{var u=e.trim(t.val()||"");if(!i.allowEmptyOption&&!u.length)return;for(d=u.split(i.delimiter),a=0,l=d.length;a<l;a++)p={},p[s]=d[a],p[r]=d[a],n.options.push(p);n.items=d}},c=function(t,n){var p,c,u,h,g=n.options,f={},v=function(e){var t=o&&e.attr(o);return"string"==typeof t&&t.length?JSON.parse(t):null},m=function(t,o){t=e(t);var l=S(t.val());if(l||i.allowEmptyOption)if(f.hasOwnProperty(l)){if(o){var d=f[l][a];d?e.isArray(d)?d.push(o):f[l][a]=[d,o]:f[l][a]=o}}else{var p=v(t)||{};p[s]=p[s]||t.text(),p[r]=p[r]||l,p[a]=p[a]||o,f[l]=p,g.push(p),t.is(":selected")&&n.items.push(l)}},y=function(t){var i,o,s,r,a;for(t=e(t),s=t.attr("label"),s&&(r=v(t)||{},r[l]=s,r[d]=s,n.optgroups.push(r)),a=e("option",t),i=0,o=a.length;i<o;i++)m(a[i],s)};for(n.maxItems=t.attr("multiple")?null:1,h=t.children(),p=0,c=h.length;p<c;p++)u=h[p].tagName.toLowerCase(),"optgroup"===u?y(h[p]):"option"===u&&m(h[p])};return this.each(function(){if(!this.selectize){var o,s=e(this),r=this.tagName.toLowerCase(),a=s.attr("placeholder")||s.attr("data-placeholder");a||i.allowEmptyOption||(a=s.children('option[value=""]').text());var l={placeholder:a,options:[],optgroups:[],items:[]};"select"===r?c(s,l):p(s,l),o=new L(s,e.extend(!0,{},n,l,t))}})},e.fn.selectize.defaults=L.defaults,e.fn.selectize.support={validity:x},L.define("drag_drop",function(t){if(!e.fn.sortable)throw new Error('The "drag_drop" plugin requires jQuery UI "sortable".');if("multi"===this.settings.mode){var n=this;n.lock=function(){var e=n.lock;return function(){var t=n.$control.data("sortable");return t&&t.disable(),e.apply(n,arguments)}}(),n.unlock=function(){var e=n.unlock;return function(){var t=n.$control.data("sortable");return t&&t.enable(),e.apply(n,arguments)}}(),n.setup=function(){var t=n.setup;return function(){t.apply(this,arguments);var i=n.$control.sortable({items:"[data-value]",forcePlaceholderSize:!0,disabled:n.isLocked,start:function(e,t){t.placeholder.css("width",t.helper.css("width")),i.css({overflow:"visible"})},stop:function(){i.css({overflow:"hidden"});var t=n.$activeItems?n.$activeItems.slice():null,o=[];i.children("[data-value]").each(function(){o.push(e(this).attr("data-value"))}),n.setValue(o),n.setActiveItem(t)}})}}()}}),L.define("dropdown_header",function(t){var n=this;t=e.extend({title:"Untitled",headerClass:"selectize-dropdown-header",titleRowClass:"selectize-dropdown-header-title",labelClass:"selectize-dropdown-header-label",closeClass:"selectize-dropdown-header-close",html:function(e){return'<div class="'+e.headerClass+'"><div class="'+e.titleRowClass+'"><span class="'+e.labelClass+'">'+e.title+'</span><a href="javascript:void(0)" class="'+e.closeClass+'">&times;</a></div></div>'}},t),n.setup=function(){var i=n.setup;return function(){i.apply(n,arguments),n.$dropdown_header=e(t.html(t)),n.$dropdown.prepend(n.$dropdown_header)}}()}),L.define("optgroup_columns",function(t){var n=this;t=e.extend({equalizeWidth:!0,equalizeHeight:!0},t),this.getAdjacentOption=function(t,n){var i=t.closest("[data-group]").find("[data-selectable]"),o=i.index(t)+n;return o>=0&&o<i.length?i.eq(o):e()},this.onKeyDown=function(){var e=n.onKeyDown;return function(t){var i,o,s,r;return!this.isOpen||37!==t.keyCode&&39!==t.keyCode?e.apply(this,arguments):(n.ignoreHover=!0,r=this.$activeOption.closest("[data-group]"),i=r.find("[data-selectable]").index(this.$activeOption),r=37===t.keyCode?r.prev("[data-group]"):r.next("[data-group]"),s=r.find("[data-selectable]"),o=s.eq(Math.min(s.length-1,i)),void(o.length&&this.setActiveOption(o)))}}();var i=function(){var e,t=i.width,n=document;return void 0===t&&(e=n.createElement("div"),e.innerHTML='<div style="width:50px;height:50px;position:absolute;left:-50px;top:-50px;overflow:auto;"><div style="width:1px;height:100px;"></div></div>',e=e.firstChild,n.body.appendChild(e),t=i.width=e.offsetWidth-e.clientWidth,n.body.removeChild(e)),t},o=function(){var o,s,r,a,l,d,p;if(p=e("[data-group]",n.$dropdown_content),(s=p.length)&&n.$dropdown_content.width()){if(t.equalizeHeight){for(r=0,o=0;o<s;o++)r=Math.max(r,p.eq(o).height());p.css({height:r})}t.equalizeWidth&&(d=n.$dropdown_content.innerWidth()-i(),a=Math.round(d/s),p.css({width:a}),s>1&&(l=d-a*(s-1),p.eq(s-1).css({width:l})))}};(t.equalizeHeight||t.equalizeWidth)&&(D.after(this,"positionDropdown",o),D.after(this,"refreshOptions",o))}),L.define("remove_button",function(t){t=e.extend({label:"&times;",title:"Remove",className:"remove",append:!0},t);var n=function(t,n){n.className="remove-single";var i=t,o='<a href="javascript:void(0)" class="'+n.className+'" tabindex="-1" title="'+_(n.title)+'">'+n.label+"</a>",s=function(e,t){return e+t};t.setup=function(){var r=i.setup;return function(){if(n.append){var a=e(i.$input.context).attr("id"),l=(e("#"+a),i.settings.render.item);i.settings.render.item=function(e){return s(l.apply(t,arguments),o)}}r.apply(t,arguments),t.$control.on("click","."+n.className,function(e){e.preventDefault(),i.isLocked||i.clear()})}}()},i=function(t,n){var i=t,o='<a href="javascript:void(0)" class="'+n.className+'" tabindex="-1" title="'+_(n.title)+'">'+n.label+"</a>",s=function(e,t){var n=e.search(/(<\/[^>]+>\s*)$/);return e.substring(0,n)+t+e.substring(n)};t.setup=function(){var r=i.setup;return function(){if(n.append){var a=i.settings.render.item;i.settings.render.item=function(e){return s(a.apply(t,arguments),o)}}r.apply(t,arguments),t.$control.on("click","."+n.className,function(t){if(t.preventDefault(),!i.isLocked){var n=e(t.currentTarget).parent();i.setActiveItem(n),i.deleteSelection()&&i.setCaret(i.items.length)}})}}()};return"single"===this.settings.mode?void n(this,t):void i(this,t)}),L.define("restore_on_backspace",function(e){var t=this;e.text=e.text||function(e){return e[this.settings.labelField]},this.onKeyDown=function(){var n=t.onKeyDown;return function(t){var i,o;return 8===t.keyCode&&""===this.$control_input.val()&&!this.$activeItems.length&&(i=this.caretPos-1)>=0&&i<this.items.length?(o=this.options[this.items[i]],this.deleteSelection(t)&&(this.setTextboxValue(e.text.apply(this,[o])),this.refreshOptions(!0)),void t.preventDefault()):n.apply(this,arguments)}}()}),L});