/// class ProspectDirector.Types.View.Menu

/**
@class Menu
@namespace ProspectDirector.Types.View
@extensionfor ProspectDirector.Types.Core.Menu
*/
ProspectDirector.Types.View.Menu = Class.extend({

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

    init: function (options) {

        this.options = $.extend({
            id: "formmenu",
            viewStateId: null,
            viewGrid: null,
            exportTitle: null
        },
        options);

    },

    //---------------------------
    //--- PUBLIC PROPERTIES
    //---------------------------

    /**
    @property get_viewGrid {Object}
    **/
    get_viewGrid: function () {
        return this.options.viewGrid;
    },

    /**
    @property get_viewGrid {String}
    **/
    get_viewStateId: function () {
        return this.get_viewGrid().get_viewStateId();
    },

    //---------------------------
    //--- PUBLIC METHODS
    //---------------------------

    /**
    @method executeCommand
    **/
    executeCommand: function (e) {

        if (e.role === "menu-view") {

            switch (e.command) {

                case "save":

                    ProspectDirector.Types.Core.Notifications.action("Saving view...");

                    PD.ajax({
                        type: "POST",
                        url: ProspectDirector.RUNTIME.resolveUrl("~/view/save?") + $.param({
                            viewStateId: this.get_viewGrid().get_viewStateId()
                        }),
                        success: function (a, b, c) {
                            ProspectDirector.Types.Core.Notifications.success("Save successful.");
                        },
                        error: function (a, b, c) {
                            ProspectDirector.Types.Core.Notifications.exception("Save command failed.");
                        }
                    });
                    break;

                case "saveas":

                    ProspectDirector.Types.Core.Dialogs.ViewSaveAs.show({
                        params: {
                            viewStateId: this.get_viewGrid().get_viewStateId()
                        }
                    });
                    break;

                case "anchor":

                    ProspectDirector.Types.Core.Dialogs.ViewAnchor.show({
                        params: {
                            viewStateId: this.get_viewGrid().get_viewStateId()
                        },
                        callbacks: {
                            ok: $.proxy(function () {
                                this.get_viewGrid().build();
                            }, this)
                        }
                    });
                    break;

                case "refresh":

                    this.get_viewGrid().refresh();
                    break;

                case "undo":

                    ProspectDirector.Types.Core.Notifications.action("Undo in progress...");

                    PD.ajax({

                        type: "POST",

                        url: ProspectDirector.RUNTIME.resolveUrl("~/view/undo?") + $.param({
                            viewStateId: this.get_viewGrid().get_viewStateId()
                        }),

                        success: $.proxy(function (a, b, c) {
                            ProspectDirector.Types.Core.Notifications.success("Undo successful.");
                            this.get_viewGrid().build();
                        }, this)

                    });
                    break;

                case "reset":

                    ProspectDirector.Types.Core.Notifications.action("Reset in progress...");

                    PD.ajax({

                        type: "POST",

                        url: ProspectDirector.RUNTIME.resolveUrl("~/view/reset?") + $.param({
                            viewStateId: this.get_viewGrid().get_viewStateId()
                        }),

                        success: $.proxy(function (a, b, c) {
                            ProspectDirector.Types.Core.Notifications.success("Reset successful.");
                            this.get_viewGrid().build();
                        }, this)

                    });
                    break;

                case "properties":

                    ProspectDirector.Types.Core.Dialogs.ViewProperties.show({
                        params: {
                            viewStateId: this.get_viewGrid().get_viewStateId()
                        }
                    });
                    break;

                case "designcolumns":

                    ProspectDirector.Modules.Views.ViewDesignColumnsDialog.Dialog.show({
                        params: {
                            viewStateId: this.get_viewStateId()
                        },
                        callbacks: {
                            ok: $.proxy(function (sender, e) {
                                this.get_viewGrid().build();
                            }, this)
                        }
                    });

                    break;

                case "clearsort":

                    this.get_viewGrid().clearSort();
                    break;

                case "columnsbounds":

                    ProspectDirector.Modules.Views.ViewBoundsColumnsDialog.Dialog.show({
                        params: {
                            viewStateId: this.get_viewStateId()
                        },
                        callbacks: {
                            ok: $.proxy(function (sender, e) {
                                this.get_viewGrid().build();
                            }, this)
                        }
                    });
                    break;

                case "designfilters":

                    ProspectDirector.Modules.Views.ViewDesignFiltersDialog.Dialog.show({
                        params: {
                            viewStateId: this.get_viewStateId()
                        },
                        callbacks: {
                            ok: $.proxy(function (sender, e) {
                                this.get_viewGrid().build();
                            }, this)
                        }
                    });

                    break;

                case "filtersdelete":

                    this.get_viewGrid().clearQuickFilters();

                    break;

                case "filtersbounds":

                    ProspectDirector.Modules.Views.ViewBoundsFiltersDialog.Dialog.show({
                        params: {
                            viewStateId: this.get_viewStateId()
                        },
                        callbacks: {
                            ok: $.proxy(function (sender, e) {
                                this.get_viewGrid().build();
                            }, this)
                        }
                    });

                    break;

                case "exportformatted":

                    var params = {
                        viewStateId: this.get_viewStateId(),
                        format: true,
                        contextualFilters: JSON.stringify(this.get_viewGrid().getContextualFilters())
                    };

                    if (this.options.exportTitle) {
                        params.title = this.options.exportTitle;
                    }

                    ProspectDirector.RUNTIME.iframeRequest("~/view/export", params);
                    break;

                case "exportdataonly":

                    ProspectDirector.RUNTIME.iframeRequest("~/view/export", {
                        viewStateId: this.get_viewStateId(),
                        format: false,
                        contextualFilters: JSON.stringify(this.get_viewGrid().getContextualFilters())
                    });
                    break;

                case "registertemplate":
                    break;

                case "searchables":

                    ProspectDirector.Modules.Views.ViewBoundsSearchablesDialog.Dialog.show({
                        params: {
                            viewStateId: this.get_viewStateId()
                        },
                        callbacks: {
                            ok: $.proxy(function (sender, e) {
                                this.get_viewGrid().build();
                            }, this)
                        }
                    });
                    break;

                case "share":

                    window.location = "mailto:?subject=Invitation to share a view&body=I would like to share a view with you. Click the link below or copy and paste it into your browser to access the view.%0d%0d" + window.location.href + "\n\n";

                    break;


            }

        }

    }

});