{

	xtype: 'container',

	requires: [
		'Ext.ux.xpd.Field'
	],

	layout: 'fit',

	xpd: '',
	widgetConfig: null,
	propertyKey: '',
	dataParent: null,
	xpdField: null,
	value: '',

	parentRecord: null,
	parentField: null,

	onInitialize: function(i_widget, i_owner) {

		var o_config = i_owner._config,
			i_dataParent = i_owner.dataParent,
			s_propertyKey = o_config.propertyKey;

		// Find the parent field and record if present
		var i_parentField = i_dataParent.getForm().findField(s_propertyKey) || null,
			i_parentRecord = i_dataParent.getRecord() || null;

		Ext.apply(i_widget, {
			xpd: o_config.xpd,
			widgetConfig: o_config,
			propertyKey: s_propertyKey,
			dataParent: i_dataParent,
			parentRecord: i_parentRecord,
			parentField: i_parentField,
			value: i_owner.value
		});

		i_widget.initValue();

		i_widget.initXpdField();

		i_widget.initEvents();
	},

	initValue: function () {

		var i_widget = this,
			o_value = i_widget.value,
			s_key = i_widget.propertyKey,
			s_value;

		// Are we creating a new
		if ( i_widget.dataParent.action == 'new' ) {
			
			s_value = ip2.JSON.stringify(o_value);

			i_widget.parentField.setValue(s_value);
		}
		// We are not - look for data in dataParent
		else {
			
			var s_fieldValue = i_widget.parentField ? i_widget.parentField.getValue() : '',
				s_recordValue = i_widget.parentRecord ? i_widget.parentRecord.get(s_key) : '';
			
			// Get the value - field value is > record value
			s_value = s_fieldValue || s_recordValue;
		}

		i_widget.value = s_value;
	},
	initXpdField: function () {

		var i_widget = this,
			o_config = i_widget.widgetConfig,
			s_value = i_widget.value;

		i_widget.xpdField = i_widget.add({
			xtype: 'xpdfield',
			name: i_widget.propertyKey + ip2.unique(),
			margin: 10,
			itemId: o_config.name,
			xpd: o_config.xpd,
			datParent: i_widget.dataParent,
			value: s_value
		});

		/*
			s_value = i_widget.xpdField.getValue();
			i_widget.setParentValue(s_value);
		*/
	},

	initEvents: function () {

		var i_widget = this;

		// Update the parent on xpdField change
		i_widget.xpdField.on('change', function (i_self, s_value) {
			
			var i_field = i_widget.parentField,
				s_match = i_widget.getParentValue();
			
			if ( s_value != s_match ) {
				i_widget.setParentValue(s_value);
			}
		});

		// Update the xpdField on parentField
		i_widget.parentField.on('change', function (i_self, s_value) {

			var i_field = i_widget.xpdField,
				s_match = i_field.getValue();
			
			if ( s_value != s_match ) {
				i_widget.xpdField.setValue(s_value);
			}
		});
	},

	getParentValue: function () {

		var i_widget = this,
			i_field = i_widget.parentField,
			i_record = i_widget.parentRecord,
			s_key = i_widget.propertyKey,
			s_value = '';

		var s_fieldValue = i_field ? i_field.getValue() : '',
			s_recordValue = i_record ? i_record.get(s_key) : '';
		
		// Get the value - field value is > record value
		return s_fieldValue || s_recordValue;
	},
	setParentValue: function (s_value) {

		var i_widget = this,
			i_field = i_widget.parentField,
			i_record = i_widget.parentRecord,
			s_key = i_widget.propertyKey;

		if ( i_field ) {
			i_field.setValue(s_value);
		}
		if ( i_record ) {
			i_record.set(s_key, s_value);
		}

		return i_widget;
	},

	isValid: function () {

		return this.xpdField.isValid();
	},
	isDirty: function() {

		return this.xpdField.isDirty();
	}
}