﻿Ext.jx={
    plugins:{},
    History:{
        hids:[],
        disabled:false,
        pause:false,
        registered:false,
        onNavigate: function( hid ) {
            var History=Ext.jx.History;                
            if (!History.paused && !History.disabled){
                var o=History.hids[Number(hid)];
                if (o){
                    History.paused=true;
                    Ext.getCmp(o.id).setHistoryValue(o.value);
                    setTimeout(function(){
                        History.paused=false;
                    });
                }
            }
        },
 
        changedPage: function (o){
            var History=Ext.jx.History;                
            if (!History.paused && !History.disabled){
                History.hids.push({id:o.id, value:o.getHistoryValue() });
                History.paused=true;
                try{
                    YAHOO.util.History.navigate('hid', String(History.hids.length-1) )
                }catch(e){
                    setTimeout(function(){
                        try{
                            YAHOO.util.History.navigate('hid', String(History.hids.length-1) )
                        }catch(e){
                            History.disabled=true;
                        }
                    });
 
                }
                History.paused=false;
            }
        }
    }
}
Ext.jx.plugins.History={
    tabpanel:{
        prototype:{
            getHistoryValue: function(){    
                return this.getActiveTab();
            },
 
            setHistoryValue: function(value){
                this.setActiveTab(value);
            }            
        },
        init:function(o){
            o.on('tabchange', function(){
                Ext.jx.History.changedPage(this);
            });
        }
    },
 
    init:function(o){
        var mod=Ext.jx.plugins.History[o.getXType()];
        if (!mod){
            alert('programmer error: this xtype('+o.getXType()+') is not configured yet to use the history.');
        }else{
            Ext.apply(o, mod.prototype);
            mod.init(o);
        }
    }
}
