﻿// JScript File
//Internal argument for an event handler, basically an instance of a delegate
EventHandlerArg = function(instance, handler)
{
    this.Instance = instance;
    this.Handler = handler;
}
//Event handler mimics a multicast delegate
EventHandler = function() {
    this.Events = new Array();
    this.Cancel = false;
    this.Add = function(instance, handler) {
        this.Events.push(new EventHandlerArg(instance, handler));
    };
    this.Remove = function(handler) {
        for (var oEventInd = 0; oEventInd < this.Events.length; oEventInd++) {
            if (this.Events[oEventInd].Handler == handler) {
                this.Events.splice(oEventInd, 1);
            }
        }
    };
    this.OnEvent = function() {
        this.Cancel = false;
        for (var oEventInd = 0; oEventInd < this.Events.length; oEventInd++) {
            if (this.Cancel) {
                if (arguments[1]) {
                    arguments[1].Cancel = true;
                }
                break;
            }
            var EventArg = this.Events[oEventInd];
            EventArg.Handler.call(EventArg.Instance, arguments, this);
        }
    };
    this.Clear = function() {
        delete this.Events;
        this.Events = new Array();
    };
}



