﻿// JScript File

Ext.namespace('FinnKodeWeb');

FinnKodeWeb.PanelFlowView = function(config) {
    config = config || {};
    this.bookSection = config.bookSection;
    this.currentObjectId = 0;
    this.nextObjectId = -1;
    this.loadingObjectId = 0;

    var _defaultNavigated = false;

    var onClickHandler = function(event, htmlEl, options) {
        var objectId = htmlEl.parentNode.id;
        this.currentObjectId = objectId;
        this.nextObjectId = objectId;
        this.loadingObjectId = objectId;
        NavigateTo(this.bookSection.BookId, this.bookSection.BookSectionId, objectId);
    };

    Ext.apply(config, {
        autoScroll: true,
        layout: 'fit',
        bodyStyle: { padding: '5px 5px 5px 5px' },
        listeners: {
            'render': {
                fn: function() {
                    this.body.addListener('click',
                        onClickHandler,
                        this,
                        {
                            delegate: '.kodenr'
                        }
                    );
                    this.body.addListener('click',
                        onClickHandler,
                        this,
                        {
                            delegate: '.gruppenr'
                        }
                    );
                },
                scope: this
            }
        },
        navigate: function(hashManager) {
            if (!hashManager || !hashManager.objectId) {
                return;
            }
            this.loadObject(hashManager.objectId);
        },
        navigateDefault: function() {
            if (!_defaultNavigated) {
                _defaultNavigated = true;
                this.loadObject(this.nextObjectId);
                this.addListener('activate', function() {
                    NavigateTo(this.bookSection.BookId, this.bookSection.BookSectionId, this.nextObjectId);
                }, this);
            }
        },
        updateHash: function(hashManager) {
            hashManager.bookSectionViewId = 'flow';
            hashManager.objectId = this.currentObjectId;
        },
        afterAppLoaded: function() {
            /*this.addListener('activate', function() {
            NavigateTo(this.bookSection.BookId, this.bookSection.BookSectionId, this.nextObjectId);
            }, this);*/
        },
        loadObject: function(objectId) {
            if (objectId == null) return;
            objectId = typeof (objectId) == "number" ? objectId.toString() : objectId;
            this.nextObjectId = objectId;
            if (this.currentObjectId != objectId && this.loadingObjectId != objectId) {
                if (this.isVisible()) {
                    this.loadingObjectId = objectId;

                    MsgSvc.addMsg({
                        ShowMaskInfo: true,
                        MaskElement: this.body,
                        Category: 'FlowView',
                        Action: 'BeforeLoadObject',
                        Label: 'Laster ' + objectId
                    });

                    if (this.nextObjectId <= 0) {
                        // Load front page for book section
                        var updater = this.getUpdater();
                        if (!updater) return;
                        updater.update({
                            text: 'Laster forside...',
                            url: this.bookSection.Book.FrontPageUrl
                        });
                        this.loadingObjectId = this.nextObjectId;
                        this.currentObjectId = this.nextObjectId;
                        return;
                    }

                    var succeededCallback = function(result, userContext, methodName) {
                        if (result && userContext) {
                            MsgSvc.addMsg({
                                MaskElement: userContext.body,
                                Category: 'FlowView',
                                Action: 'AfterLoadObject',
                                Label: 'Lastet ' + userContext.nextObjectId
                            });
                            userContext.body.update(result);
                            var domEl = Ext.getDom(userContext.nextObjectId);
                            if (domEl) {
                                userContext.currentObjectId = objectId;
                                var top = (Ext.fly(domEl).getOffsetsTo(userContext.body)[1]) + userContext.body.dom.scrollTop;
                                userContext.body.scrollTo('top', top - 25, true);
                            }
                            else {
                                MsgSvc.addMsg({
                                    ShowAlertInfo: true,
                                    Category: 'FlowView',
                                    Action: 'AfterLoadObject',
                                    Label: 'Finner ikke objekt - ' + userContext.nextObjectId + ' - etter lasting'
                                });
                            }
                        }
                    };
                    var failedCallback = function(error, userContext, methodName) {
                        MsgSvc.addMsg({
                            ShowAlertInfo: true,
                            Category: 'FlowView',
                            Action: 'AfterLoadObject',
                            Label: userContext.nextObjectId
                        });
                    };

                    var bookId = this.bookSection.BookId;
                    var bookSectionId = this.bookSection.BookSectionId;
                    var userContext = this;
                    var cs = new finnkode.kith.no.icontentservice();
                    cs.GetHtmlContent(bookId, bookSectionId, objectId, succeededCallback, failedCallback, userContext);
                }
            }
            else {
                // Try scroll to object
                var domEl = Ext.getDom(this.currentObjectId);
                if (domEl) {
                    var top = (Ext.fly(domEl).getOffsetsTo(this.body)[1]) + this.body.dom.scrollTop;
                    this.body.scrollTo('top', top - 25, true);
                }
            }
        }
    });
    // Call parent (required)
    FinnKodeWeb.PanelFlowView.superclass.constructor.call(this, config);


};

Ext.extend(FinnKodeWeb.PanelFlowView, Ext.Panel);

Ext.reg('panelflowview', FinnKodeWeb.PanelFlowView);
