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

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

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

    _groupId: null,

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

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

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

    _onGroupSelected: function (id) {
        this._selectAssembler(id);
        this._groupId = 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.SideBarLayoutDOM({
                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._populateSideBar();

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

    },

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

    getSelectedGroupId: function () {
        return this._groupId;
    },

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

    _populateSideBar: 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().populateSidebar(data.toArray());

        this.DOM().selectGroup(this._groupId);
        
    },

    _selectAssembler: function (id) {

        var assembler = this.getAssemblerByGroupId(id);

        this.DOM().clearAssemblerSelections(id);

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

        this.DOM().applyAssemblerSelection(id);

    }

}); 
