﻿var functionList = {}

///
///
///
function EventClass()
{
    this.sender = null;
    this.params = null;
    this.functionCall = null;
    this.eventType = null;
}

///
///
///
function EventListenerClass()
{
    this.functionName = "";
    this.callbackFunction = null;
    this.classObject = null;
}

///
///
///
function EventManagerClass()
{
}


///
EventManagerClass.prototype.CreateEvent = function(eventType, sender, params, functionCall)
{
    var eventObject = new EventClass();
    eventObject.sender = sender;
    eventObject.params = params;
    eventObject.functionCall = functionCall;
    eventObject.eventType = eventType;
    
    return eventObject;
}

///
///
///
EventManagerClass.prototype.CreateEventListener = function(functionName, callbackFunction, classObject)
{
    with (this)
    {
        var eventListener = new EventListenerClass();
        eventListener.functionName = functionName;
        //
        eventListener.callbackFunction = callbackFunction
        eventListener.classObject = classObject;
        
        return eventListener;
    }
}

///
///
///
EventManagerClass.prototype.AddListener = function(eventType, listenerObject)
{
    with (this)
    {
        if (functionList[eventType] == null)
            functionList[eventType] = new Array();

        functionList[eventType].push(listenerObject);
    }
}

///
///
///
EventManagerClass.prototype.RemoveListener = function(eventType, name)
{
    with (this)
    {
        var functionListArray = functionList[eventType];
        
        if (functionListArray)
            for(var i=0; i<functionListArray.length; i++)
            {
                if (functionListArray[i].functionName == name)
                {
                    functionListArray.splice(i, 1);     //needs optimization
                    i--;
                }
            }
    }
}
    
///
///
///
EventManagerClass.prototype.Notify = function(eventObject)
{
    with (this)
    {
        var functionListArray = functionList[eventObject.eventType];
        if (functionListArray != null)
        {
            for (var i=0; i<functionListArray.length; i++)
            {
                //try
                //{
                    if (functionListArray[i].callbackFunction(eventObject, functionListArray[i].classObject))
                        return;
                //}
                //catch(e){}
            }
        }
    }
}

EventManager = new EventManagerClass();
