Ext.define("Ext.ux.gallery.view.MediaEditor", {

	extend: "Ext.window.Window",

	alias: ["widget.gallerymediaeditor"],

	mixins: {
		componentController: "Ext.ux.util.ComponentController"
	},

	refs: [
		{
			ref: "tabpanel",
			selector: "tabpanel"
		},
		{
			ref: "form",
			selector: "form"
		},
		{
			ref: "picker",
			selector: "#uploadpicker"
		},
		{
			ref: "linkPanel",
			selector: "#linkpanel"
		},
		{
			ref: "layoutPanel",
			selector: "#layoutpanel"
		},
		{
			ref: "editorTabPanel",
			selector: "#editorTabPanel"
		}
	],

	layout: "fit",

	config: {
		_initialized: false,
		extensions: null,
		path: "",
		activeTab: null,
		type: "image",
		record: null,
		linkConfig: null,
		layoutConfig: null,
		ignoreStaticDir: false,
		originalValue: null,
		action: "add",
		mediaType: ""
	},

	save: function() {
		var i_self = this;
		var i_form = i_self.down("form").getForm();
		if (i_form.isValid() && i_self.fireEvent("beforesave", i_self)) {
			return i_self.doSave();
		}
		return false;
	},

	doSave: function() {
		var i_self = this;
		var el = i_self.getEl();

		var o_form = i_self.down("form");
		var delayCounter = 0;
		var b_doSave = true;

		var pop = function(status) {
			if (status === false) {
				b_doSave = status;
			}
			delayCounter--;

			if (delayCounter === 0) {
				if (b_doSave) {
					i_self._doSave(function() {
						el.unmask();
					});
				}
				else {
					el.unmask();
				}
			}
		};

		el.mask(ip2.i18n({
			da: "Gemmer...",
			en: "Saving..."
		}));
		var b_found = false;
		if (o_form) {
			var i_form = o_form.getForm();
			var o_fields = i_form.getFields();
			var a_fields = o_fields.items;

			Ext.Array.each(a_fields, function(i_field) {
				if (i_field.doSave && Ext.isFunction(i_field.doSave)) {
					delayCounter++;
					i_field.doSave(pop);
					b_found = true;
				}
			});
		}
		if (!b_found) {
			i_self._doSave();
		}
	},

	_doSave: function(fn) {
		var i_self = this;
		var o_values = this.getValues();
		var i_record = this.record;

		if (i_record) {
			i_self.fireEvent("update", o_values, i_record);
		}
		else {
			i_self.fireEvent("create", o_values);
		}
		// TODO: Make this callback fire when we are actually done with the submit
		fn && fn.call(i_self);
		i_self.fireEvent("aftersave", i_self, i_self.getRecord());
		i_self.destroy();
	},

	getRecord: function() {
		return this.record;
	},

	initComponent: function () {

		var i_cmp = this;
		var o_initial = i_cmp.initialConfig;
		i_cmp.initComponentController();

		Ext.apply(i_cmp, {
			extensions: o_initial.extensions,
			path: o_initial.path || i_cmp.path,
			ignoreStaticDir: o_initial.ignoreStaticDir || i_cmp.ignoreStaticDir,
			buttons: [
				{
					text: ip2.i18n({
						en: "OK",
						da: "OK"
					}),
					itemId: "okbutton",
					disabled: true,
					handler: function () {

						i_cmp.save();
					}
				},
				{
					text: ip2.i18n({
						en: "Cancel",
						da: "Annuller"
					}),
					handler: function () {

						i_cmp.fireEvent("cancel", i_cmp, i_cmp.getRecord());
						i_cmp.destroy();
					}
				}
			]
		});

		i_cmp.callParent(arguments);

		i_cmp.createLayout();

		i_cmp.control({
			form: {
				activate: i_cmp.onPropertiesActivate,
				afterrender: i_cmp.onPropertiesAfterrender
			},
			picker: {
				activate: i_cmp.onImagePickerActivate,
				itemselect: i_cmp.onImageSelect
			}
		});

		i_cmp.on({
			afterrender: {
				fn: i_cmp.onAfterRender
			}
		});

		return i_cmp;
	},

	createTabpanel: function(o_config) {
		var i_cmp = this;
		var activeTab = o_config.activeTab || 0;
		var i_tabpanel = Ext.widget("tabpanel", {
			xtype: "tabpanel",
			itemId: "editorTabPanel",
			border: false,
			activeTab: activeTab,
			layout: "fit",
			items: [i_cmp.createForm()]
		});
		return i_tabpanel;
	},

	createForm: function(o_config) {
		var i_cmp = this;
		var i_record = i_cmp.getRecord();
		var s_value = i_record ? i_record.get("url") : null;

		var a_items = i_cmp.createFormItems(o_config);

		a_items.push(i_cmp.createPickerField(o_config));

		var i_form = Ext.widget("form", {
			title: ip2.i18n({
				da: "Medie",
				en: "Media"
			}),
			itemId: "mediaproperties",
			layout: "anchor",
			bodyPadding: 15,
			fieldDefaults: {
				anchor: "100%",
				labelWidth: 100
			},
			trackResetOnLoad: true,
			listeners: {
				dirtychange: {
					fn: function (i_self, b_dirty) {
						i_cmp.checkChange();
					}
				},
				validitychange: {
					fn: function (i_self, b_valid) {
						if (!b_valid) {
							i_cmp.down("#okbutton").disable();
						}
						else {
							i_cmp.checkChange();
							i_cmp.down("#okbutton").enable();
						}
					}
				}
			},
			items: a_items
		});
		return i_form;
	},

	createFormItems: function(o_config) {
		var i_cmp = this;
		var a_items = [];
		a_items.push({
			xtype: "hidden",
			name: "type"
		},
		{
			xtype: "i18ntextfield",
			itemId: "title",
			name: "title",
			fieldLabel: ip2.i18n({
				da: "Overskrift",
				en: "Headline"
			}),
			emptyText: ip2.i18n({
				da: "Overskrift til mediet",
				en: "Headline for the media"
			}),
			userChange: false,
			listeners: {
				change: {
					fn: function(item, value) {
						if (!i_cmp.systemSet) {
							item.userChange = true;
						}
						if (!item.isDirty()) {
							item.userChange = false;
						}
					}
				}
			}
		},
		{
			xtype: "i18ntextmarkdownfield",
			fieldLabel: ip2.i18n({
				da: "Tekst",
				en: "Text"
			}),
			emptyText: ip2.i18n({
				da: "Indtast en kort tekst som underbygger overskriften og mediet",
				en: "Enter a short text that supports the headline and the media"
			}),
			name: "caption",
			height: 100,
			userChange: false,
			listeners: {
				change: {
					fn: function(item, value) {
						if (!i_cmp.systemSet) {
							item.userChange = true;
						}
						if (!item.isDirty()) {
							item.userChange = false;
						}
					}
				}
			}
		});
		return a_items;
	},

	createPickerField: function(o_config) {
		var i_cmp = this;
		var i_picker = Ext.widget("uploadpicker", {
			itemId: "uploadpicker",
			name: "url",
			path: i_cmp.path,
			extensions: i_cmp.extensions,
			ignoreStaticDir: i_cmp.ignoreStaticDir,
			allowBlank: true,
			userChange: false,
			submitRaw: false,
			fieldLabel: ip2.i18n({
				da: "Medie",
				en: "Media"
			}),
			emptyText: ip2.i18n({
				da: "Sti til lokalt medie eller URL til eksternt medie",
				en: "Path to local media or URL to external media"
			})
		});
		return i_picker;
	},

	createLinkStacks: function(o_linkConfig) {
		var i_cmp = this;
		var i_record = i_cmp.getRecord();
		var s_value = i_record ? i_record.get("url") : null;
		var o_stackPanel = ip2.extend({
			xtype: "ip2TreeSelector",
			wrapper: {
				getRecord: function () {
					return {
						data: {}
					};
				}
			},
			itemId: "linktypestack",
			name: "stack",
			title: ip2.i18n({
				da: "Stakke",
				en: "Stacks"
			}),
			omitChild: false,
			expandRootNode: false,
			multiSelect: false,
			extraColumns: [
				{
					text: {
						da: "Sidetype",
						en: "Page type"
					},
					dataIndex: "__cid__",
					uitype: "xpdCollectionTitle",
					flex: 1
				}
			],
			addToAllChildren: {
				allow: false
			},
			listeners: {
				change: {
					fn: function (i_self, a_checked) {
						if (!a_checked.length) {
							i_cmp.onLinkTypeDeselect("linktypestack");
						}
						else {
							i_cmp.onLinkTypeSelect("linktypestack");
						}
						i_cmp.checkChange();
					}
				}
			},
			getValue: function () {

				var i_self = this;
				var i_checked = i_self.getChecked()[0];

				if (!i_checked) {
					return "";
				}

				var o_values;
				var o_properties = Ext.decode(i_checked.get("extraProps"), true).properties;

				if (i_checked.parentNode && !i_checked.parentNode.isRoot()) {

					var i_parentNode = i_checked.parentNode;
					var o_parent = Ext.decode(i_parentNode.get("extraProps"), true).properties;

					o_values = {
						page: {
							hash: o_parent.seourl,
							token: o_parent.token
						},
						token: o_properties.token,
						title: o_properties.title,
						type: "stack"
					};
				}
				else {
					o_values = {
						type: "stack",
						token: o_properties.token,
						title: o_properties.title
					};
				}

				return o_values;
			},
			setValue: function (o_value) {

				o_value = o_value || {};

				var i_self = this;
				var i_store = i_self.getStore();
				var n_token = Number(o_value.token);
				var n_parentToken;
				var s_path;
				var a_path;

				if (!n_token) {
					return;
				}

				var b_parent = (function (o_config) {
					return (o_config && o_config.token);
				})(o_value.page);

				a_path = ["/0"];

				if (b_parent) {
					a_path.push(o_value.page.token);
					n_parentToken = o_value.page.token;

					// n_parentToken = Number(o_value.page.token); // TODO: Why was this here?
				}

				a_path.push(n_token);

				s_path = a_path.join("/");

				// Path is sorted, now wait on the rendering and
				// store loading

				i_store.on({
					load: {
						fn: function(i_store, a_records) {
							if (b_parent) {
								Ext.Array.each(a_records, function(i_record) {

									var o_properties = Ext.decode(i_record.get("extraProps"), true) || {};

									if (o_properties.token == n_parentToken) {
										i_record.expand();
										b_parent = false;
										delete n_parentToken;
									}
								});

								i_self.selectPath(s_path);
							}
							else {
								if (n_token) {
									Ext.Array.each(a_records, function(i_record) {

										var o_properties = Ext.decode(i_record.get("extraProps"), true) || {};

										if (o_properties.token == n_token) {
											var i_selectionModel = i_self.getSelectionModel();
											i_record.set("checked", true);
											i_selectionModel.select(i_record);
											delete n_token;
										}
									});
								}
							}
						}
					}
				});

				return i_self;
			}
		}, o_linkConfig.stacks);

		return o_stackPanel;
	},

	createLinkPages: function(o_linkConfig) {

		var i_cmp = this;
		var i_record = i_cmp.getRecord();
		var s_value = i_record ? i_record.get("url") : null;

		if (o_linkConfig.pages.view) {

			o_linkConfig.pages.uiName = o_linkConfig.pages.view;
			delete o_linkConfig.pages.view;
		}

		var o_info = {
			dmd: {
				path: o_linkConfig.pages.dmd,
				entityName: o_linkConfig.pages.entity || o_linkConfig.pages.entityName
			}
		};

		var o_pagePanel = ip2.extend({
			title: ip2.i18n({
				da: "Side",
				en: "Page"
			}),
			xtype: "entity.grid",
			name: "page",
			itemId: "linktypepage",
			selModel: {
				selType: "checkboxmodel", 
				mode: "SINGLE",
				allowDeselect: true
			},
			autoSelect: false,
			searchCombo: false,
			getValue: function () {

				var i_self = this;
				var s_titleId = i_self.titleId;

				var o_value = "";

				var i_record;
		
				var a_records = i_self.getSelectionModel().getSelection();
		
				if (a_records.length > 0) {
		
					i_record = a_records[0];
		
					o_value = {
						type: i_self.name,
						token: i_record.get(i_record.idProperty)
					};
		
					if (s_titleId && i_record.get(s_titleId)) {
						o_value.title = i_record.get(s_titleId);
					}
				}
		
				return o_value;
			},
			setValue: function (o_value) {
		
				var i_self = this;
				var i_store = i_self.getStore();
				var i_model = i_store.getModel();
				var i_proxy = i_store.getProxy();
				var s_primaryId = i_model.idProperty;

				var o_extraParams = i_proxy.getExtraParams();
				var n_token = o_value.token;
		
				i_self.value = o_value;
		
				if (n_token) {
		
					i_self.primaryKey = n_token;
					i_self.titleKey = o_value.title || null;
		
					o_extraParams.union = Ext.encode([{
						property: s_primaryId,
						value: n_token
					}]);
		
					i_store.load();
				}
		
				return i_self;
			}
		}, o_linkConfig.pages);

		Ext.apply(o_pagePanel, o_info);

		var i_linkGrid = Ext.widget("entity.grid", o_pagePanel);

		i_linkGrid.events.selectionchange.clearListeners();
		i_linkGrid.events.itemdblclick.clearListeners();
		i_linkGrid.events.itemcontextmenu.clearListeners();

		i_linkGrid.on({
			select: {
				fn: function (i_self, i_record) {

					var i_grid = i_self.view.up();
					var i_store = i_grid.getStore();
					var i_model = i_store.getModel();
					var s_primaryId = i_model.idProperty;
					var n_key = i_record.get(s_primaryId);

					i_grid.primaryKey = n_key;
					i_grid._record = i_record;

					var i_proxy = i_store.getProxy();
					var o_extraParams = i_proxy.getExtraParams();

					o_extraParams.union = Ext.encode([{
						property: s_primaryId,
						value: n_key
					}]);

					i_cmp.checkChange();

					i_cmp.onLinkTypeSelect("linktypepage");
				}
			},
			deselect: {
				fn: function (i_self) {

					var i_grid = i_self.view.up();
					var i_store = i_grid.getStore();

					var i_proxy = i_store.getProxy();
					var o_extraParams = i_proxy.getExtraParams();

					delete o_extraParams.union;

					i_grid.primaryKey = null;
					i_grid._record = null;

					i_cmp.checkChange();

					i_cmp.onLinkTypeDeselect();
				}
			}
		});

		var i_store = i_linkGrid.getStore();

		i_store.on({
			load: {
				fn: function(i_store, a_records) {
					
					if (i_linkGrid.value) {

						var n_token = i_linkGrid.value.token;

						var i_found = Ext.Array.findBy(a_records, function(i_record) {
							return i_record.get(i_record.idProperty) == n_token;
						});

						if (i_found) {
							var i_view = i_linkGrid.getView();
							i_view.select([i_found]);
						}
					}
				}
			}
		});

		return i_linkGrid;
	},

	createLinkDocuments: function(o_linkConfig) {
		var i_cmp = this;
		var i_record = i_cmp.getRecord();
		var s_value = i_record ? i_record.get("url") : null;
		var o_documentsGallery = ip2.extend({
			xtype: "mediagallery",
			itemId: "documentgallery",
			listeners: {
				afterrender: {
					fn: function (i_self) {

						i_self.on({
							create: {
								fn: function () {
									i_cmp.checkChange();
									i_cmp.onLinkTypeSelect("linktypedocument");
								}
							},
							empty: {
								fn: function () {
									i_cmp.checkChange();
									i_cmp.onLinkTypeDeselect("linktypedocument");
								}
							}
						});
					}
				}
			}
		}, o_linkConfig.documents);

		var o_documentPanel = ip2.extend({
			xtype: "panel",
			title: ip2.i18n({
				da: "Dokument",
				en: "Document"
			}),
			itemId: "linktypedocument",
			name: "document",
			items: [ o_documentsGallery ],
			getValue: function () {

				var i_self = this;
				var i_document = i_self.down("#documentgallery");
				var o_value = i_document.getValue();

				return {
					type: "document",
					value: o_value
				};
			},
			setValue: function (o_value) {

				var i_self = this;
				var i_document = i_self.down("#documentgallery");

				i_document.setValue(o_value.value);
			}
		});

		return o_documentPanel;
	},

	createLinkPanel: function() {
		var i_cmp = this;
		var i_record = i_cmp.getRecord();
		var s_value = i_record ? i_record.get("url") : null;
		var o_linkPanel = {
			xtype: "form",
			itemId: "linktypeurl",
			name: "url",
			title: ip2.i18n({
				da: "URL",
				en: "URL"
			}),
			layout: "anchor",
			bodyPadding: 15,
			getValue: function () {

				var i_self = this;
				var i_input = i_self.down("#linktypeurlvalue");
				var o_values;
				var value = i_input.getValue();

				if (value) {
					o_values = {
						type: i_self.name,
						url: i_input.getValue()
					};
				}
				else {
					o_values = "";
				}

				return o_values;
			},
			setValue: function (o_values) {

				var i_self = this;
				var i_input = i_self.down("#linktypeurlvalue");
				var s_value = o_values.url;

				i_input.setValue(s_value);

				return i_self;
			},
			items: [
				{
					xtype: "textfield",
					itemId: "linktypeurlvalue",
					name: "value",
					anchor: "100%",
					isFormField: false,
					fieldLabel: ip2.i18n({
						da: "Indtast URL",
						en: "Enter URL"
					}),
					listeners: {
						change: {
							fn: function (i_self, s_value) {
								if (s_value.length) {
									i_cmp.onLinkTypeSelect("linktypeurl");
								}
								else {
									i_cmp.onLinkTypeDeselect();
								}

								i_cmp.checkChange();
							}
						}
					}
				}
			]
		};
		return o_linkPanel;
	},

	createLayout: function() {
		var i_cmp = this;
		var o_initial = i_cmp.initialConfig;
		var i_tabpanel = i_cmp.createTabpanel(o_initial);
		i_cmp.add(i_tabpanel);

		var o_linkConfig = i_cmp.linkConfig;

		if (o_linkConfig) {

			var a_linkItems = [];

			if (o_linkConfig.stacks) {
				a_linkItems.push(i_cmp.createLinkStacks(o_linkConfig));
			}

			if (o_linkConfig.pages) {

				a_linkItems.push(i_cmp.createLinkPages(o_linkConfig));
			}

			if (o_linkConfig.documents) {
				a_linkItems.push(i_cmp.createLinkDocuments(o_linkConfig));
			}

			a_linkItems.push(i_cmp.createLinkPanel(o_initial));

			var o_linkPanel = ip2.extend({}, o_linkConfig, {
				xtype: "tabpanel",
				itemId: "linkpanel",
				layout: "fit",
				title: ip2.i18n({
					da: "Link",
					en: "Link"
				}),
				getValue: function () {

					var i_self = this;
					var i_activeTab = i_self.activeTab;
					var o_values;

					if (i_activeTab) {
						o_values = i_activeTab.getValue();
					}

					return o_values;
				},
				setValue: function (o_value) {

					var i_self = this;
					var s_type = o_value.type;
					var i_tab = i_self.down("[name=" + s_type + "]");

					i_cmp.onLinkTypeSelect(i_tab.itemId);

					i_tab.setValue(o_value);

					return i_self;
				},
				items: a_linkItems
			});

			i_cmp.getEditorTabPanel().add(o_linkPanel);

			// If we have a linkConfig then we will add a combo that will
			// allow us to pick how this link should behave
			i_cmp.down("#mediaproperties").add({
				xtype: "combo",
				name: "target",
				fieldLabel: ip2.i18n({
					da: "Åben indhold i",
					en: "Open contents in"
				}),
				displayField: "value",
				valueField: "key",
				value: "parent",
				store: {
					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"
							})
						}
					]
				}
			});
		}

		// If layout object config then add to tabpanel
		var o_layoutConfig = i_cmp.layoutConfig;

		if (o_layoutConfig) {
			var value = o_layoutConfig.value || {};
			if (i_cmp.record.get("layout")) {
				value = i_cmp.record.get("layout");
			}
			var o_xpdConfig = ip2.extend({}, o_layoutConfig, {
				xtype: "xpdfield",
				itemId: "layoutXpd",
				anchor: "100%",
				value: JSON.stringify(value),
				listeners: {
					dirtychange: {
						fn: function (i_self, b_dirty) {
							i_cmp.checkChange();
						}
					},
					validitychange: {
						fn: function (i_self, b_valid) {
							if (!b_valid) {
								i_cmp.down("#okbutton").disable();
							}
							else {
								i_cmp.checkChange();
								i_cmp.down("#okbutton").enable();
							}
						}
					}
				}
			});

			i_cmp.getEditorTabPanel().add({
				xtype: "panel",
				itemId: "layoutpanel",
				title: ip2.i18n({
					da: "Layout",
					en: "Layout"
				}),
				border: false,
				bodyPadding: 15,
				autoScroll: true,
				layout: "anchor",
				items: [o_xpdConfig],
				getXpdField: function () {
					return this.down("xpdfield");
				},
				resetOriginalValue: function () {

					var i_self = this;
					var i_xpd = i_self.getXpdField();

					i_xpd.resetOriginalValue();

					return i_self;
				},
				getValue: function () {

					var i_self = this;
					var i_xpd = i_self.getXpdField();
					var s_value = i_xpd.getValue();

					return JSON.parse(s_value);
				},
				setValue: function (o_value) {

					var i_self = this;
					var i_xpd = i_self.getXpdField();
					var s_value = JSON.stringify(o_value);

					i_xpd.setValue(s_value);

					return i_self;
				}
			});
		}
	},

	onAfterRender: function () {

		var i_cmp = this;
		var i_form = i_cmp.getForm();
		var i_linkPanel = i_cmp.getLinkPanel();
		var i_layoutPanel = i_cmp.getLayoutPanel();
		var i_picker = i_cmp.getPicker();
		var s_type = i_cmp.type;
		var i_record = i_cmp.record;

		// If we have a record - then we are editing
		// Load it into the form
		if (i_record) {

			i_form.loadRecord(i_record);

			// The link tab needs special treatment
			var o_link = i_record.get("link");
			if (o_link && i_linkPanel) {
				i_linkPanel.setValue(o_link);
				i_cmp._link = o_link;
			}

			// The layout tab needs special treatment
			var o_layout = i_record.get("layout");
			if (o_layout && i_layoutPanel) {
				i_layoutPanel.setValue(o_layout);
				i_layoutPanel.resetOriginalValue();
				i_cmp._layout = o_layout;
			}
		}

		i_cmp.originalValue = i_cmp.getValues();

		i_cmp._initialized = true;

		return i_cmp;
	},

	onPropertiesAfterrender: function () {

		var i_cmp = this;
		var i_form = i_cmp.getForm();

		i_form.getEl().on("keydown", function (e, i_htmlNode, eOpts) {

			var b_enter = e.browserEvent.keyIdentifier == "Enter";
			var b_shift = e.shiftKey;

			if (b_enter && !b_shift) {
				//i_cmp.save();
			}
		});

		return i_cmp;
	},

	onPropertiesActivate: function () {

		var i_cmp = this;

		setTimeout(function () {
			i_cmp.down("#uploadpicker").focus();
			i_cmp.down("#title").focus();
		}, 100);

		return i_cmp;
	},

	onImagePickerActivate: function () {

		var i_cmp = this;
		var	i_picker = i_cmp.getPicker();
		var	i_selectionModel = i_picker.getSelectionModel();

		setTimeout(function () {
			var selected = i_selectionModel.getSelection();
			if (!selected.length) {
				selected = i_picker.getStore().getAt(0);
			}
			i_picker.getView().focus();
			i_selectionModel.select(selected);
		}, 100);

		return i_cmp;
	},

	onImageSelect: function (i_grid, a_records) {

		var i_cmp = this;
		var i_form = i_cmp.getForm();
		var i_url = i_form.down("[name=url]");
		var i_record = a_records[0];
		var s_url = i_record.get("path");

		s_url = ip2.cleanPath("/" + s_url);

		i_url.setValue(s_url);

		i_form.show();

		return i_cmp;
	},

	onLinkTypeSelect: function (s_itemId) {

		var i_cmp = this;
		var i_linkPanel = i_cmp.getLinkPanel();

		i_linkPanel.items.each(function (i_item) {
			if (i_item.itemId != s_itemId) {
				i_item.disable();
			}
			else {
				i_item.enable();
				i_linkPanel.setActiveTab(i_item);
			}
		});

		return i_cmp;
	},
	onLinkTypeDeselect: function () {

		var i_cmp = this;
		var i_linkPanel = i_cmp.getLinkPanel();

		i_linkPanel.items.each(function (i_item) {
			i_item.enable();
		});

		return i_cmp;
	},

	getValues: function () {

		var i_cmp = this;
		var i_form = i_cmp.getForm();
		i_form = i_form.getForm();

		var o_formValues = i_form.getFieldValues();
		var o_values = o_formValues || {};

		var i_linkPanel = i_cmp.getLinkPanel();

		if (i_linkPanel) {
			o_values.link = i_linkPanel.getValue();
			if (o_values.link) {
				o_values.link.target = o_values.target;
			}
		}

		var i_layoutPanel = i_cmp.getLayoutPanel();

		if (i_layoutPanel) {
			o_values.layout = i_layoutPanel.getValue();
		}

		return o_values;
	},

	getPath: function () {

		return this.initialConfig.path || this.path;
	},

	checkChange: function () {

		var i_cmp = this;
		var i_button = i_cmp.down("#okbutton");
		var value = i_cmp.getValues();
		var originalValue = i_cmp.originalValue;

		if (!ip2.isEqual(value, originalValue) && i_cmp._initialized) {
			//i_button.enable();
		}
		else {
			//i_button.disable();
		}

		return i_cmp;
	}
});
