/// class ProspectDirector.Modules.Common.FeaturedObjectEditor

ProspectDirector.Modules.Common.FeaturedObjectEditor = Class.extend({

    //---------------------------
    //--- CONSTRUCTOR
    //---------------------------

    init: function (options) {

        this.options = $.extend({
            dialog: null,
            container: null,
            data: null
        }, options);

        this.init_dialog();
        this.init_featuredObject();
        this.init_form();

    },

    //---------------------------
    //--- PROPERTIES
    //---------------------------

    get_dialog: function () {
        return $(this.options.dialog);
    },

    get_container: function () {
        return this.get_dialog().length > 0 ? this.get_dialog().children(this.options.container) : $(this.options.container);
    },

    get_form: function () {
        return this.get_container().children("FORM[data-role=featuredobject-form]");
    },

    get_saveButton: function () {
        return this.get_dialog().find("INPUT[data-id=featuredobject-dialog-button-save]");
    },

    get_cancelButton: function () {
        return this.get_dialog().find("INPUT[data-id=featuredobject-dialog-button-cancel]");
    },

    get_featuredObject: function () {
        return this.get_container().data("featuredObject");
    },

    //---------------------------
    //--- EVENT HANDLERS
    //---------------------------

    dialog_onResize: function () {
        this.sizeContent();
    },

    cancelButton_onClick: function () {
        this.get_dialog().data("dialog").hide();
    },

    saveButton_onClick: function () {
        this.save();
    },

    save_onSuccess: function (data, status, jqXHR) {

        ProspectDirector.Types.Core.Notifications.success("Properties Saved");

        this.get_form().observedForm("clear");
        this.refreshValidation();
        this.get_featuredObject().updateFeatures(data);

        if ($.type(data) === "string"
            && (data.indexOf("{") === 0 || data.indexOf("[") === 0)) {
            data = JSON.parse(data);
        }

        if (this.get_dialog().length > 0) {

            this.get_dialog().data("dialog").hide({
                command: "ok",
                data: data
            });

            this.get_featuredObject().destroyFeatures();

        }

    },

    save_onError: function (jqXHR, status, error) {

        jqXHR.errorHandled = true;

        new PD.Web.AjaxErrorHandler({
            config: {
                contextData: {
                    container: this.get_form(),
                    control: this.get_saveButton()
                }
            },
            jqXHR: jqXHR
        });

        this.refreshValidation();

    },

    dialog_onKeyPress: function (e) {

        if (e.keyCode === 13 && e.target.tagName.toLowerCase() !== "textarea") {
            e.preventDefault();
            this.saveButton_onClick();
        }

    },

    //---------------------------
    //--- INITIALIZERS
    //---------------------------

    init_dialog: function () {

        if (this.get_dialog().length === 0) {
            return null;
        }

        this.get_dialog().data("kendoWindow").bind("resize", $.proxy(this.dialog_onResize, this));
        this.get_dialog().on("keypress", $.proxy(this.dialog_onKeyPress, this));
        this.get_cancelButton().on("click", $.proxy(this.cancelButton_onClick, this));
        this.get_saveButton().on("click", $.proxy(this.saveButton_onClick, this));

    },

    init_featuredObject: function () {

        this.get_container().data("featuredObjectEditor", this);

        this.get_container().data("featuredObject", new ProspectDirector.Types.Core.FeaturedObject({
            featureData: this.options.data.features,
            container: this.options.container
        }));

    },

    init_form: function () {

        this.get_form().observedForm();

    },

    //---------------------------
    //--- METHODS
    //---------------------------

    save: function (success, error) {

        var that = this,
            fieldHighlighter = new PD.Web.InvalidFieldHighlighter(),
            errorFn = function (e) {
                that.save_onError(e);
                if (error) {
                    error();
                }
            };

        fieldHighlighter.clear(that.get_form());

        ProspectDirector.Types.Core.Notifications.action("Saving...");

        this.get_featuredObject().saveFeatures();

        if (this.get_saveButton().length > 0) {
            this.get_saveButton().disable();
        }

        PD.ajax({
            type: this.get_form().attr('method'),
            url: this.get_form().attr('action'),
            data: this.get_form().serialize(),
            success: $.proxy(function (data, status, jqXHR) {

                this.save_onSuccess(data, status, jqXHR);

                if ($.isFunction(success)) {
                    success(data);
                }

            }, this),
            error: $.proxy(errorFn, this)
        });

    },

    sizeContent: function () {
        //protected method
    },

    refreshValidation: function () {
        //TODO: implement
    }

});