/* generic mouse utilities,
   if you include this we will */

//global variables: last reported mouse position
var _mouseX;
var _mouseY; 

function mouseMoveListener(e) {
	var x = getMouseX(e);
	var y = getMouseY(e);

	_mouseX = x;
	_mouseY = y;	
	for(var i = 0; i < _mouseMoveListeners.length; i++) {
		_mouseMoveListeners[i](x, y);
	}
	checkMouseOut(x, y);
}

//when the mouse out occurs, we call the listener
//and then we remove it
function checkMouseOut(x, y) {
	for(var i = 0; i < _mouseOutListeners.length; i++) {
		if(_mouseOutListeners[i].mouseOutElement) {
			var element = _mouseOutListeners[i].mouseOutElement;
			var rect = getBoundingRect(element);
			if(!rectContains(rect, x, y)) {
				_mouseOutListeners[i]();
				_mouseOutListeners.splice(i, 1);
			}
		}
	}
}

var _mouseMoveListeners = new Array();
function registerMouseMoveListener(listenerFunc) {
	_mouseMoveListeners.push(listenerFunc);
}

var _mouseOutListeners = new Array();
//since onmouseout is flakey, this lets you register a mouse out
//listener for a particular document element
//it's not really like onmouse out: you register it, and then
//as soon as we are not over the element any more, it 
//fires once and then de-registers you as a listener
function registerMouseOutListener(listenerFunc, element) {
	listenerFunc.mouseOutElement = element;
	_mouseOutListeners.push(listenerFunc);	
}

function removeMouseOutListener(listenerFunc) {
	for(var i = 0; i < _mouseOutListeners.length; i++) {
		if(_mouseOutListeners[i] == listenerFunc) {
			_mouseOutListeners.splice(i, 1);
		}	
	}
	_mouseOutListeners.remove(listenerFunc);
}

//translate an event x to a global mouse x
function getMouseX(e) {
	if(!e) e = window.event;
	if(e.pageX) return e.pageX;
	if(e.clientX) return e.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
	
	return 0;
}

//translate an event y to a global mouse y
function getMouseY(e) {
	if(!e) e = window.event;
	if(e.pageY) return e.pageY;
	if(e.clientY) return e.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
	
	return 0;
}


