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

	extend: "Ext.window.Window",

	alias: "widget.filesearch",

	modal: true,

	constain: true,

	width: 800,

	height: 600,

	closable: true,

	layout: "fit",

	config: {
		path: "/",
		okFn: Ext.emptyFn,
		isOk: false,
		cancelFn: Ext.emptyFn,
		tree: null,
		grid: null,
		doSelection: true
	},

	initComponent: function() {

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

		var s_title = ip2.i18n({
			da: "Søg efter et medie",
			en: "Search for a media"
		});

		i_self.setTitle(s_title);
		i_self.add(i_self.createLayout());

		return i_self;
	},

	createLayout: function() {

		var i_self = this;

		var i_form = Ext.widget("form", {
			layout: "border",
			items: [
				i_self.createTree(),
				i_self.createGrid()
			],
			buttons: i_self.createButtons()
		});

		return i_form;
	},

	createTree: function() {

		var i_self = this;

		var i_tree = Ext.ux.file.util.Handler.getUITree({
			useSystem: false,
			region: "west",
			flex: 1,
			path: "$root$",
			split: true,
			rootVisible: false,
			viewConfig: {
				loadMask: false
			},
			listeners: {
				select: {
					fn: function(model, i_node) {

						var i_grid = i_self.getGrid();
						var i_store = i_grid.getStore();

						i_store.loadPage(1);
					}
				}
			}
		});

		var i_store = i_tree.getStore();

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

					var a_selected = i_tree.getSelection();

					if (a_selected.length === 0) {

						var s_path = i_self.getPath();

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

							if (i_record.get("path") == s_path && i_self.getDoSelection()) {
								i_self.setDoSelection(false);

								i_tree.getSelectionModel().select(i_first);
								return false;
							}
						});
					}

					Ext.Array.each(a_records, function(i_record) {
						i_record.set("allowDrag", false);

						if (!i_record.get("hasChild")) {
							i_record.set("loaded", true);
						}
						else {
							i_record.expand();
						}
					});
				}
			}
		});

		i_self.setTree(i_tree);

		return i_tree;
	},

	createGrid: function() {

		var i_self = this;

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

		var i_grid = Ext.ux.file.util.Handler.getUIGrid({
			autoLoad: false,
			flex: 3,
			region: "center",
			selMol: i_selectionModel,
			listeners: {
				afterrender: {
					fn: function(i_grid) {

						var i_toolbar = i_grid.getActionBar();

						var n_index = i_toolbar.removeExport();
						var o_component = i_toolbar.items.getAt(n_index);
						i_toolbar.remove(o_component, true);

						n_index = i_toolbar.removeAdd();
						if (n_index) {
							i_toolbar.items.removeAt(n_index);
						}

						i_grid.events.itemdblclick.clearListeners();
						i_grid.events.itemcontextmenu.clearListeners();

						i_grid.on({
							itemdblclick: {
								fn: function(i_view, i_record, i_element, n_index) {

									i_self.isOk = true;
									i_self.okFn(record);
									i_self.close();
								}
							},
							select: {
								fn: function(i_store, i_record) {

									var i_button = i_self.down("#btnOk");

									if (i_button) {
										i_button.setDisabled(false);
									}
								}
							}
						});
					}
				}
			}
		});

		var i_store = i_grid.getStore();

		i_store.on({
			beforeload: {
				fn: function(i_store, i_operation) {

					var i_tree = i_self.getTree();

					var a_selected = i_tree.getSelection();

					var i_selected = a_selected[0];

					var b_load = false;

					if (i_selected) {
						var i_proxy = i_store.getProxy();
						var o_extraParams = i_proxy.getExtraParams();

						var s_path = "/";

						if (!i_selected.isRoot()) {
							s_path = i_selected.get("path");
						}

						Ext.apply(o_extraParams,  {
							where: "FILPATH like '" + s_path + "%'"
						});

						b_load = true;
					}

					return b_load;
				}
			}
		});

		i_self.setGrid(i_grid);

		return i_grid;
	},

	createButtons: function() {

		var i_self = this;

		var a_buttons = [
			{
				xtype: "button",
				itemId: "btnOk",
				iconCls: "pf-floppy_disk",
				disabled: true,
				text: ip2.i18n({
					da: "Ok",
					en: "Ok"
				}),
				handler: function(i_button) {

					i_self.isOk = true;

					var i_grid = i_self.getGrid();

					var a_selected = i_grid.getSelection();
					i_self.okFn(a_selected[0]);
					i_self.close();
				}
			},
			{
				xtype: "button",
				iconCls: "pf-navigate_cross",
				text: ip2.i18n({
					da: "Annuller",
					en: "Cancel"
				}),
				handler: function(i_button) {
					i_self.close();
				}
			}
		];

		return a_buttons;
	}
});
