/// class PD.Web.UI.Controls.ListBoxItemCollection
// using PD.Web.UI.Controls.Control

PD.Web.UI.Controls.ListBoxItemCollection =
PD.Web.UI.Controls.Control.extend({

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

    _data: null,

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

    init: function (data) {
        this._data = new PD.BCL.List();
    },

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

    add: function (data) {
        if (data != null && !this.contains(data.id)) {
            this._data.add(data);
        }
    },

    addRange: function (items) {

        var that = this,
            items = new PD.BCL.List(items);

        items.each(function (index, item) {
            if (item != null && !that.contains(item.id)) {
                that._data.add(item);
            }
        });

    },

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

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

    remove: function (id) {
        var item = this.getById(id);
        if (item) {
            this._data.remove(item);
        }
    },

    first: function (id) {
        this._data.first();
    },

    contains: function (id) {
        return this.getById(id) !== null;
    },

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

    getById: function (id) {

        var result = null;

        this._data.each(function (index, item) {
            if (item.id === id) {
                result = item;
                return false;
            }
        });

        return result;

    },

    sortAlpha: function () {

        this._data.sort(function (item1, item2) {

            var value1 = (item1.name || "").toLowerCase(),
                value2 = (item2.name || "").toLowerCase();

            if (value1 < value2) {
                return -1;
            }

            if (value1 > value2) {
                return 1;
            }

            return 0;

        });

    }

});