Ext.define("Ext.ux.call2action.Call2Action", {

	extend: "Ext.form.FieldContainer",

	mixins: {
		field: "Ext.form.field.Field"
	},

	alias: [
		"widget.ip2Call2ActionGrid"
	],

	originalValue: [],
	value: [],
	dirty: false,

	config: {
		behaviours: null,
		targets: null,
		grid: null
	},

	initComponent: function() {

		var i_self = this;
		var o_initialConfig = i_self.initialConfig;
		var value = o_initialConfig.value;

		Ext.tip.QuickTipManager.init();

		i_self.dirty = false;

		i_self.callParent();

		// create store for combobox, etc
		var i_behaviourStore = Ext.create("Ext.data.Store", {
			storeId: "call2action_behaviours",
			fields: ["key", "value"],
			data: [{
				key: "success",
				value: ip2.i18n({
					da: "Success",
					en: "Success"
				})
			},{
				key: "primary",
				value: ip2.i18n({
					da: "Primær",
					en: "Primary"
				})
			},{
				key: "default",
				value: ip2.i18n({
					da: "Neutral",
					en: "Neutral"
				})
			},{
				key: "info",
				value: ip2.i18n({
					da: "Info",
					en: "Info"
				})
			},{
				key: "inverse",
				value: ip2.i18n({
					da: "Negativ",
					en: "Inverse"
				})
			},{
				key: "whiteblack",
				value: ip2.i18n({
					da: "Hvid-BG / Sort-tekst",
					en: "White-BG / Black-text"
				})
			}],
			renderer: function(key) {
				var store = Ext.data.StoreManager.get("call2action_behaviours");
				var r = store.findRecord("key", key);
				return (r ? r.data.value : key);
			}
		});

		var i_targetsStore = Ext.create("Ext.data.Store", {
			storeId: "call2action_targets",
			fields: ["key", "value"],
			data: [{
				key: "parent",
				value: ip2.i18n({
					da: "Samme vindue (Normal)",
					en: "Same window (Normal)"
				})
			},{
				key: "blank",
				value: ip2.i18n({
					da: "Nyt vindue",
					en: "New window"
				})
			},{
				key: "lightbox",
				value: ip2.i18n({
					da: "Lightbox",
					en: "Lightbox"
				})
			}],renderer: function(key) {
				var store = Ext.data.StoreManager.get("call2action_targets");
				var r = store.findRecord("key", key);
				return (r ? r.data.value : key);
			}
		});

		i_self.setBehaviours(i_behaviourStore);
		i_self.setTargets(i_targetsStore);

		// Add grid with call-to-action rows
		var i_grid = Ext.widget("ip2Call2ActionGridGrid", {
			wrapper: i_self,
			data: value  || []
		});

		i_self.setGrid(i_grid);

		i_self.add(i_grid);

		// Bubble the "savecomplete" event up
		i_self.relayEvents(i_grid, ["savecomplete"]);

		i_self.initField();


		return i_self;
	},

	changeLanguage: function(s_lang) {

		var i_self = this;
		var i_grid = i_self.getGrid();
		i_grid.changeLanguage(s_lang);
	},

	/* ----------------------------------------------------------------------------------------------------
	    Get value
	   ---------------------------------------------------------------------------------------------------- */
	getValue: function() {

		var i_self = this;
		var i_grid = i_self.getGrid();
		var value;

		if (i_grid) {
			value = i_grid.getValue();
		}
		else {
			value = i_self.mixins.field.getValue.call(i_self);
		}
		if (value == "[]") {
			value = "";
		}

		return value;
	},

	/* ----------------------------------------------------------------------------------------------------
	    Get value for submit form
	   ---------------------------------------------------------------------------------------------------- */
	getSubmitValue: function() {

		var i_self = this;

		return i_self.getValue();
	},

	/* ----------------------------------------------------------------------------------------------------
	    Set value
	   ---------------------------------------------------------------------------------------------------- */
	setValue: function(value) {

		var i_self = this;
		var i_grid = i_self.getGrid();

		i_grid.setValue(value);

		i_self.value = value;
	},

	checkChange: function () {

		var i_self = this;

		var newVal = i_self.getValue();
		var oldVal = i_self.lastValue || [];

		if (!i_self.suspendCheckChange) {

			newVal = Ext.decode(newVal, true) || newVal;
			oldVal = Ext.decode(oldVal, true) || oldVal;

			var s_new = newVal;
			var s_old = oldVal;

			if (Ext.isObject(newVal)) {
				s_new = Ext.encode(newVal);
			}

			if (Ext.isObject(oldVal)) {
				s_old = Ext.encode(oldVal);
			}

			if (s_new != s_old) {
				i_self.lastValue = newVal;
				i_self.fireEvent('change', i_self, newVal, oldVal);
				i_self.onChange(newVal, oldVal);
			}
		}
	},

	disable: function() {

		var i_self = this;
		var i_grid = i_self.getGrid();

		i_self.callParent(arguments);

		if (i_self.isDisabled()) {

			i_grid.disable();
		}
	},

	enable: function() {

		var i_self = this;
		var i_grid = i_self.getGrid();

		i_self.callParent(arguments);

		if (!i_self.isDisabled()) {

			i_grid.enable();
		}
	},

	/* ----------------------------------------------------------------------------------------------------
	    Is Dirty (Children mark dirty)
	   ---------------------------------------------------------------------------------------------------- */
	isDirty: function() {

		var i_self = this;
		var i_grid = i_self.getGrid();

		var o_value = i_grid.getValue() || [];
		var o_originalValue = i_self.originalValue || [];
		var s_value = Ext.encode(o_value);
		var s_originalValue = Ext.encode(o_originalValue);

		return s_originalValue != s_value;
	},

	/* ----------------------------------------------------------------------------------------------------
	    Set Dirty - called from child components
	   ---------------------------------------------------------------------------------------------------- */
	setDirty: function(value) {
		return value
	},

	/* ----------------------------------------------------------------------------------------------------
	    Save every thing
	   ---------------------------------------------------------------------------------------------------- */
	doSave: function(f_callback) {
		//this.grid.doSave();
		if (f_callback) {
			f_callback();
		}
	}
});
