/*
Script: Drag.MoveQuad.js
	Contains <Drag.MoveQuad>, <Element.makeDraggableQuad>
	
License:
	MIT-style license.

Copyright:
	copyright (c) 2007 Andrew Buckman, <http://theoneandtheonly.com>
	
Credits:
	- Completely based on Drag.Move from MooTools (c) 2007 Valerio Proietti
*/

/*
Class: Drag.MoveQuad
	Extends <Drag.Move>, adding functionality for detecting what quadrant of the drop item
	the drag item is located over.  Quadrant information can be found in the second argument
	passed via the leave, over, and quadover events.  The new event, quadover, fires when 
	the drag item is already over the drop item, but changes quadrants.

Note:
	Drag.MoveQuad requires an XHTML doctype (inherited from Drag.Move)
	
Arguments:
	el - the $(element) to apply the drag to
	options - optional.  see Options below.
	
Options:
	all the drag.Move options
*/

Drag.MoveQuad = Drag.Move.extend({
	drag: function(event){
		this.parent.parent.attempt(event, this);
		var overed = this.out ? false : this.droppables.filter(this.checkAgainst, this).getLast();
		if (this.overed != overed){
			if (this.overed) this.overed.fireEvent('leave', [this.element, this]);
			// over event fires when over new element, second argument will have this.overnow set
			this.overed = overed ? overed.fireEvent('over', [this.element, this]) : null;
			if (overed) this.overlast = this.overnow;
		} else if (overed) {
			// over same element, check quadrant
			if (this.overlast.x != this.overnow.x || this.overlast.y != this.overnow.y) {
				// quadrant changed, fire event - quadrant info can be found in 2ndarg.overnow again
				overed.fireEvent('quadover', [this.element, this]);
				this.overlast = this.overnow;
			}
		}
		return this;
	},
	checkAgainst: function(el) {
		el = el.getCoordinates(this.options.overflown);
		var now = this.mouse.now;
		var result = (now.x > el.left && now.x < el.right && now.y < el.bottom && now.y > el.top);
		if (result) {
			this.overnow = {
				x: (now.x > ((el.left + el.right) / 2)) ? 'right' : 'left',
				y: (now.y > ((el.top + el.bottom) / 2)) ? 'bottom' : 'top'
			};
		}
		return result;
	}
});

/*
Class: Element
	Custom class to allow all of its methods to be used with any DOM element via the dollar function <$>.
*/

Element.extend({
	
	/*
	Property: makeDraggableQuad
		Makes an element draggable with the supplied options, similar to makeDraggable, but with
		added quadrant detection (what quadrant of the drop item you are over).
		
	Arguments:
		options - see <Drag.Move> and <Drag.Base> for acceptable options.
	*/
	
	makeDraggableQuad: function(options) {
		return new Drag.MoveQuad(this, options);
	}
});