

Top10.data.ContextStore = Ext.extend(Ext.data.JsonStore, {
    constructor: function(config) {
        Ext.apply(config, {
            url: Top10.APP_URL + 'widgets/context',
            root: 'records',
            successProperty: 'success',
            fields: ['id', 'name'],
            baseParams: {
                context: config.context,
                parent_id: config.parent_id
            }
        });
        
        Top10.data.ContextStore.superclass.constructor.call(this, config);
        
        this.on('load', function(store, records, options) {
            if (this.getCacheKey(options.params))
               Top10.Cache.add(this.getCacheKey(options.params), store.reader.jsonData);
        });
    },
    
    load: function(o) {
    	var key = this.getCacheKey(Ext.isDefined(o) ? Ext.apply(this.baseParams, o.params) : this.baseParams);
        var data = Top10.Cache.get(key);
        
        if (Ext.isDefined(data) && data.success) {
        	this.loadData(data, false);
        } else {
        	Top10.data.ContextStore.superclass.load.call(this, o);
        }
    },
    
    getCacheKey: function(o) {
    	var key = 'contextstore_';
    	
    	if (o) {
    		key += o.parent_id ? String.format('{0}#{1}', o.context, o.parent_id) : o.context;
    		return key;
    	}  
    	
    	return false;
    }
});

Ext.reg('contextstore', Top10.data.ContextStore);

Top10.Cache.add('contextstore_Continent', {"success":true,"records":[{"name":"All","id":0},{"name":"Africa","id":"2"},{"name":"Asia","id":"1"},{"name":"Australia","id":"3"},{"name":"Europe","id":"4"},{"name":"North America","id":"5"},{"name":"South America","id":"6"}]});
