/// class PD.Web.UI.InputMonitor
/// using PD.Web.UI.MutationMonitor

PD.Web.UI.InputMonitor =
PD.Web.UI.MutationMonitor.extend({

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

    _monitorSelector: ".-pd-monitor",

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

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

    get_selector: function () {
        return this.get_options("selector");
    },

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

    start: function () {
        this._startMonitorOfInputs();
        this._startMontiorOfSelect();
    },

    stop: function () {
        this._stopMonitor("change");
        this._stopMonitor("click");
    },

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

    _startMonitorOfInputs: function () {

        var selectors = [
            "SELECT",
            "TEXTAREA",
            "INPUT:not([type=CHECKBOX]",
            "[type=RADIO]",
            "[type=BUTTON]",
            "[type=IMAGE]",
            "[type=SUBMIT]",
            "[type=RESET]",
            "[type=FILE]",
            "[type=SEARCH])"
        ];

        this._startMonitor("change", selectors.join(","));

    },

    _startMontiorOfSelect: function () {

        var selectors = [
            "INPUT[type=CHECKBOX]",
            "INPUT[type=RADIO]"
        ];

        this._startMonitor("click", selectors.join(","));

    },

    _startMonitor: function (eventName, selectors) {

        var source = this.get_source();

        this.get_selector().off(eventName + this._monitorSelector);
        this.get_selector().on(eventName + this._monitorSelector,
            selectors,
            function () {
                source.setDirty();
            });

    },

    _stopMonitor: function (eventName) {
        this.get_selector().off(eventName + this._monitorSelector);
    }

});