/*
*  Plugin to move elements
*
*  Distributed under the terms of the GNU Library General Public License
*
*  @package ddi (Drag'n'Drop Interface)
*  @version 1.4
*  @title Plugin used to move elements
*  @author Ilya Lebedev (ilya@lebedev.net), (c) 2005
*
*
*  Plugin moves supplied object.
*  Supported event handlers:
*  
*  __onMoveStart(event) is used to:
*   1. determine if element could be moved
*   2. determine, if element willing to be moved
*  
*  @param object current event object
*  @return bool should element be moved or not
*  @access protected
*  
*  __onMove(event) is used to perform checks during move
*  @param object current event object
*  @return mixed object, described below
*  @access protected
*
*  Object structure:
*  obj = { 'x' : {                  // describes horizontal params
*                  'move': boolean, // should element be moved horizontally?
*                  'min' : 0..n,    // min left coordinate, NULL means 'no limit'
*                  'max' : 0..n     // max right coordinate (incl. element width), 0 means 'no limit'
*                },
*          'y' : {                  // describes vertical params
*                  'move': boolean, // should element be moved vertically?
*                  'min' : 0..n,    // min top coordinate, NULL means 'no limit'
*                  'max' : 0..n     // max bottom coordinate (incl. element height), 0 means 'no limit'
*                }
*        }
*
*  Revision history
*
*  v1.4
*  + plugin group DDI_SYSTEM
*  + blocking of DDI_VISUAL plugins on start
*  + self pointer
*  + plugin stores currently moved object
*  % bug with switching between moved objects during move
*
*  v1.3
*  + check of max and min parameters, supplied by user __onMove handler
*
*  v1.2
*  - getOffset method
*  % offset calculation uses e.__currentTargetDx(y) properties
*  % dx and dy now are stored within plugin, not in moved object
*
*  v1.1 
*  + first public release
*/
__DDI__.plugin.moveIT = new function () {
  var self = this;
  this.name = 'Element mover';
  this.description = 'Plugin is used to move elements';
  this.group = 'DDI_SYSTEM';
  /*
  *  shows move state
  */
  var onmove = false;
  /*
  *  stores mouse coordinates relative to __currentTarget
  */
  var dx, dy;
  /*
  *  currently moved object
  */
  var element = null;
  this.dragstart = {
    'after' : function (e) {
       if (onmove || !e.__currentTarget.__onMoveStart || e.__currentTarget.__onMoveStart(e) == false) return;
       onmove = true;
       dx = e.__pageX-e.__currentTargetOffsetX;
       dy = e.__pageY-e.__currentTargetOffsetY;
       element = e.__currentTarget;
       self.disablePluginGroup('DDI_VISUAL');
    }
  }
  this.drag = {
    'before' : function (e) {
       if (!onmove || !element) return true;
       if (element.__onMove) var r = element.__onMove(e)

       var top = e.__pageY-dy;
       if (r.y.min != null && top<r.y.min) top = r.y.min;
       var bottom = top + element.offsetHeight;
       if (r.y.max != null && bottom>r.y.max) top = bottom - element.offsetHeight;
       var left = e.__pageX-dx;
       if (r.x.min != null && left<r.x.min) left = r.x.min;
       var right = left + element.offsetWidth;
       if (r.x.max != null && right>r.x.max) left = right - element.offsetWidth;
       if (r.y.move && (r.y.min==null || top>=r.y.min) && (r.y.max==null || bottom<=r.y.max)) element.style.top = top + 'px';
       if (r.x.move && (r.x.min==null || left>=r.x.min) && (r.x.max==null || right<=r.x.max)) element.style.left = left + 'px';
       return false;
    }
  }
  this.dragend = {
    'after' : function (e) {
       onmove = false;
       element = false;
       self.enablePluginGroup('DDI_VISUAL');
    }
  }
};