// class EventRegister
//		Tutto in nome della concorrenza multi-oggetto. Per fare in modo che eventi globali come
//	document.onmouseup possano essere utilizzati da piu oggetti, occorre che non siano collegati
//	direttamente all'evento, ma attraverso una sorta di switch di eventi (come un switch di rete).
//	Quindi questa classe registra l'handle da richiamare per tutti coloro che richiedono l'evento,
// poi, quando l'evento relativo viene lanciato, passa in rassegna tutte le funzioni registrate
// lanciandole con l'argomento dell'evento.
//
// L'oggetto EVENT_REGISTER e' una super variabile globale che viene utilizzata sempre, in tutti i
// progetti, qualora ci si ritrovi a dover usare degli eventi globali del documento.

if(typeof EVENT_REGISTER=="undefined")
{
	function EventRegister(){
		var mouseup_handlers=new Array();
		var mousedown_handlers=new Array();
		var mousemove_handlers=new Array();
		var keydown_handlers=new Array();
		var keyup_handlers=new Array();
		var keypress_handlers=new Array();
		var click_handlers=new Array();
		var load_handlers=new Array();
		var unload_handlers=new Array();
		var blur_handlers=new Array();
		var mouseover_handlers=new Array();
		var mouseout_handlers=new Array();
		var change_handlers=new Array();
		var focus_handlers=new Array();
		
		function getArray(event_string){
			if(event_string=="onmouseup" || event_string=="mouseup")
				return mouseup_handlers;
			if(event_string=="onmousedown" || event_string=="mousedown")
				return mousedown_handlers;
			if(event_string=="onmousemove" || event_string=="mousemove")
				return mousemove_handlers;
			if(event_string=="onkeydown" || event_string=="keydown")
				return keydown_handlers;
			if(event_string=="onkeyup" || event_string=="keyup")
				return keyup_handlers;
			if(event_string=="onkeypress" || event_string=="keypress")
				return keypress_handlers;
			if(event_string=="onclick" || event_string=="click")
				return click_handlers;
			if(event_string=="onload" || event_string=="load")
				return load_handlers;
			if(event_string=="onunload" || event_string=="unload")
				return unload_handlers;
			if(event_string=="onblur" || event_string=="blur")
				return blur_handlers;
			if(event_string=="onmouseover" || event_string=="mouseover")
				return mouseover_handlers;
			if(event_string=="onmouseout" || event_string=="onmouseout")
				return mouseout_handlers;
			if(event_string=="onchange" || event_string=="onchange")
				return change_handlers;
			if(event_string=="onfocus" || event_string=="focus")
				return focus_handlers;
		};
		
		this.registered=function(event_string,func){
			var buffer=getArray(event_string);
			for(i=0;i<buffer.length;i++)
			{
				if(buffer[i]===func)
					return i;
			}
			return -1;
		};
		
		this.register=function(event_string,func){
			var buffer=getArray(event_string);
			if(buffer)
			{
				if(this.registered(event_string,func)<0)
					buffer.push(func);
			}
		};
		
		this.unregister=function(event_string,func){
			var buffer=getArray(event_string);
			if((index=this.registered(event_string,func))>=0)
				buffer.remove(index);
		};
		
		function fire(event_string,ev){
			var buffer=getArray(event_string);
			if(buffer.length<=0)
				return;
			
			if(!ev)	// IE
				ev=window.event;
			
			for(var i=0;i<buffer.length;i++)
			{
				if(typeof buffer[i]=="function")
					buffer[i](ev);
			}
		};

		/** WRAPPERS **/
		this.fireMouseUp=function(ev){ fire("onmouseup",ev); };
		this.fireMouseDown=function(ev){ fire("onmousedown",ev); };
		this.fireMouseMove=function(ev){ fire("onmousemove",ev); };
		this.fireKeyDown=function(ev){ fire("onkeydown",ev); };
		this.fireKeyUp=function(ev){ fire("onkeyup",ev); };
		this.fireKeyPress=function(ev){ fire("onkeypress",ev); };
		this.fireClick=function(ev){ fire("onclick",ev); };
		this.fireLoad=function(ev){ fire("onload",ev); };
		this.fireUnload=function(ev){ fire("onunload",ev); };
		this.fireBlur=function(ev){ fire("onblur",ev); };
		this.fireMouseOver=function(ev){ fire("onmouseover",ev); };
		this.fireMouseOut=function(ev){ fire("onmouseout",ev); };
		this.fireChange=function(ev){ fire("onchange",ev); };
		this.fireFocus=function(ev){ fire("onfocus",ev); };
	}

	var EVENT_REGISTER=new EventRegister();	// registratore di eventi comuni

	document.onmouseup=function(ev){ EVENT_REGISTER.fireMouseUp(ev); };
	document.onmousedown=function(ev){ EVENT_REGISTER.fireMouseDown(ev); };
	document.onmousemove=function(ev){ EVENT_REGISTER.fireMouseMove(ev); };
	document.onkeydown=function(ev){ EVENT_REGISTER.fireKeyDown(ev); };
	document.onkeyup=function(ev){ EVENT_REGISTER.fireKeyUp(ev); };
	document.onkeypress=function(ev){ EVENT_REGISTER.fireKeyPress(ev); };
	document.onclick=function(ev){ EVENT_REGISTER.fireClick(ev); };
	window.onload=function(ev){ EVENT_REGISTER.fireLoad(ev); };
	window.onunload=function(ev){ EVENT_REGISTER.fireUnload(ev); };
	document.onblur=function(ev){ EVENT_REGISTER.fireBlur(ev); };
	document.onmouseover=function(ev){ EVENT_REGISTER.fireMouseOver(ev); };
	document.onmouseout=function(ev){ EVENT_REGISTER.fireMouseOut(ev); };
	document.onfocus=function(ev){ EVENT_REGISTER.fireFocus(ev); };
}
