/*
*  Plugin to resize elements
*
*  Distributed under the terms of the GNU Library General Public License
*
*  @package ddi (Drag'n'Drop Interface)
*  @version 1.1
*  @title Plugin to resize elements
*  @author Ilya Lebedev (ilya@lebedev.net), (c) 2005
*
*
*  Plugin resizes supplied object.
*  Supported event handlers:
*  
*  __onResizeStart(event) is used to:
*   1. determine if element could be resized, to show proper resize cursor
*   2. determine, if element willing to be resized
*  
*  @param object current event object
*  @return bool should element be resized or not
*  @access protected
*  
*  __onResize(event) is used to perform checks during resize
*  @param object current event object
*  @return mixed object, described below
*  @access protected
*
*  Object structure:
*  obj = { 'x' : {                     // describes horizontal params
*                  'resize' : boolean, // should element be resized horizontally?
*                  'min' : 0..n,       // min width
*                  'max' : 0..n        // max width, 0 means 'no limit'
*                },
*          'y' : {                     // describes vertical params
*                  'resize' : boolean, // should element be resized vertically?
*                  'min' : 0..n,       // min height
*                  'max' : 0..n        // max height, 0 means 'no limit'
*                }
*        }
*
*  __onResizeBefore(event) is used to find how element could be resized
*  
*  @param object current event object
*  @return mixed, object 
*  @access protected
*
*  Object structure:
*  obj = { 'x' : boolean, // should element be resized horizontally?
*          'y' : boolean, // should element be resized vertically?
*        }
*
*  Revision history
*
*  v1.1
*  % plugin did not reset resizing flags
*  + plugin group DDI_VISUAL
*  + self pointer
*  + plugin disables visual effects during resize
*  % resize is allowed only if there's no drag in process
*  + calling of optional method __onResizeBefore to determine how element could be resized
*  % made some optimizations to execute only needed code
*  + blocks __onDrag event, object could not be dragged when resized
*  + blocking of DDI_VISUAL plugins on start
*  % calculations of mouse pointer position over the layer
*
*  v1.0 
*  + first public release
*/
__DDI__.plugin.resizeIT = new function () {
  var self = this;
  this.name = 'Element resizer';
  this.description = 'Plugin is used to resize elements';
  this.group = 'DDI_SYSTEM';
  var el = null;
  var onresize = false;
  var vr = false;
  var hr = false;
  var dx, dy;
  var resize;
  this.runtime = {
    'always' : function (e) {
      if (__DDI__.dataTransfer) return false;
      var oldel = el;
      el = e.__target;
      if (!el || !el.__onResizeStart || onresize) {
        vr = false;
        hr = false;
        document.body.style.cursor = '';
        return;
      }
      /*
      *  check how object wants to be resized
      *  if object don't know (has no __onResizeBefore) assume both vertical and horizontal resizing
      */
      if (oldel != el) resize = el&&el.__onResizeBefore?el.__onResizeBefore(e):{'x':true,'y':true};
      var dh = el.scrollTop;
      var x = el.__layerX;
      var y = el.__layerY;
      var h = el.offsetHeight;
      var w = el.offsetWidth;
//      var cursor = (y<4?"n":(y>(h-4)?"s":""));
//          cursor = cursor + (x<4?"w":(x>(w-4)?"e":""));
//      window.status = el.innerHTML+" "+x+" "+y+" "+h+" "+w+" "+e.x+" "+e.layerX
      var cursor = "";
      cursor = (resize.y&&y>(h-12)?"s":"");
      vr = (cursor.length > 0)?true:false;
      cursor = cursor + (resize.x&&x>(w-12)?"e":"");
      hr = (cursor.length > 1 || (cursor.length > 0 && !vr))?true:false;
      if (cursor) {
        el.style.cursor = cursor+"-resize";
        document.body.style.cursor = cursor+"-resize";
      }
      else {
        el.style.cursor = '';
      }
    }
  }
  this.dragstart = {
    'after' : function (e) {
       if (!vr && !hr) return false;
       if (!e.__currentTarget.__onResizeStart || e.__currentTarget.__onResizeStart(e) == false) return false;
       self.disablePluginGroup('DDI_VISUAL');
       onresize = true;
       dx = 0;
       dy = 0;
       initX = e.__currentTarget.offsetWidth;
       initY = e.__currentTarget.offsetHeight;
    }
  }
  this.drag = {
    'before' : function (e) {
      if (!onresize) return true;
      if (e.__currentTarget && e.__currentTarget.__onResize) var r = e.__currentTarget.__onResize(e);
      if (r.x.resize && hr) {
        dx += e.__dX;
        if (r.x.min<0) r.x.min = 0;
        if (r.x.max<0) r.x.max = 0;
        var ha = initX+dx;
        if (ha<=r.x.min) ha = r.x.min;
        else if (r.x.max!=0 && ha>r.x.max) ha = r.x.max;
        e.__currentTarget.style.width = ha+'px';
      }
      if (r.y.resize && vr) {
        dy += e.__dY;
        if (r.y.min<0) r.y.min = 0;
        if (r.y.max<0) r.y.max = 0;
        var va = initY+dy;
        if (va<=r.y.min) va = r.y.min;
        else if (r.y.max!=0 && va>r.y.max) va = r.y.max;
        e.__currentTarget.style.height = va+'px';
      }
      return false;
    },
    'after' : function (e) {
      if (!onresize) return;
      if (e.__currentTarget.__onResize) var r = e.__currentTarget.__onResize(e);
    }
  }
  this.dragend = {
    'after' : function (e) {
      onresize = false;
      vr = false;
      hr = false;
      self.enablePluginGroup('DDI_VISUAL');
    }
  }
};
