﻿
Ext.namespace('FinnKodeWeb');

var VERSION = '5.0.2';

FinnKodeWeb.LayoutManager =
function(config) {
    config = config || {};
    this.addEvents({
        initialized: true,
        ready: true
    });
    this.listeners = config.listeners;

    FinnKodeWeb.LayoutManager.superclass.constructor.call(this, config);
    // private section
    var _hashManager = new FinnKodeWeb.HashManager({
        wnd: window,
        listeners: {
            parsed: {
                fn: function() {
                    try {
                        _hashManager.beginNavigate();
                        if (_hashManager.mainTabId) {
                            var panelBookContent = _mainTabPanel.getComponent(_hashManager.mainTabId);
                            if (panelBookContent) {
                                _mainTabPanel.setActiveTab(panelBookContent);
                                panelBookContent.navigate(_hashManager);
                            }
                        }
                    }
                    finally {
                        _hashManager.updateLocationHash();
                        _hashManager.endNavigate();
                    }
                },
                scope: this
            }
        }
    });

    var _toolbar = FinnKodeWeb.Toolbar.create();

    var _mainTabPanel = new Ext.TabPanel({
        deferredRender: true,
        id: NamingService.createId('main', 'TabPanel'),
        border: false,
        bodyBorder: false,
        listeners: {
            'tabchange': {
                fn: function(tabpanel, panel) {
                    //alert('hovedtab' + ' tabchange');
                    if (panel.createTabs) {
                        panel.createTabs();
                        tabpanel.doLayout(false, true);
                    }
                    if (!_hashManager.isNavigating()) {
                        if (panel.updateHash) {
                            panel.updateHash(_hashManager);
                            _hashManager.updateLocationHash();
                        }
                    }
                }
            }
        }
    });
    var _logPanel = new FinnKodeWeb.PanelLogView({
        title: 'Logg',
        margins: '0 5 5 5',
        border: false,
        layout: 'fit',
        autoScroll: true,
        store: MsgSvc.store
    });
    var _viewport = new Ext.Viewport({
        layout: 'border',
        renderTo: Ext.getBody(),
        items: [
            {
                region: 'north',
                layout: 'fit',
                border: false,
                tbar: _toolbar
            },
            {
                region: 'center',
                layout: 'fit',
                border: false,
                items: _mainTabPanel
            },
            {
                region: 'south',
                layout: 'fit',
                region: 'south',
                height: 250,
                split: false,
                collapsible: true,
                collapseMode: 'mini',
                collapsed: true,
                items: _logPanel
            }
        ],
        listeners: {
            'afterlayout': {
                fn: function() {
                    Loader = new FinnKodeWeb.Loader();
                    Loader.load(this);
                },
                single: true,
                scope: this
            }
        }
    });
    // public section
    Ext.apply(this, {
        createMainTabs: function(settings) {
            var bookIndex;
            for (bookIndex = 0; bookIndex < settings.Books.length; bookIndex++) {
                var book = settings.Books[bookIndex];
                var bookContent = new FinnKodeWeb.PanelBookContent({
                    book: book,
                    title: book.BookName,
                    id: NamingService.createId(book.BookId, 'PanelBookContent'),
                    itemId: book.BookId,
                    border: false,
                    bodyBorder: false,
                    layout: 'fit',
                    hidden: true
                });
                // Create booksections in PanelBookContent
                _mainTabPanel.add(bookContent);
            }

            var searchSources = new FinnKodeWeb.SearchSources({
                padding: 5
            });
            searchSources.createSearchSources(settings);
            var PanelSearchView = new FinnKodeWeb.PanelSearchView({
                id: NamingService.createId('search', 'PanelSearchView'),
                itemId: 'search',
                searchSources: searchSources
            });
            _mainTabPanel.add(PanelSearchView);
            _mainTabPanel.doLayout();
            if (window.location.hash != "") {
                this.navigate(window.location.hash);
            }
            else {
                this.navigateDefault();
            }
            this.afterAppLoaded();
        },
        navigate: function(hash) {
            _hashManager.parseHash(hash);
        },
        navigateDefault: function() {
            _mainTabPanel.setActiveTab(0);
            // the tabchange event will trigger default views for sub panels
            /*var activeTab = _mainTabPanel.getActiveTab();
            if (activeTab) activeTab.navigateDefault();*/
        },
        afterAppLoaded: function() {
            _mainTabPanel.items.each(function(panelBookContent) {
                if (panelBookContent.afterAppLoaded) panelBookContent.afterAppLoaded();
            });
            MsgSvc.addMsg({
                ShowMaskInfo: true,
                Category: 'GUI',
                Action: 'AfterGuiBuild',
                Label: 'Laster FinnKode ' + VERSION + ' - Ferdig'
            });
            (function() {
                MsgSvc.clearMask();
            }).defer(1000);
        },
        updateLocationHash: function(hash) {
            _hashManager.updateLocationHash();
        }
    });
};

Ext.extend(FinnKodeWeb.LayoutManager, FinnKodeWeb.Observable);


FinnKodeWeb.Loader = function(config) {
    this.addEvents('stateChanged');
    config = config || {};
    this.listeners = config.listeners;
    FinnKodeWeb.Loader.superclass.constructor.call(this, config);

    var _layoutManager = null;

    var _configurationLoader = new FinnKodeWeb.ConfigurationLoader({
        listeners: {
            loaded: {
                fn: function() {
                    MsgSvc.addMsg({
                        ShowMaskInfo: true,
                        Category: 'Config',
                        Action: 'AfterLoaded',
                        Label: 'Laster FinnKode ' + VERSION + ' - Konfigurasjon lastet'
                    });
                    MsgSvc.addMsg({
                        ShowMaskInfo: true,
                        Category: 'GUI',
                        Action: 'BeforeGuiBuild',
                        Label: 'Laster FinnKode ' + VERSION + ' - Initialiserer brukergrensesnitt'
                    });
                    _layoutManager.createMainTabs(_configurationLoader.getSettings());
                },
                scope: this
            },
            failed: {
                fn: function(response) {
                    MsgSvc.clearMask();
                    MsgSvc.addMsg({
                        ShowAlertInfo: true,
                        Category: 'Config',
                        Action: 'AfterLoaded',
                        Label: 'Lasting av konfigurasjon feilet'
                    });
                    MsgSvc.addMsg({
                        Category: 'Config',
                        Action: 'AfterLoaded',
                        Label: response
                    });
                },
                scope: this
            }
        }
    });

    Ext.apply(this, {
        load: function(layoutManager) {
            _layoutManager = layoutManager;
            var msg = new FinnKodeWeb.LogMessage({
                ShowMaskInfo: true,
                Category: 'Config',
                Action: 'BeforeLoad',
                Label: 'Laster FinnKode ' + VERSION + ' - Konfigurasjon'
            });
            MsgSvc.addMsg(msg);
            _configurationLoader.load();
        },
        configurationLoader: _configurationLoader
    });
};
Ext.extend(FinnKodeWeb.Loader, FinnKodeWeb.Observable);

var Loader = null;
var LayoutManager = null;

function Start() {
    LayoutManager = new FinnKodeWeb.LayoutManager();
}

function CreateHashString(bookId, bookSectionId, nodeId, view, source) {
    var hashString = '#';
    if (bookId) {
        hashString += '|' + bookId;
        if (bookSectionId) {
            hashString += '|' + bookSectionId;
            if (nodeId) {
                hashString += '|' + nodeId;
                if (view) {
                    hashString += '|' + view;
                }
                if (source) {
                    hashString += '|' + source;
                }
            }
        }
    }
    return hashString;
}
function NavigateTo(bookId, bookSectionId, nodeId, view, source) {
    var hashString = CreateHashString(bookId, bookSectionId, nodeId, view, source);
    LayoutManager.navigate(hashString);
}
function UpdateLocationHash(bookId, bookSectionId, nodeId, view, source) {
    var hashString = CreateHashString(bookId, bookSectionId, nodeId, view, source);
    LayoutManager.updateLocationHash(hashString);
}
