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

// ----------------------------------------------
// Namensraum einrichten
// ----------------------------------------------

var STUNDENPLAN = {};

// ----------------------------------------------
STUNDENPLAN.Application_cl = Class.create({
// ----------------------------------------------
    initialize: function () {
        this.className = "Application_cl";
        var that = this;
        $.blockUI({
            fadeIn: 200,
            message: '<h1>Bitte warten...</h1>',
            onBlock: function () {
                console.log("[" + that.className + "] UI-Blocken");
            }
        });
        this.storage = new STORAGE.Storage_cl(false);                        //Lokalen Speicher anlegen/öffnen
        this.content_o = null; // das jeweils aktuelle Objekt im Contentbereich
        STUNDENPLAN.tm_o = null;
        this.nav_o = new STUNDENPLAN.Navigation_cl();
        this.views = [];
        // Registrierungen
        STUNDENPLAN.es_o.subscribe_px(this, 'app');
    },
    notify_px: function (self_opl, message_spl, data_apl) {
        switch (message_spl) {
            case 'app':
                switch (data_apl[0]) {
                    case 'init':
                        //STUNDENPLAN.tm_o = new TELIB.TemplateManager_cl();
                        break;
                    case 'templates.loaded':

                        break;

                    case 'init.nav':
                        this.nav_o.render_px(data_apl[1]);
                        break;
                    case 'nav.add':
                        this.nav_o.addMenu_px(data_apl[1]);
                        break;
                    case 'nav.action':
                        switch (data_apl[1]) {
                            case 'show.Main':

                                break;
                            default:
                                console.log('[' + this.className + '] unbekannte nav-Notification: ' + data_apl[1]);
                        }
                        break;
                    case 'nav.finishd':
                        //STUNDENPLAN.es_o.publish_px('nav', ['set_action', 'show.Main']);
                        //this.mainView_o.render_px(data_apl[1]);
                        break;
                    case 'init.view':
                        this.views[data_apl[1]].render_px(data_apl[2]);
                        break;
                    case 'show':
                        var that = this;
                        $.blockUI({
                            fadeIn: 200,
                            message: '<h1>Bitte warten...</h1>',
                            onBlock: function () {
                                console.log("[" + that.className + "] UI-Blocken");
                            }
                        });
                        this.setContent_p(this.views[data_apl[1]], data_apl[2]);
                        //STUNDENPLAN.es_o.publish_px('nav', ['set_action', 'show.Main']);
                        $.unblockUI({
                            onUnblock: function () {
                                console.log("[" + that.className + "] UI-Entblocken");
                            }
                        });
                        break;
                    default:
                        console.log('[' + this.className + '] unbekannte app-Notification: ' + data_apl[0]);
                        break;
                }
                break;
                /*case 'storage':
                 switch (data_apl[0]) {
                 case 'addList':
                 break;
                 case 'addItem':
                 break;
                 case 'removeList':
                 break;
                 case 'removeItem':
                 break;
                 }
                 break;*/
            default:
                console.log('[' + this.className + '] unbekannte Notification: ' + message_spl);
                break;
        }
    },
    setContent_p: function (newContent_opl, data_opl) {
        if (this.content_o != null) {
            if (this.content_o === newContent_opl) {
            } else {
                if (this.content_o.canClose_px()) {
                    this.content_o.close_px();
                    this.content_o = newContent_opl;
                    this.content_o.render_px(data_opl);
                    this.content_o.showView_px(data_opl);
                }
            }
        } else {
            this.content_o = newContent_opl;
            this.content_o.render_px(data_opl);
            this.content_o.showView_px(data_opl);
        }
    },
    addView_px: function (viewModel) {
        this.views[viewModel.name] = viewModel.model;
        STUNDENPLAN.es_o.publish_px('app', ['init.view', viewModel.name]);
        STUNDENPLAN.es_o.publish_px('app', ['nav.add', {action: viewModel.action, text: viewModel.text, method: viewModel.method, name: viewModel.name}]);
    },
    addHiddenView_px: function (viewModel) {
        this.views[viewModel.name] = viewModel.model;
        STUNDENPLAN.es_o.publish_px('app', ['init.view', viewModel.name]);
        //STUNDENPLAN.es_o.publish_px('app', ['nav.add', {action: viewModel.action, text: viewModel.text, method: viewModel.method, name: viewModel.name}]);
    }
});

// ----------------------------------------------

