﻿
Ext.namespace('FinnKodeWeb');

FinnKodeWeb.PanelBookContent =
function(config) {
    this.config = config || {};

    var _isBookSectionsCreated = false;
    var _book = config.book;
    var _bookSectionsTabPanel = new Ext.TabPanel({
        deferredRender: true,
        border: false,
        bodyBorder: false,
        listeners: {
            'tabchange': {
                fn: function(tabpanel, panel) {
                    //alert(_book.BookId + 'boktab' + ' tabchange');
                    panel.createContents();
                    tabpanel.doLayout();
                    var activeTab = _bookSectionsTabPanel.getActiveTab();
                    if (activeTab && activeTab.navigateDefault) activeTab.navigateDefault();
                }
            }
        }
    });

    Ext.apply(this, {
        createTabs: function() {
            if (!_isBookSectionsCreated) {
                _isBookSectionsCreated = true;
                // for each bookSection in book
                for (var bookSectionIndex = 0; bookSectionIndex < _book.BookSections.length; bookSectionIndex++) {
                    var bookSection = _book.BookSections[bookSectionIndex];

                    // Create a panel for the book section. The panel will contain a tree and another tab panel for selecting views.
                    var bookSectionPanel = new FinnKodeWeb.PanelBookSectionContent({
                        bookSection: bookSection,
                        border: false,
                        bodyBorder: false,
                        hidden: true
                    });
                    _bookSectionsTabPanel.add(bookSectionPanel);
                }
                _bookSectionsTabPanel.doLayout();
                this.navigateDefault();
            }
        },
        navigate: function(hashManager) {
            if (!hashManager) return;
            if (hashManager.mainTabId != _book.BookId) return;
            if (!hashManager.bookSectionId) return;

            // Activate book tab
            _bookSectionsTabPanel.activate(NamingService.createId(hashManager.bookSectionId, 'BookSectionPanel'));
            // Within the active tab there is a tree and a tab panel for the different views. Get the tab panel to select the correct view
            _bookSectionsTabPanel.items.each(function(item, index, length) {
                if (item.navigate) {
                    item.navigate(hashManager);
                }
            });
        },
        navigateDefault: function() {
            // Activate the search tab
            _bookSectionsTabPanel.setActiveTab(0);
        },
        updateHash: function(hashManager) {
            hashManager.reset();
            hashManager.mainTabId = _book.BookId;
            var activeTab = _bookSectionsTabPanel.getActiveTab();
            if (activeTab && activeTab.updateHash) activeTab.updateHash(hashManager);
        },
        afterAppLoaded: function() {
            _bookSectionsTabPanel.items.each(function(panelBookSectionContent) {
                if (panelBookSectionContent.afterAppLoaded) panelBookSectionContent.afterAppLoaded();
            });
        },
        items: _bookSectionsTabPanel
    });
    FinnKodeWeb.PanelBookContent.superclass.constructor.call(this, config);
};

Ext.extend(FinnKodeWeb.PanelBookContent, Ext.Panel);

