﻿/*##########################################################################################
name:			GT.HoverIntent.js
version: 		0.1
type:			class

description: 	tried porting the jquery hoverintent plugin (http://cherne.net/brian/resources/jquery.hoverIntent.js) to mootools

demo: 			http://static.cms/xsl_gamebase/_web-dev/hoverintent/

files: 			

updated:		12.04.2010, sherb
created:		26.03.2010, sherb; ok: 12.04.2010, bstei;
##########################################################################################*/
(function ($) {
	this.GT = this.GT || {};
	this.GT.HoverIntent = this.GT.HoverIntent || new Class({
		Implements: [Options],
		options: {
			element: null,
			mouseMove: $empty,
			mouseLeave: $empty,
			mouseEnter: $empty,
			hoverIntent: $empty,
			sensitivity: 10, // the distance that needs to be "traveled" untill the hoverintent function gets called, higher value means quicker response; for example: "0" means, that you need to stop your mouse for hoverintent to be fired
			delay: 100 // the speed for how often the mouse positions should be checked (in ms)
		},
		initialize: function (options) {
			this.setOptions(options);
			this.element = this.options.element;
			this.prevMouse = {
				x: 0,
				y: 0
			};
			this.mouse = {
				x: 0,
				y: 0
			};
			this.timer = null;

			if (this.element) {
				// this might look wierd, but if we save the functions like that, we can .removeEvent them later again
				this.mouseMove = function (e) {
					this.mouse.x = e.page.x;
					this.mouse.y = e.page.y;
					this.options.mouseMove.run([this.element], this);
				}.bindWithEvent(this);

				this.mouseEnter = function (e) {
					if (this.timer) {
						$clear(this.timer);
					}

					this.element.addEvent('mousemove', this.mouseMove);
					this.timer = this.compare.delay(this.options.delay, this);
					this.options.mouseEnter.run([this.element], this);
				}.bindWithEvent(this);

				this.mouseLeave = function (e) {
					$clear(this.timer);
					this.element.removeEvent('mousemove', this.mouseMove);
					this.options.mouseLeave.run([this.element], this);
				}.bindWithEvent(this);

				this.hoverIntent = function () {
					this.options.hoverIntent.run([this.element], this);
				}.bind(this);
				// end - wierd look

				this.element.addEvents({
					mouseenter: this.mouseEnter,
					mouseleave: this.mouseLeave,
					hoverintent: this.hoverIntent
				});
			}
		},
		// private:
		compare: function () {
			if ((Math.abs(this.prevMouse.x - this.mouse.x) + Math.abs(this.prevMouse.y - this.mouse.y)) <= this.options.sensitivity) {
				this.element.fireEvent('hoverintent');
			} else {
				this.timer = this.compare.delay(this.options.delay, this);
			}
			this.prevMouse.x = this.mouse.x;
			this.prevMouse.y = this.mouse.y;
		},
		abort: function () {
			$clear(this.timer);
		},
		remove: function (triggerLeave) {
			if (triggerLeave) {
				this.element.fireEvent('mouseleave');
			}

			this.element.removeEvent('mousemove', this.mouseMove);
			this.element.removeEvent('mouseenter', this.mouseEnter);
			this.element.removeEvent('mouseleave', this.mouseLeave);
			this.element.removeEvent('hoverintent', this.hoverIntent);
		},
		toElement: function () {
			return this.element;
		}
	});
})(document.id);
/*changelog:################################################################################
0.2, 12.04.2010, /$/gamebase/xsl_gamebase/x__gamebase_2.0/js/GT.HoverIntent.js;
	sherb:	camelCased some variables which where not camelCased ..
0.1, 26.03.2010, /$/gamebase/xsl_gamebase/x__gamebase_2.0/js/GT.HoverIntent.js;1
	sherb:	created
##########################################################################################*/
