Ext.define('Ext.ux.gallery.FieldRelation', {

	extend: 'Ext.ux.gallery.Field',

	alias: ['widget.mediagalleryfieldrelation'],

	// Object with the serverParams set to pass on
	// directly to the service
	// Will be gotten from the following 4 values from XPD
	serverParams: null,

	// These 4 values will be added to the serverParams config
	// object used for the service
	parentEntity: '',
	parentEnumType: '',
	childEntity: '',
	childEnumType: '',
	records: null,
	language: '',

	galleryAlias: 'mediagalleryrelation',

	initComponent: function () {

		var i_cmp = this;
		var o_config = i_cmp.initialConfig;
		i_cmp.records = {};
		i_cmp.language = wsh.getLanguages()[0];
		Ext.apply(i_cmp, {
			parentEntity: o_config.parentEntity || i_cmp.parentEntity,
			parentEnumType: o_config.parentEnumType || i_cmp.parentEnumType,
			childEntity: o_config.childEntity || i_cmp.childEntity,
			childEnumType: o_config.childEnumType || i_cmp.childEnumType
		});

		i_cmp.callParent(arguments);

		i_cmp.on({
			reset: function() {
				i_cmp.doReset();
			}
		});

		return i_cmp;
	},

	doSave: function (f_callback) {

		return this.getGallery().doSave(f_callback);
	},

	isDirty: function () {

		var i_self = this;
		var b_dirty = false;
		var a_language = wsh.getLanguages();
		var i_gallery = i_self.getGallery();
		var value = i_gallery.getValues();

		var o_value = i_self.records[i_self.language] || {
			originalValue: [],
			value: [],
			dirty: false,
			wasDirty: false
		};

		var originalValue = o_value.originalValue;
		var s_originalValue = "";
		var s_value = "";

		try {
			s_originalValue = Ext.encode(originalValue);
			s_value = Ext.encode(value);
		}
		catch (err) {}

		b_dirty = s_originalValue != s_value;

		return b_dirty;
	},

	changeLanguage: function(s_lang) {

		var i_self = this;
		var i_gallery = i_self.getGallery();
		var value = i_gallery.getValues();
		var b_dirty = i_self.isDirty();
		i_self.suspendCheckChange++;

		var o_language = i_self.records[i_self.language];
		o_language.dirty = b_dirty;
		o_language.value = value;

		i_self.language = s_lang;
		i_gallery.changeLanguage(s_lang);

		i_self.suspendCheckChange--;
		i_self.checkDirty();
	},

	checkChange: function () {

		var i_cmp = this;
		i_cmp.callParent(arguments);

		return i_cmp.isDirty();
	},

	checkDirty: function() {
		var i_self = this;
		var isDirty = i_self.isDirty();
		if (isDirty !== i_self.wasDirty) {
			i_self.fireEvent('dirtychange', i_self, isDirty);
			i_self.onDirtyChange(isDirty);
			i_self.wasDirty = isDirty;
		}
	},

	getValue: function() {

		var i_self = this;
		var i_gallery = i_self.getGallery();
		var value = i_gallery.getValues();
		var b_empty = i_self.isEmptyValue(value);

		if (i_self.isEmptyValue(value)) {
			value = '';
		}

		return value;
	},

	setValue: function(value) {

		var i_self = this;
		i_self.value = value;
	},

	doReset: function() {

		var i_self = this;
		var i_gallery = i_self.getGallery();
		var value = i_gallery.getValues();
		i_self.records[i_self.language] = {
			originalValue: value,
			value: value,
			dirty: false,
			wasDirty: false
		};

		i_self.resetOriginalValue();
		i_self.wasDirty = true;
		i_self.fireEvent("change", i_self, value);
		i_self.checkDirty();
	},

	resetOriginalValue: function() {

		var i_self = this;
		i_self.originalValue = i_self.value = i_self.getValue();
	}
});
