Ext.define("Ext.ux.file.field.Filemanager", {
	extend: "Ext.form.FieldContainer",

	mixins: {
		field: "Ext.form.field.Field"
	},

	alias: [
		"widget.fieldfilemanager"
	],

	layout: "fit",
	minHeight: 300,
	height: 300,

	config: {
		grid: null,
		tree: null,
		pnlBorder: null
	},

	initComponent: function(config) {
		var i_self = this;
		i_self.callParent(arguments);

		var o_config = config || {};
		var i_border = i_self.createLayout();
		i_self.add(i_border);
	},

	createLayout: function() {

		var i_self = this;

		var i_pnlBorder = Ext.widget("panel", {
			layout: "border",
			items: [
				i_self.createTree(),
				i_self.createGrid()
			]
		});

		i_self.setPnlBorder(i_pnlBorder);
		return i_pnlBorder;
	},

	createGrid: function() {

		var i_self = this;

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

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

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

						i_grid.on({
							select: {
								fn: function(store, record) {
									i_self.setValue(record.get("FILFILTKN"));
								}
							}
						});
					}
				}
			}
		});

		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;
	},

	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(i_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 i_first = a_records[0];
						
						i_tree.getSelectionModel().select(i_first);
					}

					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;
	},

	setValue: function(value) {

		var i_self = this;
		if (!Ext.isNumber(value)) {
			value = parseInt(value);
			if (isNaN(value)) {
				value = 0;
			}
		}

		if (!Ext.isDefined(i_self.orginalValue)) {
			i_self.orginalValue = value;
		}
		i_self.value = value;
		i_self.mixins.field.setValue.call(i_self, value);
	},

	getValue: function() {
		var i_self = this;
		return i_self.value;
	},

	isDirty: function() {
		var i_self = this;
		return i_self.orginalValue != i_self.value;
	}
});
