/// class PD.Events.EventContext

PD.Events.EventContext =
Class.extend({

    //===================================
    // FIELDS
    //===================================

    _subscriptionMap: null,

    //===================================
    // CONSTRUCTOR
    //===================================

    init: function () {
        this._subscriptionMap = {};
    },

    //===================================
    // PUBLIC METHODS
    //===================================

    subscribe: function (eventName, callback, context, subscriber) {

        if (!this._subscriptionMap.hasOwnProperty(eventName)) {
            this._subscriptionMap[eventName] = [];
        }

        this._subscriptionMap[eventName].push({
            callback: callback,
            context: context !== "undefined" ? context : null,
            subscriber: subscriber !== "undefined" ? subscriber : null
        });

    },

    unsubscribe: function (eventName, callback) {

        var eventMap = this._subscriptionMap[eventName];

        if (eventMap) {
            for (var i = 0; i < eventMap.length; i++) {
                if (callback == eventMap[i].callback) {
                    eventMap.splice(i, 1);
                    break;
                }
            }
        }

    },

    publish: function (eventName, args) {

        var eventMap = this._subscriptionMap[eventName];

        if (eventMap) {
            for (var i = 0; i < eventMap.length; i++) {
                var context = eventMap[i].context;
                var callback = eventMap[i].callback;
                if (callback) {
                    callback.call(context, args);
                }
            }
        }

    }

});