/**
 * @fileoverview
 * 
 * <p>Copyright (c) 2006 Peter Michaux. All rights reserved.<br />
 * Code licensed under the MIT License:<br />
 * http://michaux.ca/svn/dragdrop/trunk/MIT-LICENSE.txt</p>
 *
 * @author Peter Michaux - http://peter.michaux.ca
 * @version 0.1 (August 2006)
 */

/**
 * @class
 * DragDropManager subclasses DragManager and extends it by adding
 * drop target functionality.
 *
 * <p>Any HTML element with class name "target" will be a drop target.
 * The entire element and it's contents are the valid drop zone
 * (event nested anchor elements).</p>
 *
 * <p>When an target element is being hovered during a drag it will have
 * the CSS class "hovered".</p>
 *
 * <p>To add the drop target functionality, the following DragDropManager
 * properites will overwrite DragManager's properties.<p>
 *
 * <ul>
 *   <li>handleDrag()</li>
 *   <li>endDrag()</li>
 * </ul>
 *
 * <h4>interesting moment hooks</h4>
 * <p>The interesting moment hooks fire in the following order and are
 * available for use in your subclasses and instances.</p>
 *
 * <ol>
 *  <li>onDragEnter(e)</li>
 *  <li>onDragOver(e)</li>
 *  <li>onDragLeaveOrDrop(e)</li>
 *  <li>onDragLeave(e)</li>
 *  <li>onDrop(e)</li>
 * </ol>
 *
 * @extends DragManager
 *
 * @constructor
 * @param {Object} proxy The proxy object argument passed to DragManager.
 */
function DragDropManager(proxy) {
  DragDropManager.superclass.constructor.call(this, proxy);
}
YAHOO.extend(DragDropManager, DragManager);

DragDropManager.prototype.isTarget = function(element) {
  return (YAHOO.util.Dom.hasClass(element, "target")) ? true : false;
};

DragDropManager.prototype.onTarget = function(e) {
	var targ = YAHOO.util.Event.getTarget(e);
  while (targ) {
		if (this.isTarget(targ)) {
			return targ;
		}
		targ = targ.parentNode;
	}
	return null;
};

DragDropManager.prototype.checkForDrop = function(e) {
	if (!this.dragging) {return;}
	if (!this.currentTarget) {return;}
  this.onDrop(e);
  this.unselect();
};

DragDropManager.prototype.endDrag = function(e) {
	this.checkForDrop(e);
	DragDropManager.superclass.endDrag.call(this, e);
  this.currentTarget = null;
};

DragDropManager.prototype.handleDrag = function(e) {
  DragDropManager.superclass.handleDrag.call(this, e);
  
  var target = this.onTarget(e);
  
  if (this.currentTarget !== target) {
    /* target has changed */
    if (this.currentTarget) {
		  this.onDragLeave(e);
		  this.currentTarget = null;
    }
    
    if (target) {
      this.currentTarget = target;
			this.onDragEnter(e);
    }
  }
  
  if (target) {
    this.onDragOver(e);				
  }
  
};

/* Stub and default hooks for the targets */
DragDropManager.prototype.onDragEnter = function(e) {YAHOO.util.Dom.addClass(this.currentTarget, "hovered");};
DragDropManager.prototype.onDragOver = function(e) {};
DragDropManager.prototype.onDragLeaveOrDrop = function(e) {YAHOO.util.Dom.removeClass(this.currentTarget, "hovered");};
DragDropManager.prototype.onDragLeave = function(e) {this.onDragLeaveOrDrop(e);};
DragDropManager.prototype.onDrop = function(e) {this.onDragLeaveOrDrop(e);};