/// class ProspectDirector.Modules.Processes.ProcessNodes.StageOnExecuteDialog
/// using ProspectDirector.Types.Core.Dialog

ProspectDirector.Modules.Processes.ProcessNodes.StageOnExecuteDialog = (function (options) {

    var 

        PRIVATE = {

            init: function (options) {

                PRIVATE.options = $.extend({
                    selector: null,
                    processId: null
                }, options);

                PRIVATE.init_dialog();
                PRIVATE.init_tree();

            },

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

            get_dialog: function () {
                return $(PRIVATE.options.selector);
            },

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

            get_okButton: function () {
                return PRIVATE.get_dialog().find("INPUT[type=button][data-command=ok]");
            },

            get_cancelButton: function () {
                return PRIVATE.get_dialog().find("INPUT[type=button][data-command=cancel]");
            },

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

            init_dialog: function () {

                this.get_cancelButton().on("click", $.proxy(this.cancelButton_onClick, this));
                this.get_okButton().on("click", $.proxy(this.okButton_onClick, this));

            },

            init_tree: function () {

                PRIVATE.get_tree().kendoTreeView({
                    dragAndDrop: true,
                    dataSource: new kendo.data.HierarchicalDataSource({
                        transport: {
                            read: function (settings) {

                                PD.ajax({
                                    url: ProspectDirector.RUNTIME.resolveUrl("~/process/getnodes"),
                                    data: {
                                        processId: options.processId,
                                        nodeId: settings.data.id
                                    },
                                    success: function (result) {
                                        settings.success(result);
                                    }
                                });

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

                this.get_tree().on("mouseover", "LI.k-item", $.proxy(this.tree_onMouseOver, this));
                this.get_tree().on("dblclick", "LI.k-item", $.proxy(this.tree_onDblClick, this));

            },

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

            cancelButton_onClick: function () {
                this.get_dialog().data("dialog").hide();
            },

            okButton_onClick: function () {

                var tree = this.get_tree().data("kendoTreeView"),
                    node = tree.select();

                if (node.length === 0) {
                    alert("No selection has been made.");
                }
                else {
                    this.get_dialog().data("dialog").hide({
                        command: "ok",
                        data: {
                            id: tree.dataItem(node).id,
                            name: tree.dataItem(node).name
                        }
                    });
                }

            },

            tree_onDataBound: function (e) {

                var tree = PRIVATE.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++) {
                        PRIVATE.nodeTemplate(data[index]);
                    }

                }

            },

            tree_onMouseOver: function (e) {

                var node = $(e.target).closest("LI.k-item"),
                    kin = node.find(".k-in").first();

                if (node.data("type") !== "ProcessStage") {
                    kin.removeClass("k-state-hover");
                }

            },

            tree_onSelect: function (e) {

                var node = $(e.node);

                if (node.data("type") === "ProcessStage") {

                }
                else {
                    e.preventDefault();
                }

            },

            tree_onDblClick: function (e) {

                var tree = this.get_tree().data("kendoTreeView");

                if (tree.select().length > 0) {
                    this.okButton_onClick();
                }
                
            },

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

            nodeTemplate: function (data) {

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

                $node.attr("data-role", data.role);
                $node.attr("data-type", data.type);

                if (data.type !== "ProcessStage") {
                    $inner.css({
                        opacity: ".5"
                    });
                }

                $span.html("<img src=\"" + ProspectDirector.RUNTIME.resolveUrl("~/application/icons/16x16/" + data.icon) + "\" style=\"vertical-align:top;\" />&nbsp;" + data.name);

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

            }

        },

        PUBLIC = {

        };

    PRIVATE.init(options);

    return PUBLIC;

});

ProspectDirector.Modules.Processes.ProcessNodes.StageOnExecuteDialog.Dialog =
new (ProspectDirector.Types.Core.Dialog.extend({
    
    init: function () {
        this._super("stageOnExecute",
                        "Select Stage On Execute",
                        ProspectDirector.RUNTIME.resolveUrl("~/ProcessNode/StageOnExecuteDialog"));
    }

}));