Ext.ns('Ext.ux.plugins');
Ext.ns('Ext.ux.form');
Ext.ns('Ext.ux.panel');

Ext.QuickTips.init();
Ext.form.Field.prototype.msgTarget = 'side';

if (Ext && Ext.ux && Ext.ux.grid && Ext.ux.grid.GridFilters) {
	Ext.override(Ext.ux.grid.GridFilters, {
		buildQuery: function(filters){
			return {
				filters: Ext.encode(filters)
			};
		}
	});
};

Ext.form.Action.Submit.prototype.run = Ext.form.Action.Submit.prototype.run.createInterceptor(function(){
	this.form.items.each(function(item){
		if (item.el.getValue() == item.emptyText) {
			item.el.dom.value = '';
		}
	});
});

Ext.form.Action.Submit.prototype.run = Ext.form.Action.Submit.prototype.run.createSequence(function(){
	this.form.items.each(function(item){
		if (item.el.getValue() == '' && item.emptyText) {
			item.el.dom.value = item.emptyText;
		}
	});
});

//Ext.override(Ext.form.CheckboxGroup, {
//	getName: function(){
//	
//		return this.items.first().getName();
//	},
//	getNames: function(){
//		var n = [];
//		this.items.each(function(item){
//		
//			if (item.getValue()) {
//				n.push(item.getName());
//			}
//		});
//		
//		return n;
//	},
//	getValues: function(){
//		var v = [];
//		this.items.each(function(item){
//		
//			if (item.getValue()) {
//				v.push(item.getRawValue());
//			}
//		});
//		
//		return v;
//	},
//	setValue: function(v){
//		a = eval('[' + v + ']');
//		this.items.each(function(item){
//			for (var i = 0; i < a.length; i++) {
//			
//				if (a[i] == item.inputValue) {
//					item.setValue(true);
//				}
//			}
//		});
//	},
//	setValues: function(v){
//		alert('setValues:' + v);
//		var r = new RegExp('(' + v.join('|') + ')');
//		this.items.each(function(item){
//			item.setValue(r.test(item.getRawValue()));
//		});
//	}
//});

//Ext.override(Ext.form.RadioGroup, {
//	getName: function(){
//	
//		return this.items.first().getName();
//	},
//	getValue: function(){
//		var v;
//		this.items.each(function(item){
//			v = item.getRawValue();
//			
//			return !item.getValue();
//		});
//		
//		return v;
//	},
//	setValue: function(v){
//		this.items.each(function(item){
//			item.setValue(item.getRawValue() == v);
//		});
//	}
//});
/* Create a NULL plugin to deal with */
Ext.ux.plugins.Null = function(){
};
Ext.ux.plugins.Null.prototype = {
	init: function(){
	}
};


//Ext.override(Ext.form.CheckboxGroup, {
//	afterRender: function(){
//		Ext.form.CheckboxGroup.superclass.afterRender.apply(this, arguments);
//		var form = this.findParentByType('form').getForm();
//		form.add.apply(form, this.items.items);
//	}
//});


/*
 * NOTE: No override needed for ReadioBox as it extends checkbox.
 */
Ext.override(Ext.ux.grid.GridFilters, {
	buildQuery: function(filters){
		return {
			filters: Ext.encode(filters)
		}
	}
});


Ext.ux.WizardPanel = Ext.extend(Ext.Panel, {
	currentPage: 0,
	initComponent: function(config){
		Ext.apply(this, {
			layout: 'card',
			layoutConfig: {
				deferredRender: false
			},
			frame: true,
			activeItem: 0,
			bbar: [new Ext.Toolbar.Fill(), '-']
		});
		Ext.ux.WizardPanel.superclass.initComponent.call(this);
	},
	onRender: function(ct, position){
		Ext.ux.WizardPanel.superclass.onRender.call(this, ct, position);
		tb = this.getBottomToolbar();
		this.restartButton = tb.addButton({
			disabled: true,
			text: '<< Restart',
			scope: this,
			handler: this.goRestart
		});
		tb.addSeparator();
		this.backButton = tb.addButton({
			disabled: true,
			text: '<< Back',
			scope: this,
			handler: this.goPreviousPage
		});
		tb.addSeparator();
		this.nextButton = tb.addButton({
			scope: this,
			disabled: false,
			text: 'Next >>',
			handler: function(){
				this.goNextPage()
			}
		});
		tb.addSeparator();
		this.repeatButton = tb.addButton({
			disabled: true,
			text: 'Repeat >>',
			scope: this,
			handler: this.goRepeatPage
		});
		tb.addSeparator();
		this.finishButton = tb.addButton({
			disabled: true,
			text: 'Finish',
			scope: this,
			handler: this.finish
		});
		this.on('afterlayout', function(){
		
			if (this.layout.activeItem.fxAllowMultiple == 'true') {
				this.repeatButton.setDisabled(false);
			} else {
				this.repeatButton.setDisabled(true);
			}
		}, this);
	},
	onAfterLayout: function(ct, layout){
		alert('asd');
		Ext.ux.WizardPanel.superclass.onAfterLayout.call(this, ct, layout);
	},
	changePage: function(x){
		this.currentPage += x;
		this.getLayout().setActiveItem(this.currentPage);
		
		if (this.layout.activeItem.fxAllowMultiple == 'true') {
			this.repeatButton.setDisabled(false);
		} else {
			this.repeatButton.setDisabled(true);
		}
		this.backButton.setDisabled(this.currentPage == 0);
		this.nextButton.setDisabled(this.currentPage ==
		this.items.getCount() -
		2);
		this.finishButton.setDisabled(this.currentPage !=
		this.items.getCount() -
		2);
	},
	goRepeatPage: function(x){
		try {
			OnNextButton(this.layout.activeItem);
		} 
		catch (e) {
		}
		for (var i = 0; i < this.findByType('form').length; i++) {
			this.findByType('form')[i].getForm().reset();
		}
	},
	goNextPage: function(x){
		try {
			OnNextButton(this.layout.activeItem);
		} 
		catch (e) {
		}
		this.changePage(1);
	},
	goPreviousPage: function(x){
		this.changePage(-1);
	},
	goRestart: function(x){
		for (var i = 0; i < this.findByType('form').length; i++) {
			this.findByType('form')[i].getForm().reset();
		}
		this.currentPage = 0;
		this.changePage(0);
		this.restartButton.setDisabled(true);
	},
	finish: function(x){
		try {
			OnFinishButton(this.layout.activeItem);
		} 
		catch (e) {
		}
		this.changePage(1);
		this.backButton.setDisabled(true);
		this.nextButton.setDisabled(true);
		this.restartButton.setDisabled(false);
	}
});
Ext.reg("uxwizardpanel", Ext.ux.WizardPanel);

Ext.ux.form.XCheckbox = Ext.extend(Ext.form.Checkbox, {
	submitOffValue: 'false',
	submitOnValue: 'true',
	onRender: function(ct){
		this.inputValue = this.submitOnValue;
		// call parent
		Ext.ux.form.XCheckbox.superclass.onRender.apply(this, arguments);
		// create hidden field that is submitted if checkbox is not
		// checked
		this.hiddenField = this.wrap.insertFirst({
			tag: 'input',
			type: 'hidden'
		});
		// update value of hidden field
		this.updateHidden();
	} // eo function onRender
	/**
	 * Calls parent and updates hiddenField
	 *
	 * @private
	 */
	,
	setValue: function(val){
		Ext.ux.form.XCheckbox.superclass.setValue.apply(this, arguments);
		this.updateHidden();
	} // eo function setValue
	/**
	 * Updates hiddenField
	 *
	 * @private
	 */
	,
	updateHidden: function(){
	
		if (this.hiddenField) {
			this.hiddenField.dom.value = this.checked ? this.submitOnValue : this.submitOffValue;
			this.hiddenField.dom.name = this.checked ? '' : this.el.dom.name;
		}
	} // eo function updateHidden
}); // eo extend
// register xtype
Ext.reg('xcheckbox', Ext.ux.form.XCheckbox);


//By adding setValues() this will be called on form load and can set the displayValue()

Ext.override(Ext.form.ComboBox, {
	setValue: function(v, values){
		var text = v;
		var displayValue = null
		if (this.initialDisplayField && values) {
			displayValue = values[this.initialDisplayField];
		} else if (this.valueField) {
			var r = this.findRecord(this.valueField, v);
			if (r) {
				displayValue = r.data[this.displayField];
			}
		}
		if (displayValue) {
			text = displayValue;
		} else if (Ext.isDefined(this.valueNotFoundText)) {
			text = this.valueNotFoundText;
		}
		
		this.lastSelectionText = text;
		if (this.hiddenField) {
			this.hiddenField.value = v;
		}
		Ext.form.ComboBox.superclass.setValue.call(this, text);
		this.value = v;
		return this;
	}
});

Ext.override(Ext.form.BasicForm, {
	setValues: function(values){
		if (Ext.isArray(values)) { // array of objects
			for (var i = 0, len = values.length; i < len; i++) {
				var v = values[i];
				var f = this.findField(v.id);
				if (f) {
					f.setValue(v.value);
					if (this.trackResetOnLoad) {
						f.originalValue = f.getValue();
					}
				}
			}
		} else { // object hash
			var field, id;
			for (id in values) {
				if (!Ext.isFunction(values[id]) && (field = this.findField(id))) {
					field.setValue(values[id], values);
					if (this.trackResetOnLoad) {
						field.originalValue = field.getValue();
					}
				}
			}
		}
		return this;
	}
});

Ext.override(Ext.form.Field, {
	getFormPanel: function(){
		return this.findParentBy(function(x){
			return x.isXType('form');
		})
	}
});


notify = function(message,title){
	new Ext.ux.Notification({
		title: title ? title : 'Notification',
		html: message
	}).show(document);
}

