{

	xtype: 'container',

	require: [
		'Ext.ux.widget.folderorganizer.PageNavigation'
	],

	layout: 'anchor',

	key: '',

	parentKey: '',

	parentRelationKey: '',

	propertiesKey: '',

	relation: null,

	xpd: 'inpagemenu.xpd',

	dmd: 'webshop.dmd',

	entity: 'WSHRELET00',

	view: 'portfolio',

	serviceUrl: '/wsh-services/services/ip2dmdProxy.aspx',

	name: '',

	isInitialized: false,

	onInitialize: function(i_widget, i_owner) {

		var i_self = this;
		var s_proxy = ip2.getWepshopProxyUrl(i_owner);

		var  o_ownerConfig = i_owner._config;
		var o_config = {};
		o_config.dataParent = i_owner.dataParent;
		o_config.proxy || (o_config.proxy=s_proxy);
		o_config.parentKey = o_ownerConfig.parentKey;
		o_config.relation = o_ownerConfig.relation;
		o_config.parentRelationKey = o_ownerConfig.parentRelationKey;
		o_config.propertiesKey = o_ownerConfig.propertiesKey;
		o_config.key = o_ownerConfig.key;


		// We do not intialize this widget if we are on the create stage
		// which would mean parentToken is 0
		// THIS WAS THE PROBLEM, WE WHERE OVERRIDING THE WIDGET
		// THIS IS A NO GO!!!!!!
		Ext.apply(i_self, o_config);
		var n_parentToken = i_self.getParentToken();

		if (n_parentToken) {
			i_self.initWidget(function(i_record){
				i_self.createView(i_widget, i_record);
				i_widget.isInitialized = true;
			});
		}
		else {
			i_self.initEmpty();
		}
	},

	createView: function(i_widget, i_record) {

		var i_self = this;

		var a_fields = i_self.createFields(i_record);

		var i_form = Ext.widget("form",{
			xtype: 'form',
			itemId: 'inpagemenuform',
			border: false,
			bodyPadding: 15,
			defaults: {
				width: '100%',
				layout: 'anchor'
			},
			items: a_fields
		});

		i_widget.add(i_form);

		i_form.items.each(function (i_field) {
			i_field.resetOriginalValue();
		});
	},

	createFields: function (o_row) {

		var i_widget = this;
		var a_fields = [];

		var s_key = i_widget.key;
		var s_parentKey = i_widget.parentKey;
		var s_parentRelationKey = i_widget.parentRelationKey;
		var s_propertiesKey = i_widget.propertiesKey;

		a_fields.push({
			xtype: 'numberfield',
			name: s_key,
			value: o_row[s_key],
			hidden: true,
			isFormField: false
		})

		a_fields.push({
			xtype: 'numberfield',
			name: s_parentRelationKey,
			value: o_row[s_parentRelationKey],
			hidden: true,
			isFormField: false
		});

		ip2.each(i_widget.relation, function (s_value, s_key) {
			a_fields.push({
				xtype: 'numberfield',
				name: s_key,
				value: s_value,
				hidden: true,
				isFormField: false
			});
		});

		var o_xpdField = {
			xtype: 'xpdfield',
			name: s_propertiesKey,
			xpd: i_widget.xpd,
			isFormField: false
		};

		if (o_row[s_propertiesKey]) {
			o_xpdField.value = o_row[s_propertiesKey];
		}

		a_fields.push(o_xpdField);

		return a_fields;
	},

	createLoadUrl: function (o_keys) {

		var i_widget = this;
		var s_dmd = ip2.resource.getFullPath(i_widget.dmd);

		var o_payload = {
			transaction: {
				dmd: s_dmd,
				batch: [
					{
						type: 'get',
						entity: i_widget.entity,
						uiName: i_widget.view,
						context: 'form',
						fullresponse: true,
						key: o_keys
					}
				]
			}
		};

		var s_url = i_widget.serviceUrl;
		var s_payload = Ext.JSON.encode(o_payload);

		s_url += '?action=transaction&payload=' + s_payload;

		return s_url;
	},

	initWidget: function (f_callback) {

		var i_self = this;

		var i_dataParent = i_self.dataParent;
		var o_parentKeys = i_dataParent.getKeys();

		// Get and create the keys object we use to get the
		// values from the DB in case there is any
		var s_key = i_self.key;
		var s_parentRelationKey = i_self.parentRelationKey;
		var s_parentKey = i_self.parentKey;
		var o_keys = ip2.clone(i_self.relation);

		o_keys[s_parentRelationKey] = o_parentKeys[s_parentKey];

		var s_loadUrl = i_self.createLoadUrl(o_keys);

		Ext.Ajax.request({
			url: s_loadUrl,
			success: function (o_transaction) {

				var o_response = Ext.JSON.decode(o_transaction.responseText, true);
				var o_value = o_response.batch[0];

				if(o_value.success) {
					var o_row = o_value.rows[0];

					// If o_row is undefined we have no data for this yet
					if (!o_row) {
						o_row = o_keys;
						o_row[s_key] = 0;
					}
					f_callback(o_row);
				}
				else {
					Ext.Error.handle({
						restart: false,
						message: {
							message: o_value.message
						},
						debug: "error",
						description: ip2.i18n({
							da: "inpagemenu - listrepairstatus - fejl ",
							da: "inpagemenu - listrepairstatus - error "
						})
					});
					f_callback({})
				}
			}
		});
	},

	initEmpty: function () {

		var i_self = this;

		i_self.add({
			xtype: 'container',
			border: false,
			layout: 'fit',
			autoEl: {
				tag: 'div',
				html: ip2.i18n({
					da: 'Siden er ikke blevet gemt endnu.<br />Gem siden og du vil være i stand til at vælge submenu.',
					en: 'This page has not been created yet.<br />Save the page and you will be able to select submenu.'
				}),
				style: 'text-align: center; font-size: 17px; font-weight: 700; top: 50%; position: relative'
			}
		});
	},

	getForm: function () {

		var i_widget = this;
		var i_form = i_widget.down('#inpagemenuform');

		return i_form;
	},

	getValue: function () {

		var i_widget = this;
		var i_form = i_widget.getForm();
		var o_values = {};

		i_form.items.each(function (i_field) {

			var value = i_field.getValue();
			var s_name = i_field.name;

			o_values[s_name] = value;
		});

		return o_values;
	},

	getParentToken: function () {

		var i_widget = this;
		var i_dataParent = i_widget.dataParent;
		var o_keys = i_dataParent.getKeys();

		return o_keys[i_widget.parentKey];
	},

	isValid: function () {

		return true;
	},

	isDirty: function() {

		var i_widget = this;
		var i_form = i_widget.getForm();
		var b_dirty = false;
		if (i_form) {
			i_form.items.each(function (i_field) {
				if (i_field.isDirty()) {
					b_dirty = true;
				}
			});
		}
		return b_dirty;
	},

	doSave: function (f_callback) {
		f_callback || (f_callback=Ext.emptyFn);

		var i_widget = this;
		var b_init = i_widget.isInitialized;

		if (b_init) {

			var s_dmd = ip2.resource.getFullPath(i_widget.dmd);
			var o_row = i_widget.getValue();
			var o_key = {
			 	RELRELTKN: o_row["RELRELTKN"]
			}
			var a_batch = [{
					type: 'upsert',
					dmd: s_dmd,
					entity: i_widget.entity,
					key: o_key,
					row: o_row
				}
			];

			var s_url = i_widget.serviceUrl;

			Ext.Ajax.request({
				url: s_url,
				method: 'POST',
				jsonData: {
					transaction: {
						batch: a_batch
					}
				},
				success: function (o_transaction) {

					var o_response = Ext.JSON.decode(o_transaction.responseText, true) || {};
					var o_json = o_response.batch[0];
					var o_row = o_json.row;

					var i_form = i_widget.getForm();

					i_form.items.each(function (i_field) {

						var s_name = i_field.name;
						var value = o_row[s_name];

						if (value) {
							i_field.setValue(value);
						}

						i_field.resetOriginalValue();
					});

					f_callback();
				}
			});
		}
		else {
			i_widget.removeAll();
			i_widget.initWidget();
			f_callback();
		}
	},

	doDelete: function (f_callback) {

		f_callback || (f_callback=Ext.emptyFn);

		f_callback();
	}
}
