/// class PD.Web.UI.EventBus

PD.Web.UI.EventBus = Class.extend({

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

    _eventSubscriberMap: null,

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

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

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

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

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

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

    },

    unsubscribe: function (eventName, callback) {

        var eventMap = this._eventSubscriberMap[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) {
        
        //if (console) {
        //    console.log(eventName);
        //}
        
        var eventMap = this._eventSubscriberMap[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);
                }
            }
        }

    }

});