!function(){var moduleFactory=function($){var module=this;$.require().script("mvc/lang.vector","mvc/event.livehack").done(function(){var exports=function(){var bind=function(object,method){var args=Array.prototype.slice.call(arguments,2);return function(){var args2=[this].concat(args,$.makeArray(arguments));return method.apply(object,args2)}},event=$.event,clearSelection=window.getSelection?function(){window.getSelection().removeAllRanges()}:function(){};$.Drag=function(){},$.extend($.Drag,{lowerName:"drag",current:null,distance:0,mousedown:function(ev,element){var isLeftButton=0===ev.button||1==ev.button;
if(isLeftButton&&!this.current){var drag=new $.Drag,delegate=ev.delegateTarget||element,selector=ev.handleObj.selector,self=this;this.current=drag,drag.setup({element:element,delegate:ev.delegateTarget||element,selector:ev.handleObj.selector,moved:!1,_distance:this.distance,callbacks:{dragdown:event.find(delegate,["dragdown"],selector),draginit:event.find(delegate,["draginit"],selector),dragover:event.find(delegate,["dragover"],selector),dragmove:event.find(delegate,["dragmove"],selector),dragout:event.find(delegate,["dragout"],selector),dragend:event.find(delegate,["dragend"],selector)},destroyed:function(){self.current=null
}},ev)}}}),$.extend($.Drag.prototype,{setup:function(options,ev){$.extend(this,options),this.element=$(this.element),this.event=ev,this.moved=!1,this.allowOtherDrags=!1;var mousemove=bind(this,this.mousemove),mouseup=bind(this,this.mouseup);this._mousemove=mousemove,this._mouseup=mouseup,this._distance=options.distance?options.distance:0,this.mouseStartPosition=ev.vector(),$(document).bind("mousemove",mousemove),$(document).bind("mouseup",mouseup),this.callEvents("down",this.element,ev)||(this.noSelection(this.delegate),clearSelection())
},destroy:function(){$(document).unbind("mousemove",this._mousemove),$(document).unbind("mouseup",this._mouseup),this.moved||(this.event=this.element=null),this.selection(this.delegate),this.destroyed()},mousemove:function(docEl,ev){if(!this.moved){var dist=Math.sqrt(Math.pow(ev.pageX-this.event.pageX,2)+Math.pow(ev.pageY-this.event.pageY,2));if(dist<this._distance)return!1;this.init(this.element,ev),this.moved=!0}var pointer=ev.vector();this._start_position&&this._start_position.equals(pointer)||this.draw(pointer,ev)
},mouseup:function(docEl,event){this.moved&&this.end(event),this.destroy()},noSelection:function(elm){elm=elm||this.delegate,document.documentElement.onselectstart=function(){return!1},document.documentElement.unselectable="on",this.selectionDisabled=this.selectionDisabled?this.selectionDisabled.add(elm):$(elm),this.selectionDisabled.css("-moz-user-select","-moz-none")},selection:function(){this.selectionDisabled&&(document.documentElement.onselectstart=function(){},document.documentElement.unselectable="off",this.selectionDisabled.css("-moz-user-select",""))
},init:function(element,event){element=$(element);var startElement=this.movingElement=this.element=$(element);this._cancelled=!1,this.event=event,this.mouseElementPosition=this.mouseStartPosition.minus(this.element.offsetv()),this.callEvents("init",element,event),this._cancelled!==!0&&(this.startPosition=startElement!=this.movingElement?this.movingElement.offsetv():this.currentDelta(),this.makePositioned(this.movingElement),this.oldZIndex=this.movingElement.css("zIndex"),this.movingElement.css("zIndex",1e3),!this._only&&this.constructor.responder&&this.constructor.responder.compile(event,this))
},makePositioned:function(that){var style,pos=that.css("position");pos&&"static"!=pos||(style={position:"relative"},window.opera&&(style.top="0px",style.left="0px"),that.css(style))},callEvents:function(type,element,event,drop){var i,cbs=this.callbacks[this.constructor.lowerName+type];for(i=0;i<cbs.length;i++)cbs[i].call(element,event,this,drop);return cbs.length},currentDelta:function(){return new $.Vector(parseInt(this.movingElement.css("left"),10)||0,parseInt(this.movingElement.css("top"),10)||0)},draw:function(pointer,event){this._cancelled||(clearSelection(),this.location=pointer.minus(this.mouseElementPosition),this.move(event),this._cancelled||(event.isDefaultPrevented()||this.position(this.location),!this._only&&this.constructor.responder&&this.constructor.responder.show(pointer,this,event)))
},position:function(newOffsetv){var style,dragged_element_css_offset=this.currentDelta(),dragged_element_position_vector=this.movingElement.offsetv().minus(dragged_element_css_offset);this.required_css_position=newOffsetv.minus(dragged_element_position_vector),this.offsetv=newOffsetv,style=this.movingElement[0].style,this._cancelled||this._horizontal||(style.top=this.required_css_position.top()+"px"),this._cancelled||this._vertical||(style.left=this.required_css_position.left()+"px")},move:function(event){this.callEvents("move",this.element,event)
},over:function(event,drop){this.callEvents("over",this.element,event,drop)},out:function(event,drop){this.callEvents("out",this.element,event,drop)},end:function(event){if(!this._cancelled){if(!this._only&&this.constructor.responder&&this.constructor.responder.end(event,this),this.callEvents("end",this.element,event),this._revert){var self=this;this.movingElement.animate({top:this.startPosition.top()+"px",left:this.startPosition.left()+"px"},function(){self.cleanup.apply(self,arguments)})}else this.cleanup();
this.event=null}},cleanup:function(){this.movingElement.css({zIndex:this.oldZIndex}),this.movingElement[0]===this.element[0]||this.movingElement.has(this.element[0]).length||this.element.has(this.movingElement[0]).length||this.movingElement.css({display:"none"}),this._removeMovingElement&&this.movingElement.remove(),this.movingElement=this.element=this.event=null},cancel:function(){this._cancelled=!0,!this._only&&this.constructor.responder&&this.constructor.responder.clear(this.event.vector(),this,this.event),this.destroy()
},ghost:function(loc){var ghost=this.movingElement.clone().css("position","absolute");return(loc?$(loc):this.movingElement).after(ghost),ghost.width(this.movingElement.width()).height(this.movingElement.height()),ghost.offset(this.movingElement.offset()),this.movingElement=ghost,this.noSelection(ghost),this._removeMovingElement=!0,ghost},representative:function(element,offsetX,offsetY){this._offsetX=offsetX||0,this._offsetY=offsetY||0;var p=this.mouseStartPosition;this.movingElement=$(element),this.movingElement.css({top:p.y()-this._offsetY+"px",left:p.x()-this._offsetX+"px",display:"block",position:"absolute"}).show(),this.noSelection(this.movingElement),this.mouseElementPosition=new $.Vector(this._offsetX,this._offsetY)
},revert:function(val){return this._revert=void 0===val?!0:val,this},vertical:function(){return this._vertical=!0,this},horizontal:function(){return this._horizontal=!0,!0},only:function(only){return this._only=void 0===only?!0:only},distance:function(val){return void 0!==val?(this._distance=val,this):this._distance}}),event.setupHelper(["dragdown","draginit","dragover","dragmove","dragout","dragend"],"mousedown",function(e){$.Drag.mousedown.call($.Drag,e,this)})};exports(),module.resolveWith(exports)})};dispatch("mvc/event.drag").containing(moduleFactory).to("Foundry/2.1 Modules")
}();