/**
 * version 1.0
 * Script JS para armazenamento
 * e execução de funções que necessitam
 * ser executadas ao carregar da pagina.
 * Ex.: onload só pode receber 1 function.
 * Dynamix Software
 * Thyago Schleuss
 * thyago@dynamix.com.br
 */
function MyJsEvents( _alertOnloadEvents ) {
	
	MyJsEvents.ONLOAD = "load";
	MyJsEvents.ONUNLOAD = "unload";
	var obj = this;
	var myList = new List();

	// Método para inserir funções que
	// devem ser executadas ao carregar da pagina.
	// Public Method
	this.addOnLoadEvent = function( _aFunction ) {
		addEvent( _aFunction , MyJsEvents.ONLOAD );
	}

	// Método para inserir funções que devem ser
	// executadas ao fechar a pagina.
	// Public Method
	this.addOnUnloadEvent = function( _aFunction ) {
		addEvent( _aFunction , MyJsEvents.ONUNLOAD );
	}

	// Método que adiciona as funções e seus eventos
	// na lista.
	// Private Method
	var addEvent = function( _aFunction , _event ) {
		if( typeof ( _aFunction ) == "function" ) {
			myList.add( _aFunction , _event );
		} else {
			alert("Parâmetro 1 não é uma função!");
		}
	}

	// Método usado após registrar todas as suas funções.
	// Responsavel por percorrer a lista de funções
	// adicionadas para adicionar ao respectivo listener
	// Public Method
	this.triggerEvent = function() {
		var nl = myList.getPrim();
		while( nl != null ){
			addToListener( nl.getInfo() , nl.getEvent() );
			nl = nl.getProx();
		}
	}

	// Método responsavel por registrar a função na página
	// de acordo com o evento especificado
	// Private Method
	var addToListener = function( _aFunction , _event ) {
		if ( window.addEventListener ) {
			window.addEventListener( _event , _aFunction , false );
		} else if ( window.attachEvent ) {
			window.attachEvent( ( "on" + _event ) , _aFunction );
		}
	}

	// Implementação da lista de funções
	// usada para armazenar as funções e
	// os eventos que devem dispara-las
	function List () {
		
		var prim = null;

		// Método para inserir funções
		// na lista
		// Public Method
		this.add = function( _obj , _event ){
			var nl = new NoLista();
			nl.setInfo( _obj );
			nl.setEvent( _event );
			nl.setProx( prim );
			prim = nl;
		}

		// Método para verificar
		// se a lista esta vazia.
		// Public Method
		this.isEmpty = function(){
			if( prim == null ){
				return true;
			}else{
				return false;
			}
		}

		// Método que procura uma
		// função na lista, caso encontre
		// retorna a propria funcao
		// Public Method
		this.find = function( v ) {
			var nl = prim;
			while( nl != null ){
				if( nl.getInfo() == v ){
					return nl;
				}
				nl = nl.getProx();
			}
			return null;	
		}

		// Método que retorna o
		// tamanho da lista
		// Public Method
		this.size = function(){
			var c = 0;
			var nl = prim;
			while( nl != null ){
				c++;
				nl = nl.getProx();
			}
			return c;
		}

		// Método que retorna a ultima função
		// da lista
		// Public Method
		this.last = function(){
			var nl = prim;
			var u = null;
			while( nl != null ){
				if( nl.getProx() == null ){
					u = nl;
				}
				nl = nl.getProx();
			}
			return u;
		}

		// Método para remover uma função da lista
		// Public Method
		this.remove = function( v ){
			var ant =  null;
			var p = prim;
			while( ( p != null ) && ( p.getInfo() != v ) ){
				ant = p;
				p = p.getProx();
			}
			if( p == null ){
				return;
			}
			if( ant == null ){
				prim = p.getProx();
			}else{
				ant.setProx( p.getProx() );
			}
		}

		// Método que retorna o nodo
		// principal da lista
		// Public Method
		this.getPrim = function(){
			return prim;
		}

		// Método para limpar a lista
		// Public Method
		this.clear = function(){
			prim = null;
		}
	}

	// Nodos da lista.
	// Cada nodo é uma função com
	// seu respectivo evento
	function NoLista() 
	{
		var info = null;
		var event = null;
		var prox = null;
		
		this.getInfo = function() {
			return this.info;
		}

		this.setInfo = function( _info ) {
			this.info = _info;
		}

		this.getEvent = function() {
			return this.event;
		}

		this.setEvent = function( _event ) {
			this.event = _event;
		}

		this.getProx = function() {
			return this.prox;
		}

		this.setProx = function( _prox ) {
			this.prox = _prox;
		}
		
		this.toString = function(){
			return this.info;
		}
	}
}