/// class ProspectDirector.Web.UI.FeatureSets.Editors.ListEditor
/// using ProspectDirector.Web.UI.FeatureSets.MicroEditorBase

/**
[NEEDS DOCUMENTATION]
@class ListEditor
@namespace Web.UI.FeatureSets.Editors
@extends  Web.UI.FeatureSets.MicroEditorBase
@constructor
**/
ProspectDirector.Web.UI.FeatureSets.Editors.ListEditor = ProspectDirector.Web.UI.FeatureSets.MicroEditorBase.extend({

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

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

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

    get_input: function () {
        return this.get_element();
    },

    get_list: function () {
        return this.get_init()[this.get_definition().get_data().listId];
    },

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

    _select_onChange: function () {

        var definition = this.get_definition().get_data();
        var value = this.get_input().val();

        this.setPrimaryValue(value);

        if (definition.attributes["onEventPostback"] != undefined) {
            if (definition.attributes["onEventPostback"]["change"] != undefined) {
                this.get_featureSet().refresh(definition.attributes["onEventPostback"]["change"].validate);
            }
        }
        
    },

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

    activate: function () {

        var definition = this.get_definition().get_data();

        this.get_element().prop("readOnly", definition.readOnly);
        this.get_element().prop("disabled", definition.disabled);

        this.get_input().on("change", $.proxy(this._select_onChange, this));
        this._setListSelection();
    },

    render: function () {

        var index = 0,
            data = this.get_list(),
            result = "<select class=\"-pd-micro-editor-ddl\" id=\"" + this.getId() + "\">";

        for (index = 0; index < data.length; index++) {
            result += "<option value=\"" + data[index].value + "\">" + data[index].name + "</option>";
        }

        result += "</select>";

        return result;

    },


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

    _setListSelection: function () {

        var value = this.getPrimaryValue(),
            index = 0,
            options = this.get_input()[0].options;

        value = value || "";

        for (index = 0; index < options.length; index++) {
            if (options[index].value === value.toString()) {
                options[index].selected = true;
                index = options.length;
            }
        }

    }

});