/// class ProspectDirector.Web.UI.SelectionDialogBaseDOM

/**
@constructor
@class SelectionDialogBaseDOM
@namespace Web.UI
**/
ProspectDirector.Web.UI.SelectionDialogBaseDOM = Class.extend({

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

    options: null,
    _searchBox: null,
    _selection: null,
    _grid: null,

    //================================
    // CONSTRUCTOR
    //================================

    init: function (options) {
        this.options = options;
    },

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

    get_container: function () {
        return this.options.windowDOM.get_content();
    },

    get_searchBox: function () {
        return this._searchBox;
    },

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

    get_selection: function () {
        return this._selection;
    },

    get_viewToggleButton: function () {
        return this.get_container().find("BUTTON[data-id=view-toggle]");
    },

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

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

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

    //================================
    // INITIALIZERS
    //================================

    _init_grid: function () {
        var options = this.setGridOptions();
        this._grid = this.get_gridDomNode().kendoGrid(options).data("kendoGrid");
    },

    _init_grid_selection: function () {
        var options = this.setGridSelectionOptions();
        this._selection = new ProspectDirector.Web.UI.Utilities.Kendo.GridSelection(options);
    },

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

    _search_onExecute: function () {
        this.refreshGrid();
    },

    _row_onChange: function (e, data) {

        var message = "";

        this.get_viewToggleButton().empty();

        switch (data.count) {
            case 0:
                this.get_viewToggleButton().enabled(false);
                message = "View Selections";
                break;
            default:
                this.get_viewToggleButton().enabled(true);
                message = "View Selections <b>(" + data.count + ")</b>";
                break;
        }

        this.get_viewToggleButton().append(message);

    },

    _row_onDblClick: function (e, data) {
        this._callback("closeAndReturn");
    },

    _cancelButton_onClick: function (e) {
        this._callback("close");
    },

    _okButton_onClick: function (e) {
        this._callback("closeAndReturn");
    },

    //================================
    // PUBLIC METHODS
    //================================

    activate: function () {

        this.get_cancelButton().on("click", $.proxy(this._cancelButton_onClick, this));
        this.get_okButton().on("click", $.proxy(this._okButton_onClick, this));

        this._searchBox = new ProspectDirector.Web.UI.Controls.SearchBox({
            container: this.get_container().find("[data-id=search-box]"),
            search: $.proxy(this._search_onExecute, this),
            cancel: $.proxy(this._search_onExecute, this)
        });

        this._init_grid();

        this._init_grid_selection();

    },

    getSelections: function () {
        return this.get_selection().get_selections();
    },

    render: function () {
        return "";
    },

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

    _callback: function (callbackName, args) {

        var callback = this.options[callbackName];

        if ($.isFunction(callback)) {
            callback(args);
        }

    },

    //================================
    // PROTECTED METHODS
    //================================

    /**
    @protected
    @method refreshGrid
    **/
    refreshGrid: function () {
        
        var gridNode = $(this.get_grid().element);

        gridNode.find(".k-scrollbar-vertical").scrollTop(0);

        this.get_grid().dataSource.page(1);

    },

    /**
    @protected
    @method getGridRequestParameters
    @param data {Object}
    @param type {String}
    @return {Object}
    **/
    getGridRequestParameters: function (data, type) {

        return {
            request: JSON.stringify({
                keywords: this.get_searchBox().getValue(),
                orders: this.getGridSortOrder(data.sort),
                pageIndex: data.page - 1,
                pageSize: data.pageSize
            })
        };

    },

    /**
    @protected
    @method getGridSortOrder
    @param data {Object}
    @return {Array}
    **/
    getGridSortOrder: function (data) {
        return [];
    },

    /**
    @protected
    @method setGridOptions
    @param options {Object}
    **/
    setGridOptions: function (options) {

        options = options || {};

        return $.extend(true, {
            dataSource: {
                pageSize: 50,
                serverPaging: true,
                serverSorting: true,
                transport: {
                    parameterMap: $.proxy(this.getGridRequestParameters, this)
                }
            },
            height: "100%",
            scrollable: {
                virtual: true
            },
            sortable: {
                mode: "multiple",
                allowUnsort: true
            },
            selectable: false,
            resizable: true,
            pageable: {
                previousNext: false,
                numeric: false,
                buttonCount: 0,
                messages: {
                    display: "Displaying {2} items."
                }
            }
        }, options);

    },

    /**
    @protected
    @method setGridSelectionOptions
    @param options {Object}
    **/
    setGridSelectionOptions: function (options) {

        options = options || {};

        return $.extend(true, {
            grid: this.get_grid(),
            selection: {
                mode: "multiple"
            },
            change: $.proxy(this._row_onChange, this),
            dblclick: $.proxy(this._row_onDblClick, this)
        }, options);

    }

});