var tgdEPaper = tgdEPaper || null;
var app = app || null;
Ext.EpaperTrackerBase = function(config){
    Ext.apply(this, config);
    this.addEvents('load','pagechange','mediaclick','action');
    Ext.EpaperTrackerBase.superclass.constructor.call(this);
    this.initTracker();
};
Ext.extend(Ext.EpaperTrackerBase, Ext.util.Observable, {
    isNormal: false,
    isMobile: false,
    initTracker: function() {
        if (tgdEPaper) {
            this.initNormal();
        }
        if (app) {
            this.initMobile();
        }
    },
    initNormal: function() {
        this.isNormal = true;
        tgdEPaper.on({
            load: function() {
                this.fireEvent('load');
            },
            pagechange: function(page) {
                if (page == 0) page = 1;
                this.fireEvent('pagechange',page);
            },
            mediaclick: function(page,item) {
                this.fireEvent('mediaclick',page,item);
            },
            download: function() {
                this.fireEvent('download');
            },
            action: function(a) {
                this.fireEvent('action',a);
            },
            scope: this
        });
    },
    initMobile: function() {
        this.isMobile = true;
        app.on({
            launch: function() {
                this.fireEvent('load');
            },
            initplugins: function() {
                app.views.pageCarousel.on({
                    cardswitch: function(pc) {
                        this.fireEvent('pagechange',pc.realPage);
                    },
                    scope: this
                });
                if (app.plugins && app.plugins.MediaHandler) {
                    app.plugins.MediaHandler.on({
                        runitem: function(page,item) {
                            this.fireEvent('mediaclick',Number(page)+1,Number(item)+1);
                        },
                        scope: this
                    });
                }
            },
            downloadclick: function() {
                this.fireEvent('download');
            },
            cartclick: function() {
                var a = {
                    action: 'cartclicked'
                }
                this.fireEvent('action',a);
            },
            scope: this
        });
    }
});
Ext.EpaperTracker = new Ext.EpaperTrackerBase();

