//======================================================
//EventUtility
//======================================================
function EventUtility(){
	this.keyCode = {
		'enter': 13
	}

	this.getEvent = EventUtility.prototype.GetEvent;
	this.addEventListener = EventUtility.prototype.AddEventListener;
	this.addEventListenerFunctionList = EventUtility.prototype.AddEventListenerFunctionList;
}

//======================================================
//Get Event
//======================================================
EventUtility.prototype.GetEvent = function (e) {
	if (e != null && window.event == null){
		_event = e;
		_event.srcElement = _event.target;
		_event.offsetX = _event.pageX;
		_event.offsetY = _event.pageY;
	}
	else {
		_event = window.event;
		if (!EventUtility.prototype.IsOpera()) {
			_event.pageX = _event.offsetX;
			_event.pageY = _event.offsetY;
		}
	}
	return _event;
}

EventUtility.prototype.GetCurrentElement = function (e, _this) {
	var _event = EventUtility.prototype.GetEvent(e);
	if (_this == null || _this == undefined) {
		return _event.srcElement;
	}
	else {
		return _this;
	}
}

EventUtility.prototype.GetEventListenerType = function (element){
	if(element.attachEvent) {
		return 'Attach';
	}
	else if(element.addEventListener) {
		return 'Add';
	}
	else {
		return 'Substitution';
	}
}

//======================================================
//Add Event
//======================================================
EventUtility.prototype.AddEventListenerFromList = function (element, add_event_list) {
	var event_listener_type = EventUtility.prototype.GetEventListenerType(element);
	for (var event_type in add_event_list) {
		EventUtility.prototype.AddEventListener(element, event_type, add_event_list[event_type], event_listener_type);
	}
}

EventUtility.prototype.AddEventListener = function (element, event_type, call_back_func, event_listener_type) {
	var event_listener_type = (event_listener_type != null) ? event_listener_type : EventUtility.prototype.GetEventListenerType(element);
	EventUtility.prototype.AddEventListenerFunctionList[event_listener_type](element, event_type, call_back_func);
}

EventUtility.prototype.AddEventListenerFunctionList = {
	'Attach': function (element, event_type, call_back_func) {
		element.attachEvent('on'+ event_type, call_back_func);
	},
	'Add': function (element, event_type, call_back_func) {
		element.addEventListener(event_type, call_back_func, false);
	},
	'Substitution': function (element, event_type, call_back_func) {
		element['on'+ event_type] = call_back_func;
	}
}

//======================================================
//Remove Event
//======================================================
EventUtility.prototype.RemoveEventListenerFromList = function (element, remove_event_list) {
	var event_listener_type = EventUtility.prototype.GetEventListenerType(element);
	for (var event_type in remove_event_list) {
		EventUtility.prototype.RemoveEventListener(element, event_type, remove_event_list[event_type], event_listener_type);
	}
}

EventUtility.prototype.RemoveEventListener = function (element, event_type, call_back_func, event_listener_type) {
	var event_listener_type = (event_listener_type != null) ? event_listener_type : EventUtility.prototype.GetEventListenerType(element);
	EventUtility.prototype.RemoveEventListenerFunctionList[event_listener_type](element, event_type, call_back_func);
}

EventUtility.prototype.RemoveEventListenerFunctionList = {
	'Attach': function (element, event_type, call_back_func) {
		element.detachEvent('on'+ event_type, call_back_func);
	},
	'Add': function (element, event_type, call_back_func) {
		element.removeEventListener(event_type, call_back_func, false);
	},
	'Substitution': function (element, event_type, call_back_func) {
		element['on'+ event_type] = null;
	}
}

//======================================================
//Cancel Event
//======================================================
EventUtility.prototype.CancelBubble = function (_event) {
	if (document.all) {
		window.event.cancelBubble = true;
		return false;
	}
	_event.stopPropagation();
	return false;
}

EventUtility.prototype.CancelDefaultEvent = function (_event) {
	if (document.all) {
		window.event.returnValue = false;
		return false;
	}
	_event.preventDefault();
	return false;
}

EventUtility.prototype.CancelEvent = function (_event) {
	EventUtility.prototype.CancelBubble(_event);
	EventUtility.prototype.CancelDefaultEvent(_event);
	return false;
}

//======================================================
//Judge Browser
//======================================================
EventUtility.prototype.IsOpera = function () {
	return (window.opera != null);
}

EventUtility.prototype.IsIE = function () {
	return (navigator.appName == "Microsoft Internet Explorer");
}

EventUtility.prototype.GetIEVersion = function () {
	var ret = new String(navigator.appVersion).match(/MSIE ([0-9]+(:?\.[0-9]+)*)/);
	return (ret == null) ? null : ret[1];
}

//======================================================
//etc
//======================================================
EventUtility.prototype.IsIMEFixation = function (_event) {
	return (_event.keyCode == 13);
}
