// ----------------------------------------------
// forum.js
// ----------------------------------------------
// Verwendung von jquery, inheritance, Single-Page / Ajax, Event-Service
// REST-Interface
// ----------------------------------------------


var FORUM = {};

//********************************************** USER-object
FORUM.User_cl = Class.create({
    initialize: function () {
        this.name_s = '';
        this.password = '';
        this.loggedin = false;
        $('.clLoggedInText #idLoggedIn').hide();

    },
    logout: function () {
        this.id_s = "";
        this.name_s = "niemand";
        this.loggedin = false;
        this.items = [];


        $('#idNav .clLinkToLogin').show();
        $('#idNav .clLinkToSignup').show();
        $('#idNav .clLinkToLogout').hide();
        $('.clLoggedInText #idNotLoggedIn').show();
        $('.clLoggedInText #idLoggedIn').hide();
        $('.clLoggedInText #idLoggedIn').text('Sie sind eingeloggt als: ');
    },
    login: function (data_o) {
        this.name_s = data_o['username'];
        this.password = data_o['password'];
        this.loggedin = true;

        $('#idNav .clLinkToLogin').hide();
        $('#idNav .clLinkToSignup').hide();
        $('#idNav .clLinkToLogout').show();
        $('.clLoggedInText #idNotLoggedIn').hide();
        $('.clLoggedInText #idLoggedIn').show();
        $('.clLoggedInText #idLoggedIn').text($('.clLoggedInText #idLoggedIn').text() + data_o['username']);
    },
    register: function () {
        this.id_s = "";
        this.name_s = "niemand";
        this.loggedin = false;

        $('#idNav .clLinkToLogin').show();
        $('#idNav .clLinkToSignup').show();
        $('#idNav .clLinkToLogout').hide();
    }
});
//**********************************************


// ----------------------------------------------
FORUM.Application_cl = Class.create({
    // ----------------------------------------------
    initialize: function () {
        this.content_o = null; // das jeweils aktuelle Objekt im Contentbereich
        this.nav_o = new FORUM.Nav_cl();
        //this.discussionView_o = new FORUM.PostlistView_cl();
        this.topicListView_o = new FORUM.TopicListView_cl();
        this.topicDetailView_o = new FORUM.TopicDetailView_cl();
        this.discussionListView_o = new FORUM.DiscussionListView_cl();
        this.discussionDetailView_o = new FORUM.DiscussionDetailView_cl();
        this.postListView_o = new FORUM.PostListView_cl();
        this.postDetailView_o = new FORUM.PostDetailView_cl();
        this.postView_o = new FORUM.PostView_cl();

        //this.discussionlistView_o = new FORUM.DiscussionlistView_cl();
        //this.postView_o = new FORUM.PostView_cl();
        this.loginView_o = new FORUM.LoginView_cl();

        // Registrierungen
        FORUM.es_o.subscribe_px(this, 'ContentChanged');
    },
    notify_px: function (self_opl, message_spl, data_apl) {
        switch (message_spl) {
            case 'ContentChanged':
                switch (data_apl[0]) {
                    case 'init':
                        FORUM.tm_o = new TELIB.TemplateManager_cl();
                        break;

                    case 'showRegister':
                        self_opl.setContent_p(self_opl.registerView_o, data_apl[1]);
                        break;

                    case 'showLogin':
                        self_opl.setContent_p(self_opl.loginView_o, data_apl[1]);
                        break;

                    case 'logout':
                        FORUM.user_o.logout();
                        self_opl.setContent_p(self_opl.topicListView_o, data_apl[1]);
                        break;

                    case 'login':
                        self_opl.setContent_p(self_opl.loginView_o, data_apl[1]);
                        break;

                    case 'register':
                        FORUM.user_o.register();
                        self_opl.setContent_p(self_opl.topicListView_o, data_apl[1]);
                        break;

                    case 'topicList':
                        self_opl.setContent_p(self_opl.topicListView_o, data_apl[1]);
                        break;

                    case 'topicEdit':
                        self_opl.setContent_p(self_opl.topicDetailView_o, data_apl[1]);
                        break;

                    case 'discussionList':
                        self_opl.setContent_p(self_opl.discussionListView_o, data_apl[1]);
                        break;

                    case 'discussionEdit':
                        self_opl.setContent_p(self_opl.discussionDetailView_o, [data_apl[1], data_apl[2]]);
                        break;

                    case 'discussion':
                        self_opl.setContent_p(self_opl.postListView_o, [data_apl[1], data_apl[2]]);
                        break;

                    case 'postEdit':
                        self_opl.setContent_p(self_opl.postDetailView_o, [data_apl[1], data_apl[2], data_apl[3]]);
                        break;

                    case 'post':
                        self_opl.setContent_p(self_opl.postView_o, [data_apl[1], data_apl[2], data_apl[3]]);
                        break;

                    default:
                        console.warn('[Application_cl] unbekannte app-Notification: ' + data_apl[0]);
                        break;
                }
                break;
            default:
                console.warn('[Application_cl] unbekannte Notification: ' + message_spl);
                break;
        }
    },
    setContent_p: function (newContent_opl, data_opl) {
        if (this.content_o != null) {
            if (this.content_o.canClose_px()) {
                console.log(newContent_opl);
                console.log(data_opl);
                    this.content_o.close_px();
                    this.content_o = newContent_opl;
                    this.content_o.render_px(data_opl);
            }
        } else {
            this.content_o = newContent_opl;
            this.content_o.render_px(data_opl);
        }
    }
});

// ----------------------------------------------
$(document).ready(function () {
    // ----------------------------------------------
    // wird ausgeführt, wenn das Dokument vollständig geladen wurde
    FORUM.es_o = new EventService_cl();
    FORUM.app_o = new FORUM.Application_cl();
    FORUM.user_o = new FORUM.User_cl();
    FORUM.es_o.publish_px('ContentChanged', ['init', null]);
});






// EOF