Ext.define("Ext.ux.file.proxy.TreeHive", {
	extend: "Ext.data.proxy.Ajax",
	alias: "proxy.hive",

	constructor: function (config) {
		var i_proxy = this;
		var b_system = config.useSystem != undefined ? config.useSystem : true;

		var o_proxy = Ext.Object.merge(config);
		o_proxy.reader = config.reader || {};

		o_reader = {
			type: 'json',
			$configStrict: false,
			useSystem: b_system,
			getData: function(dirs) {

				var hiveList = Ext.ux.file.util.Handler.getHives(null, null, true);

				var parent_path = this.__requests.shift();

				if (dirs.length) {

					Ext.Array.some(hiveList.slice().reverse(), function(hive) {
						if (dirs[0].path.indexOf(hive.path) === 0) {
							var dir = dirs[0];
							var a_path = dir.path.split('/');
							a_path.pop();
							var s_path = a_path.join('/');
							parent_path = s_path.replace(hive.path, hive.alias + "/");
							parent_path = ip2.cleanPath(parent_path);
							return true;
						};
					});
				}

				var hives = Ext.ux.file.util.Handler.getHives(parent_path, this.useSystem);

				if (dirs.length == 0) {
					ip2.each(hives, function(o_hive) {
						if (o_hive.type == "system") {
							o_hive.hasChild = false;
						}
					});
					return hives.slice();
				}

				return Ext.Array.map(dirs, function(dir) {

					var path = ip2.cleanPath('/' + parent_path + '/' + dir.path.split('/').pop()) + '/';

					for (var x = 0; x < hives.length; x++) {
						var hive = hives[x];

						if (path && path === hive.path) {
							hive.hasChild = dir.hasChild;
							return hive;
						}
					}

					dir.path = path;
					dir.iconCls = "x-ux-filemanager-images-default-collapsed";
					dir.allowImport = true;
					dir.allowAdd = true;

					return dir;
				});
			}
		};

		Ext.Object.merge(o_proxy.reader, o_reader);

		i_proxy.callParent([o_proxy]);

		return i_proxy;
	}
});
