Ext.define("IceCap.ux.form.panel.Panel4Area", {

	extend: "Ext.panel.Panel",

	alias: [
		"widget.iceCapPanel4Area"
	],

	layout: "form",

	border: false,

	bodyPadding: 5,

	defaultType: "textfield",

	focusable: false,

	listeners: {
		activate: {
			fn: function(i_panel) {

				var a_fields = i_panel.query("field[focusable]");

				if (a_fields.length > 0) {
					var i_field = a_fields[0];
					i_field.focus(true, 10);
				}
			}
		}
	},

	initComponent: function () {

		var i_self = this;

		var a_items = Ext.Array.slice(i_self.items, 0);

		Ext.Array.each(a_items, function(o_field) {

			o_field.value = o_field.value || o_field.dta;
			o_field.fieldLabel = o_field.fieldLabel || o_field.label;

			o_field.readOnly = o_field.type === "output";

			var o_meta = o_field.metaData || {
				xtype: "textfield"
			};

			delete o_field.metaData;

			Ext.apply(o_field, o_meta);
		});

		i_self.items = a_items;

		i_self.callParent();

		i_self.enableBubble("onAddWebContainer");
	}
});
Ext.define("IceCap.ux.form.panel.TabPanel", {

	extend: "Ext.tab.Panel",

	alias: [
		"widget.tabPanel"
	],

	listeners: {
		afterrender: {
			fn: function(i_panel) {

				var i_self = this;

				i_self.initKeyMap();
			}
		}
	},

	config: {
		keyMap: null
	},

	initComponent: function() {

		var i_self = this;

		i_self.callParent(arguments);
	},

	initKeyMap: function() {

		var i_self = this;

		var a_bindings = [{
			key: "1234567890",
			ctrl: true,
			alt: false,
			handler: function(n_keyCode, i_event) {

				i_event.stopEvent();

				var n_tab = n_keyCode - 49;

				var a_items = i_self.items;

				if (n_tab > a_items.length) {
					n_tab = 0;
				}

				i_self.setActiveTab(n_tab);
			}
		}];

		var i_keyMap = Ext.create("Ext.util.KeyMap", {
			target: i_self.getEl(),
			binding: a_bindings
		});

		i_self.setKeyMap(i_keyMap);
	}
});
Ext.define("IceCap.ux.grid.editor.Controller", {
	extend: "Ext.app.ViewController",

	alias: "controller.icecap.ux.grid.editor.main"
});
Ext.define("IceCap.ux.grid.editor.Main", {

	extend: "Ext.grid.Panel",

	alias: [
		"widget.iceCapEditorGrid",
		"widget.iceCapGrid"
	],

	requires: [
		"IceCap.ux.grid.editor.Controller"
	],

	config: {
		gridConfig: null,
		toolbar: null,
		pagingToolbar: null,
		keys: [],
		extraToolbarButtons: [],
		rowMenu: null,
		defaultOption: "ENTER",
		needFocus: false,
		focusTo: 0,
		theme: ""
	},

	flex: 1,

	listeners: {
		afterrender: {
			fn: function(i_panel) {

				var i_self = this;
				var i_store = i_self.getStore();
				var i_view = i_self.getView();
				var a_records = i_store.getRange()

				Ext.suspendLayouts();

				i_self.getView().setHeight(a_records.length * IceCap.util.Handler.getLineHeight());

				Ext.resumeLayouts(true);

				i_self.setTabIndex(i_self.initialConfig.tabIndex || -1);

				var i_navigationModel = i_self.getNavigationModel();
				var i_keyNav = i_navigationModel.keyNav;

				if (Ext.isArray(i_keyNav)) {
					i_keyNav = i_keyNav[0];
				}
				
				var i_map = i_keyNav.map;

				var i_tabBinding = Ext.Array.findBy(i_map.bindings, function(i_binding) {
					return i_binding.key == Ext.event.Event.TAB;
				});

				i_map.removeBinding(i_tabBinding);

				var i_enterBinding = Ext.Array.findBy(i_map.bindings, function(i_binding) {
					return i_binding.key == Ext.event.Event.ENTER;
				});

				i_map.removeBinding(i_enterBinding);

				i_keyNav.defaultEventAction = "";
				i_map.ignoreInputFields = false;

				i_map.addBinding({
					ctrl: false,
					shift: false,
					alt: false,
					key: Ext.event.Event.TAB,
					handler: function(n_key, i_event) {

						var i_selectionModel = i_self.getSelectionModel();
						var i_current = i_selectionModel.getCurrentPosition();

						var i_store = i_self.getStore();

						if (i_current.rowIdx != (i_store.getTotalCount() - 1)) {
							i_event.stopEvent();
						}

						i_self.getSelectionModel().selectNext(false);
					}
				});

				i_map.addBinding({
					ctrl: false,
					shift: true,
					alt: false,
					key: Ext.event.Event.TAB,
					handler: function(n_key, i_event) {

						var i_selectionModel = i_self.getSelectionModel();
						var i_current = i_selectionModel.getCurrentPosition();

						if (i_current.rowIdx > 0) {
							i_event.stopEvent();
						}
						i_selectionModel.selectPrevious(false);
					}
				});

				i_map.addBinding({
					key: Ext.event.Event.ENTER,
					handler: function(n_key, i_event) {

						var i_selectionModel = i_self.getSelectionModel();
						var i_current = i_selectionModel.getCurrentPosition();

						i_event.stopEvent();

						if (i_current) {

							var i_record = i_current.record;

							var s_id = i_record.getId().split("g.").pop();

							var s_defaultOption = i_self.getDefaultOption();

							var o_data = {
								field: s_id,
								option: s_defaultOption,
								col: Ext.Number.from(i_record.get("col"), 0),
								lin: Ext.Number.from(i_record.get("lin"), 0)
							};

							i_self.fireEvent("fieldoption", [o_data]);
						}
					}
				});

				if (i_self.getNeedFocus()) {

					var i_view = i_self.getView();
					i_view.select(i_self.getFocusTo() - 1);
				}
			}
		},
		itemcontextmenu: {
			fn: function(i_view, i_record, i_html, n_index, i_event) {

				var i_self = this;

				i_event.stopEvent();

				var a_position = i_event.getXY();

				var i_menu = i_self.getRowMenu();

				i_menu.showAt(a_position);
			}
		},
		itemdblclick: {
			fn: function (i_view, i_record, i_item, n_index, i_event, o_options) {

				var i_self = this;

				i_event.stopEvent();

				var s_id = i_record.getId().split("g.").pop();

				var s_defaultOption = i_self.getDefaultOption();

				var o_data = {
					field: s_id,
					option: s_defaultOption,
					col: Ext.Number.from(i_record.get("col"), 0),
					lin: Ext.Number.from(i_record.get("lin"), 0)
				};

				i_self.fireEvent("fieldoption", [o_data]);
			}
		},
		select: {
			fn: function(i_selectionModel, i_record) {

				var i_self = this;
				var o_gridConfig = i_self.getGridConfig();
				var i_columnManager = i_self.getColumnManager();

				if (o_gridConfig.showOption) {

					var i_column = i_columnManager.getHeaderByDataIndex(o_gridConfig.optionDataIndex);

					if (i_column) {
						var i_textfield = i_column.getWidget(i_record);
						i_textfield.focus(10, true);
					}
				}
			}
		}
	},

	initComponent: function() {

		var i_self = this;

		var o_viewConfig = {
			focus: function () {

				var i_self = this;
				i_self.callOverridden(arguments);

				var i_selectionModel = i_self.getSelectionModel();
				var i_current = i_selectionModel.getCurrentPosition();

				if (!i_current) {
					i_self.select(0, false);
				}
				else {
					var i_record = i_current.record;

					i_selectionModel.deselectAll(false);
					i_self.select(i_record);
				}
			},
			getRowClass: function () {
				var i_view = this;
				var i_grid = i_view.grid;
				return "iceCap-row-" + i_grid.getTheme();
			},
			cls: "iceCap-row-" + i_self.getTheme()
		};

		Ext.apply(i_self, {
			viewConfig: o_viewConfig
		});

		var i_store = i_self.createStore();
		var a_columns = i_self.createColumns();

		var o_config = i_self.getGridConfig();

		var i_selectionModel = Ext.create("Ext.selection.RowModel", {
			mode: "MULTI"
		});

		Ext.apply(i_self, {
			store: i_store,
			columns: a_columns,
			enableColumnMove: o_config.enableColumnMove || false,
			selModel: i_selectionModel
		});

		i_self.callParent(arguments);

		if (o_config.showToolbar) {
			i_self.createToolbar();
		}

		i_self.addDocked(i_self.createPagingToolbar());

		i_self.createRowMenu();
	},

	createStore: function() {

		var i_self = this;

		var o_config = i_self.getGridConfig();

		var o_store = o_config.store;
		var o_meta = o_store.metaData;

		var i_store = Ext.create("Ext.data.Store", {
			fields: o_meta.fields,
			autoload: false,
			proxy: {
				type: "memory",
				reader: {
					type: "json"
				}
			},
			data: o_store.rows
		});

		return i_store;
	},

	createColumns: function() {

		var i_self = this;

		var o_config = i_self.getGridConfig();

		var a_return = [];

		var a_columns = o_config.header || [];

		var a_quick = o_config.quickOption || [];

		Ext.Array.each(a_columns, function(o_config, n_index) {

			var o_column = Ext.clone(o_config);

			if (Ext.isFunction(o_column.renderer) &&
					o_column.renderer == (window.renderOptionField ||
					o_column.renderer == window.renderField)
				) {

				var o_option = Ext.Array.findBy(a_quick, function(o_values) {
					return o_values.dataIndex == o_column.dataIndex;
				}) || {};

				Ext.apply(o_column, {
					xtype: "widgetcolumn",
					widget: {
						xtype: "iField",
						metaData: {
							len: o_option.len || 10,
							ffw: o_option.ffw,
							fieldStyle: {
								"text-align": "right"
							}
						},
						listeners: {
							fieldfocus: {
								fn: function(i_field) {

									var i_store = i_self.getStore();

									var i_widgetRecord = i_field.getWidgetRecord();

									var n_index = i_store.find(i_widgetRecord.idProperty, i_widgetRecord.get(i_widgetRecord.idProperty));

									if (n_index > -1) {
										var i_view = i_self.view;
										i_view.select(i_widgetRecord);
									}
								}
							},
							specialKey: {
								fn: function(i_field, i_event) {

									var i_store = i_self.getStore();

									var a_range = i_store.getRange();

									var a_options = [];

									var i_column = i_field.getWidgetColumn();

									Ext.Array.each(a_range, function(i_record) {

										var s_id = i_record.getId().split("g.").pop();

										var i_widget = i_column.getWidget(i_record);

										if (i_widget.getValue()) {

											var o_data = {
												field: s_id,
												option: i_widget.getValue(),
												col: Ext.Number.from(i_record.get("col"), 0),
												lin: Ext.Number.from(i_record.get("lin"), 0)
											};

											a_options.push(o_data);
										}
									});

									i_self.fireEvent("fieldoption", a_options);
								}
							}
						}
					}
				});

				delete o_column.renderer;
			}
			else {
				o_column.renderer = function (value, i_meta, i_record, n_row, n_col) {

					var atr = i_record.get("atr" + (n_col + 1));
					i_meta.tdCls = ["iceCap-output", atr || 20, i_self.getTheme()].filter(Boolean).join("-");

					return value;
				};
			}

			o_column.width = o_column.width || null;
			o_column.flex = o_column.width ? null : 1;

			a_return.push(o_column);
		});

		return a_return;
	},

	createToolbar: function() {

		var i_self = this;

		var o_config = i_self.getGridConfig();

		var a_bottom = Ext.Array.from(o_config.bottomKeys);

		var a_keys = i_self.getKeys();

		var a_extraButtons = o_config.extraToolbarButtons || [];

		var a_omited = Ext.Array.filter(a_bottom, function(o_key) {
			return o_key.key;
		});

		var a_filtered = Ext.Array.filter(a_keys, function(o_key) {

			var i_found = Ext.Array.findBy(a_omited, function(o_filtered) {
				return o_key.id == o_filtered.key;
			});

			return !i_found;
		});

		var a_buttons = i_self.createButtons(a_filtered);

		if (a_extraButtons.length > 0) {

			a_extra = Ext.Array.filter(a_extraButtons, function(o_key) {

				var i_found = Ext.Array.findBy(a_omited, function(o_filtered) {
					return o_key.id == o_filtered.key;
				});

				return !i_found;
			});

			var a_return = i_self.createButtons(a_extra);

			a_buttons = a_buttons.concat(a_return);
		}

		if (a_buttons.length > 0) {
			var i_toolbar = Ext.widget("toolbar", {
				dock: "top",
				items: a_buttons
			});

			i_self.setToolbar(i_toolbar);

			i_self.addDocked(i_toolbar);
		}
	},

	createPagingToolbar: function() {

		var i_self = this;
		var i_store = i_self.getStore();

		var i_toolbar = Ext.widget("toolbar", {
			dock: "bottom",
			items: [
				{
					xtype: "button",
					iconCls: "icecap-icon-page-prev",
					handler: function() {

						i_self.fireEvent("key", "ROLLDOWN");
					}
				},
				'->',
				{
					xtype: "button",
					iconCls: "icecap-icon-page-next",
					handler: function() {

						i_self.fireEvent("key", "ROLLUP");
					}
				}
			]
		});

		i_self.setPagingToolbar(i_toolbar);

		return i_toolbar;
	},

	createRowMenu: function() {

		var i_self = this;
		var o_config = i_self.getGridConfig();

		var a_items = [];

		var s_options = o_config.defaultOption || "";

		var a_defaultsOptions = s_options.split(",");

		var n_position = 99;

		var s_defaultOption = "";

		Ext.Array.each(o_config.menu, function(o_item) {

			var i_item = Ext.widget("menuitem", {
				text: o_item.text + " ( " + o_item.id + " )",
				option: o_item.id,
				breakout: o_item.breakout || false,
				icon: "/system/snapOnIcon.aspx.png?keyword=" + escape(o_item.text) + "&default=folder.png&insertRoot=true",
				handler: function(i_menuitem) {

					var a_selected = i_self.getSelection();

					var f_breakout = eval(i_menuitem.breakout);

					if (!i_menuitem.breakout) {

						var a_options = [];

						Ext.Array.each(a_selected, function(i_record) {

							var s_id = i_record.getId().split("g.").pop();

							var o_data = {
								field: s_id,
								option: i_menuitem.option,
								col: Ext.Number.from(i_record.get("col"), 0),
								lin: Ext.Number.from(i_record.get("lin"), 0)
							};

							a_options.push(o_data);
						});

						i_self.fireEvent("fieldoption", a_options);
					}
					else {
						if (Ext.isFunction(f_breakout)) {

							Ext.Array.each(a_selected, function(i_record) {
								f_breakout(i_record.getData());
							});
						}
					}
				}
			});

			var n_index = a_defaultsOptions.indexOf(o_item.id);

			if (n_index > -1) {
				if (n_index < n_position) {
					n_position = n_index;
					s_defaultOption = o_item.id;
				}
			}

			a_items.push(i_item);
		});

		var i_found = Ext.Array.findBy(a_items, function(i_item) {
			return i_item.option == s_defaultOption;
		});

		if (i_found) {
			i_found.text = "<strong>" + i_found.text + "</strong>";
		}

		var i_menu = Ext.widget("menu", {
			items: a_items
		});

		i_self.setRowMenu(i_menu);
		i_self.setDefaultOption(s_defaultOption);
	},

	createButtons: function(a_config) {

		var i_self = this;

		var a_buttons = [];

		Ext.Array.each(a_config, function(o_config) {

			var o_key = {
				xtype: "button",
				key: o_config.id ? o_config.id : o_config.key,
				text: o_config.text,
				icon: "/system/snapOnIcon.aspx.png?keyword=" + escape(o_config.text) + "&default=folder.png&insertRoot=true",
				iconCls: o_config.iconCls,
				handler: function(i_button) {
					i_self.fireEvent("key", i_button.key);
				}
			};

			a_buttons.push(o_key);
		});

		return a_buttons;
	}

}, function() {

	Ext.apply(window, {
		onRowRightClick: Ext.emptyFn,
		renderField: function (val, meta, rec, row, col) {
			var atr = rec.data["atr" + (col + 1)],
			io = rec.data["io" + (col + 1)];

			if (io != "b" && io != "i") {
				return val;
			}

			var html = "<div id=\"" + Ext.id() + "." + rec.id + ".c" + (col + 1) + "\"";
			return html + "></div>";
		},
		renderOptionField: function(value, i_meta, i_record, n_row, n_col) {

			var s_attr = i_record.get("atr" + (n_col + 1));
			var s_html = "";

			s_html = "<div id=\"win-opt-" + n_row + "\""  ;

			if (!Ext.isEmpty(s_attr)) {
				s_html += " class=\"iceCap-output-" + s_attr + "\"";
			}

			s_html += "></div>";

			return s_html;
		},
		renderQuickKeyField: function(val, meta, rec, row, col) {
			var atr = rec.data['atr' + (col + 1)],
			html;

			if (rec.id.indexOf('q.') > -1) {
				html = '<div id=' + Ext.id() + '.qCol.' + col + '"';
				if (!Ext.isEmpty(atr)) {
					html += ' class="iceCap-output-' + atr + '"';
				}

				html += '></div>';
			}
			else if (!Ext.isEmpty(atr)) {
				html = '<div class="iceCap-output-' + atr + '">' + val + '</div>';
			}
			else {
				html = val;
			}
			return html;
		}
	});
});
Ext.define("IceCap.ux.container.Column", {
	
	extend: "Ext.Container",

	alias: [
		"widget.iColumn"
	],

	defaults: {
		xtype: "container",
		layout: {
			type: "form"
		},
		defaults: {
			autoEl: {},
			style: "padding:8px 0 0 5px"
		}
	},

	layout: {
		type: "column"
	},

	flex: 1,
	focusable: false,

  	constructor: function(o_config) {

		var i_self = this;

		i_self.name = i_self.id || o_config.id;

		delete i_self.id;
		delete o_config.id;

		i_self.callParent([o_config]);
	},

 	initComponent: function() {

 		var i_self = this;

 		i_self.callParent(arguments);
 	}
});Ext.define("IceCap.ux.form.label.Label", {
	
	extend: "Ext.form.field.Display",

	alias: [
		"widget.iLabel"
	],

	config: {
		theme: "default"
	},

	focusable: false,

	initComponent: function () {

		var i_self = this;

		var o_config = i_self.initialConfig;

		var s_value = o_config.text;

		if (o_config.ta == "right" && !o_config.off) {
			//o_config.col += o_config.len - o_config.dta.length;
		}

		if (o_config.off) {
			s_value = Ext.String.leftPad(s_value, s_value.length + o_config.off);
		}

		var o_field = {
			value: s_value,
			htmlEncode: true,
			cls: ["icecap-field", i_self.getTheme()].filter(Boolean).join("-")
		};

		var o_style = {
			"font": "normal " + IceCap.util.Handler.getTextSize() + "px Segoe UI,Segoe,DejaVu Sans,Trebuchet MS,Verdana,sans-serif",
			"margin-top": IceCap.util.Handler.getMargin() + "px !important",
			"min-height": "0px !important"
		};

		Ext.apply(o_field, {
			fieldStyle: o_style
		});

		if (o_config.atr == "27") {
			o_field.hidden = true;
		}

		Ext.apply(i_self, o_field);

		i_self.callParent();

		if (o_config.atr == 23) {

			i_self.addCls("icecap-invalid");
		}
	}
});Ext.define("IceCap.ux.form.field.Field", {

	extend: "Ext.form.FieldContainer",

	alias: [
		"widget.iField"
	],

	config: {
		theme: "default",
		field: null
	},

	border: false,

	layout: "fit",

	mixins: [
		"Ext.form.field.Field"
	],

	listeners: {
		focus: {
			fn: function(i_container) {

				var i_field = i_container.getField();
				i_field.focus(true, 10);
			}
		}
	},

	initComponent: function () {

		var i_self = this;

		var o_config = i_self.initialConfig;

		var n_inputWidth = o_config.width || 80;

		var n_default = IceCap.util.Handler.getDefaultWidth();
		var n_width = n_default;
		if (o_config.len > 1) {
			n_width = ((o_config.len - 1) * IceCap.util.Handler.getColSize()) + n_default;
		}

		if (o_config.len > n_inputWidth) {
			var n_temp = n_inputWidth - o_config.col;
			n_width = ((n_temp - 1) * IceCap.util.Handler.getColSize()) + n_default;
		}

		Ext.apply(i_self, {
			width: n_width
		});

		i_self.callParent();

		i_self.createField();

		var i_field = i_self.getField();

		if (o_config.atr == 25) {

			if (o_field.readOnly) {
				i_self.addCls("icecap-invalid");
			}
			else {
				i_field.markInvalid(true);
			}
		}
	},

	focus: function() {

		var i_self = this;
		var i_field = i_self.getField();

		i_field.focus(true, 10);
		i_self.fireEvent("fieldfocus", i_self);
	},

	createField: function() {

		var i_self = this;

		var o_config = i_self.initialConfig;

		var o_meta = o_config.metaData || {};

		var o_field = {
			inputType: "text",
			xtype: "textfield"
		};

		Ext.apply(o_field, i_self.applyFFW(o_config));

		var s_type = o_field.xtype || "textfield";
		var b_password = o_config.atr == "27";

		if (o_field.readOnly) {

			o_field.xtype = s_type = "displayfield";

			Ext.apply(o_field.fieldStyle, {
				"padding": "1px 3px 2px 3px",
				"margin-top": IceCap.util.Handler.getMargin() + "px !important",
				"margin-left": "1px",
				"border": "1px solid transparent"
			});
		}

		var o_listeners = Ext.apply({}, o_meta.listeners);
		delete o_meta.listeners;

		var a_field = ["icecap-field", i_self.getTheme()];

		var a_pass = [b_password ? "iceCap-input-password" : "iceCap-input-" + (o_config.atr || 20)];

		a_pass.push(i_self.getTheme());

		var a_cls = [a_field.filter(Boolean).join("-"), a_pass.filter(Boolean).join("-")];

		var s_cls = a_cls.join(" ");

		Ext.Object.merge(o_field, {
			enforceMaxLength: true,
			maxLength: o_config.len,
			flex: 1,
			minWidth: IceCap.util.Handler.getDefaultWidth(),
			lin: o_config.lin,
			col: o_config.col,
			emuId: o_config.name,
			value: o_config.dta || "",
			cls: s_cls,
			enableKeyEvents: true,
			selectOnFocus: true,
			listeners: {
				afterrender: {
					fn: function(i_field) {

						if (i_field.inputEl && !i_field.metaField) {
							i_field.inputEl.setMinHeight(0);
							i_field.inputEl.setHeight(IceCap.util.Handler.getDefaultHeight());

							i_field.inputEl.set({
								autocomplete: "off"
							});
						}
					}
				},
				specialKey: {
					fn: function(i_field, i_event) {

						if (i_event.getKey() == Ext.event.Event.ENTER) {
							i_self.fireEvent("specialKey", i_self, i_event);
						}
					}
				},
				focus: {
					fn: function(i_field) {

						i_self.fireEvent("fieldfocus", i_self);
					}
				}
			}
		}, o_meta);

		var o_clone = Ext.clone(o_field);

		if (o_meta.xtype) {

			o_clone.metaField = true;

			o_clone.xtype = s_type = o_meta.xtype;

			delete o_clone.cls;
			delete o_clone.fieldStyle;
			delete o_clone.inputType;
		}

		delete o_clone.width;

		var i_field = Ext.widget(o_clone.xtype, o_clone);

		i_self.setField(i_field);

		i_self.add(i_field);
	},

	getValue: function() {

		var i_self = this;
		var i_field = i_self.getField();
		return i_field.getValue();
	},

	setValue: function(value) {

		var i_self = this;
		var i_field = i_self.getField();
		return i_field.setValue(value);
	},

	applyFFW: function(o_config) {

		var i_self = this;
		var s_hex = o_config.ffw;
		var b_fixed = IceCap.util.Handler.getFixed();

		var o_field = {
			xtype: "textfield"
		};

		var o_fieldStyle = {
			font: "normal " + IceCap.util.Handler.getTextSize() + "px Segoe UI,Segoe,DejaVu Sans,Trebuchet MS,Verdana,sans-serif"
		};

		if (b_fixed) {

			Ext.apply(o_fieldStyle, {
				font: "normal " + IceCap.util.Handler.getTextSize() + "px Lucida Console, Courier New, monospace"
			});
		}

		if (o_config.ta) {

			Ext.apply(o_fieldStyle, {
				"text-align": o_config.ta
			});
		}

		if (s_hex) {

			var n_hex = parseInt(s_hex);

			var n_sanHex = IceCap.util.Handler.createIndicators(s_hex);

			if (n_sanHex & IceCap.util.Handler.FFW.BYPASS) {

				Ext.apply(o_field, {
					readOnly: true
				});
			}

			if (n_sanHex & IceCap.util.Handler.FFW.ALPHA_ONLY) {

				Ext.apply(o_field, {
					maskRe: new RegExp("[a-z A-Z ,.-]")
				});
			}

			if (n_sanHex & IceCap.util.Handler.FFW.NUMERIC_ONLY) {

				Ext.apply(o_field, {
					maskRe: new RegExp("[+\\-,. 0-9]")
				});
			}

			if (n_sanHex & IceCap.util.Handler.FFW.DIGITS_ONLY) {

				Ext.apply(o_field, {
					maskRe: new RegExp("[0-9]")
				});
			}

			if (n_sanHex & IceCap.util.Handler.FFW.MOD_DATA) {

				Ext.apply(o_field, {
					listeners: {
						afterrender: {
							fn: function(i_field) {

								i_field.originalValue = 0xFF;
							},
							single: true
						}
					}
				});
			}

			if (n_sanHex & IceCap.util.Handler.FFW.AUTO_ENTER) {

				// THIS WE DONT NEED IN THE MODERN WORLD...

				// Ext.apply(o_field, {
				// 	listeners: {
				// 		blur: {
				// 			fn: function(i_field) {
				// 				i_self.doKeyEvent("ENTER");
				// 			}
				// 		}
				// 	}
				// });
			}

			if (n_sanHex & IceCap.util.Handler.FFW.MONOCASE) {

				Ext.apply(o_fieldStyle, {
					"text-transform": "uppercase"
				});
			}

			if (n_sanHex & IceCap.util.Handler.FFW.MAND_ENTER) {

				Ext.apply(o_field, {
					allowBlank: false
				});
			}

			if (o_config.atr == "27" && n_hex & (1 << 13)) {

				Ext.apply(o_field, {
					hidden: true,
					disabled: true
				});
			}
		}

		Ext.apply(o_field, {
			fieldStyle: o_fieldStyle
		});

		return o_field;
	}
});
Ext.define("IceCap.ux.form.panel.fusionChart", {

	extend: "Ext.panel.Panel",

	alias: [
		"widget.FUSIONCHART"
	],

	header: false,

	width: 500,

	height: 500,

	bodyPadding: 5,

	focusable: false,

	layout: "fit",

	config: {
		chartData: [],
		chartType: ""
	},

	border: false,

	constructor: function(o_config) {

		var i_self = this;


		Ext.apply(o_config, {
			chartData: o_config.data ? Ext.Array.from(o_config.data) : []
		});

		delete o_config.data;

		i_self.callParent([o_config]);

	},

	initComponent: function() {

		var i_self = this;
		i_self.callParent();

		var i_chart = i_self.createChart();

		i_self.add(i_chart);

	},

	createChart: function() {

		var i_self = this;

		var s_type = i_self.getChartType().toLowerCase();

		var i_chart;
		switch (s_type) {
			case "column3d":
				i_chart = i_self.createColumn3D(i_self.getChartData());
				break;
			case "column":
				i_chart = i_self.createColumn(i_self.getChartData());
				break;
			default:
				break;
		}

		return i_chart;
	},

	createColumn3D: function(a_input) {

		var a_data = [];

		var a_fields = [
			"category",
			"value"
		];

		Ext.Array.each(a_input, function(a_value) {

			var value = a_value[1] || 0;

			if(value) {
				value = value.replace(".", "").replace(",", ".");
				value = parseFloat(value);
			}

			var o_record = {
				category: a_value[0],
				value: value
			};

			a_data.push(o_record);
		});

		a_data = a_data.reverse();

		var i_store = Ext.create("Ext.data.Store", {
			proxy: {
				type: "memory",
				reader: {
					type: "json",
					rootProperty: "rows",
					totalProperty: "totalRows"
				}
			},
			fields: a_fields,
			data: a_data
		});

		var i_chart = Ext.widget("cartesian", {
			store: i_store,
			flex: 1,
			insetPadding: 40,
			axes: [
				{
					type: "numeric",
					position: "right",
					grid: true,
					fields: "value",
					label: {
						rotate: {
							degrees: -30
						}
					}
				},
				{
					type: "category3d",
					fields: "category",
					position: "bottom"
				}
			],
			series: {
				type: "bar3d",
				xField: "category",
				yField: "value",
				style: {
					maxBarWidth: 50,
					minGapWidth: 20
				},
				label: {
					field: "value",
					display: "insideEnd",
					renderer: function (value) {
						return value.toFixed(2);
					}
				}
			}
		});

		return i_chart;
	},

	createColumn: function (a_input) {

		var a_data = [];

		var a_fields = [
			"category",
			"value"
		];

		Ext.Array.each(a_input, function (a_value) {

			var value = a_value[1] || 0;

			if (value) {
				value = value.replace(".", "").replace(",", ".");
				value = parseFloat(value);
			}

			var o_record = {
				category: a_value[0],
				value: value
			};

			a_data.push(o_record);
		});

		a_data = a_data.reverse();

		var i_store = Ext.create("Ext.data.Store", {
			proxy: {
				type: "memory",
				reader: {
					type: "json",
					rootProperty: "rows",
					totalProperty: "totalRows"
				}
			},
			fields: a_fields,
			data: a_data
		});

		var i_chart = Ext.widget("cartesian", {
			store: i_store,
			flex: 1,
			insetPadding: 40,
			axes: [
				{
					type: "numeric",
					position: "right",
					grid: true,
					fields: "value",
					label: {
						rotate: {
							degrees: -30
						}
					}
				},
				{
					type: "category",
					fields: "category",
					position: "bottom"
				}
			],
			series: {
				type: "bar",
				xField: "category",
				yField: "value",
				style: {
					maxBarWidth: 50,
					minGapWidth: 20
				},
				label: {
					field: "value",
					display: "insideEnd",
					renderer: function (value) {
						return value.toFixed(2);
					}
				}
			}
		});

		return i_chart;
	}
});
Ext.define("IceCap.ux.form.field.SimpleComboBox", {

	extend: "Ext.form.field.ComboBox",

	alias: [
		"widget.COMBOBOX"
	],

	valueField: "key",

	displayField: "value",

	typeAhead: true,

	mode: "local",

	forceSelection: true,

	triggerAction: "all",

	selectOnFocus: true,

	minChars: 1,

	config: {
		valueMap: []
	},

	listeners: {
		keydown: {
			fn: function(i_field, i_event) {

				var n_key = i_event.getKey();
				
				if (i_field.isExpanded) {
					if (n_key == Ext.event.Event.DOWN || n_key == Ext.event.Event.UP) {
						i_event.stopEvent();
					}
				}
			}
		}
	},

	initComponent: function() {

		var i_self = this;
		var i_store = i_self.createStore();

		i_self.store = i_store;
		i_self.callParent();
	},

	createStore: function() {

		var i_self = this;

		var a_data = [];

		var a_valueData = i_self.getValueMap();

		Ext.Array.each(a_valueData, function(a_value) {

			if (a_value.length >= 2) {
				var o_data = {
					key: a_value[0],
					value: a_value[1]
				};

				a_data.push(o_data);
			}
		});

		var i_store = Ext.create("Ext.data.Store", {
			fields: [
				{
					name: "key",
					type: "auto"
				},
				{
					name: "value",
					type: "auto"
				}
			],
			autoLoad: true,
			data: a_data
		});

		return i_store;
	}
});
Ext.define("IceCap.ux.form.field.sqlComboBox", {

	extend: "Ext.form.field.ComboBox",

	alias: [
		"widget.DDSQL"
	],

	minChars: 1,

	valueField: "KEY",

	displayField: "VALUE",

	typeAhead: true,

	mode: "remote",

	triggerAction: "all",

	selectOnFocus: true,

	matchFieldWidth: false,

	editable: true,

	config: {
		sqlVoucher: ""
	},

	listeners: {
		keydown: {
			fn: function(i_field, i_event) {

				var n_key = i_event.getKey();
				
				if (i_field.isExpanded) {
					if (n_key == Ext.event.Event.DOWN || n_key == Ext.event.Event.UP) {
						i_event.stopEvent();
					}
				}
			}
		}
	},

	initComponent: function() {

		var i_self = this;
		var i_store = i_self.createStore();

		i_self.store = i_store;
		i_self.callParent();

		var i_model = i_store.getModel();

		var value = i_self.getValue();
		var rawValue = i_self.getRawValue();

		var i_created = i_model.create({
			KEY: rawValue,
			VALUE: value
		});

		if (i_created.get(i_created.idProperty)) {
			i_store.add(i_created);
		}
	},

	initValue: function() {

		var i_self = this;

		var i_store = i_self.getStore();

		var i_model = i_store.getModel();

		var value = i_self.value || null;

		var a_loaded = [];

		if (Ext.isEmpty(value)) {
			value = null;
		}

		var a_values = Ext.Array.from(value);

		var n_load = a_values.length;

		if (n_load) {
			var a_load = [];

			var a_result = [];

			Ext.Array.each(a_values, function(value) {

				var i_test = i_model.create({});
				i_test.set(i_model.idProperty, value);

				if (i_test.get(i_model.idProperty) === value) {
					a_load.push(value);
				}
			});

			if (a_load.length > 0) {

				i_store.load({
					callback: function(a_records, i_store) {

						a_records = Ext.Array.from(a_records);

						if (a_records.length === 0) {
							value = null;
						}

						i_self.suspendCheckChange++;
						i_self.setValue(value);
						i_self.suspendCheckChange--;
					}
				});

				i_self.originalValue = i_self.lastValue = value;
				i_self.resetOriginalValue();
			}
			else {

				i_self.suspendCheckChange++;
				i_self.setValue(null);
				i_self.suspendCheckChange--;
				i_self.originalValue = i_self.lastValue = null;
				i_self.resetOriginalValue();
			}
		}
		else {
			i_self.originalValue = i_self.lastValue = i_self.getValue();
			i_self.resetOriginalValue();
		}
	},

	createStore: function() {

		var i_self = this;

		var i_store = Ext.create("Ext.data.Store", {
			fields: [
				{
					name: "KEY",
					type: "auto"
				},
				{
					name: "VALUE",
					type: "auto"
				}
			],
			pageSize: i_self.pageSize || 25,
			proxy: {
				url: "/icecap/DDSQL.aspx?runSql=runsql&sqlVoucher=" + i_self.getSqlVoucher(),
				type: "ajax",
				reader: {
					type: "json"
				}
			},
			idProperty: "VALUE"
		});

		return i_store;
	}
});
Ext.define("IceCap.ux.form.field.sqlSearchBox", {

	extend: "Ext.form.field.ComboBox",

	alias: [
		"widget.DDSEARCH"
	],

	minChars: 1,
	maxLength: 10,
	valueField: "KEY",
	displayField: "VALUE",
	forceSelection: false,
	typeAhead: false,
	pageSize: 25,
	matchFieldWidth: false,

	config: {
		sqlVoucher: ""
	},

	listeners: {
		keydown: {
			fn: function(i_field, i_event) {

				var n_key = i_event.getKey();
				
				if (i_field.isExpanded) {
					if (n_key == Ext.event.Event.DOWN || n_key == Ext.event.Event.UP) {
						i_event.stopEvent();
					}
				}
			}
		},
		select: {
			fn: function(i_field, i_record, n_number) {

				var a_fields = i_record.getFields();

				Ext.Array.each(a_fields, function(i_recordField) {

					var a_elements = document.getElementsByName(i_recordField.name);

					if (a_elements.length > 0) {

						var i_element = Ext.get(a_elements[0]);
						var i_component = i_element.component;

						var value = i_record.get(i_recordField.name);

						if (i_component) {
							i_component.setValue(value);
						}
						else {
							i_element.dom.value = value;
						}
					}
				});
			}
		}
	},

	initComponent: function() {

		var i_self = this;
		var i_store = i_self.createStore();

		i_self.store = i_store;
		i_self.tpl = i_self.createTemplate();

		i_self.callParent();
	},

	createTemplate: function() {

		var i_template = new Ext.XTemplate(
			"<tpl for=\".\">",
				"<li role=\"option\" class=\"x-boundlist-item\" style=\"text-overflow: ellipsis; white-space: nowrap; overflow:hidden;\">",
					"{VALUE}",
				"</li>",
			"</tpl>"
		);

		return i_template;
	},

	createStore: function() {

		var i_self = this;

		var i_store = Ext.create("Ext.data.Store", {
			fields: [
				{
					name: "KEY",
					type: "auto"
				},
				{
					name: "VALUE",
					type: "auto"
				}
			],
			pageSize: i_self.pageSize || 25,
			proxy: {
				url: "/icecap/DDSQL.aspx?runSql=runsql&sqlVoucher=" + i_self.getSqlVoucher(),
				type: "ajax",
				reader: {
					type: "json"
				}
			}
		});

		return i_store;
	}
});
Ext.define("IceCap.ux.form.field.LINKFIELD", {

	extend: "Ext.form.field.Text",

	alias: [
		"widget.LINKFIELD"
	],

	triggers: {
		switch: {
			cls: "icecap-form-link-trigger",
			overCls: "no-overcls",
			clickCls: "no-clickcls",
			weight: 1,
			scope: "this",
			handler: function(i_trigger) {

				var i_field = this;

				if (!i_field.getValue()) {
					Ext.MessageBox.alert("Message", "Url required");
				}
				else {
					window.open(i_field.getValue(), i_field.getValue(), "");
				}
			}
		}
	},

	validationEvent: false,
	validateOnBlur: false,

	initComponent: function () {

		var i_self = this;
		i_self.callParent();
	}
});
Ext.define("IceCap.ux.form.field.AdvancedComboBox", {

	extend: "Ext.form.field.ComboBox",

	alias: [
		"widget.DDLIB"
	],

	minChars: 1,  // character needed to start server search,
	maxLength: 10,
	displayField: "key",
	forceSelection: false,
	valueField: "key",
	typeAhead: false,
	pageSize: 25,
	hideTrigger: false,
	matchFieldWidth: false,

	listeners: {
		keydown: {
			fn: function(i_field, i_event) {

				var n_key = i_event.getKey();
				
				if (i_field.isExpanded) {
					if (n_key == Ext.event.Event.DOWN || n_key == Ext.event.Event.UP) {
						i_event.stopEvent();
					}
				}
			}
		},
	},

	initComponent: function() {

		var i_self = this;
		i_self.tpl = i_self.createTemplate();

		var i_store = Ext.create("Ext.data.Store", {
			proxy: {
				url: "/icecap/ddlibs.aspx?func=getRows",
				type: "ajax",
				reader: {
					type: "json"
				}
			},
			pageSize: i_self.pageSize || 25,
			fields: [
				{
					name: "key",
					type: "auto"
				},
				{
					name: "value",
					type: "auto"
				}
			],
			autoLoad: true
		});

		i_self.store = i_store;

		i_self.callParent();
	},

	createTemplate: function() {

		var i_template = new Ext.XTemplate(
			"<tpl for=\".\">",
				"<li role=\"option\" class=\"x-boundlist-item\" style=\"text-overflow: ellipsis; white-space: nowrap; overflow:hidden;\">",
					"<b>{key} - </b> {value}",
				"</li>",
			"</tpl>"
		);

		return i_template;
	}
});
Ext.define("IceCap.ux.form.field.CommandField", {

	extend: "Ext.form.field.TextArea",

	alias: [
		"widget.CommandField"
	],

	config: {
		fldNo: 0
	},

	cls: "iceCap-input-field"

});
Ext.define("IceCap.ux.form.field.LibraryFileDependency", {

	extend: "Ext.form.field.Text",

	alias: [
		"widget.LibraryFileDependency"
	],

	listeners: {
		blur: {
			fn: function(i_field) {

				var i_self = this;
				var i_ownerCt = i_self.ownerCt;

				var i_owner = Ext.getCmp(i_ownerCt.id);

				var a_inputs = i_owner.query("input");

				Ext.Array.each(a_inputs, function(i_input) {

					if (i_input.getStore) {

						var i_store = i_input.getStore();
						var i_proxy = i_store.getProxy();

						var o_params = i_proxy.getExtraParams();

						Ext.apply(o_params, {
							lib: i_field.getValue()
						});

						i_store.load();
					}
				});
			}
		}
	}
});
Ext.define("IceCap.ux.form.field.FileLibraryDependency", {

	extend: "Ext.form.field.ComboBox",

	alias: [
		"widget.FileLibraryDependency"
	],

	valueField: "obj",
	displayField: "txt",
	fieldLabel: "File",

	typeAhead: true,

	mode: "remote",

	triggerAction: "all",

	selectOnFocus: true,

	matchFieldWidth: false,

	editable: true,

	config: {
		lib: "qtemp",
		obj: "*all",
		typ: "*all",
		atr: "*all"
	},

	initComponent: function() {

		var i_self = this;
		i_self.tpl = i_self.createTemplate();

		var i_store = Ext.create("Ext.data.Store", {
			params: {
				lib: i_self.getLib(),
				obj: i_self.getObj(),
				typ: i_self.getTyp(),
				atr: i_self.getAtr()
			},
			proxy: {
				url: "/icecap/ddlibs.aspx?func=getRows",
				type: "ajax",
				reader: {
					type: "json"
				}
			},
			fields: [
				{
					name: "obj",
					type: "auto"
				},
				{
					name: "txt",
					type: "auto"
				}
			],
			autoLoad: true
		});

		i_self.store = i_store;

		i_self.callParent();
	},

	createTemplate: function() {

		var i_template = new Ext.XTemplate(
			"<tpl for=\".\">",
				"<li role=\"option\" class=\"x-boundlist-item\" style=\"text-overflow: ellipsis; white-space: nowrap; overflow:hidden;\">",
					"<b>{key} - </b> {value}",
				"</li>",
			"</tpl>"
		);

		return i_template;
	}
})
Ext.define("IceCap.ux.form.field.RadioGroup", {

	extend: "Ext.form.RadioGroup",

	alias: [
		"widget.RADIOGROUP"
	],

	config: {
		valueMap: []
	},

	layout: {
		type: "hbox",
		align: "stretch"
	},

	flex: 1,

	defaults: {
		margin: "0 20 0 0"
	},

	initComponent: function() {

		var i_self = this;

		i_self.items = i_self.createButtons();

		i_self.on({
			afterender: {
				fn: function(i_field) {

					var value = i_field.value;

					if (value) {

						i_field.setValue(value);
					}
				}
			},
			specialkey: {
				fn: function(i_field, e) {

					if (e.keyCode == 39 || e.keyCode == 37) {

						var n_index = 0;

						var b_found = false;

						var n_len = i_field.items.length;

						var a_items = i_field.getBoxes();

						Ext,Array.each(a_items, function(i_radio) {
							if (i_radio.innerWrap.id == e.target.id) {
								b_found = true;
							}
							if (!b_found) {
								n_index++;
							}
						});

						if (e.keyCode == 39) {
							n_index++;
						}
						else if (e.keyCode == 37) {
							n_index--;
						}
						if (b_found) {

							var i_radio;

							if (n_index == n_len) {
								i_radio = i_field.items.get(0);
							}
							else {
								if (n_index < 0) {
									i_radio = i_field.items.get(n_len - 1);
								}
								else {
									i_radio = i_field.items.get(n_index);
								}
							}

							i_radio.innerWrap.focus();

							i_field.setValue(i_radio.inputValue);
						}
					}
				}
			}
		});

		i_self.width = null;
		i_self.flex = 1;

		i_self.callParent(arguments);

		return i_self;
	},

	createButtons: function(a_values) {

		var i_self = this;
		var a_values = i_self.getValueMap();
		var a_return = [];

		Ext.Array.each(a_values, function(o_input) {

			var o_radio = {
				inputValue: o_input.key,
				boxLabel: o_input.value,
				name: i_self.name,
				flex: 1
			};
			a_return.push(o_radio);
		});

		return a_return;
	},

	getValue: function() {

		var i_self = this;
		var value = i_self.value;

		var a_items = i_self.getBoxes();

		Ext.Array.each(a_items, function(i_radio) {
			if (i_radio.inputValue == value) {
				value = i_radio.getGroupValue();
			}
		});

		return value;
	},

	setValue: function(value) {

		var i_self = this;

		var a_items = i_self.getBoxes();

		Ext.Array.each(a_items, function(i_radio) {
			if (i_radio.inputValue == value) {
				i_radio.setValue(true);
			}
			else {
				i_radio.setValue(false);
			}
		});

		i_self.value = value;
	},

	setFocus: function() {

		var i_self = this;
		var b_found = false;

		var a_items = i_self.getBoxes();

		Ext.Array.each(a_items, function(i_radio) {
			if (i_radio.checked) {
				b_found = true;
				i_radio.innerWrap.focus();
				i_self.setValue(i_radio.inputValue);
			}
		});

		if (!b_found) {
			var i_radio = i_self.items.get(0);
			i_radio.innerWrap.focus(true);
			i_self.setValue(i_radio.inputValue);
		}
	}
});
Ext.define("IceCap.ux.form.field.i18n.TextField", {

	extend: "Ext.form.field.Text",

	xtype: "icecap-i18n-textfield",

	alias: [
		"widget.icecap-i18n-textfield",
	],

	ui: "default",

	triggers: {
		"type": {
			cls: "",
			hideOnReadOnly: false,
			handler: function() {

				var i_self = this;

				var o_box = i_self.ownerCt.boundEl.component.ownerCt.getBox();

				var i_trigger = i_self.getTrigger("type");
				var i_menu = i_self.getSelections();

				i_menu.show();
				i_menu.alignTo(i_trigger.getEl(), "tr-br");

				var o_menuBox = i_menu.getBox();

				if (o_menuBox.bottom > o_box.bottom) {
					i_menu.alignTo(i_self.getEl(), "br-tr");
				}

				if (o_menuBox.left < o_box.left) {
					i_menu.alignTo(i_self.getEl(), "bl-tl");
				}

				if (o_menuBox.right > o_box.right) {
					i_menu.alignTo(i_self.getEl(), "br-tl");
				}

				i_menu.toFront();
			}
		}
	},

	listeners: {
		afterrender: {
			fn: function(i_field) {

				var i_self = this;
				var b_active = i_self.getLanguageActive();
				i_self.updateLanguageActive(b_active);

			}
		},
		writeablechange: {
			fn: function(i_field, b_readOnly) {

				var i_self = this;

				var i_trigger = i_self.getTrigger("type");
				i_trigger.show();
			},
			buffer: 10
		},
		enable: {
			fn: function(i_field) {

				var i_self = this;

				var i_trigger = i_self.getTrigger("type");

				i_trigger.show();
			},
			buffer: 10
		},
		disable: {
			fn: function(i_field) {

				var i_self = this;

				var i_trigger = i_self.getTrigger("type");
				i_trigger.hide();
			},
			buffer: 10
		}
	},

	config: {
		languageActive: false,
		language: "en",
		primary: "en",
		languages: [
			"da",
			"en",
			"de"
		],
		stored: {},
		selections: null,
		asString: false,
		global: false,
		originalText: ""
	},

	initComponent: function() {

		var i_self = this;

		i_self.callParent(arguments);

		i_self.createMenu();

		i_self.enableBubble([
			"globalchange",
			"selectionopen",
			"selectionclose"
		]);
	},

	initValue: function() {

		var i_self = this;
		var value = i_self.value;
		var s_current = i_self.getLanguage();
		var b_active = i_self.getLanguageActive();
		var a_languages = i_self.getLanguages();

		var o_stored = i_self.getStored();

		var s_raw = value;

		if (Ext.String.startsWith(s_raw, "{") && Ext.String.endsWith(s_raw, "}")) {
			s_raw = Ext.decode(value, true) || value;
		}

		if (Ext.isObject(s_raw)) {

			o_stored = Ext.clone(s_raw);

			s_raw = o_stored[s_current];

			b_active = true;
		}
		else {
			o_stored[s_current] = s_raw;
		}

		i_self.setValue(s_raw);

		Ext.Array.each(a_languages, function(s_key) {
			var s_value	= o_stored[s_key] || "";
			o_stored[s_key] = s_value;
		});

		i_self.setStored(o_stored);

		if (b_active) {
			i_self.setLanguageActive(true);
		}

		i_self.originalValue = i_self.lastValue = i_self.getValue();
		i_self.resetOriginalValue();
	},

	updateLanguageActive: function(b_active) {

		var i_self = this;
		var i_typeTrigger = i_self.getTrigger("type");

		var s_current = i_self.getLanguage();
		var s_primary = i_self.getPrimary();
		var a_languages = i_self.getLanguages();

		if (i_typeTrigger) {
			if (i_typeTrigger.rendered) {
				var i_element = i_typeTrigger.getEl();
				if (b_active) {
					i_element.removeCls("pf-earth2");
					i_element.addCls("icon flag " + s_current);
					i_self.inputEl.dom.removeAttribute("readonly");
				}
				else {

					Ext.Array.each(a_languages, function(s_lan) {
						i_element.removeCls("icon flag " + s_lan + "-icecap-field");
					});

					i_element.removeCls("icon flag " + s_current);


					i_element.addCls("pf-earth2");
					i_self.inputEl.dom.setAttribute("readonly", true);
				}
			}
			else {
				if (b_active) {
					i_typeTrigger.cls = " icon flag " + s_current;
				}
			}
		}

		if (!b_active) {
			i_self.setLanguage(null);
		}
	},

	createMenu: function() {

		var i_self = this;

		var a_items = [];

		var i_typeTrigger = i_self.getTrigger("type");

		var a_languages = i_self.getLanguages();

		Ext.Array.each(a_languages, function(s_key) {

			var i_menuItem = Ext.widget("menuitem", {
				itemId: s_key,
				text: s_key,
				iconCls: "icon flag " + s_key,
				handler: function(i_item) {

					var i_element = i_typeTrigger.getEl();
					var b_active = i_self.getLanguageActive();
					var s_current = i_self.getLanguage();

					i_element.removeCls("icon flag " + s_current);

					i_self.setLanguage(i_item.itemId);

					if (!b_active) {
						i_self.setLanguageActive(true);
					}
					else {
						i_self.updateLanguageActive(b_active);
					}

					i_self.checkChange();

					i_self.focus(false, 10);
				}
			});

			a_items.push(i_menuItem);
		});

		var i_menuItem = Ext.widget("menuitem", {
			itemId: "off",
			text: "Original",
			iconCls: "pf-earth2",
			handler: function (i_item) {
				i_self.setLanguageActive(false);
				i_self.checkChange();
			}
		});

		a_items.push(i_menuItem);

		a_items.push("-");

		var i_checkGeneral = Ext.widget("menucheckitem", {
			itemId: "check-global",
			text: "Global",
			checked: i_self.getGlobal(),
			listeners: {
				checkchange: {
					fn: function(i_menuitem, b_checked) {

						i_self.setGlobal(b_checked);
					}
				}
			}
		});

		a_items.push(i_checkGeneral);

		var i_menu = Ext.widget("menu", {
			items: a_items,
			listeners: {
				beforeshow: {
					fn: function(i_menu) {

						var a_items = i_menu.query("menuitem");

						var s_current = i_self.getLanguage();

						var b_active = i_self.getLanguageActive();

						Ext.Array.each(a_items, function(i_item) {

							i_item.enable();

							if (i_item.itemId == "off") {
								if (!b_active) {
									i_item.disable();
								}
							}
						});

						if (b_active) {

							var i_found = Ext.Array.findBy(a_items, function(i_item) {
								return s_current == i_item.itemId;
							});

							i_found.disable();
						}

						i_self.fireEvent("selectionopen", i_self);
					}
				},
				beforehide: {
					fn: function() {
						i_self.fireEvent("selectionclose", i_self);
						return true;
					}
				}
			}
		});

		i_self.setSelections(i_menu);
	},

	getValue: function() {

		var i_self = this;
		var s_current = i_self.getLanguage();
		var s_value = i_self.getRawValue();

		var b_active = i_self.getLanguageActive();

		if (b_active) {

			var o_stored = i_self.getStored();
			var s_raw = i_self.getRawValue();

			o_stored[s_current] = s_raw;

			s_value = Ext.clone(o_stored);
		}


		if (i_self.getAsString()) {

			if (Ext.isObject(s_value)) {
				s_value = Ext.encode(s_value);
			}
		}

		return s_value;
	},

	getSubmitValue: function () {

		var i_self = this;
		var s_current = i_self.getLanguage();
		var s_value = i_self.getRawValue();

		var b_active = i_self.getLanguageActive();

		if (b_active) {

			var o_stored = i_self.getStored();
			var s_raw = i_self.getRawValue();

			o_stored[s_current] = s_raw;

			s_value = o_stored;
		}

		if (i_self.getAsString()) {

			if (Ext.isObject(s_value)) {
				s_value = Ext.encode(s_value);
			}
		}

		return s_value;
	},

	getSubmitData: function() {

		var i_self = this;
		return i_self.getValue();
	},

	setValue: function(value) {

		var i_self = this;

		var s_current = i_self.getLanguage();

		var o_stored = i_self.getStored();

		var s_raw = value;

		if (Ext.String.startsWith(s_raw, "{") && Ext.String.endsWith(s_raw, "}")) {
			s_raw = Ext.decode(value, true) || value;
		}

		if (Ext.isObject(s_raw)) {

			o_stored = Ext.clone(s_raw);

			i_self.setLanguageActive(true);

			s_raw = o_stored[s_current];
		}
		else {
			o_stored[s_current] = s_raw;
		}

		i_self.setRawValue(s_raw);

		if (i_self.rendered) {
			i_self.checkChange();
		}
	},

	isDirty: function() {

		var i_self = this;
		var o_initialConfig = i_self.initialConfig;

		var b_original = o_initialConfig.hasOwnProperty("general") ? o_initialConfig.general : false;

		var b_global = i_self.getGlobal();

		var value = i_self.getValue();

		var s_originalValue = Ext.encode(i_self.originalValue, true) || i_self.originalValue;
		var s_value = Ext.encode(value, true) || value;

		return s_originalValue != s_value || b_original != b_global;
	},

	updateLanguage: function(value) {

		var i_self = this;

		if (value) {
			var o_stored = i_self.getStored();

			var s_value = o_stored[value];

			i_self.setValue(s_value);
		}
		else{
			i_self.setRawValue(i_self.getOriginalText());
		}
	},

	didValueChange: function (newValue, oldValue) {

		var i_self = this;

		var s_value = Ext.encode(newValue, true) || newValue;
		var s_originalValue = Ext.encode(i_self.originalValue, true) || i_self.originalValue;

		return s_originalValue != s_value;
	},

	clear: function() {

		var i_self = this;

		i_self.suspendCheckChange++;
		i_self.setValue("");
		i_self.suspendCheckChange--;
	},

	updateGlobal: function(value) {

		var i_self = this;

		if (i_self.rendered) {
			i_self.fireEvent("globalchange", i_self, value);
		}
	}
});
Ext.define("IceCap.ux.container.CommandPanel", {
	
	extend: "Ext.panel.Panel",

	alias: [
		"widget.COMMANDPANEL"
	],
    
    focusable: false,

    width:280,

    height:24,

    border: false,

    header: false,

 	initComponent: function() {

         var i_self = this;
         
         i_self.buttons = i_self.createButtons();

        i_self.callParent(arguments);

        i_self.add(i_self.createItems());
     },
     
     createItems: function() {

        var a_items = [];

        var i_store = Ext.create("Ext.data.Store", {
            autoLoad: true,
			fields: [
				"cmd",
				"description"
			],
			data:[
				{
					cmd: "wrksplf",
					description: "Work with spooled files"
				},
				{
					cmd: "wrksplf",
					description: "Work with spooled files"
				}
			]
        });

        var i_comboBox = Ext.widget("combobox", {
            store: i_store,
            displayField: "description",
            valueField: "cmd",
            width: 250,
            editable: false,
            selectOnFocus: true,
            emptyText: "Select a command and click OK..."
        });

        a_items.push(i_comboBox);

        return a_items;
     },

     createButtons: function() {

        var i_self = this;

        var a_buttons = [];

        var i_bntOK = Ext.widget("button", {
            text: "OK",
            handler: function() {
                debugger;
            }
        });

        a_buttons.push(i_bntOK);

        return a_buttons;
     }
});Ext.define("IceCap.ux.container.GMapPanel", {
	extend: "Ext.panel.Panel",

	alias: "widget.icecap-gmappanel",

	config: {
		center: null,
		mapOption: {},
		scaleControl: true,
		geoCoder: null,
		map: null,
		markers: [],
		zoom: 7,
		stored: null,
		added: [],
        api: "//maps.googleapis.com/maps/api/js",
        gestureHandling: "cooperative"
	},

	border: false,

	listeners: {
		afterrender: {
			fn: function(i_panel) {

				var i_self = this;

				var o_google = window.google || {};

				if (o_google.maps) {
					i_self.initMap();
				}
				else {
					var i_entry = Ext.Boot.getEntry(i_self.getApi(), {
						crossDomain: true,
						listeners: [
							function(i_entry) {
								o_google = window.google || {};
								if (o_google.maps) {
									i_self.initMap();
								}
								else {
									Ext.raise("Error loading google maps api at :" + i_self.getApi());
								}
							}
						]
					});
					i_entry.load(false);
				}
			},
			buffer: 10
		},
		beforedestroy: {
			fn: function(i_panel) {

				var i_self = this;
				var a_markers = i_self.getAdded();

				Ext.Array.each(a_markers, function(i_marker) {
					i_marker.setMap(null);
				});

				i_self.setAdded(null);
				i_self.setMap(null);
				i_self.setStored(null);
				i_self.setCenter(null);
				i_self.setGeoCoder(null);
			}
		}
	},

	initComponent: function() {

		var i_self = this;
		i_self.callParent();
	},

	initMap: function() {

		var i_self = this;

		var o_center = i_self.getCenter() || {
			lat: 55.846330,
			lng: 10.593858
		};

		var o_stored = i_self.getStored() || "";

		if (o_stored) {
			if (o_stored.lat && o_stored.lng) {
				o_center = o_stored;
			}
		}

		if (o_center) {
			if (o_center.geoCodeAddr) {
				i_self.lookupCode(o_center.geoCodeAddr, o_center.marker);
			}
			else {
				i_self.createMap(o_center);
			}
		}
		else {
			Ext.raise("center is required");
		}
	},

	createMap: function(o_center, o_marker) {

		var i_self = this;
		var a_markers = i_self.getMarkers();
		var google = window.google;

		var o_options = Ext.apply({}, i_self.mapOptions);

		o_options = Ext.applyIf(o_options, {
			zoom: i_self.getZoom(),
			center: o_center,
			mapTypeId: google.maps.MapTypeId.MAP,
            scaleControl: i_self.getScaleControl(),
            gestureHandling: i_self.getGestureHandling()
		});

		var i_map = new google.maps.Map(i_self.body.dom, o_options);

		i_self.setMap(i_map);

		if (o_marker) {
			i_self.addMarker(Ext.applyIf(o_marker, {
				position: o_center
			}));
		}

		Ext.Array.each(a_markers, i_self.addMarker, i_self);

		i_self.setCenter(o_center);

		i_self.fireEvent("mapready", i_self, i_map);
	},

	addMarker: function(o_config) {

		var i_self = this;
		var google = window.google;

		o_config = Ext.apply({
			map: i_self.getMap()
		}, o_config);

		if (!o_config.position) {
			o_config.position = new google.maps.LatLng(o_config.lat, o_config.lng);
		}
		var i_marker = new google.maps.Marker(o_config);

		Ext.Object.each(i_marker.listeners, function(s_name, f_handler) {
			google.maps.event.addListener(i_marker, s_name, f_handler);
		});

		var a_added = i_self.getAdded();
		a_added.push(i_marker);

		return i_marker;
	},

	removeMarker: function(i_marker) {

		var i_self = this;
		var a_added = i_self.getAdded();

		Ext.Array.remove(a_added, i_marker);

		i_marker.setMap(null);
	},

	lookupCode: function(addr, marker) {

		var i_self = this;
		var i_geocoder = i_self.getGeoCoder();
		var google = window.google;

		if (!i_geocoder) {
			i_geocoder = new google.maps.Geocoder();
			i_self.setGeoCoder(i_geocoder);
		}

		i_geocoder.geocode({
			address: addr
		}, Ext.Function.bind(i_self.onLookupComplete, i_self, [marker], true));
	},

	lookupAddr: function(addr, f_callback) {

		var i_self = this;

		f_callback = f_callback || function(o_data) {
			var i_element = i_self.getEl();
			if (i_element && i_element.dom) {
				if (i_self.getMap()) {
					var i_map = i_self.getMap();
					if (o_data[0]) {
						i_map.setCenter(o_data[0].geometry.location);

						var a_markers = i_self.getAdded();

						Ext.Array.each(a_markers, function(i_marker) {
							i_marker.setMap(null);
						});

						i_self.setAdded([]);

						i_self.addMarker({
							position: o_data[0].geometry.location
						});
					}
				}
				else {
					if (o_data[0]) {
						i_self.setStored(o_data[0].geometry.location);
					}
				}
			}
			else {
				if (o_data[0]) {
					i_self.setStored(o_data[0].geometry.location);
				}
			}
		};

		var i_geocoder = i_self.getGeoCoder();
		var google = window.google;

		if (!i_geocoder) {
			i_geocoder = new google.maps.Geocoder();
			i_self.setGeoCoder(i_geocoder);
		}

		i_geocoder.geocode({
			address: addr
		}, Ext.Function.bind(f_callback, i_self, [{}], true));
	},

	onLookupComplete: function(o_data, response, marker) {

		var i_self = this;

		if (response != "OK") {
			Ext.MessageBox.alert("Error", "An error occured: \"" + response + "\"");
			return;
		}
		var i_element = i_self.getEl();
		if (i_element.dom) {
			i_self.createMap(o_data[0].geometry.location, marker);
		}
		else {
			i_self.setStored(o_data[0].geometry.location);
		}
	},

	afterComponentLayout: function(w, h) {

		var i_self = this;

		i_self.callParent(arguments);
		i_self.redraw();
	},

	redraw: function() {

		var i_self = this;
		var google = window.google;
		var i_map = i_self.getMap();

		if (i_map) {
			google.maps.event.trigger(i_map, "resize");
		}
	}

});

