!function(e){"use strict";var t=function(t,i){this.options=i,this.$element=e(t),this.$container=e("<div/>",{"class":"ms-container"}),this.$selectableContainer=e("<div/>",{"class":"ms-selectable"}),this.$selectionContainer=e("<div/>",{"class":"ms-selection"}),this.$selectableUl=e("<ul/>",{"class":"ms-list",tabindex:"-1"}),this.$selectionUl=e("<ul/>",{"class":"ms-list",tabindex:"-1"}),this.scrollTo=0,this.elemsSelector="li:visible:not(.ms-optgroup-label,.ms-optgroup-container,."+i.disabledClass+")"};t.prototype={constructor:t,init:function(){var t=this,i=this.$element;if(0===i.next(".ms-container").length){i.css({position:"absolute",left:"-9999px"}),i.attr("id",i.attr("id")?i.attr("id"):Math.ceil(1e3*Math.random())+"multiselect"),this.$container.attr("id","ms-"+i.attr("id")),this.$container.addClass(t.options.cssClass),i.find("option").each(function(){t.generateLisFromOption(this)}),this.$selectionUl.find(".ms-optgroup-label").hide(),t.options.selectableHeader&&t.$selectableContainer.append(t.options.selectableHeader),t.$selectableContainer.append(t.$selectableUl),t.options.selectableFooter&&t.$selectableContainer.append(t.options.selectableFooter),t.options.selectionHeader&&t.$selectionContainer.append(t.options.selectionHeader),t.$selectionContainer.append(t.$selectionUl),t.options.selectionFooter&&t.$selectionContainer.append(t.options.selectionFooter),t.$container.append(t.$selectableContainer),t.$container.append(t.$selectionContainer),i.after(t.$container),t.activeMouse(t.$selectableUl),t.activeKeyboard(t.$selectableUl);var n=t.options.dblClick?"dblclick":"click";t.$selectableUl.on(n,".ms-elem-selectable",function(){t.select(e(this).data("ms-value"))}),t.$selectionUl.on(n,".ms-elem-selection",function(){t.deselect(e(this).data("ms-value"))}),t.activeMouse(t.$selectionUl),t.activeKeyboard(t.$selectionUl),i.on("focus",function(){t.$selectableUl.focus()})}var s=i.find("option:selected").map(function(){return e(this).val()}).get();t.select(s,"init"),"function"==typeof t.options.afterInit&&t.options.afterInit.call(this,this.$container)},generateLisFromOption:function(t,i){for(var n=this,s=n.$element,a="",r=e(t),o=0;o<t.attributes.length;o++){var l=t.attributes[o];"value"!==l.name&&"disabled"!==l.name&&(a+=l.name+'="'+l.value+'" ')}var c=e("<li "+a+"><span>"+n.escapeHTML(r.text())+"</span></li>"),u=c.clone(),h=r.val(),d=n.sanitize(h);c.data("ms-value",h).addClass("ms-elem-selectable").attr("id",d+"-selectable"),u.data("ms-value",h).addClass("ms-elem-selection").attr("id",d+"-selection").hide(),(r.prop("disabled")||s.prop("disabled"))&&(u.addClass(n.options.disabledClass),c.addClass(n.options.disabledClass));var p=r.parent("optgroup");if(p.length>0){var f=p.attr("label"),m=n.sanitize(f),g=n.$selectableUl.find("#optgroup-selectable-"+m),y=n.$selectionUl.find("#optgroup-selection-"+m);if(0===g.length){var v='<li class="ms-optgroup-container"></li>',b='<ul class="ms-optgroup"><li class="ms-optgroup-label"><span>'+f+"</span></li></ul>";g=e(v),y=e(v),g.attr("id","optgroup-selectable-"+m),y.attr("id","optgroup-selection-"+m),g.append(e(b)),y.append(e(b)),n.options.selectableOptgroup&&(g.find(".ms-optgroup-label").on("click",function(){var t=p.children(":not(:selected)").map(function(){return e(this).val()}).get();n.select(t)}),y.find(".ms-optgroup-label").on("click",function(){var t=p.children(":selected").map(function(){return e(this).val()}).get();n.deselect(t)})),n.$selectableUl.append(g),n.$selectionUl.append(y)}i=void 0==i?g.children().length:i+1,c.insertAt(i,g.children()),u.insertAt(i,y.children())}else i=void 0==i?n.$selectableUl.children().length:i,c.insertAt(i,n.$selectableUl),u.insertAt(i,n.$selectionUl)},addOption:function(t){var i=this;t.value&&(t=[t]),e.each(t,function(t,n){if(n.value&&0===i.$element.find("option[value='"+n.value+"']").length){var s=e('<option value="'+n.value+'">'+n.text+"</option>"),t=parseInt("undefined"==typeof n.index?i.$element.children().length:n.index),a=void 0==n.nested?i.$element:e("optgroup[label='"+n.nested+"']");s.insertAt(t,a),i.generateLisFromOption(s.get(0),t,n.nested)}})},escapeHTML:function(t){return e("<div>").text(t).html()},activeKeyboard:function(t){var i=this;t.on("focus",function(){e(this).addClass("ms-focus")}).on("blur",function(){e(this).removeClass("ms-focus")}).on("keydown",function(n){switch(n.which){case 40:case 38:return n.preventDefault(),n.stopPropagation(),i.moveHighlight(e(this),38===n.which?-1:1),void 0;case 37:case 39:return n.preventDefault(),n.stopPropagation(),i.switchList(t),void 0;case 9:if(i.$element.is("[tabindex]")){n.preventDefault();var s=parseInt(i.$element.attr("tabindex"),10);return s=n.shiftKey?s-1:s+1,e('[tabindex="'+s+'"]').focus(),void 0}n.shiftKey&&i.$element.trigger("focus")}return e.inArray(n.which,i.options.keySelect)>-1?(n.preventDefault(),n.stopPropagation(),i.selectHighlighted(t),void 0):void 0})},moveHighlight:function(e,t){var i=e.find(this.elemsSelector),n=i.filter(".ms-hover"),s=null,a=i.first().outerHeight(),r=e.height();if("#"+this.$container.prop("id"),i.off("mouseenter"),i.removeClass("ms-hover"),1===t){if(s=n.nextAll(this.elemsSelector).first(),0===s.length){var o=n.parent();if(o.hasClass("ms-optgroup")){var l=o.parent(),c=l.next(":visible");s=c.length>0?c.find(this.elemsSelector).first():i.first()}else s=i.first()}}else if(-1===t&&(s=n.prevAll(this.elemsSelector).first(),0===s.length)){var o=n.parent();if(o.hasClass("ms-optgroup")){var l=o.parent(),u=l.prev(":visible");s=u.length>0?u.find(this.elemsSelector).last():i.last()}else s=i.last()}if(s.length>0){s.addClass("ms-hover");var h=e.scrollTop()+s.position().top-r/2+a/2;e.scrollTop(h)}},selectHighlighted:function(e){var t=e.find(this.elemsSelector),i=t.filter(".ms-hover").first();i.length>0&&(e.parent().hasClass("ms-selectable")?this.select(i.data("ms-value")):this.deselect(i.data("ms-value")),t.removeClass("ms-hover"))},switchList:function(e){e.blur(),e.parent().hasClass("ms-selectable")?this.$selectionUl.focus():this.$selectableUl.focus()},activeMouse:function(t){var i=this,n=!1;t.on("mousemove",function(){if(n!==this){n=this;var s=t.find(i.elemsSelector);s.on("mouseenter",function(){s.removeClass("ms-hover"),e(this).addClass("ms-hover")})}})},refresh:function(){this.destroy(),this.$element.multiSelect(this.options)},destroy:function(){e("#ms-"+this.$element.attr("id")).remove(),this.$element.css("position","").css("left",""),this.$element.removeData("multiselect")},select:function(t,i){"string"==typeof t&&(t=[t]);var n=this,s=this.$element,a=e.map(t,function(e){return n.sanitize(e)}),r=this.$selectableUl.find("#"+a.join("-selectable, #")+"-selectable").filter(":not(."+n.options.disabledClass+")"),o=this.$selectionUl.find("#"+a.join("-selection, #")+"-selection").filter(":not(."+n.options.disabledClass+")"),l=s.find("option:not(:disabled)").filter(function(){return e.inArray(this.value,t)>-1});if("init"===i&&(r=this.$selectableUl.find("#"+a.join("-selectable, #")+"-selectable"),o=this.$selectionUl.find("#"+a.join("-selection, #")+"-selection")),r.length>0){r.addClass("ms-selected").hide(),o.addClass("ms-selected").show(),l.prop("selected",!0);var c=n.$selectableUl.children(".ms-optgroup-container");if(c.length>0){c.each(function(){var t=e(this).find(".ms-elem-selectable");t.length===t.filter(".ms-selected").length&&e(this).find(".ms-optgroup-label").hide()});var u=n.$selectionUl.children(".ms-optgroup-container");u.each(function(){var t=e(this).find(".ms-elem-selection");t.filter(".ms-selected").length>0&&e(this).find(".ms-optgroup-label").show()})}else if(n.options.keepOrder&&"init"!==i){var h=n.$selectionUl.find(".ms-selected");h.length>1&&h.last().get(0)!=o.get(0)&&o.insertAfter(h.last())}"init"!==i&&(s.trigger("change"),"function"==typeof n.options.afterSelect&&n.options.afterSelect.call(this,t))}},deselect:function(t){"string"==typeof t&&(t=[t]);var i=this,n=this.$element,s=e.map(t,function(e){return i.sanitize(e)}),a=this.$selectableUl.find("#"+s.join("-selectable, #")+"-selectable"),r=this.$selectionUl.find("#"+s.join("-selection, #")+"-selection").filter(".ms-selected").filter(":not(."+i.options.disabledClass+")"),o=n.find("option").filter(function(){return e.inArray(this.value,t)>-1});if(r.length>0){a.removeClass("ms-selected").show(),r.removeClass("ms-selected").hide(),o.prop("selected",!1);var l=i.$selectableUl.children(".ms-optgroup-container");if(l.length>0){l.each(function(){var t=e(this).find(".ms-elem-selectable");t.filter(":not(.ms-selected)").length>0&&e(this).find(".ms-optgroup-label").show()});var c=i.$selectionUl.children(".ms-optgroup-container");c.each(function(){var t=e(this).find(".ms-elem-selection");0===t.filter(".ms-selected").length&&e(this).find(".ms-optgroup-label").hide()})}n.trigger("change"),"function"==typeof i.options.afterDeselect&&i.options.afterDeselect.call(this,t)}},select_all:function(){var t=this.$element,i=t.val();if(t.find('option:not(":disabled")').prop("selected",!0),this.$selectableUl.find(".ms-elem-selectable").filter(":not(."+this.options.disabledClass+")").addClass("ms-selected").hide(),this.$selectionUl.find(".ms-optgroup-label").show(),this.$selectableUl.find(".ms-optgroup-label").hide(),this.$selectionUl.find(".ms-elem-selection").filter(":not(."+this.options.disabledClass+")").addClass("ms-selected").show(),this.$selectionUl.focus(),t.trigger("change"),"function"==typeof this.options.afterSelect){var n=e.grep(t.val(),function(t){return e.inArray(t,i)<0});this.options.afterSelect.call(this,n)}},deselect_all:function(){var e=this.$element,t=e.val();e.find("option").prop("selected",!1),this.$selectableUl.find(".ms-elem-selectable").removeClass("ms-selected").show(),this.$selectionUl.find(".ms-optgroup-label").hide(),this.$selectableUl.find(".ms-optgroup-label").show(),this.$selectionUl.find(".ms-elem-selection").removeClass("ms-selected").hide(),this.$selectableUl.focus(),e.trigger("change"),"function"==typeof this.options.afterDeselect&&this.options.afterDeselect.call(this,t)},sanitize:function(e){var t,i,n=0;if(0==e.length)return n;var s=0;for(t=0,s=e.length;s>t;t++)i=e.charCodeAt(t),n=(n<<5)-n+i,n|=0;return n}},e.fn.multiSelect=function(){var i=arguments[0],n=arguments;return this.each(function(){var s=e(this),a=s.data("multiselect"),r=e.extend({},e.fn.multiSelect.defaults,s.data(),"object"==typeof i&&i);a||s.data("multiselect",a=new t(this,r)),"string"==typeof i?a[i](n[1]):a.init()})},e.fn.multiSelect.defaults={keySelect:[32],selectableOptgroup:!1,disabledClass:"disabled",dblClick:!1,keepOrder:!1,cssClass:""},e.fn.multiSelect.Constructor=t,e.fn.insertAt=function(e,t){return this.each(function(){0===e?t.prepend(this):t.children().eq(e-1).after(this)})}}(window.jQuery);