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

/**
[NEEDS DOCUMENTATION]
@class ActionEditor
@namespace Web.UI.FeatureSets.Editors
@extends Web.UI.FeatureSets.Editors.CompositeEditor
@constructor
**/

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

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

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

    _input_onFocus: function (e) {

        var $target = $(e.target);

        $target.closest("TD").find("INPUT[type=button]").focus();

    },

    _button_onClick: function () {

        var that = this;

        PD.Manage.Modules.Actions.ActionEditorDialog.open({
            data: {
                action: that.getPrimaryValue()
            },
            callbacks: {
                ok: function (sender, e) {
                    that.setPrimaryValue(e.toLiteral());
                    that.setActionName();
                }
            }
        });

    },

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

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

        var that = this;

        this.get_input().on("focus", $.proxy(this._input_onFocus, this));

        this.get_input().on("mouseover", function (e) {
            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({ name: "", id: null, pluginId: null });
            that.setInputName("");
        });

        this.setActionName();
    },
    
    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>";

    },

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

    setActionName: function () {

        var value = this.getPrimaryValue();
        var action = null;

        if (value == null) {
            return;
        }

        action = PD.Actions.ActionDefinition.getActionDefinition(value.pluginId, value.id);

        if (action != null) {
            this.setInputName(action.getFullName());
        }

    },

    setInputName: function (value) {
        this.get_input().val(value);
    }

});