/*
 *  (c) 2000-2018 deviantART, Inc. All rights reserved.
 */
DWait.ready(["jms/lib/Base.js","jms/lib/jquery/jquery.current.js"],function(){window.cumulativeOffset=function(t){var e=0,o=0;if(t.parentNode)do e+=t.offsetTop||0,o+=t.offsetLeft||0,t=t.offsetParent;while(t);return[o,e]},window.Dragger=Base.extend({constructor:function(t,e,o,n,s,i){this.clickNodes=t,this.$offsetNode=$(e),this.offsetNode=this.$offsetNode[0],this.startDragFunc=o,this.moveDragFunc=n,this.endDragFunc=s,this.transformFunc=i,this.type="unknown",this.$win=$(window),this.initEvents()},determineTouchEvent:function(){this.touchstartevent="touchstart",this.touchmoveevent="touchmove",this.touchendevent="touchend",this.pointer=!1,window.PointerEvent?(this.touchstartevent="pointerdown",this.touchendevent="pointerup",this.touchmoveevent="pointermove",this.pointer=["touch","pen"]):window.MSPointerEvent&&(this.touchstartevent="MSPointerDown",this.touchendevent="MSPointerUp",this.touchmoveevent="MSPointerMove",this.pointer=[window.MSPointerEvent.MSPOINTER_TYPE_TOUCH]),ddt.log("touchdebug","init",this.touchstartevent,this.touchendevent,this.pointer)},initEvents:function(){this.determineTouchEvent();for(var t=this,e=function(e){if(e=e||window.event,e.which){if(1!=e.which)return!1}else if(e.button&&e.button>1)return!1;return t.clearEvents(),$(document).on("mousemove",t.moveDrag.bind(t)),$(document).on("mouseup",t.endDrag.bind(t)),t.$win.on("mouseout",t.mouseOut.bind(t)),t.startDrag(e,this),!1},o=function(e){ddt.log("touchdebug",t.touchstartevent,e),e=e||window.event,e.originalEvent&&(e=e.originalEvent);var o=e.touches,n=t.moveDrag.bind(t),s=t.endDrag.bind(t);if(t.pointer){if(!e.isPrimary||!t.pointer.length||!t.pointer.includes(e.pointerType))return!0;t.activePointer=e.pointerId,n=t.validatePointer.bind(t,n),s=t.validatePointer.bind(t,s),e.pointerId&&t.offsetNode&&(t.offsetNode.setPointerCapture?t.offsetNode.setPointerCapture(e.pointerId):t.offsetNode.msSetPointerCapture&&t.offsetNode.msSetPointerCapture(e.pointerId))}if(t.pointer||1==o.length){t.clearEvents();for(var i=0;t.clickNodes.length>i;i++)t.clickNodes[i].onmousedown=null;$(document).on(t.touchmoveevent,n).on(t.touchendevent,s),t.startDrag(e,this)}return!0},n=0;this.clickNodes.length>n;n++){var s=this.clickNodes[n];"object"==typeof s?(s.onmousedown=e,$(s).on(this.touchstartevent,o)):"string"==typeof s&&($(document).on("mousedown",s,e),$(document).on(this.touchstartevent,s,o))}},startDrag:function(t,e){return this.obj=e,this.type=null,this.getOffset(t),t=t||window.event,coords=this.transformCoords(t),this.startDragFunc(coords,t,this.obj),this.moveDragFunc(coords,t,this.obj),t.preventDefault&&t.preventDefault(),!1},validatePointer:function(t,e){return this.pointer?(e=e||window.event,e.originalEvent&&(e=e.originalEvent),e.pointerId==this.activePointer&&e.isPrimary?t(e):(ddt.log("touchdebug","wrong pointer",e.pointerId),!0)):t(e)},moveDrag:function(t){return t=t||window.event,t.originalEvent&&(t=t.originalEvent),coords=this.transformCoords(t),this.numTouches>1?!0:(this.moveDragFunc(coords,t,this.obj),t.preventDefault&&t.preventDefault(),!1)},endDrag:function(t){if(ddt.log("touchdebug",this.touchendevent,t),t=t||window.event,t.originalEvent&&(t=t.originalEvent),this.clearEvents(),this.activePointer=null,t){coords=this.transformCoords(t);try{coords[0]>0&&coords[1]>0&&this.moveDragFunc(coords,t,this.obj)}catch(t){}t.preventDefault&&t.preventDefault()}else coords=t=null;return this.endDragFunc(coords,t,this.obj),this.offset=null,!1},mouseOut:function(t){return t=t||window.event,t.relatedTarget||t.fromElement||t.toElement?!0:(this.clearEvents(),coords=this.transformCoords(t),this.endDragFunc(coords,t),t.preventDefault&&t.preventDefault(),!1)},clearEvents:function(){$(document).off("mouseup").off("mousemove").off(this.touchmoveevent).off(this.touchendevent),this.$win.off("mouseout")},getOffset:function(t){this.numTouches=0;try{this.numTouches=t.touches.length}catch(e){}this.numTouches>0?(this.offset=cumulativeOffset(this.offsetNode),this.$offsetNode.data("offset",this.offset)):(this.offset=this.$offsetNode.offset(),this.$offsetNode.data("offset",[this.offset.left,this.offset.top]))},transformCoords:function(t){if(t=t||window.event,"regular"!=this.type){this.numTouches=0;try{this.numTouches=t.touches.length}catch(e){this.type="regular"}}else this.numTouches=0;if(this.numTouches>0&&!this.pointer)this.type="ipad",(!this.offset||2>this.offset.length||this.offset[0]!=parseInt(this.offset[0]))&&this.getOffset(t),coords=[parseInt(t.touches[0].pageX-this.offset[0]),parseInt(t.touches[0].pageY-this.offset[1])];else{this.offset||this.getOffset(t);var o=t.pageX||(document.body.scrollLeft||document.documentElement.scrollLeft)+t.clientX,n=t.pageY||(document.body.scrollTop||document.documentElement.scrollTop)+t.clientY;coords=[0|o-this.offset.left,0|n-this.offset.top]}return this.transformFunc?this.transformFunc(coords):coords}}),window.DWait&&DWait.run("jms/lib/dragger.js")});if(window.DWait){DWait.count()}