/// class ProspectDirector.Web.UI.FeatureSets.Editors.RuleScriptSelectorEditor
/// using ProspectDirector.Web.UI.FeatureSets.Editors.CompositeEditor

ProspectDirector.Web.UI.FeatureSets.Editors.RuleScriptSelectorEditor =
    ProspectDirector.Web.UI.FeatureSets.Editors.CompositeEditor.extend({

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

        get_input: function () {
            return this.get_element().find("INPUT[type=text]");
        },

        get_defaultProperty: function () {
            return this.get_definition().get_data().defaultProperty;
        },

        get_textProperty: function () {
            return this.get_definition().get_data().textProperty;
        },

        get_viewStateIdProperty: function () {
            return this.get_definition().get_data().viewStateIdProperty;
        },

        get_image: function () {
            return this.get_element().find("IMG");
        },

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

        _button_onClick: function () {

            var that = this;

            PDJS.Manage.Reports.RuleScriptSelectionDialog.open({
                viewStateId: this.getValue(this.get_viewStateIdProperty()),
                callbacks: {
                    ok: function (sender, e) {
                        that.get_input().val(e.name);
                        that.setValue(that.get_textProperty(), e.name);
                        that.setPrimaryValue(e.id);
                    }
                }
            });

        },

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

        activate: function () {
            this._super();

            var that = this;
            var value = this.getValue(this.get_textProperty()) || "";
            var defValue = "";

            if (this.get_defaultProperty() != null) {
                defValue = this.getValue(this.get_defaultProperty()) || "";
            }

            this.get_input().val(value);

            this.get_input().on("mouseover", function (e) {
                if (that.getPrimaryValue() != null) {
                    that.get_image().show();
                }
            });

            this.get_input().on("mouseout", function (e) {
                if (e.relatedTarget.tagName !== "IMG") {
                    that.get_image().hide();
                }
            });

            this.get_image().on("click", function () {
                that.setPrimaryValue(null);
                that.get_input().val(defValue);
                that.get_image().hide();
            });

        },

        render: function () {

            return "<div id=\"" + this.getId() + "\" class=\"-pd-micro-editor-composite\">" +
                "<input type=\"text\" readonly style=\"color:#AAAAAA;padding-left:5px;\">" +
                "<input type=\"button\" value=\"...\">" +
                "<img src='" + PD.Path.resolve("~/application/icons/misc/cancel.png") + "' style='position:absolute;right:25px;top:3px;display:none;'/>" +
            "</div>";

        }

    });