﻿// JScript File

Ext.namespace('FinnKodeWeb');

FinnKodeWeb.PanelIframeFlowView = function(config) {
    config = config || {};
    this.bookSection = config.bookSection;
    this.currentObjectId = -1;
    this.nextObjectId = -1;
    this.loadingObjectId = -1;

    var defaultNavigated = false;
    var panel = this;

    var tb = new Ext.Toolbar({
        items: [ {
            xtype: 'tbfill'
            },
            new Ext.Toolbar.Button({
                icon: 'images/printer.png',
                text: 'Utskrift av kapittel',
                cls: 'x-btn-text-icon',
                handler: function() {
                    panel.frameEl.print();
                }
            })
        ]
    });

    var pathNavigator = new FinnKodeWeb.PathNavigatorToolbar({
        toolbar: tb,
        bookSection: this.bookSection,
        category: 'TableViewPathNavigator',
        listeners: {
            navigate: {
                fn: function(bookSectionId, objectId) {
                    if (bookSectionId != this.bookSection.BookSectionId || objectId != this.currentObjectId) {
                        NavigateTo(this.bookSection.BookId, bookSectionId, objectId);
                    }
                },
                scope: this
            }
        }
    });

    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);
    };
    var scrollToElement = function(element, framePanel) {
        var body = framePanel.frameEl.getBody();
        var scroll = body.getScroll();
        body.dom.scrollTop = element.getY() + scroll.top;
        //element.scrollIntoView(null, false);
        //        var top = element.getOffsetsTo(framePanel.getFrameBody())[1];
        //        framePanel.getFrameBody().scrollTo('top', top - 25, true);
    };

    Ext.apply(config, {
        autoScroll: true,
        layout: 'fit',
        tbar: tb,
        defaultSrc: this.bookSection.Book.FrontPageUrl,
        navigate: function(hashManager) {
            if (!hashManager || !hashManager.objectId) {
                return;
            }
            this.loadObject(hashManager.objectId);
            pathNavigator.loadObjectPath(this.bookSection.BookId, this.bookSection.BookSectionId, 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;
                    var element = this.getFrame().get(this.nextObjectId);
                    if (element) {
                        // loading of document not neccessary - just scroll to element
                        this.currentObjectId = objectId;
                        scrollToElement(element, this);
                        return;
                    }
                    if (this.nextObjectId <= 0) {
                        this.setSrc(this.bookSection.Book.FrontPageUrl);
                        this.loadingObjectId = this.nextObjectId;
                        this.currentObjectId = this.nextObjectId;
                        return;
                    }

                    MsgSvc.addMsg({
                        ShowMaskInfo: true,
                        MaskElement: this.body,
                        Category: 'FlowView',
                        Action: 'BeforeLoadObject',
                        Label: 'Laster ' + objectId,
                        TrackEvent: false
                    });

                    var succeededCallback = function(result, userContext, methodName) {
                        if (result && userContext) {
                            userContext.setSrc(result, true, function() {
                                var userContext = this;
                                var frameEl = userContext.getFrame();
                                MsgSvc.addMsg({
                                    MaskElement: userContext.body,
                                    Category: 'FlowView',
                                    Action: 'AfterLoadObject',
                                    Label: 'Lastet ' + userContext.nextObjectId,
                                    TrackEvent: false
                                });
                                var element = frameEl.get(userContext.nextObjectId);
                                if (element) {
                                    userContext.currentObjectId = userContext.nextObjectId;
                                    scrollToElement(element, userContext);
                                }
                                else {
                                    MsgSvc.addMsg({
                                        ShowAlertInfo: true,
                                        Category: 'FlowView',
                                        Action: 'AfterLoadObject',
                                        Label: 'Finner ikke objekt - ' + userContext.nextObjectId + ' - etter lasting',
                                        TrackEvent: false
                                    });
                                }
                                frameEl.writeScript('function NavigateTo(bookId, bookSectionId, objectId) { parent.NavigateTo(bookId, bookSectionId, objectId);}');
                                frameEl.getDoc().addListener('click', onClickHandler, this, { delegate: '.kodenr' });
                                frameEl.getDoc().addListener('click', onClickHandler, this, { delegate: '.gruppenr' });
                            }, userContext);
                            //                            Ext.Ajax.request({
                            //                                url: 'FinnKodeAppCss/HtmlLayout.css',
                            //                                success: function(response, opts) {
                            //                                    this.frameEl.CSS.createStyleSheet(response.responseText, 'FinnKodeHtmlCss');
                            //                                },
                            //                                failure: function(response, opts) {
                            //                                    console.log('server-side failure with status code ' + response.status);
                            //                                },
                            //                                scope: userContext
                            //                            });
                        }
                    };
                    var failedCallback = function(error, userContext, methodName) {
                        MsgSvc.addMsg({
                            ShowAlertInfo: true,
                            Category: 'FlowView',
                            Action: 'AfterLoadObject',
                            Label: userContext.nextObjectId,
                            TrackEvent: false
                        });
                    };

                    var bookId = this.bookSection.BookId;
                    var bookSectionId = this.bookSection.BookSectionId;
                    var userContext = this;
                    finnkode.kith.no.icontentservice.GetAppRelativeUrl(bookId, bookSectionId, objectId, succeededCallback, failedCallback, userContext);
                }
            }
            else {
                // Try scroll to object
                var element = this.getFrame().get(this.currentObjectId);
                if (element) {
                    scrollToElement(element, this);
                }
            }
        }
    });
    // Call parent (required)
    FinnKodeWeb.PanelIframeFlowView.superclass.constructor.call(this, config);


};

Ext.extend(FinnKodeWeb.PanelIframeFlowView, Ext.ux.ManagedIFrame.Panel);

Ext.reg('paneliframeflowview', FinnKodeWeb.PanelIframeFlowView);
