/* Copyright © Yogotopia.com. All Rights Reserved. */
ART.Drag=ART.Class({current_handler:null,current_root:null,last_mouse_x:0,last_mouse_y:0,init:function(){ART.bind_methods(this);},drag_able:function(root,kws){kws=kws||{};var handler=kws.handler||root;handler=ART.GE(handler);handler._kws=kws;handler._root=ART.GE(root);handler._start_fn=function(ev){ART.DD._start(handler,ev);ART.prevent_default(ev);return false;};ART.AEV(handler,'mousedown',handler._start_fn);},remove_drag_able:function(elm){if(elm._start_fn)ART.REV(elm,'mousedown',elm._start_fn);},_start:function(handler,ev){this.current_handler=handler;var root=this.current_root=handler._root;this.last_mouse_pos=ART.cursor_pos(ev);this.old_z_index=root.style.zIndex;root.style.zIndex=1000;if(handler._kws.on_start)handler._kws.on_start();ART.AEV(document,'mousemove',this._move);ART.AEV(document,'mouseup',this._end);},_move:function(ev){var handler=this.current_handler;if(!handler)return false;var root=this.current_root;var kws=handler._kws;var cur_mouse_pos=ART.cursor_pos(ev);var last_mouse_pos=this.last_mouse_pos;var new_x=(cur_mouse_pos.x-last_mouse_pos.x);var new_y=(cur_mouse_pos.y-last_mouse_pos.y);new_y+=root.offsetTop;new_x+=root.offsetLeft;if(kws.move_filter){var vals=kws.move_filter(new_x,new_y);new_x=vals[0];new_y=vals[1];}if(kws.move_x!=false)ART.set_left(root,new_x);if(kws.move_y!=false)ART.set_top(root,new_y);if(kws.on_drag)kws.on_drag(new_x,new_y);this.last_mouse_pos=cur_mouse_pos;if(kws.scroll_on_overflow!=false){var sc_top=ART.get_scroll_top();var sc_bottom=sc_top+ART.page_height();var d_e_top=ART.absolute_pos(root).y;var d_e_bottom=root.offsetTop+root.offsetHeight;if(d_e_top<=sc_top+30)window.scrollBy(0,-20);else if(d_e_bottom>=sc_bottom-30)window.scrollBy(0,20);}ART.prevent_default(ev);return false;},_end:function(ev){var root=this.current_root;var handler=this.current_handler;ART.REV(document,'mousemove',this._move);ART.REV(document,'mouseup',this._end);if(handler._kws.on_end)handler._kws.on_end();this.current_handler=null;this.current_root=null;root.style.zIndex=this.old_z_index;ART.prevent_default(ev);return false;}});ART.DD=new ART.Drag();
