/// class ProspectDirector.Web.ObservableList
/// using ProspectDirector.Web.List

/**
A an observable list class that wraps a JavaScript array.
@class ObservableList
@constructor
@namespace Web
@param {Array} [array] The array with which to populate the {List}.
**/
ProspectDirector.Web.ObservableList = ProspectDirector.Web.List.extend({

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

    _options: null,

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

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

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

    add: function (item) {
        
        this._super(item);
        
        this.triggerChange({
            type: "add",
            item: item,
            index: -1
        });

    },

    insert: function (item, index) {

        if (this._array.length < index) {
            return;
        }

        this._super(item, index);

        this.triggerChange({
            type: "insert",
            item: item,
            index: index
        });

    },

    removeAt: function (index) {

        var item = this._super(index);

        this.triggerChange({
            type: "remove",
            item: item,
            index: index
        });

        return item;

    },

    triggerChange: function (args) {
        this._trigger("change", args);
    },

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

    _trigger: function (eventName, args) {

        var fn = this._options[eventName];

        if ($.isFunction(fn)) {
            fn(this, args);
        }

    }

});