/// class ProspectDirector.Modules.EntityTypes.DefaultDocumentType
/// using ProspectDirector.Types.Core.Feature

ProspectDirector.Modules.EntityTypes.DefaultDocumentType = ProspectDirector.Types.Core.Feature.extend({

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

    init: function (options) {

        this._super(options);

        this.init_browser();

    },

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

    get_nameInput: function () {
        return this.get_container().find("[data-id=document-type-name]");
    },

    get_valueInput: function () {
        return this.get_container().find("[data-id=document-type-id]");
    },

    //===========================
    // INITIALIZERS
    //===========================

    init_browser: function () {

        this.get_nameInput().popUpBox({
            click: $.proxy(this.button_onClick, this)
        });

        this.get_nameInput().on("focus", $.proxy(this.nameInput_onFocus, this));
        this.get_nameInput().on("click", $.proxy(this.nameInput_onClick, this));
        this.get_nameInput().on("keydown", $.proxy(this.nameInput_onKeyDown, this));

        this.get_valueInput().val(this.options.data.documentTypeId);
        this.get_nameInput().val(this.options.data.documentTypeName);

    },

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

    button_onClick: function (e) {

        var that = this;
        
        PDJS.Shared.DocumentTypes.DocumentTypeBrowserDialog.open({
            viewCustomId: PDJS.Services.Views.BuiltInViews.DOCUMENT_TYPE_SELECTION,
            selectionMode: PD.Web.UI.Controls.GridSelectionMode.single,
            callbacks: {
                ok: function (sender, e) {

                    var documentType = _.first(e);
                    that.get_valueInput().val(documentType.id);
                    that.get_nameInput().val(documentType.name);
                    
                }
            }
        });

    },

    nameInput_onFocus: function (e) {
        $(e.target).get(0).focus();
        $(e.target).get(0).select();
    },

    nameInput_onClick: function (e) {
        $(e.target).get(0).select();
    },

    nameInput_onKeyDown: function (e) {

        if (e.keyCode === 8 || e.keyCode === 46) {
            this.get_valueInput().val("");
            this.get_nameInput().val("");
        }

        e.stopPropagation();
        e.preventDefault();

    }

});