// ----------------------------------------------
// list_post.js
// ----------------------------------------------

// ----------------------------------------------
FORUM.PostView_cl = Class.create({
    // ----------------------------------------------
    onClickButtons_p: function (event_opl) {
        var action_s = $(event_opl.target).attr("data-action");
        switch (action_s) {
            case 'deletePost':
                if (confirm("Soll der Datensatz gelöscht werden?")) {
                    var path_s = "/post/" + this.id_s;
                    $.ajax({
                        context: this,
                        // dataType: "json",
                        url: path_s,
                        type: 'DELETE'
                    })
                    .done(function (data_opl) {
                        FORUM.es_o.publish_px('ContentChanged', ['discussion', this.tId, this.dId]);
                        this.initList_p();
                    })
                    .fail(function (jqXHR_opl, textStatus_spl) {
                        alert("[ThemenListe] Fehler bei Anforderung: " + textStatus_spl);
                    });
                }
                break;

            case 'editPost':
                FORUM.es_o.publish_px('ContentChanged', ['postEdit', this.tId, this.dId, this.id_s]);
                break;

            case 'back':
                FORUM.es_o.publish_px('ContentChanged', ['discussion', this.tId, this.dId]);
                break;
        }
        event_opl.stopPropagation();
        event_opl.preventDefault();
    },
    storeContentToEditForm_p: function () {
        content_s = $('#p' + this.rowId_s + ' #idPostTitle').text();

        $('#idPostContentHeader #edittitle_s').text(content_s);
    },
    initialize: function () {
        var that = this;
        $.get('/html/post.html', function (data_spl) {
            $("#idContentOuter").append(data_spl);
            $("#idPostContent").hide();
            that.initHandler_p();
            that.initList_p();
        });
    },
    render_px: function (data_opl) {
        $("#pEditTitle").val('');
        $("#pEditText").val('');
        $.ajax({
            dataType: "json",
            url: '/post/' + data_opl[0] + '/' + data_opl[1] + '/' + data_opl[2],
            type: 'GET'
        })
        .done($.proxy(this.doRender_p, this))
        .fail(function (jqXHR_opl, textStatus_spl) {
            alert("[Liste] Fehler bei Anforderung: " + textStatus_spl);
        });
    },
    doRender_p: function (data_opl) {
        this.initList_p();

        this.tId = data_opl['tId'];
        this.dId = data_opl['dId'];
        this.id_s = data_opl['id'];

        data_o = {};
        data_o[this.id_s] = data_opl['data'];
        data_opl['data'] = data_o

        data_opl['indices'] = [this.id_s];
        var rows_s = FORUM.tm_o.execute_px('list_post.tpl', data_opl);

        $("#idPost li").remove();
        $("#idPost").append(rows_s);
        $("#idPostContent").show();
        console.log("[PostView_cl] doRender");
        this.updateButtons_p();
    },
    initList_p: function () {
        $(".clSelected").removeClass("clSelected");
        this.id_s = '';
    },
    initHandler_p: function () {
        $("#idPostContent").on("click", "button", $.proxy(this.onClickButtons_p, this));
    },
    enableButtons_p: function () {

        $("#idPostContent .clButtonArea button[data-action=back]").prop("disabled", false);
        if (FORUM.user_o.loggedin && $("#p" + this.id_s + " .clOwner").text() == FORUM.user_o.name_s) {
            this.enableEditButtons_p();
        }
    },
    enableEditButtons_p: function () {
        $("#idPostContent .clButtonArea button").each(function () {
            if ($(this).attr("data-action") == "deletePost" || $(this).attr("data-action") == "editPost") {
                $(this).prop("disabled", false);
            }
        });
    },
    updateButtons_p: function () {
        $("#idPostContent .clButtonArea button").each(function () {
            $(this).prop("disabled", true);
        });
        this.enableButtons_p();
    },
    canClose_px: function () {
        return true;
    },
    close_px: function () {
        this.updateButtons_p();
        $("#idPost li").remove();
        $("#idPostContent").hide();
    }
});
// EOF