/// class ProspectDirector.Modules.Common.FeaturedObjectEditorSidebar
/// using ProspectDirector.Modules.Common.FeaturedObjectEditor

ProspectDirector.Modules.Common.FeaturedObjectEditorSidebar = ProspectDirector.Modules.Common.FeaturedObjectEditor.extend({

    //---------------------------
    //--- CONSTRUCTOR
    //---------------------------

    init: function (options) {

        this._super(options)

        this.init_groups();
        this.init_splitter();
        this.sizeContent();

    },

    //---------------------------
    //--- PROPERTIES
    //---------------------------

    get_groups: function () {
        return this.get_container().find("UL[data-role=feature-group]");
    },

    get_splitter: function () {
        return this.get_container().find("DIV[data-role=featuredobject-splitter]");
    },

    get_settingsContent: function () {
        return this.get_container().find("[data-id=featuredobject-settings-content]");
    },

    //---------------------------
    //--- EVENT HANDLERS
    //---------------------------

    splitter_onResize: function () {
        this.get_groups().height(this.get_splitter().height() - 23);
    },

    groups_onChange: function (e) {

        var uid = $(e.sender.select()).data().uid,
            idx = this.get_groups().find("[data-uid=" + uid + "]").index(),
            featureContainer = this.get_container().find("div[data-role=feature-panel]:eq(" + idx + ")");

        this.get_container().find("div[data-role=feature-panel]").hide();

        featureContainer.show();

        this.get_featuredObject().selectFeature(featureContainer.find("DIV").first());

    },

    //---------------------------
    //--- INITIALIZERS
    //---------------------------

    init_groups: function () {

        var data = [],
            index = 0;

        for (index = 0; index < this.options.data.groups.length; index++) {
            data[data.length] = { name: this.options.data.groups[index] };
        }

        this.get_groups().kendoListView({
            dataSource: data,
            template: "<div data-role=\"group-name\" class=\"featuredobject-group\"><span>${name}</span></div>",
            selectable: true,
            change: $.proxy(this.groups_onChange, this)
        });

        this.get_groups().disableTextSelection();

        try {
            this.get_groups().data("kendoListView").select(this.get_groups().children().first());
        }
        catch (ex) {

        }

    },

    init_splitter: function () {

        this.get_splitter().kendoSplitter({
            orientation: "horizontal",
            panes: [
                { size: "150px", collapsible: true, scrollable: false },
                { scrollable: false }
            ]
        });

    },

    //---------------------------
    //--- METHODS
    //---------------------------

    sizeContent: function () {

        if (this.get_dialog().length > 0) {
            this.get_splitter().height(this.get_dialog().height() - 55);
            this.get_splitter().width(this.get_dialog().width() - 24);
        }

        this.get_splitter().data("kendoSplitter")._resize();
        this.get_groups().height(this.get_splitter().height() - 23);
        this.get_settingsContent().height(this.get_splitter().height() - 23);

    },

    refreshValidation: function () {

        $("[data-role=group-name] > span").each(function (idx, el) {

            var panel = $("DIV[data-role=feature-panel]:eq(" + idx + ")"),
                errors = panel.find(".validation-failure-label");

            if (errors.length > 0) {
                $(el).addClass("validation-failure-group");
            }
            else {
                $(el).removeClass("validation-failure-group");
            }

        });

    }

});