

Top10.ContextToolbar = Ext.extend(Ext.Toolbar, {
	
	viewText: I18n.View,

    initComponent: function() {
    	
    	var world = new Top10.form.ComboField({
    	    context: 'World',
    	    parentContext: '',
    	    hidden: true
    	});
    	
    	var continent = new Top10.form.ComboField({
            context: 'Continent',
            parentContext: world,
            value: I18n.World
        });
        continent.store.load();
        
        this.previousContext = {name: 'World', id: ''};
        this.currentContext = {name: 'World', id: ''};
        
        var country = new Top10.form.ComboField({
            context: 'Country',
            parentContext: continent,
            value: I18n.Countries,
            disabled: true
        });
        
        var city = new Top10.form.ComboField({
            context: 'City',
            parentContext: country,
            value: I18n.Cities,
            disabled: true
        });
        
        var config = {
            items: [this.viewText, world, '', continent, '', country, '', city]
        };
        
        Ext.apply(this, config);
        Ext.apply(this.initialConfig, config);
        
        Top10.ContextToolbar.superclass.initComponent.apply(this, arguments);
        
        this.addEvents('contextchange');
        this.on('contextchange', this.onContextChange, this);
        
        this.ownerCt.ContextMgr = this;
    },
    
    onRender: function(ct, position) {
        Top10.ContextToolbar.superclass.onRender.call(this, ct, position);
    },
    
    onContextChange: function(context, contextId) {
        this.previousContext.name = this.currentContext.name;
        this.previousContext.id = this.currentContext.id;
    	this.currentContext.name = context;
    	this.currentContext.id = contextId;
    	
    	var f = this.getField(context);
    	
    	if (f) {
    		f.setValue(contextId);
    	}
    },
    
    getField: function(context) {
    	return this.find('context', context)[0];
    },
    
    getChangeDirection: function() {
    	var direction;
    	
    	if (this.currentContext.name === this.previousContext.name) {
    		direction = 'same';
    	} else if (this.getField(this.currentContext.name).isParent(this.previousContext.name)) {
    		direction = 'down';
    	} else if (this.getField(this.currentContext.name).isChild(this.previousContext.name)) {
    		direction = 'up';
    	}
    	
    	return direction;
    }
});

Ext.reg('contexttoolbar', Top10.ContextToolbar);