/// class ProspectDirector.Web.UI.Utilities.Kendo.GridSelectionCollection
/// using ProspectDirector.Web.List

ProspectDirector.Web.UI.Utilities.Kendo.GridSelectionCollection = ProspectDirector.Web.List.extend({

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

    _options: {},

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

    init: function (options) {
        this._options = $.extend(this._options, options);
        this._super();
        this.get_grid().dataSource.bind("change", $.proxy(this._dataSource_onChange, this));
    },

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

    get_options: function () {
        return this._options;
    },

    get_primaryKey: function () {
        return this.get_options().primaryKey;
    },

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

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

    _dataSource_onChange: function (e) {

        var that = this,
            rowId = null,
            rowData = null;

        if (this.get_grid()) {

            this.each(function (index, selection) {

                rowId = that.extractPrimaryKey(selection);
                rowData = that.get_grid().dataSource.get(rowId);

                if (rowData != null) {
                    that._array[index] = rowData.toJSON();
                }

            });

        }

    },

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

    add: function (item) {
        if (!this.exists(item)) {
            this._super(item);
        }
    },

    insert: function (item, index) {
        if (!this.exists(item)) {
            this._super(item, index);
        }
    },

    exists: function (row) {
        return this.matchByPrimaryKey(row) !== null;
    },

    remove: function (item) {

        var row = this.matchByPrimaryKey(item);

        if (row !== null) {
            this._super(row);
        }

    },

    findByPrimaryKey: function (value) {

        var that = this,
            list = new ProspectDirector.Web.List(value),
            result = null;

        this.each(function (index, item) {
            if (list.contains(that.extractPrimaryKey(item))) {
                result = item;
            }
        });

        return result;

    },

    matchByPrimaryKey: function (item) {

        var that = this,
            primaryKey = this.extractPrimaryKey(item),
            result = null;

        this.each(function (index, item1) {

            if (primaryKey === that.extractPrimaryKey(item1)) {
                result = item1;
                return false;
            }

        });

        return result;

    },

    extractPrimaryKey: function (item) {
        return item[this.get_options().primaryKey];
    },

    toKeysArray: function () {

        var that = this,
            result = [];

        this.each(function (index, item) {
            result[result.length] = that.extractPrimaryKey(item);
        });

        return result;

    },

    toKeysList: function () {
        return new ProspectDirector.Web.List(this.toKeysArray());
    }

});