Ext.define('TP.Pantab', {
    extend: 'Ext.panel.Panel',

    title:       'Dashboard',
    tooltip:     'double click to rename',
    closable:    true,
    stateful:    true,
    window_ids:  false,
    stateEvents: ['add', 'titlechange'],
    initComponent: function() {
        this.callParent();
        this.window_ids = [];
    },
    listeners: {
        beforedestroy: function( This, eOpts ) {
            for(nr in This.window_ids) {
                Ext.getCmp(This.window_ids[nr]).destroy();
            }
            var tabpan = Ext.getCmp('tabpan');
            tabpan.item_ids = TP.removeFromList(tabpan.item_ids, this.id);
            tabpan.saveState();
            cp.clear(this.id);
        },
        activate: function(This, eOpts) {
            for(nr in This.window_ids) {
                Ext.getCmp(This.window_ids[nr]).show();
            }
        },
        hide: function(This, eOpts) {
            for(nr in This.window_ids) {
                var pantab = Ext.getCmp(This.window_ids[nr]);
                if(pantab && !pantab.xdata.pinned) {
                    pantab.hide();
                }
            }
        },
        afterrender: function(This, eOpts) {
            var header = This.getDockedItems()[0];
            if(header) { header.hide() }
        },
        beforerender: function(This, eOpts) {
            for(nr in This.window_ids) {
                var pantab = Ext.getCmp(This.window_ids[nr]);
                if(pantab && !pantab.xdata.pinned) {
                    pantab.hide();
                }
            }
        }
    },
    getState: function() {
        return {
            title:      this.title,
            window_ids: this.window_ids
        }
    },
    applyState: function(state) {
        if(state) {
            for(nr in state.window_ids) {
                if(state.window_ids[nr]) {
                    TP.add_panlet({id:state.window_ids[nr], skip_state:true}, false);
                }
            };
            this.window_ids = state.window_ids;
            this.setTitle(state.title);
            Ext.apply(this, state);
        }
    }
});
