Ext.define("IceCap.controller.Main", {
	extend: "Ext.app.ViewController",

	alias: "controller.icecap.view.main",

	listen: {
		global: {
			beforelogout: {
				fn: function() {

					var i_controller = this;
					i_controller.onBeforeLogout();
				}
			}
		}
	},

	onAfterRender: function(i_panel) {

		var i_self = i_panel;

		i_self.addKeyEvents();

		var i_content = i_self.getContent();
		var i_main = i_self.getMain();
		var o_rules = i_self.getCssRules();

		var o_inputData = i_self.getInputData() || {};

		if (o_inputData.window) {
			i_self.doCreateFromData(o_inputData);
		}

		if (i_content) {

			var a_fields = IceCap.util.Handler.getInputFields(i_content);

			var i_found = Ext.Array.findBy(a_fields, function (i_field) {
				return i_field.getTabIndex() == i_self.getNeedFocus();
			});

			if (i_found) {
				i_found.focus(false, 10);
			}
			else {
				i_self.setCurrentPosition({
					col: o_inputData.csrCol,
					lin: o_inputData.csrLin
				});

				i_main.focus(true, 10);
			}

			i_content.on({
				mousedown: {
					element: "el",
					fn: function (i_event) {

						var i_selector = i_self.getSelector();

						var i_box = i_content.getRegion(true, false);

						var i_target = Ext.get(i_event.getTarget());
						var i_component = i_target.component || Ext.ComponentManager.getActiveComponent();

						if (!i_component.isXType("field") &&
							!i_event.within(i_selector.getEl())) {

							var o_click = Ext.apply({
								x: i_event.pageX - i_box.x,
								y: i_event.pageY - i_box.y
							});

							var o_position = IceCap.util.Handler.toColAndLine(o_click, o_rules);

							var n_x = IceCap.util.Handler.getX(o_position.col, o_rules.fontScale);
							var n_y = IceCap.util.Handler.getY(o_position.lin, o_rules.lineScale);

							i_selector.resetPos(n_x, n_y);

							i_selector.mouseDown = true;

							if (i_selector.isVisible()) {
								i_selector.setVisible(false);
							}
						}
					},
					buffer: 10
				},
				mousemove: {
					element: "el",
					fn: function (i_event) {

						var i_selector = i_self.getSelector();

						if (i_selector && i_selector.mouseDown) {

							var i_box = i_content.getRegion(true, false);

							var o_click = Ext.apply({
								x: i_event.pageX - i_box.x,
								y: i_event.pageY - i_box.y
							});

							var o_position = IceCap.util.Handler.toColAndLine(o_click, o_rules);

							var n_x = IceCap.util.Handler.getX(o_position.col, o_rules.fontScale);
							var n_y = IceCap.util.Handler.getY(o_position.lin, o_rules.lineScale);


							i_selector.setDragX(n_x);
							i_selector.setDragY(n_y);

							i_selector.setSelecting(true);
						}
					}
				},
				mouseup: {
					element: "el",
					fn: function (i_event) {

						var i_selector = i_self.getSelector();

						if(i_selector) {
							i_selector.mouseDown = false;
							i_selector.setSelecting(false);
							i_selector.fireEvent("dragcomplete", i_selector);
						}
					}
				}
			});

			Ext.getBody().on({
				paste: {
					fn: function (i_event) {

						var i_target = i_event.getTarget();
						var i_element = Ext.get(i_target);

						if (i_element.isInputField()) {

							i_event.stopEvent();

							var i_clipboardData = i_event.browserEvent.clipboardData;

							var s_data = i_clipboardData.getData("text/plain") || "";

							var a_split = s_data.split("\n");

							if (a_split.length > 0) {

								Ext.suspendLayouts();

								var i_component = i_element.component;

								var i_window = i_component.up("icecap-window");

								var i_container = i_component.up("icecap-panel");

								if (i_window) {

									i_container = i_window.getContent();
								}

								var a_inputs = IceCap.util.Handler.getInputFields(i_container);

								var a_filtered = Ext.Array.filter(a_inputs, function (i_field) {

									var b_found = false;

									if (i_field.lin >= i_component.lin) {

										b_found = i_field.col == i_component.col;

										if (!b_found) {
											b_found = ((i_field.col + 2) == i_component.col || (i_field.col - 2 == i_component.col));
										}
									}

									return b_found;
								});

								var i_currentField = null;
								var a_temp = [];

								var n_index = 0;

								while (a_split.length > 0) {

									var s_value = a_split.shift();

									var i_found = Ext.Array.findBy(a_filtered, function (i_search) {
										return i_search.lin == (i_component.lin + n_index);
									});

									//s_value = s_value.trim();

									var s_result = "";

									if (i_found) {

										i_currentField = i_found;
										var a_temp = [];
										var n_maxLength = i_found.maxLength;
										var a_chars = s_value.split("");
										var n_char = 0;
										var b_allowed = true;
										
										s_result += a_chars[n_char];

										if (n_maxLength > 0 && s_value.length) {

											s_result = "";
											var b_allowed = true;
											while (b_allowed && s_result.length < n_maxLength && s_result.length < a_chars.length) {

												var s_char = a_chars[n_char];

												if (i_found.maskRe) {
													b_allowed = i_found.maskRe.test(s_char);
												}

												if (b_allowed) {
													s_result += s_char;
													n_char++;
												}
											}

										}

										if (s_result.length > 0) {
											a_temp.push(s_result);
										}
										
										if (a_split.length === 0 && n_char == a_chars.length) {

											i_currentField.setValue(a_temp.join("\n"));
											a_temp = [];
											i_currentField = null;
										}
										else {

											if (a_temp.length > 0) {
												i_currentField.setValue(a_temp.join("\n"));
											}

											var n_col = i_found.col + n_char;
											var n_currentCol = n_char;

											while(a_chars[n_currentCol] == " ") {
												n_currentCol++;
											}

											while (a_chars.length > n_char) {

												var i_temp = Ext.Array.findBy(a_inputs, function (i_search) {
													return i_search.col == n_col && i_search.lin == i_found.lin;
												});

												if (!i_temp) {
													n_col++;
													n_char++;
												}
												else {

													n_maxLength = i_temp.maxLength;
													if (n_maxLength > 0) {

														while(a_chars[n_currentCol] == " ") {
															n_currentCol++;
														}

														n_char = n_currentCol;

														var s_chars = "";
														var b_allowed = true;
														while (b_allowed && n_char < a_chars.length && s_chars.length < n_maxLength) {

															var s_char = a_chars[n_currentCol];

															if (i_temp.maskRe) {
																b_allowed = i_temp.maskRe.test(s_char);
															}
			
															if (b_allowed) {
																s_chars += s_char;
															}

															n_col++;
															n_char++;
															n_currentCol++;
														}
														if (s_chars.length > 0) {
															i_temp.setValue(s_chars);
														}
														
													}
												}

											}
										}
									}
									else {
										if (i_currentField) {

											var i_box = i_currentField.getRegion(false, true);

											var o_pos = {
												x: IceCap.util.Handler.getX(i_component.col, o_rules.fontScale),
												y: IceCap.util.Handler.getY(i_component.lin + n_index, o_rules.lineScale) - 9,
												width: IceCap.util.Handler.getWidth(1, o_rules.fontScale),
												height: IceCap.util.Handler.getHeight(1, o_rules.lineScale),
											};

											Ext.apply(o_pos, {
												left: o_pos.x,
												right: o_pos.x + o_pos.width,
												top: o_pos.y,
												bottom: o_pos.y + o_pos.height
											});

											if (i_box.intersect(o_pos)) {

												a_temp.push(s_value);

												if (a_split.length == 0) {

													i_currentField.setValue(a_temp.join("\n"));
													a_temp = [];
													i_currentField = null;
												}
											}
											else {
												i_currentField.setValue(a_temp.join("\n"));
												a_temp = [];
												i_currentField = null;
											}
										}
									}

									n_index++;
								}

								i_component.focus(true, 10);

								Ext.resumeLayouts(true);
							}
						}
					}
				}
			});
		}
	},

	onSendData: function(o_config, o_data) {

		var i_controller = this;
		var o_saved = Ext.clone(o_config);
		var i_view = i_controller.getView();
		var s_session = i_view.getSessionValue();

		var o_options = i_view.getOptions();

		o_data = Ext.apply(o_data, {
			i18nLearning: false
		});

		Ext.apply(o_config, {
			session: s_session,
			translateMode:i_view.getEdit()
		});

		Ext.apply(o_config, o_options);

		if (!i_view.getWorking()) {
			i_view.setWorking(true);
			i_view.doTimer();
			IceCap.util.Handler.getData(o_config, o_data, function(o_inputData) {

				if (o_inputData.statusUpdate) {

					i_view.setWorking(false);
					i_view.updateMessage(o_inputData);
					if (o_inputData.status > -1) {
						i_view.doCreateFromData(o_inputData);
					}
					i_controller.onSendData(o_saved, o_data);
				}
				else {

					i_view.setLoading(false);
					i_view.cancelTimer();

					var f_callback = i_view.getResponseCallBack() || Ext.emptyFn;

					if (f_callback != Ext.emptyFn) {

						f_callback(o_inputData);
					}

					var o_plugin = o_inputData.plugin || Ext.apply({}, {
						require: []
					});

					var a_require = o_plugin.require || [];
					var s_run = o_plugin.run || "";

					i_controller.doRequire(a_require, function() {

						if (s_run) {
							eval(s_run);
						}

						if (o_inputData.close) {
							i_view.close();
						}
						else {

							i_view.setWorking(false);
							i_view.doCreateFromData(o_inputData);

							i_view.fireEvent("stateupdate", i_view);
						}
					});
				}
			});
		}
	},

	onWheel: function(i_event) {

		var i_controller = this;
		var i_view = i_controller.getView();

		var i_content = i_view.getContent();

		if (i_content.body.getScrollTop() == 0) {
			var b_hasGrid = i_view.query("gridpanel").length > 0;
			var b_mappanel = i_view.query("icecap-gmappanel").length > 0;

			var n_delta = i_event.getWheelDelta();
			var s_event = "";

			if (n_delta > 0) {
				s_event = "ROLLDOWN";
			}
			else {
				s_event = "ROLLUP";
			}

			if (!b_hasGrid && !b_mappanel && !i_event.ctrlKey){
				i_controller.doKeyEvent(s_event);
			}
		}
	},

	onDblClick: function(i_event) {

		var i_controller = this;
		var i_view = i_controller.getView();
		var o_inputData = i_view.getInputData();
		var i_cursor = i_view.getCursor();
		var o_rules = i_view.getCssRules();

		var i_content = i_view.getContent();
		var i_region = i_content.getRegion(true, false);

		var b_fireEvent = i_view.getEnterOnDblClick();

		var o_click = Ext.apply({
			x: i_event.pageX - i_region.x,
			y: i_event.pageY - i_region.y
		});

		var o_position = IceCap.util.Handler.toColAndLine(o_click, o_rules);

		if (b_fireEvent) {

			var a_metaFields = o_inputData.metaFields || [];

			var i_currentField = i_view.getCurrentField();

			if (i_currentField) {
				o_position = Ext.apply(o_position, {
					lin: i_currentField.lin,
					col: i_currentField.col
				});
				if (i_currentField.inputEl && i_currentField.getValue()) {
					o_position = IceCap.util.Handler.toColAndLine(o_click, o_rules);
				}
			}
			else {

				if (o_position.col > o_inputData.width) {
					o_position.col = o_inputData.width;
				}

				if (o_position.lin > o_inputData.height) {
					o_position.lin = o_inputData.height;
				}

				i_cursor.moveCursor(o_position);
				i_cursor[a_metaFields.length === 0 ? "show" : "hide" ]();
			}

			i_view.setCurrentPosition(o_position);

			if (a_metaFields.length === 0) {
				i_controller.doKeyEvent("ENTER");
			}
		}
		else {

			var n_x = IceCap.util.Handler.getX(o_position.col, o_rules.fontScale);
			var n_y = IceCap.util.Handler.getY(o_position.lin, o_rules.lineScale);

			var o_rect = Ext.apply(o_click, {
				x: n_x,
				y: n_y,
				width: IceCap.util.Handler.getWidth(1, o_rules.fontScale),
				height: IceCap.util.Handler.getHeight(1, o_rules.lineScale),
				left: n_x,
				right: n_x+  IceCap.util.Handler.getWidth(1, o_rules.fontScale),
				top: n_y,
				bottom: n_y + IceCap.util.Handler.getHeight(1, o_rules.lineScale)
			});

			var a_labels = IceCap.util.Handler.getLabels(i_view);

			Ext.Array.each(a_labels, function(i_label) {

				var i_labelRegion = i_label.getRegion(false, true);

				if (i_labelRegion.intersect(o_rect)) {

					var i_selector = i_view.getSelector();

					var o_labelPos = IceCap.util.Handler.toColAndLine(i_labelRegion);

					var n_x = IceCap.util.Handler.getX(o_labelPos.col - 1, o_rules.fontScale);
					var n_y = IceCap.util.Handler.getY(o_labelPos.lin + 1, o_rules.lineScale);

					var a_split = i_label.el.dom.innerText.split(" ");

					var i_result;

					var n_posCol = 0;

					Ext.Array.each(a_split, function(s_word) {

						var n_wordX = i_labelRegion.x + IceCap.util.Handler.getX(n_posCol, o_rules.fontScale);
						var n_wordY = i_labelRegion.y;

						var i_wordRegion = Ext.util.Region.create();

						if(/[\/]/g.test(s_word)) {

							var a_temp = s_word.split(/[\/]/g);

							Ext.Array.each(a_temp, function(s_temp) {

								n_wordX = i_labelRegion.x + IceCap.util.Handler.getX(n_posCol, o_rules.fontScale);
								n_wordY = i_labelRegion.y

								Ext.apply(i_wordRegion, {
									x: n_wordX,
									y: n_wordY,
									width: IceCap.util.Handler.getWidth(s_temp.length, o_rules.fontScale),
									height: i_label.getHeight(),
									left: n_wordX,
									right: n_wordX +  IceCap.util.Handler.getWidth(s_temp.length, o_rules.fontScale),
									top: n_wordY,
									bottom: n_wordY + i_label.getHeight()
								});

								if (i_wordRegion.intersect(o_rect)) {
									i_result = i_wordRegion;
									return false;
								}

								n_posCol = n_posCol + s_temp.length + 1;
							});

						}
						else {

							Ext.apply(i_wordRegion, {
								x: n_wordX,
								y: n_wordY,
								width: IceCap.util.Handler.getWidth(s_word.length, o_rules.fontScale),
								height: i_label.getHeight(),
								left: n_wordX,
								right: n_wordX +  IceCap.util.Handler.getWidth(s_word.length, o_rules.fontScale),
								top: n_wordY,
								bottom: n_wordY + i_label.getHeight()
							});

							if (i_wordRegion.intersect(o_rect)) {
								i_result = i_wordRegion;
							}

							n_posCol = n_posCol + s_word.length + 1;
						}

						if (i_result) {
							return false;
						}
					});

					var n_dragX = IceCap.util.Handler.getX(o_labelPos.col, o_rules.fontScale);
					var n_dragY = IceCap.util.Handler.getY(o_labelPos.lin + 2, o_rules.lineScale);

					if (i_result) {

						n_x = i_result.x;
						n_y = i_result.y;

						n_dragX = i_result.right;
						n_dragY = i_result.bottom;
					}

					i_selector.resetPos(n_x, n_y);

					i_selector.show();

					i_selector.setSelecting(true);

					i_selector.setDragX(n_dragX);
					i_selector.setDragY(n_dragY);

					i_event.stopEvent();

					return false;
				}
			});
		}
	},

	onClick: function(i_event) {

		var i_controller = this;
		var i_view = i_controller.getView();
		var o_inputData = i_view.getInputData();
		var i_cursor = i_view.getCursor();
		var i_content = i_view.getContent();
		var o_rules = i_view.getCssRules();

		var i_activeComponent = Ext.ComponentManager.getActiveComponent();

		var o_click = Ext.apply({
			x: i_event.pageX - i_content.getX(),
			y: i_event.pageY - i_content.getY() 
		});

		var o_position = IceCap.util.Handler.toColAndLine(o_click, o_rules);

		if (i_content.contains(i_activeComponent) || i_content.ownerCt == i_activeComponent) {

			var i_currentField = i_view.getCurrentField();

			var a_metaFields = o_inputData.metaFields || [];

			if (i_currentField) {
				o_position = Ext.apply(o_position, {
					lin: i_currentField.lin,
					col: i_currentField.col
				});
				if (i_currentField.inputEl && i_currentField.getValue()) {
					o_position = IceCap.util.Handler.toColAndLine(o_click, o_rules);
				}
			}
			else {

				var a_inputs = IceCap.util.Handler.getInputFields(i_view);

				var n_width = Math.ceil(IceCap.util.Handler.getWidth(1));
				var n_height = 2;

				var o_box = Ext.apply({
					x: IceCap.util.Handler.getX(o_position.col),
					y: IceCap.util.Handler.getY(o_position.lin),
					width: i_cursor.getWidth() || n_width,
					height: i_cursor.getHeight() || n_height,
					right: IceCap.util.Handler.getX(o_position.col) + i_cursor.getWidth(),
					bottom: IceCap.util.Handler.getY(o_position.lin) + i_cursor.getHeight()
				});

				var b_intersect = false;

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

					var i_element = i_field.getEl();

					if (i_element) {

						var i_region = i_field.getRegion(false, true);

						if (i_region.intersect(o_box)) {

							b_intersect = true;
							i_field.focus(false, 10);

							var n_start = o_position.col - i_field.col;
							var i_inputEl = i_field.inputEl;
							i_inputEl.dom.selectionStart = i_inputEl.dom.selectionEnd = n_start;

							return false;
						}
					}
				});

				if (!b_intersect) {

					if (o_position.col > o_inputData.width) {
						o_position.col = o_inputData.width;
					}

					if (o_position.lin > o_inputData.height) {
						o_position.lin = o_inputData.height;
					}

					i_cursor.moveCursor(o_position);

					i_cursor[a_metaFields.length === 0 ? "show" : "hide"]();
				}
			}
		}

		i_view.setCurrentPosition(o_position);
	},

	doArrowKeys: function (n_direction) {

		var i_controller = this;
		var i_view = i_controller.getView();
		var a_fields = IceCap.util.Handler.getInputFields(i_view);
		var n_tabIndex = i_view.getCurrentTabIndex();

		var i_lastFocus = Ext.ComponentManager.getActiveComponent();

		var n_part = n_direction ? 1 : -1;

		if (i_lastFocus.isXType("field")) {

			var n_next = n_part > 0 ? n_tabIndex + 1 : n_tabIndex -1;

			var i_found;

			i_found = Ext.Array.findBy(a_fields, function (i_field) {
				return i_field.getTabIndex() === n_next;
			});

			if (!i_found) {
				i_found = n_part > 0 ? a_fields[0] : a_fields[a_fields.length - 1];
			}

			if (i_found) {
				i_found.focus(true, 10);
				n_tabIndex = i_found.getTabIndex();
			}
		}

		i_view.setCurrentTabIndex(n_tabIndex);
	},

	doTabKey: function (n_direction) {

		var i_controller = this;
		var i_view = i_controller.getView();
		var a_fields = IceCap.util.Handler.getInputFields(i_view);
		var n_tabIndex = i_view.getCurrentTabIndex();

		var i_lastFocus = Ext.ComponentManager.getActiveComponent();

		var n_part = n_direction ? 1 : -1;

		if (i_lastFocus.isXType("field")) {

			var n_next = n_part > 0 ? n_tabIndex + 1 : n_tabIndex - 1;

			var i_found;

			i_found = Ext.Array.findBy(a_fields, function (i_field) {
				return i_field.getTabIndex() === n_next;
			});

			if (!i_found) {
				i_found = n_part > 0 ? a_fields[0] : a_fields[a_fields.length - 1];
				n_next = i_found.getTabIndex();
			}

			if (i_found) {
				i_found.focus(true, 10);
				n_tabIndex = i_found.getTabIndex();
			}
		}
		else {
			var i_field = a_fields[0];
			i_field.focus(true, 10);
			n_tabIndex = i_field.getTabIndex();
		}

		i_view.setCurrentTabIndex(n_tabIndex);
	},

	doKeyEvent: function (s_value) {

		var i_controller = this;
		var i_view = i_controller.getView();
		var o_rules = i_view.getCssRules();

		var i_cursor = i_view.getCursor();

		var o_data = {
			key: s_value
		};

		var o_position = i_view.getCurrentPosition() || {
			col: 1,
			lin: 1
		};

		if (i_cursor && i_cursor.getEl()) {
			o_position = IceCap.util.Handler.toColAndLine({
				x: i_cursor.getLocalX(),
				y: i_cursor.getLocalY()
			}, o_rules);
		}


		var i_content = i_view.getContent();
		var i_form = i_content.getForm();
		var i_collectionField = i_form.getFields();
		var o_values = {};

		Ext.Array.each(i_collectionField.items, function (i_field) {
			if (i_field.isDirty()) {
				var value = i_field.getSubmitValue ? i_field.getSubmitValue() : i_field.getValue();
				o_values[i_field.itemId || i_field.initialConfig.id || i_field.name] = value;
			}
		});

		Ext.Object.each(o_values, function (s_key, value) {
			var s_id = "ses2.win." + s_key;
			o_data[s_id] = value;
		});

		o_data = Ext.apply(o_data, {
			csrLin: o_position.lin,
			csrCol: o_position.col
		});

		var f_callback = i_view.getRequestCallBack() || Ext.emptyFn;

		if (f_callback != Ext.emptyFn) {

			f_callback(o_data);
		}

		i_controller.onSendData({
			func: ""
		}, o_data);
	},

	doFKey: function(s_value) {

		var i_controller = this;

		i_controller.doKeyEvent(s_value);
	},

	doRequire: function(a_require, f_callback) {

		f_callback = f_callback || Ext.emptyFn;

		a_require = a_require || [];

		if (a_require.length > 0) {

			var a_external = Ext.Array.filter(Ext.Array.map(a_require, function(s_value) {
				if (s_value.indexOf("/") > -1) {
					return s_value;
				}
			}), function(value) {
				return value;
			});

			var a_internal = Ext.Array.filter(Ext.Array.map(a_require, function(s_value) {
				if (s_value.indexOf("/") < 0) {
					return s_value;
				}
			}), function(value) {
				return value;
			});

			if (a_external.length > 0) {
				Ext.Loader.loadScriptsSync(a_external);
			}

			if (a_internal.length > 0) {
				Ext.syncRequire(a_internal);
			}
		}

		f_callback();
	},

	onPCO: function(o_inputData) {

		var i_controller = this;
		var i_view = i_controller.getView();
		var i_keyMap = i_view.getKeyHandler();

		switch (o_inputData.cmdAct) {

			case 0:
				window.open(o_inputData.cmd, o_inputData.cmdTitle, "");
				break;
			case 1:

				var i_webcontainer = Ext.create("IceCap.view.WebContainer", {
					src: o_inputData.cmd
				});

				var i_window = Ext.widget("window", {
					title: o_inputData.cmdTitle,
					layout: "fit",
					collapsible: true,
					items: [
						i_webcontainer
					],
					width: window.outerWidth * 0.9,
					height: window.outerHeight * 0.9
				});

				i_window.show();
				break;
			default:

				var n_act = o_inputData.cmdWait || 0;

				if (n_act) {
					i_keyMap.disable();
				}


				var i_store = Ext.create('Ext.data.Store', {
					proxy: {
						type: "jsonp",
						timeout: "86400000",
						url: o_inputData.cmd,
						nocache: false,
						listeners: {
							exception: {
								fn: function(i_proxy, i_request, i_operation) {
									i_controller.onPCOError(i_operation.error, function() {
										if (n_act) {
											i_keyMap.enable();
											i_view.findFocus();
										}
									});
								}
							}
						}
					}
				});

				i_store.load({
					callback: function (o_result) {
						if (n_act) {
							i_keyMap.enable();
							i_view.findFocus();
						}
					}
				});
				break;
		}
	},

	onPCOError: function(s_error, f_callback) {

		f_callback = f_callback || Ext.emptyFn;

		var i_template = new Ext.XTemplate(
			"<div class=\"iceCap-msg\">",
				"<h3>{header}</h3>",
				"<ul>",
					"<li>{message}</li>",
				"</ul>",
			"</div>"
		);

		var i_window = Ext.widget("window", {
			title: "PCO Error",
			width: 800,
			modal: true,
			closable: false,
			html: i_template.apply({
				header: "Error",
				message: s_error
			}),
			bodyPadding: 5,
			buttons: [{
				text: "Ok",
				tabIndex: 1,
				focusable: true,
				listeners: {
					afterrender: {
						fn: function(i_button) {
							setTimeout(function() {
								i_button.focus(false, 10);
							});
						}
					}
				},
				handler: function() {
					i_window.close();
					f_callback();
				}
			}]
		});

		i_window.show();
	},

	onDestroy: function(i_panel) {

		var s_session = i_panel.getSessionValue();

		var o_rules = i_panel.getCssRules();

		Ext.util.CSS.removeStyleSheet(o_rules.sheet);

		IceCap.util.Handler.removeSession(s_session);

		Ext.Ajax.request({
			method: "POST",
			url: IceCap.util.Handler.getPath({
				session: s_session,
				func: "",
				close: "on"

			})
		});
	},

	onBeforeDestroy: function() {

		var i_controller = this;
		var i_view = i_controller.getView();

		i_controller.onDestroy(i_view);

		var i_window = i_view.up("window");

		if (i_window) {
			i_window.close();
		}
	},

	onActivated: function() {

		var i_controller = this;
		var i_view = i_controller.getView();
		var i_cursor = i_view.getCursor();
		var i_main = i_view.getMain();
		var o_rules = i_view.getCssRules();

		var o_position = i_view.getCurrentPosition();

		var a_inputs = IceCap.util.Handler.getInputFields(i_view);


		var n_width = Math.ceil(IceCap.util.Handler.getWidth(1, o_rules.fontScale));
		var n_height = 2;

		var n_x = IceCap.util.Handler.getX(o_position.col, o_rules.fontScale);
		var n_y = IceCap.util.Handler.getY(o_position.lin, o_rules.lineScale);

		if (i_cursor && i_cursor.rendered) {
			n_x = i_cursor.getLocalX();
			n_y = i_cursor.getLocalY();
		}

		var o_box = Ext.apply({
			x: n_x,
			y: n_y,
			width: n_width,
			height: n_height,
			right: n_x + n_width,
			bottom: n_y + n_height
		});

		var b_intersect = false;

		var o_inputData = i_view.getInputData() || {
			metaFields: []
		};

		var a_metaFields = o_inputData.metaFields || [];

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

			var i_element = i_field.getEl();

			if (i_element) {

				var i_region = i_field.getRegion(false, true);

				if (i_region.intersect(o_box)) {
					b_intersect = true;
					i_field.focus(false, 10);
					return false;
				}
			}
		});

		if (!b_intersect) {

			if (i_cursor) {

				i_cursor.moveCursor(o_position);

				i_cursor[a_metaFields.length === 0 ? "show" : "hide"]();
			}

			if (i_main) {
				setTimeout(function() {
					i_main.focus(true, 10);
				}, 10);
			}
		}
	},

	onApplicationClose: function() {

		var i_controller = this;
		var i_view = i_controller.getView();

		i_view.close();
	},

	onBeforeLogout: function() {

		var i_controller = this;
		var i_view = i_controller.getView();

		i_view.close();
	}
});
