var Keyboard = {
	VK_1 : 49,
	VK_2 : 50,
	VK_3 : 51,
	VK_4 : 52,
	VK_5 : 53,
	VK_6 : 54,
	VK_7 : 55,
	VK_8 : 56,
	VK_9 : 57,
	VK_0 : 48,
	VK_A : 65,
	VK_B : 66,
	VK_C : 67,
	VK_D : 68,
	VK_E : 69,
	VK_F : 70,
	VK_G : 71,
	VK_H : 72,
	VK_I : 73,
	VK_J : 74,
	VK_K : 75,
	VK_L : 76,
	VK_M : 77,
	VK_N : 78,
	VK_O : 79,
	VK_P : 80,
	VK_Q : 81,
	VK_R : 82,
	VK_S : 83,
	VK_T : 84,
	VK_U : 85,
	VK_V : 86,
	VK_W : 87,
	VK_X : 88,
	VK_Y : 89,
	VK_Z : 90,
	VK_F1 : 112,
	VK_F2 : 113,
	VK_F3 : 114,
	VK_F4 : 115,
	VK_F5 : 116,
	VK_F6 : 117,
	VK_F7 : 118,
	VK_F8 : 119,
	VK_F9 : 120,
	VK_F10 : 121,
	VK_F11 : 122,
	VK_F12 : 123,
	VK_ENTER : 13,
	VK_SPACE : 10,
	VK_DELETE : 46,
	VK_BACKSPACE : 8,
	VK_LEFT : 37,
	VK_RIGHT : 39,
	VK_UP : 38,
	VK_DOWN : 40
}

var GlobalEvent = {
	list : {'keydown':[], 'keyup':[], 'mousedown':[], 'mouseup':[], 'mouseover':[], 'mouseout':[], 'mousemove':[], 'scroll':[], 'load':[]}, 
	keydown : function(e) {
		this.actionEvent('keydown', e);
	}, 
	keyup : function(e) {
		this.actionEvent('keyup', e);
	}, 
	mousedown : function(e) {
		this.actionEvent('mousedown', e);
	}, 
	mouseup : function(e) {
		this.actionEvent('mouseup', e);
	}, 
	mouseover : function(e) {
		this.actionEvent('mouseover', e);
	}, 
	mouseout : function(e) {
		this.actionEvent('mouseout', e);
	}, 
	mousemove : function(e) {
		this.actionEvent('mousemove', e);
	}, 
	scroll : function(e) {
		this.actionEvent('scroll', e);
	}, 
	load : function(e) {
		this.actionEvent('load', e);
	}, 
	actionEvent : function(action, e) {
		var	object;
		for(var	i=0; i<this.list[action].length; i++)	{
			object = this.list[action][i];
			if(object) {
				object['args'].push('');
				object['args'].unshift();
				object['args'][0] = e;
				if(!object['func'].apply(this, object['args'])) {
					return;
				}
			}
		}
	}, 

	allow : function(action) {
		switch(action) {
			case 'keydown':
			case 'keyup':
			case 'mousedown':
			case 'mouseup':
			case 'mouseover':
			case 'mouseout':
			case 'mousemove':
			case 'scroll':
			case 'load':
				return true;
			break;
			default:
				return false;
			break;
		}
	}, 
	addEvent : function(action, id, func, sequence) {
		if(!this.allow(action)) return;
		if(!sequence) sequence = 0;

		var args = [];
		if(arguments.length > 4) {
			args = $A(arguments).slice(4);
		}

		for(var	i=0; i<this.list[action].length; i++) if(this.list[action][i].id==id) return;
		this.list[action][this.list[action].length] = {
			'id' : id, 
			'func' : func, 
			'sequence' : sequence, 
			'args' : args
		}

		var	temp;
		for(var	i=0; i<this.list[action].length; i++)	{
			for(var	j=1; j<this.list[action].length; j++)	{
				if(this.list[action][i].sequence < this.list[action][j].sequence) {
					dontcare = this.list[action][i];
					this.list[action][i] = this.list[action][j];
					this.list[action][j] = temp;
				}
			}
		}
	}, 

	removeEvent : function(action, id) {
		if(!this.allow(action)) return;

		var	temp;
		for(var	i=0; i<this.list[action].length; i++)	{
			if(this.list[action][i].id==id) {
				temp = this.list[action].slice(0, i);
				temp = temp.concat(this.list[action].slice(i+1));
				this.list[action] = temp;
			}
		}
	}
}

Element.observe($(document), 'keydown', GlobalEvent.keydown.bindAsEventListener(GlobalEvent));
Element.observe($(document), 'keyup', GlobalEvent.keyup.bindAsEventListener(GlobalEvent));
Element.observe($(document), 'mousedown', GlobalEvent.mousedown.bindAsEventListener(GlobalEvent));
Element.observe($(document), 'mouseup', GlobalEvent.mouseup.bindAsEventListener(GlobalEvent));
Element.observe($(document), 'mouseover', GlobalEvent.mouseover.bindAsEventListener(GlobalEvent));
Element.observe($(document), 'mouseout', GlobalEvent.mouseout.bindAsEventListener(GlobalEvent));
Element.observe($(document), 'mousemove', GlobalEvent.mousemove.bindAsEventListener(GlobalEvent));
Element.observe($(window), 'scroll', GlobalEvent.scroll.bindAsEventListener(GlobalEvent));
Element.observe($(window), 'load', GlobalEvent.load.bindAsEventListener(GlobalEvent));

