/// class PD.Manage.ModuleTabStrip
// using PD.Web.UI.Controls.TabStrip

PD.Manage.ModuleTabStrip =
PD.Web.UI.Controls.TabStrip.extend({

    //===================================
    // PROTECTED EVENT HANDLERS
    //===================================

    onComplete: function (e) {

        PD.context.get_global().get_events().subscribe("Module:Loaded", function (e) {
            var tab = this._createTab(e);
            this.addTab(tab);
        }, this);

        PD.context.get_global().get_events().subscribe("Module:Unloaded", function (e) {
            this.removeTab(e.data("id"));
        }, this);

        PD.context.get_global().get_events().subscribe("Module:Focused", function (e) {
            this._focusTab(e.data("id"));
        }, this);

    },

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

    findTabById: function (id) {

        var result = null;

        this.get_children().each(function (index, tab) {
            if (tab.get_moduleId() === id) {
                result = tab;
                return false;
            }
        });

        return result;

    },

    addTab: function (e) {
        this.get_children().insert(e, 0);
        this.refresh();
    },

    removeTab: function (id) {

        var tab = this.findTabById(id);

        if (tab) {
            this.get_children().remove(tab);
            this.refresh();
        }

    },

    //===================================
    // PRIVATE METHODS
    //===================================

    _createTab: function (data) {

        var result = null;

        result = new PD.Manage.ModuleTabStripTab({
            context: function () {
                return data;
            }
        });

        return result;

    },

    _focusTab: function (tabId) {

        var that = this;

        this.get_children().each(function (index, tab) {
            tab.get_node().removeClass("selected");
            if (tabId === tab.get_moduleId()) {
                tab.get_node().addClass("selected");
                that.scrollSelectedTabIntoView();
            }
        });

    }

});