Event = {};

Event.list = [];
Event.anonFunc = [];

Event.asFunc = function(func) {
	if ( func && typeof func == "string" ) {
		eval ("func = function (e) { "+func+" }");
		for ( var i = 0 ; i < Event.anonFunc.length; i++ )
			if ( Event.anonFunc[i].toString() == func.toString() )
				return Event.anonFunc[i];
		Event.anonFunc[Event.anonFunc.length] = func;
	}
	return func;
}

Event.add = function(evtype, tgt, func) {
	tgt = getElem(tgt);
	if ( ! tgt ) return;
	func = Event.asFunc(func);
	if ( navigator.appVersion.toLowerCase().indexOf('msie') >= 0 )
		tgt.attachEvent('on'+evtype, func);
	else
		tgt.addEventListener(evtype, func, true);
	Event.list[Event.list.length] = {
		'evtype': evtype, 'tgt': tgt, 'func': func
	}
}

Event.remove = function(evtype, tgt, func) {
	tgt = getElem(tgt);
	func = Event.asFunc(func);
	for ( var i = 0 ; i < Event.list.length ; i ++ ) {
		if (( evtype ? Event.list[i]['evtype'] == evtype : 1 ) &&
			( tgt ? Event.list[i]['func'] == func &&
					Event.list[i]['tgt'] == tgt : 1 ) ) {
			if ( navigator.appVersion.toLowerCase().indexOf('msie') >= 0 )
				Event.list[i]['tgt'].detachEvent('on'+evtype, Event.list[i]['func']);
			else
				Event.list[i]['tgt'].removeEventListener(evtype, Event.list[i]['func'], true);
			Event.list[i]['tgt'] = null;
			Event.list.splice(i, 1);
			i --;
		}
	}
}

Event.cleanup = function() {
	Event.remove();
}

Event.target = function(e) {
	if ( window.event )
		e = window.event;
	var elem = (e.srcElement||e.target);
	return elem;
}

Event.get = function (e) {
	if ( window.event )
		e = window.event;
	return e;
}

Event.stop = function(e) {
	if ( window.event )
		e = window.event;
	e.cancelBubble = true;
	if ( e.stopPropagation ) e.stopPropagation();
}

Event.add('unload', window, function () { Event.cleanup(); } );


