/**
 * Correção de diversos bugs dos componentes  
 * Ext.form.Checkbox e Ext.form.Radio reportados
 * na secao de bugs do Forum do Ext.
 * 
 * http://extjs.com/forum/showthread.php?t=44603
 *  
 */
Ext.override(Ext.form.Checkbox, {
	onRender: function(ct, position){
		Ext.form.Checkbox.superclass.onRender.call(this, ct, position);
		if(this.inputValue !== undefined){
			this.el.dom.value = this.inputValue;
		}
		//this.el.addClass('x-hidden');
		this.innerWrap = this.el.wrap({
			//tabIndex: this.tabIndex,
			cls: this.baseCls+'-wrap-inner'
		});
		this.wrap = this.innerWrap.wrap({cls: this.baseCls+'-wrap'});
		if(this.boxLabel){
			this.labelEl = this.innerWrap.createChild({
				tag: 'label',
				htmlFor: this.el.id,
				cls: 'x-form-cb-label',
				html: this.boxLabel
			});
		}
		this.imageEl = this.innerWrap.createChild({
			tag: 'img',
			src: Ext.BLANK_IMAGE_URL,
			cls: this.baseCls
		}, this.el);
		if(this.checked){
			this.setValue(true);
		}else{
			this.checked = this.el.dom.checked;
		}
		this.originalValue = this.checked;
	},
	afterRender: function(){
		Ext.form.Checkbox.superclass.afterRender.call(this);
		//this.wrap[this.checked ? 'addClass' : 'removeClass'](this.checkedCls);
		this.imageEl[this.checked ? 'addClass' : 'removeClass'](this.checkedCls);
	},
	initCheckEvents: function(){
		//this.innerWrap.removeAllListeners();
		this.innerWrap.addClassOnOver(this.overCls);
		this.innerWrap.addClassOnClick(this.mouseDownCls);
		//this.innerWrap.on('click', this.onClick, this);
		this.el.on('click', this.onClick, this);
		this.imageEl.on('click', this.onClick, this);
		//this.innerWrap.on('keyup', this.onKeyUp, this);
	},
	onFocus: function(e) {
		Ext.form.Checkbox.superclass.onFocus.call(this, e);
		//this.el.addClass(this.focusCls);
		this.innerWrap.addClass(this.focusCls);
	},
	onBlur: function(e) {
		Ext.form.Checkbox.superclass.onBlur.call(this, e);
		//this.el.removeClass(this.focusCls);
		this.innerWrap.removeClass(this.focusCls);
	},
	onClick: function(e){
		if (Ext.isSafari) {
			this.el.focus();
		}
		if (!this.disabled && !this.readOnly) {
			this.toggleValue();
		}
		//e.stopEvent();
	},
	onEnable: Ext.form.Checkbox.superclass.onEnable,
	onDisable: Ext.form.Checkbox.superclass.onDisable,
	onKeyUp: undefined,
	setValue: function(v) {
		var checked = this.checked;
		this.checked = (v === true || v === 'true' || v == '1' || String(v).toLowerCase() == 'on');
		if(this.rendered){
			this.el.dom.checked = this.checked;
			this.el.dom.defaultChecked = this.checked;
			//this.wrap[this.checked ? 'addClass' : 'removeClass'](this.checkedCls);
			this.imageEl[this.checked ? 'addClass' : 'removeClass'](this.checkedCls);
		}
		if(checked != this.checked){
			this.fireEvent("check", this, this.checked);
			if(this.handler){
				this.handler.call(this.scope || this, this, this.checked);
			}
		}
	}
});
Ext.override(Ext.form.Radio, {
	checkedCls: 'x-form-radio-checked'
});

/**
 * Adição do método serialize aos comonentes Store.
 * Esse método retorna um array de records codificados como
 * objetos JSON.
 * Recebe como parametro o valor modifiedOnly, se for true envia somente os registros alterados
 * se for false envia todos os registros. Default: true.
 * 
 * Obs: Até o momento só foi testado com JsonStore.
 */
Ext.override(Ext.data.Store, {
	serialize: function(modifiedOnly){
		var recordsRange;
		var retornar = [];

		if (modifiedOnly == null){
			modifiedOnly = true;
		}

		if (modifiedOnly){
			recordsRange = this.getModifiedRecords();
		} else {
			recordsRange = this.getRange();
		}

		for (var r=0; r<recordsRange.length; r++){
			retornar.push(Ext.util.JSON.encode(recordsRange[r].data));
		}

		return retornar;
	}
});



