
//  red.event.add(document,'click',function(e){alert(red.mouse.getxy(e))});
//  red.event.add(document,'mousemove',function(e){div.innerHTML = red.mouse.getxy(e)[0]+","+red.mouse.getxy(e)[1]});

red.prototype.mouse = {
	
	menu : true,
	
	button : null,
	
	getxy : function( e ) {
		var posx = 0;
		var posy = 0;
		
		e = red.prototype.mouse.fixE(e);
		
		if (e.pageX || e.pageY) 	{
			posx = e.pageX;
			posy = e.pageY;
		}
		else if (e.clientX || e.clientY) 	{
			posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
			posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
		}
		return [posx,posy];
	},
	
	mousedown : function(e){
		if (!e){e = window.event;}
		if ((e.button && e.button == 1) && (e.which && e.which == 2)){
			red.prototype.mouse.button = 2;
		}
		else if ((e.button && e.button == 1) || (e.which && e.which == 1)){
			red.prototype.mouse.button = 1;
		}
		else if ((e.button && e.button == 4) || (e.which && e.which == 2)){
			red.prototype.mouse.button = 2;
		}
		else if ((e.button && e.button == 2) || (e.which && e.which == 3)){
			red.prototype.mouse.button = 3;
		}
		
		if(red.prototype.mouse.down != null){
			red.prototype.mouse.down();
		}
	},
	
	mouseup : function(e){
		if (!e){e = window.event;}
		if ((e.button && e.button == 1) && (e.which && e.which == 2)){
			red.prototype.mouse.button = 2;
		}
		else if ((e.button && e.button == 1) || (e.which && e.which == 1)){
			red.prototype.mouse.button = 1;
		}
		else if ((e.button && e.button == 4) || (e.which && e.which == 2)){
			red.prototype.mouse.button = 2;
		}
		else if ((e.button && e.button == 2) || (e.which && e.which == 3)){
			red.prototype.mouse.button = 3;
		}
		
		if(red.prototype.mouse.up != null){
			red.prototype.mouse.up();
		}
		
		red.prototype.mouse.button = null;		
	},
	
	down : null,
	
	up : null,
		
	contextmenu : function(e){
		if(!red.prototype.mouse.menu){	
			if (!e){e = window.event;}
			if(e.type && e.type == "contextmenu"){
				return false;
			}
		}
	},
	
	delta : null,
	
	wheel : function(event){
		var delta = 0;
		if (!event) event = window.event;
		if (event.wheelDelta) {
			delta = event.wheelDelta/120; 
			if (window.opera) delta = -delta;
		} else if (event.detail) {
			delta = -event.detail/3;
		}
		if (delta)
			red.prototype.mouse.handlewheel(delta);
	        if (event.preventDefault)
	                event.preventDefault();
	        event.returnValue = false;
	},
	
	handlewheel : function(delta) {
		red.prototype.mouse.delta = delta;
		if (delta < 0){
			if(red.prototype.mouse.wheeldown){
				red.prototype.mouse.wheeldown();
			}
		}		
		else{
			if(red.prototype.mouse.wheelup){
				red.prototype.mouse.wheelup();
			}
		}
	},
	
	fixE: function(e) {
        if (typeof e == 'undefined' || e==null || e=='undefined') var e = window.event;
        if (typeof e.elemX == 'undefined') e.elemX = e.offsetX;
        if (typeof e.elemY == 'undefined') e.elemY = e.offsetY;
        return e;
    }
	
};


(function() {
	document.onmousedown 	= red.prototype.mouse.mousedown;
	document.onmouseup 		= red.prototype.mouse.mouseup;
	document.oncontextmenu 	= red.prototype.mouse.contextmenu;
	/*
	if (window.addEventListener){
		window.addEventListener('DOMMouseScroll', red.prototype.mouse.wheel, false);
	}
	window.onmousewheel = document.onmousewheel = red.prototype.mouse.wheel;
	*/
})();

