

Top10.form.ComboField = Ext.extend(Ext.form.ComboBox, {
	
    width: 120,
    context: null,
    parentContext: null,
    childContext: null,
    
    initComponent: function() {
        var config = {
        	mode: 'local',
            name: 'context_id',
            displayField: 'name',
            valueField: 'id',
            triggerAction: 'all',
            listClass: 'x-combo-list-small',
            editable: false,
            store: {xtype: 'contextstore', context: this.context}
        };

        Ext.apply(this, config);
        Ext.apply(this.initialConfig, config);
        
        Top10.form.ComboField.superclass.initComponent.apply(this, arguments);
    },
    
    onRender: function(ct, position) {
        Top10.form.ComboField.superclass.onRender.call(this, ct, position);
        
        this.manager = this.ownerCt;
        
        if (this.parentContext)
            this.parentContext.childContext = this;
        
        this.store.on({
        	scope: this,
            beforeload: this.disable,
            load: this.enable
        });
        
        this.on('select', function() {
        	var context, contextId;
        	
        	this.disableChildContexts();
        	
            if (this.getValue() == 0) {
            	context = this.getParentContext();
            	contextId = this.getParentContextId();
            } else {
                context = this.context;
                contextId = this.getValue();
                this.enableChildContext();
            }
            
            this.manager.fireEvent('contextchange', context, contextId);
        }, this);
    },
    
    disableChildContexts: function() {
        var cc = this.childContext;
        
    	if (cc) {
    		cc.disable();
    		cc.setValue(I18n.All);
    		cc.disableChildContexts();
    	}
    },
    
    enableChildContext: function() {
    	var cc = this.childContext;
    	
        if (cc) {
            cc.enable();
            cc.setValue(I18n.All);
            cc.store.load({params: {parent_id: this.getValue()}});
        }
    },
    
    getParentContext: function() {
    	return this.parentContext.context || this.parentContext;
    },
    
    getParentContextId: function() {
        return this.parentContext.context ? this.parentContext.getValue() : false;
    },
    
    isParent: function(context, recursive) {
    	var recursive = recursive || true;
    	
    	if (!this.getParentContext()) return false;
    	
    	if (this.getParentContext() === context) {
    		return true;
    	} else {
    		if (recursive) {
    			return this.parentContext.isParent(context);
    		}
    	}
    	
    	return false;
    },
    
    isChild: function(context, recursive) {
        var context = context.context || context,
            recursive = recursive || true;
        
        if (!this.childContext) return false;
        
        if (this.childContext.context === context) {
            return true;
        } else {
            if (recursive) {
                return this.childContext.isChild(context);
            }
        }
        
        return false;
    }
});

Ext.reg('contextfield', Top10.form.ComboField);