(function(a){a.widget("ui.selectable",a.ui.mouse,{options:{appendTo:"body",autoRefresh:true,distance:0,filter:"*",tolerance:"touch"},_create:function(){var c=this;this.element.addClass("ui-selectable");this.dragged=false;var b;this.refresh=function(){b=a(c.options.filter,c.element[0]);b.each(function(){var b=a(this),c=b.offset();a.data(this,"selectable-item",{element:this,$element:b,left:c.left,top:c.top,right:c.left+b.outerWidth(),bottom:c.top+b.outerHeight(),startselected:false,selected:b.hasClass("ui-selected"),selecting:b.hasClass("ui-selecting"),unselecting:b.hasClass("ui-unselecting")})})};this.refresh();this.selectees=b.addClass("ui-selectee");this._mouseInit();this.helper=a("<div class='ui-selectable-helper'></div>")},destroy:function(){this.selectees.removeClass("ui-selectee").removeData("selectable-item");this.element.removeClass("ui-selectable ui-selectable-disabled").removeData("selectable").unbind(".selectable");this._mouseDestroy();return this},_mouseStart:function(b){var d=this;this.opos=[b.pageX,b.pageY];if(this.options.disabled)return;var c=this.options;this.selectees=a(c.filter,this.element[0]);this._trigger("start",b);a(c.appendTo).append(this.helper);this.helper.css({left:b.clientX,top:b.clientY,width:0,height:0});c.autoRefresh&&this.refresh();this.selectees.filter(".ui-selected").each(function(){var c=a.data(this,"selectable-item");c.startselected=true;if(!b.metaKey){c.$element.removeClass("ui-selected");c.selected=false;c.$element.addClass("ui-unselecting");c.unselecting=true;d._trigger("unselecting",b,{unselecting:c.element})}});a(b.target).parents().andSelf().each(function(){var c=a.data(this,"selectable-item");if(c){var e=!b.metaKey||!c.$element.hasClass("ui-selected");c.$element.removeClass(e?"ui-unselecting":"ui-selected").addClass(e?"ui-selecting":"ui-unselecting");c.unselecting=!e;c.selecting=e;c.selected=e;if(e)d._trigger("selecting",b,{selecting:c.element});else d._trigger("unselecting",b,{unselecting:c.element});return false}})},_mouseDrag:function(b){var g=this;this.dragged=true;if(this.options.disabled)return;var h=this.options,c=this.opos[0],d=this.opos[1],e=b.pageX,f=b.pageY;if(c>e){var i=e;e=c;c=i}if(d>f){var i=f;f=d;d=i}this.helper.css({left:c,top:d,width:e-c,height:f-d});this.selectees.each(function(){var i=a.data(this,"selectable-item");if(!i||i.element==g.element[0])return;var j=false;if(h.tolerance=="touch")j=!(i.left>e||i.right<c||i.top>f||i.bottom<d);else if(h.tolerance=="fit")j=i.left>c&&i.right<e&&i.top>d&&i.bottom<f;if(j){if(i.selected){i.$element.removeClass("ui-selected");i.selected=false}if(i.unselecting){i.$element.removeClass("ui-unselecting");i.unselecting=false}if(!i.selecting){i.$element.addClass("ui-selecting");i.selecting=true;g._trigger("selecting",b,{selecting:i.element})}}else{if(i.selecting)if(b.metaKey&&i.startselected){i.$element.removeClass("ui-selecting");i.selecting=false;i.$element.addClass("ui-selected");i.selected=true}else{i.$element.removeClass("ui-selecting");i.selecting=false;if(i.startselected){i.$element.addClass("ui-unselecting");i.unselecting=true}g._trigger("unselecting",b,{unselecting:i.element})}if(i.selected)if(!b.metaKey&&!i.startselected){i.$element.removeClass("ui-selected");i.selected=false;i.$element.addClass("ui-unselecting");i.unselecting=true;g._trigger("unselecting",b,{unselecting:i.element})}}});return false},_mouseStop:function(b){var c=this;this.dragged=false;var d=this.options;a(".ui-unselecting",this.element[0]).each(function(){var d=a.data(this,"selectable-item");d.$element.removeClass("ui-unselecting");d.unselecting=false;d.startselected=false;c._trigger("unselected",b,{unselected:d.element})});a(".ui-selecting",this.element[0]).each(function(){var d=a.data(this,"selectable-item");d.$element.removeClass("ui-selecting").addClass("ui-selected");d.selecting=false;d.selected=true;d.startselected=true;c._trigger("selected",b,{selected:d.element})});this._trigger("stop",b);this.helper.remove();return false}});a.extend(a.ui.selectable,{version:"1.8.6"})})(jQuery)