// ----------------------------------------------
// register.js
// ----------------------------------------------

// ----------------------------------------------
FORUM.RegisterView_cl = Class.create({  
// ----------------------------------------------
    initialize: function () {
        var that = this;
        $.get('/html/register.html', function (data_spl) {
            $("#idContentOuter").append(data_spl);
            $("#idRegisterContent").hide();
            that.initHandler_p();
        });
        FORUM.es_o.subscribe_px(this, 'register');
    },

    notify_px: function (self_opl, message_spl, data_apl) {
        switch (message_spl) {
            case 'register':
                switch (data_apl[0]) {
                    case 'refresh':
                        self_opl.render_px(null);
                        break;
                    default:
                        console.warning('[RegisterView_cl] unbekannte list-Notification: ' + data_apl[0]);
                        break;
                }
                break;
            default:
                console.warning('[RegisterView_cl] unbekannte Notification: ' + message_spl);
                break;
        }
    },

    canClose_px: function () {
        return true;
    },

    close_px: function () {
        $("#idRegisterContent").hide();
    },

    render_px: function (data_opl) {
        $.ajax({
            dataType: "json",
            url: '/register',
            type: 'GET'
        })
        .done($.proxy(this.doRender_p, this))
        .fail(function (jqXHR_opl, textStatus_spl) {
            alert("[Register] Fehler bei Anforderung: " + textStatus_spl);
        });
    },

    doRender_p: function (data_opl) {     
        $("#idRegisterContent").show();
        console.log("[RegisterView_cl] doRender");
    },
 
    initHandler_p: function () {
        $("#idRegisterContent #idButtonArea").on("click", "button", $.proxy(this.onClickButtons_p, this));

    },
  
    onClickButtons_p: function (event_opl) {

        var action_s = $(event_opl.target).attr("data-action");

        switch (action_s) {
            case 'registerme':
                var data_s = $("#idForm").serialize();
                alert(data_s);
                $.ajax({
                    context: this,
                    data: data_s,
                    url: '/register',
                    type: 'PUT'
                })
                .done(function (data_opl) {               
                    alert("Speichern ausgeführt!");
                })
                .fail(function (jqXHR_opl, textStatus_spl) {
                    alert( "Fehler bei Anforderung: " + textStatus_spl );
                });
                FORUM.es_o.publish_px('app', [action_s, null]);
                break;
        }
        event_opl.stopPropagation();
        event_opl.preventDefault();

    },

    enableButtons_p: function () {
        $("#idRegisterContent #idButtonArea button").each(function () {
            if ($(this).attr("data-action") != "add") {
                $(this).prop("disabled", false);
            }
        });
    },

    disableButtons_p: function () {
        $("#idRegisterContent #idButtonArea button").each(function () {
            if ($(this).attr("data-action") != "add") {
                $(this).prop("disabled", true);
            }
        });
    }
});

// EOF