/* Mouse class. Copyright by ADenis (adeniss@ukr.net) */

function Mouse(){
	this.y = 0;
	this.x = 0;
	this.onMove = null;

	this.vDirection = "";
	this.hDirection = "";
	
	this._onMove = function(_event){
		var x = 0;
		var y = 0;
		
		if(_event.pageX){
			x = _event.pageX;
			y = _event.pageY;
		}else{
			x = _event.clientX + document.documentElement.scrollLeft;
			y = _event.clientY + document.documentElement.scrollTop;
		}
		
		this.hDirection = (x > this.x) ? "right" : "left";
		this.vDirection = (y > this.y) ? "bottom" : "top";
		this.hDirection = (x == this.x) ? "center" : this.hDirection;
		this.vDirection = (y == this.y) ? "middle" : this.vDirection;
		
		this.x = x;
		this.y = y;
		
		if (this.onMove) {
			this.onMove.call(_event);
		}
	}
	
	this.isOver = function(object){
		var x = getX(object);
		var y = getY(object);
		
		/*
		debug(object+" "+
			  "x="+x + " y=" + y +
			  " width=" + object.offsetWidth + " height=" + object.offsetHeight +
			  " mouseX=" + this.x + " mouseY=" + this.y);
			
		}
		*/
		
		if((this.x > x && this.x < (x + object.offsetWidth))&&
		   (this.y > y && this.y < (y + object.offsetHeight))){
			return true;
		}
		
		return false;
	}
	
	this.getActiveChild = function (object) {
		if (this.isOver(object)) {
			var childs = object.childNodes;
			var length = childs.length;
			
			for (var i = (length-1); i >= 0 ; i--) {			
				if (this.isOver(childs[i])) {
					return this.getActiveChild(childs[i]);
				}
			}			
			return object;
		} else {
			return false;
		}
	}	

	addListener(document, "onmousemove", this, this._onMove, true);
}

var MOUSE = new Mouse();

function getMouse() {
	if (!MOUSE) {
		MOUSE = new Mouse();
	}
	
	return MOUSE;
}