// crear un espacio de nombre para los plugins
Ext.namespace('Ext.ux.plugins');
 
/**
 * Ext.ux.plugins.linkedCombo plugin for Ext.form.Combobox
 *
 * @autor  Ing. Omar Vidaña
 * @fecha    1 de junio del 2009
 *
 * @class Ext.ux.plugins.linkedCombo
 * @extends Ext.util.Observable
 * propiedad linkedCombo: id o Ext.component  para ahi tener la referencia al objeto combo hijo
 * propiedad reloadParams: array de variables para ser evaluadas al recargar el dataset del combo
 */
Ext.ux.plugins.linkedCombo = function(config) {
    Ext.apply(this, config);
};
 
// codigo del plugin
Ext.extend(Ext.ux.plugins.linkedCombo, Ext.util.Observable, {
    init:function(combo) {
		combo.addEvents(
			'changeValue'
        );		
 		Ext.apply(combo, {
			onSelect: combo.onSelect.createSequence(function(record, index){				
 				var cbxHijo = null;
				if(Ext.type(combo.linkedCombo)== 'string'){
					cbxHijo = Ext.getCmp(combo.linkedCombo);	
				}
				if(Ext.type(combo.linkedCombo) == 'object'){
					cbxHijo = combo.linkedCombo;
				}
				cbxHijo.reset();
    		}),
			onChangeValue: function(t,oldValue,newValue){
				var cbxHijo = null;
				if(Ext.type(combo.linkedCombo)== 'string'){
					cbxHijo = Ext.getCmp(combo.linkedCombo);	
				}
				if(Ext.type(combo.linkedCombo) == 'object'){
					cbxHijo = combo.linkedCombo;
				}
				if (cbxHijo.store.replaceParams) {
					var obj = new Object();
					obj.params = new Object();
					for (var i in cbxHijo.store.baseParams) {
						if (Ext.type(cbxHijo.store.baseParams[i]) == 'string') {
							eval("obj.params."+i+" = String.format(cbxHijo.store.baseParams[i], "+cbxHijo.store.replaceParams+")");						
						}
					}
				}
				cbxHijo.store.reload(obj);
			}//,
			//beforeSetValue: combo.setValue.createInterceptor(function(v){this.oldValue = this.value; return true;}),
			//afterSetValue: combo.setValue.createSequence(function(v){if(v!=this.oldValue){this.oldValue = v;this.fireEvent('changeValue', this, this.oldValue, v);}}) 
		}); // fin del apply combo
		combo.on('changeValue',combo.onChangeValue,combo);		
    } // fin del init
}); // fin del extend
 

