Ext.define("Ext.ux.file.view.Edit", {

	extend: "Ext.window.Window",

	alias: "widget.fileedit",

	record: undefined,
	form: undefined,
	pnlInfo: undefined,
	pnlRelations: undefined,
	i_dropzone: undefined,
	type: undefined,
	extension: undefined,
	uploadname: undefined,
	layout: "fit",
	oldName: undefined,
	hasUploaded: false,

	initComponent: function(config) {

		var i_cmp = this;
		i_cmp.record = i_cmp.record || Ext.ux.file.util.Handler.createModel();
		var i_record = i_cmp.record;

		i_cmp.addListeners();
		i_cmp.callParent(config);

		var s_preset = ip2.preset.staticPath || "pp-static";

		if (!i_cmp.uploadname) {
			i_cmp.uploadname = ip2.cleanPath(s_preset + "/_upload" + ip2.getDcTimestamp());
		}

		if (i_cmp.type == "edit") {
			i_cmp.oldName = i_record.get("FILNAME");
		}

		i_cmp.form = i_cmp.add(i_cmp.createLayout());
		i_cmp.form.loadRecord(i_record);
		i_cmp.form.on({
			dirtychange: function(i_form, b_dirty) {
				i_cmp.setSavable(b_dirty);
			}
		});
	},

	addListeners: function(o_config) {
		var i_cmp = this;
		var i_record = i_cmp.record;

		Ext.Object.merge(i_cmp, {
			listeners: {
				beforeshow: function(i_item) {
					var i_grid = i_cmp.grid;
					var i_store = i_grid.getStore();
					i_store.load();
				},
				afterlayout: function() {
					if (i_cmp.type == "edit") {
						if (i_record.get("FILWIDTH") === 0 && i_record.get("FILHEIGHT") === 0 && !i_record.get("FILEXTERNAL")) {
							var absPath = i_record.get("FILPATH") + i_record.get("FILNAME");
							i_cmp.getImageInfo(absPath);
						}
					}
					else {
						if (i_cmp.uploadname.indexOf(".") != -1) {
							i_cmp.getImageInfo(i_cmp.uploadname);
						}
					}
				}
			}
		});
	},

	createConvertButton: function(o_record) {
		var i_self = this;
		var i_record = o_record;
		var o_button = Ext.widget("button", {
			text: ip2.i18n({
				da: "Konverter til intern",
				en: "Convert to internal"
			}),
			itemId: "btn_convert",
			hidden: o_record.get("FILEXTERNAL") ? false : true,
			handler: function(item) {
				var msg = ip2.i18n({
					da: "Ønsker du virkelig konverter til intern dette er en permenent handling...?",
					en: "Do you really wish to convert to an internal file, this is a permanet action..?"
				});

				var title = ip2.i18n({
					da: "Konverter",
					en: "Convert"
				});

				ip2.messageBox({
					icon: Ext.MessageBox.WARNING,
					title: title,
					closable: false,
					message: msg,
					yesText: ip2.i18n({
						da: "OK",
						en: "OK"
					}),
					yesFn: function () {

						var el = i_self.getEl();
						var i_form = i_self.form.getForm();
						var i_field = i_form.findField("FILNAME");
						var s_path = i_record.get("FILPATH");
						var s_url = i_record.get("FILORIPATH");
						var s_name = i_field.getValue();

						var msg = ip2.i18n({
							da: "Konverter...",
							en: "Converting..."
						});

						el.mask(msg);

						Ext.ux.file.util.Handler.curlFileFromURLWithName(s_path, s_url, s_name, function() {
							i_record.set("FILEXTERNAL", false);
							Ext.ux.file.util.Handler.upsertRecords([i_record], function(a_records) {
								var s_abs = i_record.get("FILPATH") + i_record.get("FILNAME");
								i_self.getImageInfo(s_abs);
								i_self.form.getForm().setValues({
									"FILEXTERNAL": 0
								});
								i_self.convertLbl.show();
								i_self.convertBtn.hide();
								el.unmask();
							});
						});
					},
					noText: ip2.i18n({
						da: "Annuller",
						en: "Cancel"
					})
				});
			}
		});
		i_self.convertBtn = o_button;
		return o_button;
	},

	createConvertLabel: function(o_record) {
		var i_self = this;
		var o_label = Ext.widget("displayfield", {
			itemId: "lbl_convert",
			hidden: o_record.get("FILEXTERNAL") ? true : false,
			value: ip2.i18n({
				da: "Filen er uploaded",
				en: "File is uploaded"
			}),
			fieldStyle: {
				"color": "green;",
				"cursor": "default"
			}
		});
		i_self.convertLbl = o_label;
		return o_label;
	},

	createLayout: function() {
		var i_cmp = this;
		var i_form = Ext.widget("form", {
			layout: "border",
			trackResetOnLoad: true,
			items: [
				i_cmp.createPanelWest(),
				i_cmp.createPnlInfo()
			],
			buttons: i_cmp.createButtons()
		});

		return i_form;
	},

	createPnlInfo: function() {
		var i_cmp = this;
		i_cmp.pnlInfo = Ext.widget("panel", {
			region: "center",
			layout: "border",
			flex: 1,
			items: [
				i_cmp.createFieldSetInfo(),
				i_cmp.createFieldSetImage()
			]
		});
		return i_cmp.pnlInfo;
	},

	createFieldSetImage: function() {
		var fieldset = Ext.widget("panel", {
			flex: 1,
			bodyPadding: 5,
			autoScroll: true,
			region: "south",
			title: ip2.i18n({
				da: "Flere oplysninger",
				en: "More information"
			}),
			layout: "anchor",
			items: [
				{
					xtype: "textfield",
					name: "FILWIDTH",
					anchor: "99%",
					fieldLabel: ip2.i18n({
						da: "Brede",
						en: "Width"
					}),
					readOnly: true
				},
				{
					xtype: "textfield",
					name: "FILHEIGHT",
					anchor: "99%",
					fieldLabel: ip2.i18n({
						da: "Højde",
						en: "Height"
					}),
					readOnly: true
				},
				{
					xtype: "textfield",
					name: "FILSIZE",
					anchor: "99%",
					fieldLabel: ip2.i18n({
						da: "Størrelse",
						en: "Size"
					}),
					readOnly: true,
					getValue: function() {
						return this.trueValue;
					},
					setValue: function(_value) {
						var value = _value || 0;
						var rawValue = ip2.humanFileSize(value);
						this.trueValue = value;
						this.mixins.field.setValue.call(this, value);
						this.setRawValue(rawValue);
						return this;
					}
				},
				{
					xtype: "textfield",
					name: "FILEXT",
					anchor: "99%",
					fieldLabel: ip2.i18n({
						da: "Fil type",
						en: "File type"
					}),
					readOnly: true
				}
			]
		});
		return fieldset;
	},

	createFieldSetInfo: function() {
		var i_cmp = this;
		var fieldset = Ext.widget("panel", {
			flex: 2,
			region: "center",
			bodyPadding: 5,
			autoScroll: true,
			title: ip2.i18n({
				da: "Generelt",
				en: "General"
			}),
			layout: "anchor",
			items: [
				{
					xtype: "textfield",
					name: "FILFILTKN",
					anchor: "99%",
					fieldLabel: ip2.i18n({
						da: "ID",
						en: "ID"
					}),
					readOnly: true,
					hidden: false
				},
				{
					xtype: "textfield",
					name: "FILMEDIA",
					anchor: "99%",
					fieldLabel: ip2.i18n({
						da: "Media",
						en: "Media"
					}),
					readOnly: true,
					hidden: true
				},
				{
					xtype: "textfield",
					name: "FILNAME",
					anchor: "99%",
					fieldLabel: ip2.i18n({
						da: "Navn",
						en: "Name"
					}),
					readOnly: false,
					hidden: false,
					msgTarget: "side",
					listeners: {
						change: function(item) {

							var value = this.getValue();
							i_cmp.form.getForm().setValues({
								"FILMEDIA": value
							});
							if (i_cmp.type == "new") {
								if (!i_cmp.originalTiltle) {
									i_cmp.originalTiltle = i_cmp.title;
								}
								var rawValue = this.getRawValue();
								if (rawValue === "") {
									i_cmp.setTitle(i_cmp.originalTiltle);
								}
								else {
									i_cmp.setTitle(i_cmp.originalTiltle + " - " + rawValue);
								}
							}
						}
					},
					getValue: function() {
						var value = this.getRawValue();
						return (value + "." + this.extension).trim();
					},
					setValue: function(_value) {
						var value = _value || "";
						var extension = value.split(".").pop();
						this.extension = extension;
						i_cmp.extension = extension;
						var name = value.replace("." + extension, "");
						this.mixins.field.setValue.call(this, name);
						this.setRawValue(name);
						if (i_cmp.type == "new") {
							if (!i_cmp.originalTiltle) {
								i_cmp.originalTiltle = i_cmp.title;
							}
							var rawValue = this.getRawValue();
							if (rawValue === "") {
								i_cmp.setTitle(i_cmp.originalTiltle);
							}
							else {
								i_cmp.setTitle(i_cmp.originalTiltle + " - " + rawValue + "." + extension);
							}
						}
						return this;
					}
				},
				{
					xtype: "textfield",
					name: "FILHEAD",
					anchor: "99%",
					fieldLabel: ip2.i18n({
						da: "Overskrift",
						en: "Header"
					}),
					readOnly: false,
					emptyText: ip2.i18n({
						da: "ALT tekst",
						en: "ALT text"
					})
				},
				{
					xtype: "textarea",
					name: "FILSUB",
					anchor: "99%",
					fieldLabel: ip2.i18n({
						da: "Underoverskrift",
						en: "Sub header"
					}),
					readOnly: false,
					emptyText: ip2.i18n({
						da: "Brug hvor template-design benytter ALT tekst delt i overskrift og underoverskrift ALT tekst",
						en: "Brug hvor template-design benytter ALT tekst delt i overskrift og underoverskrift ALT tekst"
					})
				},
				{
					xtype: "textfield",
					name: "FILTAG",
					anchor: "99%",
					fieldLabel: ip2.i18n({
						da: "Tags",
						en: "Tags"
					}),
					readOnly: false
				},
				{
					xtype: "isotimestampfield",
					name: "FILCREATE",
					fieldLabel: ip2.i18n({
						da: "Oprettet",
						en: "Created"
					}),
					readOnly: true
				},
				{
					xtype: "isotimestampfield",
					name: "FILMODIFY",
					fieldLabel: ip2.i18n({
						da: "Ændret",
						en: "Changed"
					}),
					readOnly: true
				},
				{
					xtype: "textfield",
					name: "FILCRTBY",
					anchor: "99%",
					fieldLabel: ip2.i18n({
						da: "Oprettet af",
						en: "Created by"
					}),
					readOnly: true
				},
				{
					xtype: "textfield",
					name: "FILPATH",
					anchor: "99%",
					fieldLabel: ip2.i18n({
						da: "Sti",
						en: "Path"
					}),
					readOnly: true,
					hidden: false
				},
				{
					xtype: "textfield",
					name: "FILORIPATH",
					anchor: "99%",
					fieldLabel: ip2.i18n({
						da: "Original sti",
						en: "Original path"
					}),
					readOnly: true,
					hidden: false
				},
				{
					xtype: "fieldcontainer",
					name: "FILEXTERNAL",
					anchor: "99%",
					fieldLabel: ip2.i18n({
						da: "Ekstern",
						en: "External"
					}),
					layout: {
						type: "hbox",
						align: "center"
					},
					defaults: {
						"margin": "0 5 0 0"
					},
					items: [
						{
							xtype: "checkbox",
							name: "FILEXTERNAL",
							readOnly: true,
							hidden: false
						},
						i_cmp.createConvertButton(i_cmp.record),
						i_cmp.createConvertLabel(i_cmp.record)
					]
				}

			]
		});
		return fieldset;
	},

	createPanelWest: function() {
		var i_cmp = this;
		var i_panel_west = Ext.widget("panel", {
			layout: "border",
			region: "west",
			flex: 1,
			items: [
				i_cmp.createAreaDropzone(),
				i_cmp.createPnlRelations()
			]
		});
		return i_panel_west;
	},

	createAreaDropzone: function() {
		var i_cmp = this;
		var i_record = i_cmp.record;
		var url = "";
		if (i_cmp.type == "edit") {
			var b_isExternal = i_record.get("FILEXTERNAL");

			if (b_isExternal) {
				url = i_record.get("FILORIPATH");
			}
			else {
				url = i_record.get("FILPATH") + i_record.get("FILNAME");
			}
		}
		else {
			if (i_cmp.uploadname.indexOf(".") == -1) {
				url = "";
			}
			else {
				url = i_cmp.uploadname;
			}
		}

		var pnl = Ext.widget("form", {
			title: ip2.i18n({
				da: "Fil",
				en: "File"
			}),
			itemId: "frmUpload",
			layout: "fit",
			region: "center",
			flex: 2,
			bodyPadding: 20,
			dockedItems: [{
					xtype: "toolbar",
					dock: "top",
					items: [
						"->",
						{
							xtype: "fileuploadfield",
							itemId: "fileup",
							width: 60,
							buttonOnly: true,
							buttonConfig: {
								text: "",
								width: 60,
								cls: "x-ux-filemanager-images-upload"
							},
							listeners: {
								change: {
									fn: function(item, value) {
										var form = item.up("form");
										var a_files = item.fileInputEl.dom.files;
										if (a_files.length == 0) {
											return;
										}
										var s_name = a_files[0].name;
										var s_newExtension = value.split(".").pop().toLowerCase();

										if (Ext.ux.file.util.Handler.extensions.indexOf(s_newExtension) != -1) {
											if (i_cmp.uploadname.indexOf(".") == -1) {
												i_cmp.uploadname = i_cmp.uploadname + "." + s_newExtension;
											}
											else {
												i_cmp.uploadname = i_cmp.uploadname.replace("." + i_cmp.extension, "." + s_newExtension);
											}
											if (value) {
												var msg = ip2.i18n({
													da: "Uploader fil..",
													en: "Uploading file..."
												});
												form.setLoading(msg, true);
												i_cmp.setSavable(false);
												Ext.ux.file.util.Handler.uploadToTemp(a_files[0], function(i_record) {
													if (i_cmp.type == "new") {
														i_cmp.form.getForm().setValues({
															"FILNAME": s_name,
															"FILMEDIA": s_name,
															"FILEXT": s_newExtension
														});
													}
													else {
														i_cmp.form.getForm().setValues({
															"FILEXT": s_newExtension,
															"FILEXTERNAL": 0
														});
													}
													i_cmp.hasUploaded = true;
													i_cmp.uploadname = i_record.uploadname;
													i_cmp.onImageUpdate();
													form.setLoading(false);
												});
											}
										}
										else {
											var s_title = ip2.i18n({
												da: "Fejl",
												en: "Error"
											});
											var s_msg = ip2.i18n({
												da: "Denne fil type er ikke tilladt",
												en: "This fil type is not allowed"
											});
											Ext.Msg.show({
												title: s_title,
												msg: s_msg,
												buttons: Ext.Msg.OK,
												icon: Ext.Msg.ERROR
											});
										}
										item.reset();
									}
								}
							}
						}
					]
				}

			],
			items: [
				{
					xtype: "container",
					layout: "fit",
					setImage: function(url) {
						var size = this.getSize();
						var el = this.getEl();
						var o_record = i_cmp.record;
						var s_extension = o_record.get("FILEXT");
						var a_extension_image = ["jpg","jpeg","png","gif", "bmp"];
						if (o_record.get("FILEXTERNAL")) {
							if (a_extension_image.indexOf(s_extension) == -1) {
								url = Ext.ux.file.util.Handler.getUrlByExtension(url);
							}
							el.setStyle({
								"background-size": "contain"
							});
						}
						else {
							if (a_extension_image.indexOf(s_extension) != -1) {
								url = "/ip2-services/scaleImage.aspx?".concat(
									Ext.Object.toQueryString(ip2.extend({
										path: url,
										padCanvas: false,
										_dc: ip2.getDcTimestamp()
									}, size))
								);
							}
							else {
								url = Ext.ux.file.util.Handler.getUrlByExtension(url);
								el.setStyle({
									"background-size": "contain"
								});
							}
						}
						el.setStyle({
							"background-image": "url(\"" + url + "\")",
							"background-repeat": "no-repeat",
							"background-position": "50% 50%"
						});
					},
					listeners: {
						afterrender: {
							single: true,
							fn: function(item) {
								var dropzone = i_cmp.createDropzone();
								var el = item.getEl();
								dropzone.getEl().insertBefore(el);
								i_cmp.image = item;
							}
						},
						resize: function(item) {
							var url = "";
							var i_record = i_cmp.record;
							if (i_cmp.hasUploaded) {
								url = i_cmp.uploadname;
							}
							else {
								var b_isExternal = i_record.get("FILEXTERNAL");
								if (b_isExternal) {
									url = i_record.get("FILORIPATH");
								}
								else {
									url = i_record.get("FILPATH") + i_record.get("FILNAME");
								}
							}

							setTimeout(function() {
								item.setImage(url);
							}, 5);
						}
					}
				}
			]
		});
		return pnl;
	},

	createPnlRelations: function() {
		var i_cmp = this;
		var i_grid = i_cmp.createGridRelation(i_cmp.record);
		i_cmp.pnlRelations = Ext.widget("panel", {
			title: ip2.i18n({
				da: "Relationer",
				en: "Relations"
			}),
			layout: "fit",
			region: "south",
			flex: 1,
			items: [
				i_grid
			]
		});
		i_cmp.grid = i_grid;
		return i_cmp.pnlRelations;
	},

	createDropzone: function() {
		var i_cmp = this;
		var dropbox = Ext.widget("gallerydropbox", {
			flex: 1,
			dropText: ip2.i18n({
				en: "Drag-and-drop your image here<br /><small><small>*Allowed file types.. jpg, jpeg, gif or png</small></small>",
				da: "Træk-og-slip dit billede her<br /><small><small>*Tillade fil typer.. jpg, jpeg, gif, eller png</small></small>"
			}),
			extensions: Ext.ux.file.util.Handler.extensions,
			maxItems: 1,
			style: {
				"position": "absolute",
				"padding": "10px",
				"top": "0px",
				"left": "0px",
				"right": "0px",
				"bottom": "0px",
				"font-size": "25px"
			},
			listeners: {
				drop: {
					fn: function(i_dropbox, a_files, o_event) {
						var plugin = i_dropbox.getDDPlugin();
						var form = i_cmp.down("#frmUpload");
						i_cmp.setSavable(false);
						if (plugin.enableDrop) {

							var msg = ip2.i18n({
								da: "Uploader fil..",
								en: "Uploading file..."
							});
							form.setLoading(msg, true);
							Ext.ux.file.util.Handler.uploadToTemp(a_files[0], function(i_record) {
								if (i_cmp.type == "new") {
									i_cmp.form.getForm().setValues({
										"FILNAME": i_record.name,
										"FILMEDIA": i_record.name,
										"FILEXT": i_record.extension
									});
								}
								else {
									i_cmp.form.getForm().setValues({
										"FILEXT": i_record.extension,
										"FILEXTERNAL": 0
									});
								}
								i_cmp.hasUploaded = true;
								i_cmp.uploadname = i_record.uploadname;
								i_cmp.onImageUpdate();
								form.setLoading(false);
							});
						}
					}
				},
				bodydragenter: function(i_dropbox, o_event) {
					var i_el = i_cmp.i_dropzone.getEl();
					if (i_el.dom) {
						var size = i_cmp.i_dropzone.getSize();
						i_dropbox.setSize(size);
						i_el.setStyle("opacity", "0.8");
					}
				},
				bodydragleave: function(i_dropbox, o_event) {
					var i_el = i_cmp.i_dropzone.getEl();
					if (i_el.dom) {
						i_el.setStyle("opacity", "0");
					}
				},
				dragenter: {
					fn: function(i_dropbox, o_event) {
						var items = o_event.dataTransfer.items || o_event.dataTransfer.files;
						var plugin = i_dropbox.getDDPlugin();
						if (items.length > i_dropbox.maxItems) {
							i_dropbox.addCls(plugin.failureCls);
							plugin.enableDrop = false;
						}
					}
				},
				dragleave: {
					fn: function(i_dropbox, o_event) {
						var plugin = i_dropbox.getDDPlugin();
						plugin.enableDrop = true;
					}
				}
			}
		});

		i_cmp.i_dropzone = Ext.widget("container", {
			layout: "fit",
			renderTo: Ext.getBody(),
			style: {
				"background-color": "white",
				"position": "absolute",
				"top": "0px",
				"left": "0px",
				"right": "0px",
				"bottom": "0px",
				"display": "inline",
				"opacity": "0"
			},
			items: [dropbox]
		});

		return i_cmp.i_dropzone;
	},

	createButtons: function() {
		var i_cmp = this;

		var buttons = [
				{
					xtype: "button",
					width: 50,
					text: ip2.i18n({
						da: "Gem",
						en: "Save"
					}),
					listeners: {
						added: function(button) {
							i_cmp.buttonSave = button;
							i_cmp.setSavable(false);
						}
					},
					handler: function(item) {
						item.setDisabled(true);
						var i_form = i_cmp.form.getForm();
						var o_fieldValues = i_form.getFieldValues();
						var i_record = i_cmp.record;
						var i_store = i_cmp.store;
						var i_field = i_form.findField("FILNAME");
						if (i_field.getRawValue() === "") {

							i_field.markInvalid(ip2.i18n({
								da: "Dette må ikke være tom",
								en: "This must not be empty"
							}));
							return false;
						}
						var a_files = [{
							name: o_fieldValues.FILNAME
						}];
						var msg = ip2.i18n({
							da: "Valider fil navn...",
							en: "Validating filename.."
						});
						i_cmp.setLoading(msg, true);
						Ext.ux.file.util.Handler.checkFiles(o_fieldValues.FILPATH, a_files, function(o_result) {
							i_cmp.setLoading(false);
							var a_update = o_result.update;
							var field = i_form.findField("FILNAME");
							if (i_cmp.type == "new") {
								if (a_update.length > 0) {
									var msg = ip2.i18n({
										da: "Navnet findes allerede",
										en: "Name allready exists"
									});
									field.markInvalid(msg);
									item.setDisabled(false);
									return false;
								}
								else {
									i_record.set(o_fieldValues);
									var extension = i_record.get("FILNAME").split(".").pop();
									i_record.set("FILNAME", i_record.get("FILNAME").replace("." + extension, "." + extension.toLowerCase()));
									i_record.set("FILMEDIA", i_record.get("FILNAME"));
									i_cmp.form.setLoading(ip2.i18n({
										da: "Gemmer...",
										en: "Saving..."
									}), true);
									i_record.set("FILCREATE", ip2.createIsoTimeStamp());
									i_record.set("FILFILTKN", 0);
									Ext.ux.file.util.Handler.upsertRecords([i_record], function(a_records) {
										var s_fromPath = i_cmp.uploadname;
										Ext.ux.file.util.Handler.moveFromRecords(s_fromPath, a_records, function(a_records) {
											i_cmp.form.setLoading(false);
											i_store.load();
											i_cmp.close();
										});
									});
								}
							}
							else {
								if (a_update.length > 0 && i_cmp.oldName != o_fieldValues.FILNAME) {
									field.markInvalid(ip2.i18n({
										da: "Navnet findes allerede",
										en: "Name allready exists"
									}));
									item.setDisabled(false);
									return false;
								}
								else {
									i_record.set(o_fieldValues);
									if (i_cmp.hasUploaded) {
										var extension = i_record.get("FILNAME").split(".").pop();
										var newExtension = i_record.get("FILEXT");
										var s_newName = i_record.get("FILNAME").replace("." + extension, "." + newExtension);
										var s_path = i_record.get("FILPATH");
										i_record.set("FILNAME", s_newName);
										i_record.set("FILORIPATH", s_path);
									}
									i_record.set("FILMEDIA", i_record.get("FILNAME"));
									i_cmp.form.setLoading(ip2.i18n({
										da: "Gemmer...",
										en: "Saving..."
									}), true);
									Ext.ux.file.util.Handler.upsertRecords([i_record], function(a_records) {
										if (i_cmp.hasUploaded) {
											var s_fromPath = i_cmp.uploadname;
											Ext.ux.file.util.Handler.moveFromRecords(s_fromPath, a_records, function(a_records) {
												i_cmp.form.setLoading(false);
												i_store.load();
												i_cmp.close();
											});
										}
										else {
											if (i_cmp.oldName != i_record.get("FILNAME")) {
												var s_fromPath = i_record.get("FILPATH") + i_cmp.oldName;
												Ext.ux.file.util.Handler.moveFromRecords(s_fromPath, [i_record], function(a_records) {
													i_cmp.form.setLoading(false);
													i_store.load();
													i_cmp.close();
												});
											}
											else {
												i_cmp.form.setLoading(false);
												i_cmp.close();
												i_store.load();
											}
										}
									});
								}
							}
						});
					}
				},
				{
					xtype: "button",
					width: 50,
					text: ip2.i18n({
						da: "Annuller",
						en: "Cancel"
					}),
					handler: function(item) {
						i_cmp.close();
					}

				}
			];

		return buttons;
	},

	createGridRelation: function(i_record) {
		var i_self = this;
		var o_config = {
			filterable: false,
			b_load: false,
			allowNew: false,
			dmd: {
				path: "dmd/webshop.dmd",
				entityName: "WSHRELET00"
			}
		};

		var i_grid = Ext.widget("ip2pagingGridOld", o_config);
		var i_store = i_grid.getStore();

		i_store.on({
			beforeload: {
				fn: function(store) {
					store.proxy.extraParams.where = "RELCHDTKN = " + i_record.get("FILFILTKN");
					store.currentPage = 1;
				}
			}
		});

		i_grid.on({
			afterrender: {
				fn: function(i_item) {
					i_grid.events.itemdblclick.clearListeners();
					i_grid.events.itemcontextmenu.clearListeners();
					var i_toolbar = i_grid.getToolbar();
					var index = i_toolbar.removeExport();
					i_toolbar.items.removeAt(index);
				}
			}
		});

		return i_grid;
	},

	setSavable: function(saveable) {
		var i_cmp = this;
		i_cmp.buttonSave.setDisabled(!saveable);
	},

	onImageUpdate: function() {
		var i_cmp = this;
		var url = ip2.cleanPath(i_cmp.uploadname);
		i_cmp.image.setImage(url);
		var absPath = ip2.cleanPath(i_cmp.uploadname);
		i_cmp.getImageInfo(absPath);
	},

	getImageInfo: function(filepath) {
		var i_cmp = this;
		Ext.Ajax.request({
			url: "/ip2-services/imageIdentify.aspx",
			params: {
				file: filepath
			},
			success: function(o_response) {
				var o_info = JSON.parse(o_response.responseText);
				var width = o_info.width;
				var height = o_info.height;
				var size = o_info.filesize;

				i_cmp.form.getForm().setValues({
					FILWIDTH: width,
					FILHEIGHT: height,
					FILSIZE: size
				});
				i_cmp.setSavable(true);
			}
		});
	}
});
