/// class PD.Web.UI.Controls.GridSelection

PD.Web.UI.Controls.GridSelection =
Class.extend({

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

    _innerList: null,
    _pk: null,

    //===================================
    // CONSTRUCTORS
    //===================================

    init: function (pk, items) {
        this._pk = pk;
        this._innerList = new PD.BCL.List(items);
    },

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

    get_pk: function () {
        return this._pk;
    },

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

    add: function (item) {
        if (!this.contains(item)) {
            this._innerList.add(item);
        }
    },

    contains: function (item) {
        return this.getItemByPK(item) !== null;
    },

    remove: function (item) {
        this._innerList.remove(item);
    },

    clear: function () {
        this._innerList.clear();
    },

    clone: function () {
        return this._innerList.clone();
    },

    count: function () {
        return this._innerList.count();
    },

    each: function (fn) {
        this._innerList.each(fn);
    },

    first: function () {
        return this._innerList.first();
    },

    last: function () {
        return this._innerList.last();
    },

    toIDs: function () {

        var that = this;

        return this._innerList.transform(function (item) {
            return item[that.get_pk()];
        });

    },

    toList: function () {
        return new PD.BCL.List(this.toArray());
    },

    toArray: function () {
        return this._innerList.toArray();
    },

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

    getItemByPK: function (item) {

        var pk = this._pk,
            result = null;

        this._innerList.each(function (index, source) {
            if (source[pk] === item[pk]) {
                result = source;
                return false;
            }
        });

        return result;

    }

});