/*
 jquery.event.drag - v 2.2
 Copyright (c) 2010 Three Dub Media - http://threedubmedia.com
 Open Source MIT License - http://threedubmedia.com/code/license
*/
(function(f){f.fn.drag=function(b,a,e){var d=typeof b=="string"?b:"",l=f.isFunction(b)?b:f.isFunction(a)?a:null;d.indexOf("drag")!==0&&(d="drag"+d);e=(b==l?a:e)||{};return l?this.bind(d,e,l):this.trigger(d)};var h=f.event,k=h.special,c=k.drag={defaults:{which:1,distance:0,not:":input",handle:null,relative:!1,drop:!0,click:!1},datakey:"dragdata",noBubble:!0,add:function(b){var a=f.data(this,c.datakey),e=b.data||{};a.related+=1;f.each(c.defaults,function(d,l){e[d]!==void 0&&(a[d]=e[d])})},remove:function(){--f.data(this,
c.datakey).related},setup:function(){if(!f.data(this,c.datakey)){var b=f.extend({related:0},c.defaults);f.data(this,c.datakey,b);h.add(this,"touchstart mousedown",c.init,b);this.attachEvent&&this.attachEvent("ondragstart",c.dontstart)}},teardown:function(){(f.data(this,c.datakey)||{}).related||(f.removeData(this,c.datakey),h.remove(this,"touchstart mousedown",c.init),c.textselect(!0),this.detachEvent&&this.detachEvent("ondragstart",c.dontstart))},init:function(b){if(!c.touched){var a=b.data;if(!(b.which!=
0&&a.which>0&&b.which!=a.which||f(b.target).is(a.not)||a.handle&&!f(b.target).closest(a.handle,b.currentTarget).length)){c.touched=b.type=="touchstart"?this:null;a.propagates=1;a.mousedown=this;a.interactions=[c.interaction(this,a)];a.target=b.target;a.pageX=b.pageX;a.pageY=b.pageY;a.dragging=null;var e=c.hijack(b,"draginit",a);if(a.propagates&&((e=c.flatten(e))&&e.length&&(a.interactions=[],f.each(e,function(){a.interactions.push(c.interaction(this,a))})),a.propagates=a.interactions.length,a.drop!==
!1&&k.drop&&k.drop.handler(b,a),c.textselect(!1),c.touched?h.add(c.touched,"touchmove touchend",c.handler,a):h.add(document,"mousemove mouseup",c.handler,a),!c.touched||a.live))return!1}}},interaction:function(b,a){a=f(b)[a.relative?"position":"offset"]()||{top:0,left:0};return{drag:b,callback:new c.callback,droppable:[],offset:a}},handler:function(b){var a=b.data;switch(b.type){case !a.dragging&&"touchmove":b.preventDefault();case !a.dragging&&"mousemove":if(Math.pow(b.pageX-a.pageX,2)+Math.pow(b.pageY-
a.pageY,2)<Math.pow(a.distance,2))break;b.target=a.target;c.hijack(b,"dragstart",a);a.propagates&&(a.dragging=!0);case "touchmove":b.preventDefault();case "mousemove":if(a.dragging){c.hijack(b,"drag",a);if(a.propagates){a.drop!==!1&&k.drop&&k.drop.handler(b,a);break}b.type="mouseup"}default:c.touched?h.remove(c.touched,"touchmove touchend",c.handler):h.remove(document,"mousemove mouseup",c.handler),a.dragging&&(a.drop!==!1&&k.drop&&k.drop.handler(b,a),c.hijack(b,"dragend",a)),c.textselect(!0),a.click===
!1&&a.dragging&&f.data(a.mousedown,"suppress.click",(new Date).getTime()+5),a.dragging=c.touched=!1}},hijack:function(b,a,e,d,l){if(e){var t=b.originalEvent,u=b.type,p=a.indexOf("drop")?"drag":"drop",m,q=d||0,g;d=isNaN(d)?e.interactions.length:d;b.type=a;b.originalEvent=null;e.results=[];do if((g=e.interactions[q])&&(a==="dragend"||!g.cancelled)){var r=c.properties(b,e,g);g.results=[];f(l||g[p]||e.droppable).each(function(v,n){r.target=n;b.isPropagationStopped=function(){return!1};m=n?h.dispatch.call(n,
b,r):null;m===!1?(p=="drag"&&(g.cancelled=!0,--e.propagates),a=="drop"&&(g[p][v]=null)):a=="dropinit"&&g.droppable.push(c.element(m)||n);a=="dragstart"&&(g.proxy=f(c.element(m)||g.drag)[0]);g.results.push(m);delete b.result;if(a!=="dropinit")return m});e.results[q]=c.flatten(g.results);a=="dropinit"&&(g.droppable=c.flatten(g.droppable));a!="dragstart"||g.cancelled||r.update()}while(++q<d);b.type=u;b.originalEvent=t;return c.flatten(e.results)}},properties:function(b,a,e){var d=e.callback;d.drag=e.drag;
d.proxy=e.proxy||e.drag;d.startX=a.pageX;d.startY=a.pageY;d.deltaX=b.pageX-a.pageX;d.deltaY=b.pageY-a.pageY;d.originalX=e.offset.left;d.originalY=e.offset.top;d.offsetX=d.originalX+d.deltaX;d.offsetY=d.originalY+d.deltaY;d.drop=c.flatten((e.drop||[]).slice());d.available=c.flatten((e.droppable||[]).slice());return d},element:function(b){if(b&&(b.jquery||b.nodeType==1))return b},flatten:function(b){return f.map(b,function(a){return a&&a.jquery?f.makeArray(a):a&&a.length?c.flatten(a):a})},textselect:function(b){f(document)[b?
"unbind":"bind"]("selectstart",c.dontstart).css("MozUserSelect",b?"":"none");document.unselectable=b?"off":"on"},dontstart:function(){return!1},callback:function(){}};c.callback.prototype={update:function(){k.drop&&this.available.length&&f.each(this.available,function(b){k.drop.locate(this,b)})}};var w=h.dispatch;h.dispatch=function(b){if(f.data(this,"suppress."+b.type)-(new Date).getTime()>0)f.removeData(this,"suppress."+b.type);else return w.apply(this,arguments)};var x=h.fixHooks.touchstart=h.fixHooks.touchmove=
h.fixHooks.touchend=h.fixHooks.touchcancel={props:"clientX clientY pageX pageY screenX screenY".split(" "),filter:function(b,a){if(a){var e=a.touches&&a.touches[0]||a.changedTouches&&a.changedTouches[0]||null;e&&f.each(x.props,function(d,l){b[l]=e[l]})}return b}};k.draginit=k.dragstart=k.dragend=c})(jQuery);
