var EventManager = new function ()
{
    this.initialize = function()
    {
        if (this._registry == null)
        {
            this._registry = [];

            // Register the cleanup handler on page unload.
            this.add(window, "unload", this.cleanUp);
        }
    }

    this.add = function(obj, type, fn, useCapture)
    {
        // W3C
        if (obj.addEventListener)
        {
           obj.addEventListener(type, fn, useCapture);
           this._registry.push({obj: obj, type: type, fn: fn, useCapture: useCapture});
           return true;
        }

        // IE
        if (obj.attachEvent && obj.attachEvent('on' + type, fn))
        {
           this._registry.push({obj: obj, type: type, fn: fn, useCapture: false});
           return true;
        }

        return false;
    }

    this.cleanUp = function()
    {
        for (var i = 0; i < this._registry.length; i++)
        {
            with (EventManager._registry[i])
            {
                // Mozilla/W3C listeners?
                if (obj.removeEventListener)
                    obj.removeEventListener(type, fn, useCapture);

                // IE-style listeners?
                else if (obj.detachEvent)
                    obj.detachEvent('on' + type, fn);
            }
        }

        this._registry = new Array ();
    }

    this._registry = new Array ();
    this.initialize ();
}