/// class ProspectDirector.Modules.Processes.ProcessNodes.FeatureHiddenFolders
/// using ProspectDirector.Types.Core.Feature

ProspectDirector.Modules.Processes.ProcessNodes.FeatureHiddenFolders = ProspectDirector.Types.Core.Feature.extend({

    init: function (options) {

        this._super(options);

        this.init_tree();

        this.get_hiddenFolders().val(JSON.stringify(this.options.data.hiddenFolders));

    },

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

    get_tree: function () {
        return this.get_container().find("[data-role=tree]");
    },

    get_hiddenFolders: function () {
        return this.get_container().find("[data-id=hiddenFolders]");
    },

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

    init_tree: function () {

        this.get_tree().kendoTreeView({
            dataSource: new kendo.data.HierarchicalDataSource({
                transport: {
                    read: $.proxy(function (settings) {

                        PD.ajax({
                            url: ProspectDirector.RUNTIME.resolveUrl("~/EntityType/GetExplorerModel"),
                            data: {
                                typeId: this.options.data.entityTypeId,
                                id: settings.data.id === undefined ? this.options.data.rootFolderId : settings.data.id
                            },
                            success: function (result) {
                                settings.success(result);
                            }
                        });

                    }, this)
                },
                schema: {
                    model: {
                        id: "id",
                        hasChildren: "hasChildren"
                    }
                }
            }),
            dataBound: $.proxy(this.tree_onDataBound, this),
            select: $.proxy(this.tree_onSelect, this)
        });

        this.get_tree().on("click", "INPUT[type=checkbox]", $.proxy(this.tree_onCheckBoxCheck, this));

    },

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

    tree_onDataBound: function (e) {

        var tree = this.get_tree().data("kendoTreeView"),
            data = null,
            index = 0;

        if (e.node === undefined || e.node.length === 0) {
            data = e.sender.dataSource._data;
        }
        else {
            data = tree.dataItem(e.node).children._data;
        }

        if (data !== undefined) {

            for (index = 0; index < data.length; index++) {
                this.nodeTemplate(data[index]);
            }

        }

    },

    tree_onSelect: function (e) {
        e.preventDefault();
    },

    tree_onCheckBoxCheck: function (e) {

        var checkbox = $(e.target),
            node = checkbox.closest("LI.k-item"),
            tree = this.get_tree().data("kendoTreeView"),
            data = tree.dataItem(node),
            hidden = this.options.data.hiddenFolders,
            hiddenIndex = this.isHiddenIndex(data.id);

        if (checkbox.is(":checked") && hiddenIndex === -1) {
            hidden[hidden.length] = data.id;
        }
        else {
            hidden.splice(hiddenIndex, 1);
        }

        this.get_hiddenFolders().val(JSON.stringify(hidden));

        this.nodeTemplate(data);

    },

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

    nodeTemplate: function (data) {

        var $span = $("<span></span>"),
             $node = this.get_tree().data("kendoTreeView").findByUid(data.uid),
             $inner = $node.find(".k-in").first(),
             html = "";


        if (this.isHiddenIndex(data.id) > -1) {
            html += "<input type=\"checkbox\" style=\"position:relative;top:2px;margin-right:5px;\" checked />";
            html += "<img src=\"" + ProspectDirector.RUNTIME.resolveUrl("~/application/icons/16x16/SIGN_FORBIDDEN.PNG") + "\" style=\"vertical-align:top;margin-right:5px;\"/>";
        }
        else {

            html += "<input type=\"checkbox\" style=\"position:relative;top:2px;margin-right:5px;\" />";
            if (data.icon !== null) {
                html += "<img src=\"" + ProspectDirector.RUNTIME.resolveUrl("~/application/icons/16x16/") + data.icon + "\" style=\"vertical-align:top;margin-right:5px;\"/>";
            }

        }

        $span.html(html + data.name);

        $inner.html($span[0].outerHTML);

    },

    isHiddenIndex: function (id) {
        return $.inArray(id, this.options.data.hiddenFolders);
    }

});