(function(b){function a(e,c){var f=this,d=e.width();this.$el=e.hide();this.options=c;this.$parent=b('
');this.$choice=b('");this.$drop=b('');this.$el.after(this.$parent);this.$parent.append(this.$choice);this.$parent.append(this.$drop);if(this.$el.prop("disabled")){this.$choice.addClass("disabled")}this.$choice.css("width",d+"px").find("span").css("width",(d-28)+"px");this.$drop.css({width:(c.width||d)+"px"});b("body").click(function(g){if(b(g.target)[0]===f.$choice[0]||b(g.target).parents(".ms-choice")[0]===f.$choice[0]){return}if((b(g.target)[0]===f.$drop[0]||b(g.target).parents(".ms-drop")[0]!==f.$drop[0])&&f.options.isopen){f.close()}});if(this.options.isopen){this.open()}}a.prototype={constructor:a,init:function(){var d=this,c=[];if(this.options.filter){c.push('','',"
")}c.push("");this.$drop.html(c.join(""));this.$drop.find("ul").css("max-height",this.options.maxHeight+"px");this.$drop.find(".multiple").css("width",this.options.multipleWidth+"px");this.$searchInput=this.$drop.find(".ms-search input");this.$selectAll=this.$drop.find('input[name="selectAll"]');this.$selectGroups=this.$drop.find('input[name="selectGroup"]');this.$selectItems=this.$drop.find('input[name="selectItem"]:enabled');this.$disableItems=this.$drop.find('input[name="selectItem"]:disabled');this.events();this.update()},optionToHtml:function(h,l,o,d){var k=this,e=b(l),j=[],q=this.options.multiple;if(e.is("option")){var n=e.val(),p=e.text(),g=e.prop("selected"),f=d||e.prop("disabled");j.push("","","")}else{if(!o&&e.is("optgroup")){var c="group_"+h,m=e.attr("label"),f=e.prop("disabled");j.push('','","");b.each(e.children(),function(r,s){j.push(k.optionToHtml(r,s,c,f))})}}return j.join("")},events:function(){var c=this;this.$choice.off("click").on("click",function(){c[c.options.isopen?"close":"open"]()});this.$parent.off("keydown").on("keydown",function(d){switch(d.which){case 27:c.close();c.$choice.focus();break}});this.$parent.off("focusout").on("focusout",function(){setTimeout(function(){if(c.$parent.has(document.activeElement).length===0){c.close()}},1000)});this.$searchInput.off("keyup").on("keyup",function(){c.filter()});this.$selectAll.off("click").on("click",function(){var d=b(this).prop("checked"),e=c.$selectItems.filter(":visible");if(e.length===c.$selectItems.length){c[d?"checkAll":"uncheckAll"]()}else{e.prop("checked",d);c.update()}});this.$selectGroups.off("click").on("click",function(){var f=b(this).parent().attr("data-group"),g=c.$selectItems.filter(":visible"),d=g.filter('[data-group="'+f+'"]'),e=d.length!==d.filter(":checked").length;d.prop("checked",e);c.updateSelectAll();c.update();c.options.onOptgroupClick({label:b(this).parent().text(),checked:e,children:d.get()})});this.$selectItems.off("click").on("click",function(){c.updateSelectAll();c.update();c.updateOptGroupSelect();c.options.onClick({label:b(this).parent().text(),value:b(this).val(),checked:b(this).prop("checked")})})},open:function(){if(this.$choice.hasClass("disabled")){return}this.options.isopen=true;this.$choice.find(">div").addClass("open");this.$drop.show();if(this.options.filter){this.$searchInput.val("");this.filter()}this.options.onOpen()},close:function(){this.options.isopen=false;this.$choice.find(">div").removeClass("open");this.$drop.hide();this.options.onClose()},update:function(){var d=this.getSelects("text"),c=this.$choice.find(">span");if(d.length){c.removeClass("placeholder").html(d.join(","))}else{c.addClass("placeholder").html(this.options.placeholder)}this.$el.val(this.getSelects())},updateSelectAll:function(){var c=this.$selectItems.filter(":visible");this.$selectAll.prop("checked",c.length&&c.length===c.filter(":checked").length)},updateOptGroupSelect:function(){var c=this.$selectItems.filter(":visible");b.each(this.$selectGroups,function(e,g){var f=b(g).parent().attr("data-group"),d=c.filter('[data-group="'+f+'"]');b(g).prop("checked",d.length&&d.length===d.filter(":checked").length)})},getSelects:function(d){var c=[];this.$drop.find('input[name="selectItem"]:checked').each(function(){c.push(d==="text"?b(this).parent().text():b(this).val())});return c},setSelects:function(c){var d=this;this.$selectItems.prop("checked",false);b.each(c,function(e,f){d.$selectItems.filter('[value="'+f+'"]').prop("checked",true)});this.$selectAll.prop("checked",this.$selectItems.length===this.$selectItems.filter(":checked").length);this.update()},enable:function(){this.$choice.removeClass("disabled")},disable:function(){this.$choice.addClass("disabled")},checkAll:function(){this.$selectItems.prop("checked",true);this.$selectGroups.prop("checked",true);this.$selectAll.prop("checked",true);this.update();this.options.onCheckAll()},uncheckAll:function(){this.$selectItems.prop("checked",false);this.$selectGroups.prop("checked",false);this.$selectAll.prop("checked",false);this.update();this.options.onUncheckAll()},refresh:function(){this.init()},filter:function(){var c=this,d=b.trim(this.$searchInput.val()).toLowerCase();if(d.length===0){this.$selectItems.parent().show();this.$disableItems.parent().show();this.$selectGroups.show()}else{this.$selectItems.each(function(){var e=b(this).parent();e[e.text().toLowerCase().indexOf(d)<0?"hide":"show"]()});this.$disableItems.parent().hide();this.$selectGroups.each(function(){var e=b(this).attr("data-group"),f=c.$selectItems.filter(":visible");b(this)[f.filter('[data-group="'+e+'"]').length===0?"hide":"show"]()})}this.updateSelectAll()}};b.fn.multipleSelect=function(){var e=arguments[0],d=arguments,f,c=["getSelects","setSelects","enable","disable","checkAll","uncheckAll","refresh"];this.each(function(){var i=b(this),h=i.data("multipleSelect"),g=b.extend({},b.fn.multipleSelect.defaults,typeof e==="object"&&e);if(!h){h=new a(i,g);i.data("multipleSelect",h)}if(typeof e==="string"){if(b.inArray(e,c)<0){throw"Unknown method: "+e}f=h[e](d[1])}else{h.init()}});return f?f:this};b.fn.multipleSelect.defaults={isopen:false,placeholder:"",selectAll:true,selectAllText:"Select all",multiple:false,multipleWidth:80,filter:false,width:undefined,maxHeight:250,onOpen:function(){return false},onClose:function(){return false},onCheckAll:function(){return false},onUncheckAll:function(){return false},onOptgroupClick:function(){return false},onClick:function(){return false}}})(jQuery);