/*

Utilisation typique :

		function imageMouseDown(e) {
			var evt = new anet.Event(e);
			dx = evt.getX() - anet.dom.getX(image);
			dy = evt.getY() - anet.dom.getY(image);
			anet.dom.addEventListener(document.body, "mousemove", imageMouseMove, false);
			anet.dom.addEventListener(document.body, "mouseup", imageMouseUp, false);
			evt.consume();
		}
*/

/**
 *	@constructor
 *	@param MouseEvent evt
 */
anet.Event = function(evt) {
	if (window.event) {
		this._evt = window.event;
		this._src = window.event.fromElement;
		this._dst = window.event.toElement;
		this._x = window.event.x; // window.event.clientX
		this._y = window.event.y; // window.event.clientY
	} else if (evt) {
		this._evt = evt;
		this._src = evt.target;
		this._dst = evt.relatedTarget;
		this._x = evt.pageX;
		this._y = evt.pageY;
	}
}

anet.Event.prototype = {
	/**
	 *	@params Element element
	 *	@returns Boolean
	 */
	exitedFromElement: function(element) {
		if (!this._src || !this._dst)
			return (false);
		var fromNode = this._src;
		var toNode = this._dst;
		// verif : le noeud d'origine est contenu dans l'element
		// (ou est l'element lui-meme)
		for (; fromNode; fromNode = fromNode.parentNode)
			if (fromNode == element)
				break;
		if (!fromNode)
			return (false);
		// verif : le noeud de destination n'est pas dans l'element
		for (; toNode; toNode = toNode.parentNode) {
			if (toNode == fromNode)
				return (false);
		}
		return (true);
	},

	/**
	 *	@returns Element
	 */
	getSource: function () {
		return (this._src);
	},

	/**
	 *	@returns Element
	 */
	getDestination: function() {
		return (this._dst);
	},

	/**
	 *	@returns void
	 */
	consume: function () {
		if (!this._evt)
			return;
		if (this._evt.stopPropagation) {
			this._evt.stopPropagation();
			this._evt.preventDefault();
		}
		try {
			this._evt.cancelBubble = true;
			this._evt.returnValue  = false;
		} catch (e) { }
	},

	/**
	 *	@returns string
	 */
	toString: function () {
		return ("Evt [ x = " + this._x + ", y = " + this._y + " ]");
	},

	/**
	 *	@returns number
	 */
	getX: function () {
		return (this._x);
	},

	/**
	 *	@returns number
	 */
	getY: function () {
		return (this._y);
	},

	/*
	 * getButton()
	 * Retourne le numero du bouton de souris qui a ete clique
	 */
	getButton: function() {
		if (!this._evt)
			return (null);
		if (this._evt.button)
			return (this._evt.button);
		return (this._evt.which);
	}
};
