Ext.define("Portfolio.entity.util.Toolbar", {

	extend: "Ext.toolbar.Toolbar",

	xtype: "portfolio-entity-toolbar",

	alias: [
		"widget.entitytoolbar",
		"widget.portfolio.entity.toolbar"
	],

	alternateClassName: [
		"Ext.ux.entity.Toolbar"
	],

	dock: "top",

	defaults: {
		margin: "0px 20px 0px 0px",
		height: 30
	},

	config: {
		grid: null,
		store: null,
		dmdInfo: null,
		model: null,
		allowNew: true,
		allowLink: true,
		allowExport: true,
		allowSearch: true,
		allowFilter: true,
		customFilter: null,
		searchField: null,
		searchCombo: true,
		entityInfo: null
	},

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

				var i_self = i_toolbar;

				var a_items = i_self.createItems();

				i_self.add(a_items);
			},
			single: true
		},
		add: {
			fn: function(i_toolbar, i_item) {

				if (i_item.isXType) {

					if (i_item.isXType("button")) {

						if (i_item.ui.indexOf("default") > -1) {

							i_item.setUI("function");
						}
					}
				}
			}
		}
	},

	requires: [
		"Ext.ux.form.field.EntityComboBox",
		"Ext.ux.form.field.Search",
		"Ext.ux.data.dmd.Proxy",
		"Ext.ux.data.dmd.Store",
		"Ext.ux.data.dmd.Reader",
		"Ext.ux.data.dmd.Writer",
		"Ext.ux.data.dmd.Types",
		"Ext.ux.themed.Button",
		"Ext.ux.themed.MenuItem"
	],

	initComponent: function() {

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

		i_self.enableBubble([
			"editentity",
			"updateTotalCount"
		]);
	},

	createItems: function() {

		var i_self = this;
		var a_return = [];
		var a_buttons = i_self.createButtons();
		var a_functions = [];
		var i_grid = i_self.getGrid();
		var i_store = i_grid.getStore();
		var i_model = i_self.getModel();
		var a_inital = [];

		if (i_self.getAllowSearch()) {
			a_inital.push(i_self.createSearchField());
		}

		if (a_buttons.length > 0) {
			Ext.Array.each(a_buttons, function (i_button) {
				a_inital.push(i_button);
			});
		}

		var i_resultContainer = Ext.widget("container", {
			layout: "anchor",
			flex: 1
		});

		var i_searchField = Ext.widget("searchfield", {
			grid: i_grid,
			store: i_store,
			anchor: "100%",
			listeners: {
				change: {
					fn: function(i_field, value) {

						if (value) {
							i_field.onSearchClick();
						}
						else {
							if (!i_field.isDirty()) {

								i_field.onSearchClick();
							}
						}
					},
					buffer: 100
				},
				specialkey: {
					fn: function(i_field, i_event) {

						if (i_event.getKey() == Ext.event.Event.TAB) {

							i_event.stopEvent();

							var a_fields = i_resultContainer.query("checkbox") || [];

							if (a_fields.length > 0) {
								var i_focus = a_fields[!i_event.shiftKey ? 0 : a_fields.length - 1];

								i_focus.focus(false, 10);
							}
						}
					}
				}
			},
			onSearchClick: function() {

				var i_searchField = this;

				var s_value = i_searchField.getValue();

				s_value = s_value.toLowerCase();

				var i_grid = i_self.getGrid();
				var i_columnManager = i_grid.getColumnManager();
				var a_columns = i_columnManager.getColumns();

				var a_filtered = Ext.Array.filter(a_columns, function(i_column) {

					var b_searchable = i_column.hideable;

					if (s_value && b_searchable) {
						var s_text = i_column.text || "";
						s_text = s_text.toLowerCase();
						b_searchable = s_text.indexOf(s_value) > -1;
					}

					return b_searchable;
				});

				i_resultContainer.removeAll(true);

				var a_checks = [];

				Ext.Array.each(a_filtered, function(i_column) {
					var i_checkbox = Ext.widget("checkbox", {
						boxLabel: i_column.text,
						checked: i_column.isVisible(),
						dataIndex: i_column.dataIndex,
						listeners: {
							change: {
								fn: function (i_checkbox, b_checked) {

									var i_header = i_columnManager.getHeaderByDataIndex(i_checkbox.dataIndex);
									if (i_header) {
										i_header[b_checked ? "show" : "hide"]();
									}
								}
							},
							specialkey: {
								fn: function(i_checkbox, i_event) {

									if (i_event.getKey() == Ext.event.Event.TAB) {

										var a_fields = i_resultContainer.query("checkbox") || [];
										var n_index = Ext.Array.indexOf(a_fields, i_checkbox);

										if (a_fields.length - 1 == n_index && !i_event.shiftKey) {

											i_event.stopEvent();

											i_searchField.focus(true, 10);
										}
									}
								}
							}
						}
					});

					a_checks.push(i_checkbox);
				});

				i_resultContainer.add(a_checks);
			},
			onClearClick: function() {
				var i_field = this;
				i_field.setValue("");


			}
		});

		var i_searchContainer = Ext.widget("container", {
			layout: "anchor",
			bodyPadding: 20,
			items: [
				i_searchField
			]
		});

		var i_menuPanel = Ext.widget("form", {
			layout: {
				type: "vbox",
				align: "stretch"
			},
			width: 200,
			bodyPadding: 20,
			items: [
				i_searchContainer,
				i_resultContainer
			]

		});

		i_self.add(a_inital);

		i_grid.fireEvent("beforetoolbarfill", i_grid, i_self);

		a_return.push({
			xtype: "tbfill"
		});

		a_return.push(i_self.createRefreshButton());

		var i_buttonColumns = Ext.widget("button", {
			iconCls: "pf-columns",
			ui: "function-action",
			tooltip: ip2.i18n({
				da: "Kolonner",
				en: "Columns"
			}),
			arrowVisible: false,
			menu: {
				items: [
					i_menuPanel
				],
				listeners: {
					beforeshow: {
						fn: function() {
	
							var i_grid = i_self.getGrid();
							var i_columnManager = i_grid.getColumnManager();
							var a_columns = i_columnManager.getColumns();
	
							Ext.suspendLayouts();
	
							i_resultContainer.removeAll(true);
	
							var a_filtered = Ext.Array.filter(a_columns, function(i_column) {
								return i_column.hideable;
							});
	
							var a_checks = [];
	
							Ext.Array.each(a_filtered, function(i_column) {
	
								var i_checkbox = Ext.widget("checkbox", {
									boxLabel: i_column.text,
									checked: i_column.isVisible(),
									dataIndex: i_column.dataIndex,
									listeners: {
										change: {
											fn: function (i_checkbox, b_checked) {
	
												var i_header = i_columnManager.getHeaderByDataIndex(i_checkbox.dataIndex);
												if (i_header) {
													i_header[b_checked ? "show" : "hide"]();
												}
											}
										},
										specialkey: {
											fn: function(i_checkbox, i_event) {
	
												if (i_event.getKey() == Ext.event.Event.TAB) {
	
													var a_fields = i_resultContainer.query("checkbox") || [];
													var n_index = Ext.Array.indexOf(a_fields, i_checkbox);
	
													if (a_fields.length - 1 == n_index && !i_event.shiftKey) {
	
														i_event.stopEvent();
	
														i_searchField.focus(true, 10);
													}
												}
											}
										}
									}
								});
	
								a_checks.push(i_checkbox);
							});
	
							i_resultContainer.add(a_checks);
	
							Ext.resumeLayouts(true);
						}
					}
				}
			},
			listeners: {
				afterrender: {
					fn: function(i_button) {

						var i_grid = i_self.getGrid();
						var i_columnManager = i_grid.getColumnManager();
						var a_columns = i_columnManager.getColumns();

						var b_visible  = a_columns.length > 0;

						i_button[b_visible ? "show":"hide"]();
					}
				}
			}
		});

		a_return.push(i_buttonColumns);

		var i_linkButton = i_self.createLinkButton(i_model.create());

		if (i_self.getAllowExport()) {
			a_functions.push(i_self.createExportButton());
		}

		if (!i_self.up("ip2widget") && i_self.getAllowLink()) {
			a_functions.push(i_linkButton);
		}

		var i_filterButton = Ext.widget("button", {
			iconCls: "pf-funnel",
			itemId: "button-filter",
			tooltip: ip2.i18n({
				da: "Filter",
				en: "Filter"
			}),
			enableToggle: true,
			ui: "function-action",
			minWidth: 32,
			hidden: !i_self.getAllowFilter(),
			toggleHandler: function(i_button, b_pressed) {

				var i_grid = i_self.getGrid();

				if (i_self.getAllowFilter()) {
					var i_filterBar = i_grid.getFilterBar();

					i_filterBar[b_pressed ? "show" : "hide"]();

					var a_filters = i_filterBar.getFilters();
					var n_count = a_filters.length;

					var s_iconCls = "pf-funnel";

					if (n_count) {
						s_iconCls = "pf-funnel_active";
					}
					else {
						s_iconCls = b_pressed ? "pf-funnel_active" :
							"pf-funnel";
					}

					i_button.setIconCls(s_iconCls);
				}
			}
		});

		i_self.on({
			afterfilter: {
				fn: function(n_filters) {

					var b_pressed = i_filterButton.pressed;

					var s_icons = "pf-funnel";

					if (n_filters) {
						s_icons = "pf-funnel_active";
					}
					else {
						s_icons = b_pressed ? "pf-funnel_active" :
							"pf-funnel";
					}

					i_filterButton.setIconCls(s_icons);
				}

			}
		});

		a_return.push(i_filterButton);

		var i_buttonFunction;

		if (a_functions.length > 1) {
			i_buttonFunction = Ext.widget("button", {
				iconCls: "pf-form_options",
				arrowVisible: false,
				itemId: "button-functions",
				ui: "function-action",
				tooltip: ip2.i18n({
					da: "Mere",
					en: "More"
				}),
				hidden: a_functions.length === 0,
				listeners: {
					afterrender: {
						fn: function (i_button) {

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

							var b_visible = a_items.length > 0;

							var b_show = false;

							Ext.Array.each(a_items, function (i_item) {
								if (!i_item.hidden) {
									b_show = true;
									return;
								}
							});

							i_button[b_visible && b_show ? "show" : "hide"]();
						}
					}
				},
				menu: {
					defaults: {
						minHeight: 30
					},
					items: a_functions,
					listeners: {
						beforeshow: {
							fn: function(i_menu) {
								
								if (i_grid.getEntityInfo) {

									var i_shift = i_menu.getComponent("menu-shift");

									if (i_shift) {
										i_menu.remove(i_shift);
									}

									var o_info = i_grid.getEntityInfo();
									var o_entity = o_info.entity || {};

									var o_ui = o_entity.ui || {};
									var o_grids = o_ui.grids || {};

									var a_grids = Ext.Object.getKeys(o_grids);

									var s_current = i_grid.getUiName();

									var a_filtered = Ext.Array.filter(a_grids, function(s_view) {
										return s_view != s_current;
									});

									var a_items = [];

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

										var o_view = o_grids[s_key];
										var s_title = ip2.i18n(o_view.title);

										if (o_view.listable) {
											var i_menuitem = Ext.widget("menuitem", {
												text: s_title || s_key,
												handler: function() {
													i_grid.fireEvent("changeview", s_key);
												}
											});
	
											a_items.push(i_menuitem);
										}
									});

									if (a_items.length > 0) {

										i_shift = Ext.widget("menuitem", {
											itemId: "menu-shift",
											text: ip2.i18n({
												da: "Vis",
												en: "Show"
											}),
											menu: {
												plain: true,
												items: a_items
											}
										});
	
										i_menu.add(i_shift);
									}
								}
							}
						}
					}
				}
			});
		}
		else {

			var o_initConfig = Ext.clone(i_linkButton.initialConfig);

			Ext.apply(o_initConfig, {
				ui: "function-action",
				text: "",
				tooltip: o_initConfig.text,
				hidden: !i_self.getAllowLink()
			});

			i_buttonFunction = Ext.widget("button", o_initConfig);
		}

		a_return.push(i_buttonFunction);

		a_return.push({
			xtype: "tbspacer",
			width: 20
		});

		if (i_model) {
			var i_dummy = i_model.create({});
			var o_entity = ip2.getEntityFromRecord(i_dummy);

			if (o_entity.language) {
				a_return.unshift("-");
				a_return.unshift(i_self.createLanguageField());
			}
		}

		i_self.fireEvent("afterfunctions", i_self, i_buttonFunction);

		return a_return;
	},

	createTotalLabel: function(n_total) {

		var a_items = [
			{
				xtype: "tbfill"
			},
			{
				xtype: "tbtext",
				itemId: "lbl_total",
				html: ip2.i18n({
					da: "Total: ",
					en: "Total: "
				}) + n_total
			}
		];
		return a_items;
	},

	createRefreshButton: function() {

		var i_self = this;

		var i_button = Ext.widget("button", {
			iconCls: "pf-arrow_circle",
			itemId: "button-refresh",
			ui: "function-action",
			handler: function() {

				var i_store = i_self.getStore();
				var o_sorters = i_store.getSorters();

				if (i_store.clearAndLoad) {
					i_store.clearAndLoad({
						callback: function() {
							i_store.fireEvent("sort", i_store, o_sorters.getRange());
						}
					});
				}
				else {

					if (i_store.isTreeStore)  {

						var i_rootNode = i_store.getRootNode();

						i_rootNode.collapse();
						i_rootNode.removeAll(false);

						i_store.load({
							callback: function() {
								i_rootNode.expand();
							}
						});
					}
					else {
						i_store.load({
							callback: function() {
								i_store.fireEvent("sort", i_store, o_sorters.getRange());
							}
						});
					}

				}
			}
		});

		return i_button;
	},

	createButtons: function() {

		var i_self = this;
		var a_return = [];
		var o_dmdCfg = i_self.getDmdInfo();

		var o_dmdInfo = ip2.dmd.getInfo({
			dmd: o_dmdCfg.dmd,
			entity: o_dmdCfg.entityName,
			view: o_dmdCfg.uiName,
			context: "grid"
		});

		var b_editable = true;
		var o_entity = o_dmdInfo.entity;
		var o_permission = o_entity.permission;

		if (o_permission) {
			if (o_permission.add) {
				if (o_permission.add.grid !== undefined) {
					b_editable = o_permission.add.grid;
				}
			}
		}

		if (i_self.getAllowNew()) {

			var o_view = o_dmdInfo.view;

			var o_drop = o_view.drop || {
				entities: []
			};

			var i_addButton;

			if (o_drop.entities.length < 2) {
				i_addButton = Ext.widget("button", {
					itemId: "button-add",
					text: ip2.i18n({
						en: "Create",
						da: "Opret"
					}),
					ui: "function",
					disabled: !b_editable ? true : false,
					handler: function() {
						if (i_self.addMode == "form") {

							var i_grid = i_self.getGrid();

							i_grid.handleAction(null, "create", 0);
						}
					}
				});
				a_return.push(i_addButton);
			}
			else {

				var a_items = [];

				var o_model = o_dmdInfo.dmd.model;
				var a_entities = o_model.entities;

				Ext.Array.each(o_drop.entities, function(o_value) {

					var o_entity = Ext.Array.findBy(a_entities, function(o_config) {
						return o_config.name == o_value.name;
					});

					var i_menuitem = Ext.widget("menuitem", {
						itemId: "button-add-" + o_value.name,
						text: ip2.i18n(o_entity.title),
						iconCls: "pf-add",
						disabled: !b_editable ? true : false,
						handler: function() {

							var o_config = Ext.apply({}, {
								dmd: o_value.dmd,
								entity: o_value.name,
								entityName: o_value.name,
								uiName: o_value.view || "portfolio",
								context: "form"
							});

							var i_model = ip2.model(o_config);

							var i_record = i_model.create({});

							if (i_self.addMode == "form") {
								var i_grid = i_self.getGrid();

								i_grid.handleAction(i_record, "update", 0);
							}
						}
					});

					a_items.push(i_menuitem);
				});

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

				var i_button = Ext.widget("button", {
					itemId: "button-add",
					ui: "function",
					text: ip2.i18n({
						en: "Create",
						da: "Opret"
					}),
					disabled: !b_editable ? true : false,
					menu: i_menu
				});

				a_return.push(i_button);
			}
		}

		return a_return;
	},

	createLinkButton: function(i_model) {


		var i_button = Ext.widget("menuitem", {
			text: "Link",
			itemId: "button-link",
			iconCls: "pf-link",
			handler: function() {

				var i_window = Ext.widget("link.window", {
					model: i_model
				});
				i_window.show();
			}
		});

		return i_button;
	},

	createSearchField: function() {

		var i_self = this;
		var i_grid = i_self.getGrid();
		var i_store = i_grid.getStore();
		var o_dmdCfg = i_self.getDmdInfo();
		var s_view = o_dmdCfg.view ? o_dmdCfg.view : o_dmdCfg.uiName;
		var b_searchCombo = i_self.getSearchCombo();
		var o_entityInfo = i_self.getEntityInfo();

		var o_dmd = o_entityInfo.dmd;
		var o_model = o_dmd.model;
		var o_proxy = o_model.proxy || {};

		var s_xtype = b_searchCombo ? "entitycombo" : "searchfield";

		var o_fieldConfig = {
			itemId: "search",
			width: 300,
			isFormField: false,
			enableKeyEvents: true,
			grid: i_self.getGrid(),
			ui: "function-field"
		};

		var o_extra = Ext.applyIf({
			limit: 100
		}, i_grid.extraParams, {
			filter: i_grid.filter ? i_grid.filter : (i_self.customFilter ? [] : void 0)
		});

		if (b_searchCombo) {

			Ext.apply(o_fieldConfig, {
				dmd: o_dmdCfg.dmd,
				entity: o_dmdCfg.entityName,
				view: s_view,
				autoSelect: false,
				autoLoad: false,
				typeAhead: false,
				pageSize: 100,
				customFilter: i_self.customFilter
			});

			if (o_proxy.hasOwnProperty("portfolio") && !o_proxy.portfolio) {
				s_xtype = "restentitypicker";
			}
		}
		else {
			Ext.apply(o_fieldConfig, {
				extraParams: o_extra,
				enableKeyEvents: true,
				store: i_store
			});
		}

		var i_field = Ext.widget(s_xtype, o_fieldConfig);

		if (b_searchCombo) {

			i_field.onSearchClick = function() {

				var i_field = this;
				var i_grid = i_self.getGrid() ? i_self.getGrid() : i_self.up("gridpanel");
				var i_store = i_grid.getStore();
				var i_proxy = i_store.getProxy();
				var o_extraParams = i_proxy.getExtraParams();
				var o_sorters = i_store.getSorters();
				var value = i_field.getRawValue();

				if (value.length > 0) {
					o_extraParams.search = value;
				}
				else {
					delete o_extraParams.search;
				}
				if (i_store.clearAndLoad) {
					i_store.clearAndLoad({
						callback: function() {
							i_store.fireEvent("sort", i_store, o_sorters.getRange());
						}
					});
				}
				else {
					i_store.load({
						callback: function() {
							i_store.fireEvent("sort", i_store, o_sorters.getRange());
						}
					});
				}
			};

			i_field.onClearClick = function() {

				var i_grid = i_self.getGrid() ? i_self.getGrid() : i_self.up("gridpanel");
				var i_store = i_grid.getStore();
				var i_proxy = i_store.getProxy();
				var o_extraParams = i_proxy.getExtraParams();
				var o_sorters = i_store.getSorters();

				i_field.setSelection(null);
				i_field.clearValue();

				i_field.cancelSearch = false;

				if (Ext.isFunction(i_field.setUnion)) {
					i_field.setUnion("");
				}

				delete o_extraParams.search;

				if (i_store.clearAndLoad) {
					i_store.clearAndLoad({
						callback: function() {
							i_store.fireEvent("sort", i_store, o_sorters.getRange());
						}
					});
				}
				else {
					i_store.load({
						callback: function() {
							i_store.fireEvent("sort", i_store, o_sorters.getRange());
						}
					});
				}

				return i_field;
			};

			i_field.on({
				select: {
					fn: function(i_field, i_selected) {

						var i_record = Ext.isArray(i_selected) ? i_selected[0] : i_selected;

						if (i_field.selectFn) {
							i_field.selectFn(i_record);
						}
						else {
							var i_grid = i_self.getGrid() ? i_self.getGrid() : i_self.up("gridpanel");

							if (i_grid.getModelConfig && i_record) {

								var o_config = i_grid.getModelConfig();
								var s_dmd = i_grid.dmdName;
								var s_entity = o_config.entityName;

								if (ip2.isAccessible("app." + s_dmd + ".data.update") ||
									ip2.isAccessible("app." + s_dmd + ".data.read") ||
									ip2.isAccessible("app." + s_dmd + ".data.copy") ||
									ip2.isAccessible("app." + s_dmd + ".data." + s_entity + ".update") ||
									ip2.isAccessible("app." + s_dmd + ".data." + s_entity + ".read") ||
									ip2.isAccessible("app." + s_dmd + ".data." + s_entity + ".copy")) {
									i_grid.handleAction(i_record, "update", 0);
								}
							}
						}
					}
				},
				specialkey: {
					fn: function(f, e) {
						if (e.getKey() == e.ENTER) {
							if (i_field.picker) {
								if (i_field.picker.getNavigationModel()) {
									if (!i_field.picker.getNavigationModel().record) {
										i_field.cancelSearch = true;
										i_field.onSearchClick();
										i_field.collapse();
									}
								}
							}
							else {
								i_field.cancelSearch = true;
								i_field.onSearchClick();
							}
						}
					}
				},
				afterrender: {
					fn: function(i_item) {
						setTimeout(function() {
							i_field.focus(false, 100);
						}, 0);
					}
				},
				collapse: {
					fn: function(i_field) {
						if (i_field.picker) {
							if (i_field.picker.getNavigationModel()) {
								i_field.picker.getNavigationModel().record = null;
							}
						}
					}
				},
				beforequery: {
					fn: function() {

						var i_self = this;
						if (Ext.isFunction(i_self.setUnion)) {
							i_self.setUnion("");
						}

						var b_cancel = !i_self.cancelSearch;
						if (!b_cancel) {
							i_self.cancelSearch = false;
						}
						return b_cancel;
					}
				}
			});

			i_store.field = i_field;
			i_store.grid = i_grid;
		}

		i_self.setSearchField(i_field);

		return i_field;
	},

	createLanguageField: function() {

		var wsh = wsh;
		var i_self = this;
		var a_languages = wsh.getLanguages();
		var s_lang = "";

		if (navigator.appName == "Microsoft Internet Explorer") {
			s_lang = navigator.browserLanguage.split("-").shift();
		}
		else {
			s_lang = navigator.language.split("-").shift();
		}

		if (a_languages.indexOf(s_lang) == -1) {
			s_lang = wsh.i18n.getPrimary();
		}
		else {
			if (a_languages.indexOf(s_lang) !== 0) {
				var n_index = a_languages.indexOf(s_lang);
				var s_value = a_languages.splice(n_index, 1)[0];
				a_languages.unshift(s_value);
				s_lang = s_value;
			}
		}

		var a_items = [];

		Ext.Array.each(a_languages, function(s_value) {
			var o_record = {
				xtype: "menuitem",
				key: s_value,
				text: wsh.i18n.getEnum(s_value),
				iconCls: "AIL-icon AIL-icon-16 icon-flag_" + s_value,
				handler: function(i_menuitem) {
					var i_button = i_menuitem.up("#button-language");
					if (i_button) {
						i_button.setIconCls("AIL-icon AIL-icon-16 icon-flag_" + i_menuitem.key);
						i_button.key = i_menuitem.key;
					}
					var i_store = i_self.getGrid().getStore();
					i_store.load();
				}
			};
			a_items.push(o_record);
		});

		var i_menu = Ext.widget("button", {
			key: s_lang,
			itemId: "button-language",
			iconCls: "AIL-icon AIL-icon-16 icon-flag_" + s_lang,
			menu: {
				items: a_items
			}
		});

		return i_menu;
	},

	createRadioRows: function() {

		var i_self = this;
		var a_items = [];
		var o_dmdInfo = i_self.getDmdInfo();
		var s_dmd = o_dmdInfo.dmd;
		var b_all = true;

		a_items.push({
			boxLabel: ip2.i18n({
				en: "All",
				da: "Alle"
			}),
			checked: true,
			name: "rb-row",
			inputValue: 0
		});

		a_items.push({
			boxLabel: ip2.i18n({
				en: "Displayed",
				da: "Viste"
			}),
			checked: !b_all,
			name: "rb-row",
			inputValue: 1
		});

		// if (ip2.isAccessible("app." + s_dmd + ".data." + i_grid.entity.name + ".export.rows")) {
		// 	a_items.push({
		// 		boxLabel: ip2.i18n({
		// 			en: "All",
		// 			da: "Alle"
		// 		}),
		// 		checked: true,
		// 		name: "rb-row",
		// 		inputValue: 0
		// 	});
		// }
		// if (ip2.isAccessible("app." + s_dmd + ".data." + i_grid.entity.name + ".export.visiblerows")) {
		// 	a_items.push({
		// 		boxLabel: ip2.i18n({
		// 			en: "Displayed",
		// 			da: "Viste"
		// 		}),
		// 		checked: !b_all,
		// 		name: "rb-row",
		// 		inputValue: 1
		// 	});
		// }

		return a_items;
	},

	createExportButton: function () {

		var i_self = this;
		var i_store = Ext.create("Ext.data.Store", {
			fields: ["key", "value"],
			data: [
				{
					"key": 0,
					"value": ip2.i18n({
						en: "Excel",
						da: "Excel"
					})
				}
			]
		});

		var i_button = Ext.widget("menuitem", {
			text: ip2.i18n({
				en: "Export",
				da: "Eksporter"
			}),
			itemId: "buttonExport",
			iconCls: "pf-cloud_download",
			storeFormat: i_store,
			handler: function (item) {

				var i_formpanel = Ext.widget("form", {
					itemId: "formExport",
					layout: "anchor",
					items: [
						{
							xtype: "radiogroup",
							itemId: "col",
							name: "col",
							fieldLabel: ip2.i18n({
								en: "Columns",
								da: "Kolonner"
							}),
							items: [
								{
									boxLabel: ip2.i18n({
										en: "All",
										da: "Alle"
									}),
									checked: true,
									name: "rb-col",
									inputValue: 0
								},
								{
									boxLabel: ip2.i18n({
										en: "Visible",
										da: "Synlige"
									}),
									name: "rb-col",
									inputValue: 1
								}
							]
						},
						{
							xtype: "radiogroup",
							itemId: "row",
							name: "row",
							fieldLabel: ip2.i18n({
								en: "Rows",
								da: "Rækker"
							}),
							items: i_self.createRadioRows()
						},
						{
							xtype: "combobox",
							itemId: "format",
							name: "format",
							store: i_button.storeFormat,
							anchor: "100%",
							labelAlign: "left",
							fieldLabel: ip2.i18n({
								en: "Format",
								da: "Format"
							}),
							queryMode: "local",
							displayField: "value",
							valueField: "key",
							readOnly: true,
							listeners: {
								afterrender: function (item) {
									this.setValue(0);
								}
							}
						}
					]
				});

				var i_window = Ext.widget("window", {
					title: ip2.i18n({
						en: "Export data",
						da: "Eksporter data"
					}),
					ui: "message",
					closable: true,
					modal: true,
					width: 350,
					layout: "fit",
					items: [
						i_formpanel
					],
					buttons: [
						{
							text: ip2.i18n({
								en: "Export",
								da: "Eksporter"
							}),
							handler: function (button) {

								var i_window = button.up("window");
								var i_form = i_formpanel.getForm();
								var o_values = i_form.getFieldValues();
								var url = "ip2-services/ip2dmdProxy.aspx?action=export";

								var i_grid = i_self.getGrid();
								var i_filterBar = i_grid.getFilterBar();
								var i_store = i_grid.getStore();
								var i_proxy = i_store.getProxy();
								var o_extraParams = i_proxy.getExtraParams();
								var o_sorters = i_store.getSorters();
								var s_format = "CVS";

								var o_dmdCfg = i_self.getDmdInfo();
								var a_columns = [];
								var s_search = o_extraParams.search || "";

								//Extracting values from rbColumns
								var n_col = o_values["rb-col"] || 0;

								//Extracting values from rbRows
								var n_row = o_values["rb-row"] || 0;

								//Extracting values from cmbFormat
								var n_format = o_values.format || 0;

								var n_start = 0;
								var n_limit = -1;
								var a_gridcolumns = [];

								if (n_col == 1) {
									a_gridcolumns = i_grid.getColumnManager().getColumns();
								}
								else {
									a_gridcolumns = i_grid.columns;
								}

								Ext.Array.each(a_gridcolumns, function(i_column) {
									a_columns.push({
										name: i_column.dataIndex,
										label: ip2.i18n(i_column.text)
									});
								});

								if (n_row == 1) {
									n_limit = i_store.getPageSize();
								}

								var s_sort = Ext.Array.map(o_sorters.items, function (i_sorter) {
									return i_sorter.getProperty() + " " + i_sorter.getDirection() || "ASC";
								}).join(", ");

								switch (n_format) {
									case 0:
										s_format = "CVS";
										break;
									case 1:
										s_format = "XML";
										break;
								}

								var s_where = o_extraParams.where;

								if (!s_where) {
									s_where = i_filterBar ? i_filterBar.getWhere() : "";
								}

								var i_submitpanel = Ext.create("Ext.form.Panel", {
									standardSubmit: true,         // this is the important part
									url: url
								});

								i_submitpanel.submit({
									params: {
										entity: o_dmdCfg.entityName,
										context: "grid",
										uiName: o_dmdCfg.view || o_dmdCfg.uiName || "portfolio",
										dmd: o_dmdCfg.dmd,
										columns: JSON.stringify(a_columns),
										sort: s_sort,
										search: s_search,
										start: n_start,
										limit: n_limit,
										format: s_format,
										where: s_where
									}
								});

								i_window.close();
							}
						},
						{
							text: ip2.i18n({
								en: "Cancel",
								da: "Annuller"
							}),
							handler: function (item) {
								i_window.close();
							}
						}
					]
				});
				i_window.show();
			}
		});
		return i_button;
	},

	removeRefresh: function() {

		var i_self = this;
		var n_return = -1;

		var i_button = i_self.items.get("button-refresh");

		if (i_button) {
			n_return = i_self.items.indexOf(i_button);
			i_self.remove(i_button, true);
		}

		return -1;
	},

	removeExport: function() {

		var i_self = this;
		var n_return = -1;

		var i_button = i_self.items.get("buttonExport");

		if (i_button) {
			n_return = i_self.items.indexOf(i_button);
			i_self.remove(i_button, true);
		}

		return -1;
	},

	removeAdd: function() {

		var i_self = this;
		var n_return = -1;

		var i_button = i_self.items.get("button-add");

		if (i_button) {
			n_return = i_self.items.indexOf(i_button);
			i_self.remove(i_button, true);
		}

		return -1;
	},

	hideLink: function() {

		var i_self = this;
		var i_button = i_self.items.get("button-link");
		if (i_button) {
			i_button.hide();
		}
	},

	updateAllowFilter: function(b_allow) {

		var i_self = this;
		if (!i_self.isConfiguring) {
			var i_filterButton = i_self.getComponent("button-filter");

			if (i_filterButton) {
				i_filterButton[b_allow ? "show" : "hide"]();
			}
		}
	},

	updateStore: function(i_store) {

		var i_self = this;
		var i_grid = i_self.getGrid();
		var i_searchField = i_self.getSearchField();

		if (i_searchField && !i_searchField.isXType("combobox")) {
			i_searchField.store = i_store;
		}

		if (i_grid) {

			if (i_grid.hasOwnProperty("getShowTotalCount") && i_grid.getShowTotalCount()) {

				i_store.on({
					load: {
						fn: function(store, a_records) {

							var n_total = i_store.getTotalCount();

							if (i_grid.xtype == "ux.entity.tree") {

								n_total = 0;

								var i_root = i_grid.getRootNode();
								i_root.cascadeBy(function(i_node) {
									n_total++;
								});
							}

							i_self.fireEvent("updateTotalCount", n_total);
						}
					}
				});
			}
		}
	}
});
