/// class ProspectDirector.Web.UI.FeatureSets.Layouts.TabStripLayout

/**
@class TabStripLayout
@namespace Web.UI.FeatureSets.Layouts
@constructor
@extends Web.UI.FeatureSets.Layouts.Layout
**/
ProspectDirector.Web.UI.FeatureSets.Layouts.TabStripLayout = ProspectDirector.Web.UI.FeatureSets.Layouts.Layout.extend({

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

    init: function (options) {
        this._super(options);
        this.get_eventBus().subscribe("validationErrors", $.proxy(this._onValidationErrors, this));
    },

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

    _onGroupSelected: function (id) {
        this._selectAssembler(id);
    },

    _onValidationErrors: function (e, data) {

        var errors = data.validationErrors;

        this.DOM().showGroupValidationErrors(errors.getGroupIds());

        this.get_assemblers().each(function (index, assembler) {
            assembler.setValidationErrors(errors.getByGroupId(assembler.get_groupId()));
        });

    },

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

    DOM: function () {

        if (!this._DOM) {
            this._DOM = new ProspectDirector.Web.UI.FeatureSets.Layouts.TabStripLayoutDOM({
                containerID: this.get_options().containerId,
                groupSelected: $.proxy(this._onGroupSelected, this)
            });
        }

        return this._DOM;

    },

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

    resize: function (e) {

        this.get_assemblers().each(function (index, assembler) {
            assembler.resize();
        });

    },

    render: function () {

        var that = this,
            groups = new ProspectDirector.Web.List();

        this.get_featureSet().get_groups().each(function (index, group) {

            var assembler = that.getAssemblerByGroupId(group.get_id());

            groups.add({
                id: group.get_id(),
                content: assembler.render()
            });

        });

        return this.DOM().render(groups);

    },

    activate: function () {

        var assembler = this.get_assemblers().getAtIndex(0);

        this._populateTabStrip();

        if (assembler) {
            assembler.activate();
        }

    },

    reload: function (featureSet) {
        this._super(featureSet);
        this.DOM().clear();
        this._populateSideBar();
    },

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

    _populateTabStrip: function () {

        var data = new ProspectDirector.Web.List();

        this.get_featureSet().get_groups().each(function (index, group) {
            data.add({
                id: group.get_id(),
                name: group.get_name()
            });
        });

        this.DOM().populateTabStrip(data.toArray());

    },

    _selectAssembler: function (id) {

        var assembler = this.getAssemblerByGroupId(id);

        this.DOM().clearAssemblerSelections(id);

        if (assembler) {
            assembler.focus();
        }

        this.DOM().applyAssemblerSelection(id);

    }

}); 