﻿// event control
// 2005. 6. 16. by guoo.net
function VirtualKeyboard() {
	this.VK_1 = 49;
	this.VK_2 = 50;
	this.VK_3 = 51;
	this.VK_4 = 52;
	this.VK_5 = 53;
	this.VK_6 = 54;
	this.VK_7 = 55;
	this.VK_8 = 56;
	this.VK_9 = 57;
	this.VK_0 = 48;
	this.VK_A = 65;
	this.VK_B = 66;
	this.VK_C = 67;
	this.VK_D = 68;
	this.VK_E = 69;
	this.VK_F = 70;
	this.VK_G = 71;
	this.VK_H = 72;
	this.VK_I = 73;
	this.VK_J = 74;
	this.VK_K = 75;
	this.VK_L = 76;
	this.VK_M = 77;
	this.VK_N = 78;
	this.VK_O = 79;
	this.VK_P = 80;
	this.VK_Q = 81;
	this.VK_R = 82;
	this.VK_S = 83;
	this.VK_T = 84;
	this.VK_U = 85;
	this.VK_V = 86;
	this.VK_W = 87;
	this.VK_X = 88;
	this.VK_Y = 89;
	this.VK_Z = 90;
	this.VK_F1 = 112;
	this.VK_F2 = 113;
	this.VK_F3 = 114;
	this.VK_F4 = 115;
	this.VK_F5 = 116;
	this.VK_F6 = 117;
	this.VK_F7 = 118;
	this.VK_F8 = 119;
	this.VK_F9 = 120;
	this.VK_F10 = 121;
	this.VK_F11 = 122;
	this.VK_F12 = 123;
	this.VK_ENTER = 13;
	this.VK_SPACE = 10;
	this.VK_DELETE = 46;
	this.VK_BACKSPACE = 8;
	this.VK_LEFT = 37;
	this.VK_RIGHT = 39;
	this.VK_UP = 38;
	this.VK_DOWN = 40;
	this.VK_LEFT_BRACKET = 219;
	this.VK_RIGHT_BRACKET = 221;
}
var keyboard = new VirtualKeyboard();


// global event start
var keydownEventFunctions = [];
var keyupEventFunctions = [];
var mousedownEventFunctions = [];
var mouseupEventFunctions = [];
var mouseoverEventFunctions = [];
var mousemoveEventFunctions = [];
var scrollEventFunctions = [];

function CustomEvent(id, pFunction, sequence) {
	this.id = id;
	this.pFunction = pFunction;
	if(!sequence) this.sequence = 0;
	this.sequence = sequence;
	this.args = [];
	if(arguments.length > 3) {
		var count = 0;
		for(var i=3; i<arguments.length; i++) {
			this.args[count] = arguments[i];
			count++;
		}
	}
}

function mousedownEvent() {
	var object;
	var args = '';
	var functionCall;
	for(var i=0; i<mousedownEventFunctions.length; i++) {
		object = mousedownEventFunctions[i];
		if(typeof(object)!='undefined') {
			for(var j=0; j<object.args.length; j++) {
				if(args) args += ',';
				args += 'object.args['+j+']';
			}
			functionCall = 'object.pFunction('+args+');';
			eval(functionCall);
		}
	}
}

function addMousedownEvent(customEvent) {
	for(var i=0; i<mousedownEventFunctions.length; i++) if(mousedownEventFunctions[i].id==customEvent.id) return;
	mousedownEventFunctions[mousedownEventFunctions.length] = customEvent;
	var dontcare;
	for(var i=0; i<mousedownEventFunctions.length; i++) {
		for(var j=1; j<mousedownEventFunctions.length; j++) {
			if(mousedownEventFunctions[i].sequence < mousedownEventFunctions[j].sequence) {
				dontcare = mousedownEventFunctions[i];
				mousedownEventFunctions[i] = mousedownEventFunctions[j];
				mousedownEventFunctions[j] = dontcare;
			}
		}
	}
}
function removeMousedownEvent(id) {
	var tempArray;
	for(var i=0; i<mousedownEventFunctions.length; i++) {
		if(mousedownEventFunctions[i].id==id) {
			tempArray = mousedownEventFunctions.slice(0, i);
			tempArray = tempArray.concat(mousedownEventFunctions.slice(i+1));
			mousedownEventFunctions = tempArray;
		}
	}
}

function mousemoveEvent() {
	var object;
	var args = '';
	var functionCall;
	for(var i=0; i<mousemoveEventFunctions.length; i++) {
		object = mousemoveEventFunctions[i];
		if(typeof(object)!='undefined') {
			for(var j=0; j<object.args.length; j++) {
				if(args) args += ',';
				args += 'object.args['+j+']';
			}
			functionCall = 'object.pFunction('+args+');';
			if(eval(functionCall)==false) return;
		}
	}
}

function addMousemoveEvent(customEvent) {
	for(var i=0; i<mousemoveEventFunctions.length; i++) if(mousemoveEventFunctions[i].id==customEvent.id) return;
	mousemoveEventFunctions[mousemoveEventFunctions.length] = customEvent;
	var dontcare;
	for(var i=0; i<mousemoveEventFunctions.length; i++) {
		for(var j=1; j<mousemoveEventFunctions.length; j++) {
			if(mousemoveEventFunctions[i].sequence < mousemoveEventFunctions[j].sequence) {
				dontcare = mousemoveEventFunctions[i];
				mousemoveEventFunctions[i] = mousemoveEventFunctions[j];
				mousemoveEventFunctions[j] = dontcare;
			}
		}
	}
}
function removeMousemoveEvent(id) {
	var tempArray;
	for(var i=0; i<mousemoveEventFunctions.length; i++) {
		if(mousemoveEventFunctions[i].id==id) {
			tempArray = mousemoveEventFunctions.slice(0, i);
			tempArray = tempArray.concat(mousemoveEventFunctions.slice(i+1));
			mousemoveEventFunctions = tempArray;
		}
	}
}

function mouseupEvent() {
	var object;
	var args = '';
	var functionCall;
	for(var i=0; i<mouseupEventFunctions.length; i++) {
		object = mouseupEventFunctions[i];
		if(typeof(object)!='undefined') {
			for(var j=0; j<object.args.length; j++) {
				if(args) args += ',';
				args += 'object.args['+j+']';
			}
			functionCall = 'object.pFunction('+args+');';
			if(eval(functionCall)==false) return;
		}
	}
}

function addMouseupEvent(customEvent) {
	for(var i=0; i<mouseupEventFunctions.length; i++) if(mouseupEventFunctions[i].id==customEvent.id) return;
	mouseupEventFunctions[mouseupEventFunctions.length] = customEvent;
	var dontcare;
	for(var i=0; i<mouseupEventFunctions.length; i++) {
		for(var j=1; j<mouseupEventFunctions.length; j++) {
			if(mouseupEventFunctions[i].sequence < mouseupEventFunctions[j].sequence) {
				dontcare = mouseupEventFunctions[i];
				mouseupEventFunctions[i] = mouseupEventFunctions[j];
				mouseupEventFunctions[j] = dontcare;
			}
		}
	}
}
function removeMouseupEvent(id) {
	var tempArray;
	for(var i=0; i<mouseupEventFunctions.length; i++) {
		if(mouseupEventFunctions[i].id==id) {
			tempArray = mouseupEventFunctions.slice(0, i);
			tempArray = tempArray.concat(mouseupEventFunctions.slice(i+1));
			mouseupEventFunctions = tempArray;
		}
	}
}

function mouseoverEvent() {
	var object;
	var args = '';
	var functionCall;
	for(var i=0; i<mouseoverEventFunctions.length; i++) {
		object = mouseoverEventFunctions[i];
		if(typeof(object)!='undefined') {
			for(var j=0; j<object.args.length; j++) {
				if(args) args += ',';
				args += 'object.args['+j+']';
			}
			functionCall = 'object.pFunction('+args+');';
			if(eval(functionCall)==false) return;
		}
	}
}

function addMouseoverEvent(customEvent) {
	for(var i=0; i<mouseoverEventFunctions.length; i++) if(mouseoverEventFunctions[i].id==customEvent.id) return;
	mouseoverEventFunctions[mouseoverEventFunctions.length] = customEvent;
	var dontcare;
	for(var i=0; i<mouseoverEventFunctions.length; i++) {
		for(var j=1; j<mouseoverEventFunctions.length; j++) {
			if(mouseoverEventFunctions[i].sequence < mouseoverEventFunctions[j].sequence) {
				dontcare = mouseoverEventFunctions[i];
				mouseoverEventFunctions[i] = mouseoverEventFunctions[j];
				mouseoverEventFunctions[j] = dontcare;
			}
		}
	}
}
function removeMouseoverEvent(id) {
	var tempArray;
	for(var i=0; i<mouseoverEventFunctions.length; i++) {
		if(mouseoverEventFunctions[i].id==id) {
			tempArray = mouseoverEventFunctions.slice(0, i);
			tempArray = tempArray.concat(mouseoverEventFunctions.slice(i+1));
			mouseoverEventFunctions = tempArray;
		}
	}
}

function keydownEvent() {
	var object;
	var args = '';
	var functionCall;
	for(var i=0; i<keydownEventFunctions.length; i++) {
		object = keydownEventFunctions[i];
		if(typeof(object)!='undefined') {
			for(var j=0; j<object.args.length; j++) {
				if(args) args += ',';
				args += 'object.args['+j+']';
			}
			functionCall = 'object.pFunction('+args+');';
			if(eval(functionCall)==false) return;
		}
	}
}
function addKeydownEvent(customEvent) {
	for(var i=0; i<keydownEventFunctions.length; i++) if(keydownEventFunctions[i].id==customEvent.id) return;
	keydownEventFunctions[keydownEventFunctions.length] = customEvent;
	var dontcare;
	for(var i=0; i<keydownEventFunctions.length; i++) {
		for(var j=1; j<keydownEventFunctions.length; j++) {
			if(keydownEventFunctions[i].sequence < keydownEventFunctions[j].sequence) {
				dontcare = keydownEventFunctions[i];
				keydownEventFunctions[i] = keydownEventFunctions[j];
				keydownEventFunctions[j] = dontcare;
			}
		}
	}
}
function removeKeydownEvent(id) {
	var tempArray;
	for(var i=0; i<keydownEventFunctions.length; i++) {
		if(keydownEventFunctions[i].id==id) {
			tempArray = keydownEventFunctions.slice(0, i);
			tempArray = tempArray.concat(keydownEventFunctions.slice(i+1));
			keydownEventFunctions = tempArray;
		}
	}
}
function keyupEvent() {
	var object;
	var args = '';
	var functionCall;
	for(var i=0; i<keyupEventFunctions.length; i++) {
		object = keyupEventFunctions[i];
		if(typeof(object)!='undefined') {
			for(var j=0; j<object.args.length; j++) {
				if(args) args += ',';
				args += 'object.args['+j+']';
			}
			functionCall = 'object.pFunction('+args+');';
			if(eval(functionCall)==false) return;
		}
	}
}
function addKeyupEvent(customEvent) {
	for(var i=0; i<keyupEventFunctions.length; i++) if(keyupEventFunctions[i].id==customEvent.id) return;
	keyupEventFunctions[keyupEventFunctions.length] = customEvent;
	var dontcare;
	for(var i=0; i<keyupEventFunctions.length; i++) {
		for(var j=1; j<keyupEventFunctions.length; j++) {
			if(keyupEventFunctions[i].sequence < keyupEventFunctions[j].sequence) {
				dontcare = keyupEventFunctions[i];
				keyupEventFunctions[i] = keyupEventFunctions[j];
				keyupEventFunctions[j] = dontcare;
			}
		}
	}
}
function removeKeyupEvent(id) {
	var tempArray;
	for(var i=0; i<keyupEventFunctions.length; i++) {
		if(keyupEventFunctions[i].id==id) {
			tempArray = keyupEventFunctions.slice(0, i);
			tempArray = tempArray.concat(keyupEventFunctions.slice(i+1));
			keyupEventFunctions = tempArray;
		}
	}
}

function scrollEvent() {
	var object;
	var args = '';
	var functionCall;
	for(var i=0; i<scrollEventFunctions.length; i++) {
		object = scrollEventFunctions[i];
		if(typeof(object)!='undefined') {
			for(var j=0; j<object.args.length; j++) {
				if(args) args += ',';
				args += 'object.args['+j+']';
			}
			functionCall = 'object.pFunction('+args+');';
			if(eval(functionCall)==false) return;
		}
	}
}
function addScrollEvent(customEvent) {
	for(var i=0; i<scrollEventFunctions.length; i++) if(scrollEventFunctions[i].id==customEvent.id) return;
	scrollEventFunctions[scrollEventFunctions.length] = customEvent;
	var dontcare;
	for(var i=0; i<scrollEventFunctions.length; i++) {
		for(var j=1; j<scrollEventFunctions.length; j++) {
			if(scrollEventFunctions[i].sequence < scrollEventFunctions[j].sequence) {
				dontcare = scrollEventFunctions[i];
				scrollEventFunctions[i] = scrollEventFunctions[j];
				scrollEventFunctions[j] = dontcare;
			}
		}
	}
}
function removeScrollEvent(id) {
	var tempArray;
	for(var i=0; i<scrollEventFunctions.length; i++) {
		if(scrollEventFunctions[i].id==id) {
			tempArray = scrollEventFunctions.slice(0, i);
			tempArray = tempArray.concat(scrollEventFunctions.slice(i+1));
			scrollEventFunctions = tempArray;
		}
	}
}

if(window.Event) document.addEventListener('mousedown', function(e) { window.event = e; }, true);
if(window.Event) document.addEventListener('mouseup', function(e) { window.event = e; }, true);
if(window.Event) document.addEventListener('mousemove', function(e) { window.event = e; }, true);
if(window.Event) document.addEventListener('mouseover', function(e) { window.event = e; }, true);
if(window.Event) document.addEventListener('keydown', function(e) { window.event = e; }, true);
if(window.Event) document.addEventListener('keyup', function(e) { window.event = e; }, true);
document.onkeydown = keydownEvent;
document.onkeyup = keyupEvent;
document.onmousedown = mousedownEvent;
document.onmouseup = mouseupEvent;
document.onmousemove = mousemoveEvent;
document.onmouseover = mouseoverEvent;
