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

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

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

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

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

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

    _onColumnReorder: function (e) {

        var isDummyColumnReorder = e.column.field === null,
            isDummyColumnDrop = e.newIndex == e.sender.columns.length - 1;

        if (isDummyColumnReorder) {
            this._handleDummyColumnReorder(e);
            return;
        }

        if (isDummyColumnDrop) {
            this._handlerDummyColumnDrop(e);
            return;
        }

        this._commitColumnReorder(e);

    },

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

    _handleDummyColumnReorder: function (e) {

        /* 
        ====================================================================== 
         If moving the last column (which is a dummy column), prevent this 
         by moving it back to the last index. Works around lack of support 
         for canceling the column reorder event in kendo grid.
        ====================================================================== 
        */

        /* A short timeout is required to allow grid column reorder event to bubble before attempting programmatic reorder. */
        setTimeout(function () {
            e.sender.reorderColumn(e.sender.columns.length - 1, e.column)
        }, 50);

    },

    _handlerDummyColumnDrop: function (e) {

        /* 
        ====================================================================== 
        If moving a column after the last column (which is a dummy column), 
        prevent this by moving it back to its previous index. Works around 
        lack of support for canceling the column reorder event in kendo grid.  
        ====================================================================== 
        */

        /* A short timeout is required to allow grid column reorder event to bubble before attempting programmatic reorder. */
        setTimeout(function () {
            e.sender.reorderColumn(e.sender.columns.length - 2, e.column)
        }, 50);

    },

    _commitColumnReorder: function (e) {

        var url = PD.Path.resolve("~/viewcolumn/move");

        PD.ajax({
            url: url,
            type: "POST",
            data: {
                viewStateId: this.get_viewStateId(),
                columnId: e.column.field,
                index: (e.newIndex == e.sender.columns.length - 1) ? e.sender.columns.length - 2 : e.newIndex
            }
        });

    },

    _registerEventHandlers: function () {
        this.get_kendoGrid().bind("columnReorder", $.proxy(this._onColumnReorder, this));
    }

});