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

/**
@class FileUploadEditor
@namespace Web.UI.FeatureSets.Editors
@extends Web.UI.FeatureSets.Editors.CompositeEditor
@constructor
**/
ProspectDirector.Web.UI.FeatureSets.Editors.FileUploadEditor = ProspectDirector.Web.UI.FeatureSets.Editors.CompositeEditor.extend({

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

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

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

    _input_onFocus: function (e) {

        var $target = $(e.target);

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

    },

    _button_onClick: function () {

        var that = this;

        ProspectDirector.Web.UI.FeatureSets.Editors.FileUploadEditorDialog.open({
            config: {
                title: "Upload " + this.get_definition().get_data().label
            },
            data: {
                value: this.getPrimaryValue()
            },
            callbacks: {
                ok: function (sender, e) {
                    that.setPrimaryValue(e.id);
                    that._fileNameValue(e.name);
                    that.updateUI(e.name);
                }
            }
        });

    },

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

    activate: function () {
        this._super();
        this.updateUI(this._fileNameValue());
        this.get_input().on("focus", $.proxy(this._input_onFocus, this));
    },

    updateUI: function (value) {
        if (value) {
            this.get_input().val(value);
        }
    },

    render: function () {
        return "<div id=\"" + this.getId() + "\" class=\"-pd-micro-editor-composite\">" +
                "<input type=\"text\" readonly disabled style=\"color:#AAAAAA;padding-left:5px;\">" +
                "<input type=\"button\" value=\"...\">" +
                "</div>";
    },

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

    _fileNameValue: function (value) {

        var data = this.get_feature().get_data(),
            name = this.get_definition().get_data()["fileNameProperty"];

        if (data[name] !== undefined) {
            if (value !== undefined) {
                data[name] = value;
            }
            else {
                return data[name];
            }
        }

    }

});