Ext.define('Ext.ux.folderorganizer.Content', {
	extend: 'Ext.tab.Panel',
	border: 0,
	initComponent: function() {
		var me = this;

		me.counter = 0;
		me.folderSelection = {};

		Ext.tip.QuickTipManager.init();

		// flatten xpd, if passed
		if (me.xpd) {
			me.xpd.flatten = ip2.xpd.flatten(me.xpd);
			ip2.each(me.xpd.flatten.collections, function(collection) {
				collection.uiName = me.uiName || 'portfolio';
			});
		}

		me.items = [];
		me.contents = [];

		me.listeners = {
			nodeselection: function(node) {
				me.folderNodeSelected(node);
			}
		};

		me.callParent();
	},

	/* ----------------------------------------------------------------------------------------------------
	    Folder node selected -> Show content depending on cid
	   ---------------------------------------------------------------------------------------------------- */
	folderNodeSelected: function(node) {
		var me = this;
		var o_rowprops;

		me.folderSelection = node;

		if (node.get("rowProps")) {
			o_rowprops = Ext.decode(node.get("rowProps"));
		}

		if (me.alwaysShowGrid) {
			var collection = this.xpd.flatten.collections[3];
			if (collection && collection.entity) {
				if (Ext.isEmpty(me.contents[collection.id])) {
					me.contents[collection.id] = me.createChild(this.xpd, collection, true);
					me.add(me.contents[collection.id]);
				}
			}
		}
		else {
			// hide and collaps all children
			me.hideChildren();

			var properties = Ext.decode(node.data.PROPERTIES, true) || {};
			var parentCid = properties.__cid__;

			if (!parentCid || parentCid == 0) {
				return;
			}

			var childrenCids = me.getChildrenCids(parentCid);
			if (!childrenCids) {
				return;
			}

			var o_collections = me.xpd.flatten.collections;
			for (var i = 0; i < childrenCids.length; i++) {
				var collection = o_collections[childrenCids[i]];
				if (collection && collection.entity) {
					if (Ext.isEmpty(me.contents[collection.id])) {
						me.contents[collection.id] = me.createChild(this.xpd, collection, true);
						me.add(me.contents[collection.id]);
					}
				}
			}
		}
		me.showChildren(node, childrenCids, o_rowprops);
	},

	showChildren: function(i_node, a_childrenCids, o_rowprops) {

		var i_self = this;
		var o_properties = Ext.decode(i_node.get("PROPERTIES"));
		var o_collections = i_self.xpd.flatten.collections;

		Ext.Array.each(a_childrenCids, function(n_cid) {

			var o_collection = o_collections[n_cid];

			if (o_collection.entity) {

				var i_child = i_self.contents[n_cid];

				var i_element = i_child.getEl();

				if (i_element) {

					i_child.tab.show();
					i_element.setDisplayed(true);

					if (Ext.isFunction(i_child.doLayout)) {

						i_child.doLayout();
					}
					else if (Ext.isFunction(i_child.updateLayout)) {
						i_child.updateLayout();
					}
				}
			}
		});

		i_self.setActiveTab(0);
	},

	hideChildren: function() {

		var i_self = this;
		var a_children = i_self.items.items;

		Ext.Array.each(a_children, function(i_child) {

			var i_element = i_child.getEl();

			if (i_element) {

				i_child.tab.hide();
				i_element.setDisplayed(false);
			}
		});
	},

	/* ----------------------------------------------------------------------------------------------------
	    get children cids
	   ---------------------------------------------------------------------------------------------------- */
	getChildrenCids: function(cid) {
		var collection = this.xpd.flatten.collections[cid];
		if (collection) {
			return collection.allowedchilds;
		}
		else {
			return [];
		}
	},

	/* ----------------------------------------------------------------------------------------------------
	    create child
	   ---------------------------------------------------------------------------------------------------- */
	createChild: function(o_xpd, collection, collapsed) {
		var me = this;

		if (collection.widget) {
			return me.createWidget(collection);
		}
		else if (collection.dmd && collection.entity) {
			return me.createDmdPagingGrid(collection);
		}
		else {
			return Ext.create('Ext.panel.Panel', {
				wrapper: me,
				title: ip2.xpd.translate(me.xpd, collection.title),
				iconCls: 'ip2-tree-application-group',
				collection: me.xpd.flatten.collections[collection.id],
				collapsed: collapsed
			});
		}
	},

	/* ----------------------------------------------------------------------------------------------------
	    create Item grid
	   ---------------------------------------------------------------------------------------------------- */
	createDmdPagingGrid: function(collection) {
		var me = this;

		var panel = Ext.create('Ext.ux.folderorganizer.Itemgrid', {
			wrapper: me,
			displayFilterField: true,
			namePrefix: collection.entity,
			buttons: ['refresh'],
			collection: this.xpd.flatten.collections[collection.id],
			filter: (this.filter) ? this.filter : null,
			copyNode: (me.copyNode === true) ? true : false
		});
		panel.relayEvents(me, ['nodeselection', 'nodeadded', 'nodechanged', 'noderemoved']);

		return panel;
	},

	/* ----------------------------------------------------------------------------------------------------
	    create Widget
	   ---------------------------------------------------------------------------------------------------- */
	createWidget: function(collection) {
		return Ext.create('Ext.ux.container.Ip2Widget', {
			src: collection.widget,
			border: false,
			collection: collection
		});
	},

	/* ----------------------------------------------------------------------------------------------------
	    Set menu button status (disabled / enable)
	   ---------------------------------------------------------------------------------------------------- */
	setMenuStatus: function(menu, id, status) {
		menu.getComponent(id).setDisabled(status);
	},

	/* ----------------------------------------------------------------------------------------------------
	    Get i18n text by language
	   ---------------------------------------------------------------------------------------------------- */
	getI18N: function(key) {
		return ip2.folderorganizer.getTextValue(key);
	}
});
