/// class PD.Web.UI.Controls.ViewGridColumnMenuModule
/// using PD.Web.UI.Controls.GridModule

PD.Web.UI.Controls.ViewGridColumnMenuModule =
PD.Web.UI.Controls.GridModule.extend({

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

    m_menu: null,

    //===================================
    // PROPERTIES
    //===================================

    get_kendoGrid: function () {
        return this.get_grid().get_kendo();
    },

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

    start: function () {
        this._initContextMenuHandles();
        this._initContextMenu();
    },

    unload: function () {
        if (this.m_menu) {
            this.m_menu.remove();
        }
    },

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

    getOptionSettings: function () {
        return {
            sortable: {
                mode: "multiple",
                allowUnsort: true
            }
        };
    },

    _initContextMenuHandles: function () {

        var headers = this.get_kendoGrid().element.find(".k-header .k-link");

        headers.each(function (index, header) {

            var $header = $(header);

            $header.css({
                position: "relative",
                paddingRight: "20px"
            });

            $header.append("<div class='context-menu-anchor -pd-viewgrid-column-menu-btn'>&nbsp;</div>");

        });

    },

    _initContextMenu: function () {

        var grid = this.get_grid();
        this.m_menu = new PD.Web.UI.Controls.ContextMenu({
                animation: false,
                target: this.get_kendoGrid().element.find(".k-header"),
                filter: ".context-menu-anchor",
                direction: "right",
                alignToAnchor: true,
                showOn: "click",
                segments: [
                    new PD.Web.UI.Controls.ViewGridColumnMenuSegment(function (e) {
                        return grid;
                    })
                ]
            });

        this.get_grid().get_children().add(this.m_menu);

    }

});