!function(a){a.fn.extend({select2SortableOrder:function(){var b=this.filter("[multiple]");return b.each(function(){var b=a(this);if("object"!=typeof b.data("select2"))return!1;var c,d=b.siblings(".select2-container"),e=[];b.find("optgroup").each(function(a,b){e.push(b)}),b.find("option").each(function(b,c){var d=a(this).parent("optgroup").prop("label"),f=this;void 0===d&&(""===this.value||this.selected||e.push(f))}),c=a(d.find('.select2-choices li[class!="select2-search-field"]').map(function(){if(!this)return void 0;if(void 0!=a(this).data("select2Data")){var c=a(this).data("select2Data").id;return b.find('option[value="'+c+'"]')[0]}})),c.push.apply(c,e),b.children().remove(),b.append(c)}),b},select2Sortable:function(){var b=Array.prototype.slice.call(arguments,0);if($this=this.filter("[multiple]"),validMethods=["destroy"],0===b.length||"object"==typeof b[0]){var c={bindOrder:"formSubmit",sortableOptions:{placeholder:"ui-state-highlight",items:"li:not(.select2-search-field)",tolerance:"pointer"}},d=a.extend(c,b[0]);"object"!=typeof $this.data("select2")&&$this.select2(),$this.each(function(){var b=a(this),c=b.siblings(".select2-container").find(".select2-choices");switch(c.sortable(d.sortableOptions),d.bindOrder){case"sortableStop":c.on("sortstop.select2sortable",function(a,c){b.select2SortableOrder()}),b.on("change",function(b){a(this).select2SortableOrder()});break;default:b.closest("form").unbind("submit.select2sortable").on("submit.select2sortable",function(){b.select2SortableOrder()})}})}else{if(-1==a.inArray(b[0],validMethods))throw"Unknown method: "+b[0];"destroy"===b[0]&&$this.select2SortableDestroy()}return $this},select2SortableDestroy:function(){var b=this.filter("[multiple]");return b.each(function(){var b=a(this),c=b.parent().find(".select2-choices");b.closest("form").unbind("submit.select2sortable"),c.unbind("sortstop.select2sortable"),c.sortable("destroy")}),b}})}(jQuery);