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

	singleton: true,

	create: function(o_layout, a_groups, a_widgets, a_relations, a_affiliations, dataParent) {
		var i_self = this;
		var i_panel = {};
		switch (o_layout.type) {
			case "border":
				i_panel = i_self.border(o_layout, a_groups, a_widgets, a_relations, a_affiliations, dataParent);
				break;
			case "accordion":
				i_panel = i_self.accordion(o_layout, a_groups, a_widgets, a_relations, a_affiliations, dataParent);
				break;
			case "hbox":
				i_panel = i_self.hbox(o_layout, a_groups, a_widgets, a_relations, a_affiliations, dataParent);
				break;
			case "vbox":
				i_panel = i_self.vbox(o_layout, a_groups, a_widgets, a_relations, a_affiliations, dataParent);
				break;
			case "fit":
				i_panel = i_self.fit(o_layout, a_groups, a_widgets, a_relations, a_affiliations, dataParent);
				break;
			case "anchor":
				i_panel = i_self.anchor(o_layout, a_groups, a_widgets, a_relations, a_affiliations, dataParent);
				break;
			case "tabpanel":
				i_panel = i_self.tabpanel(o_layout, a_groups, a_widgets, a_relations, a_affiliations, dataParent);
				break;
			case "portal":
				i_panel = i_self.portal(o_layout, a_groups, a_widgets, a_relations, a_affiliations, dataParent);
				break;
		}
		return i_panel;
	},

	border: function(o_layout, a_groups, a_widgets, a_relations, a_affiliations, dataParent) {

		var i_self = this;
		var a_layoutItems = o_layout.items || [];
		var a_panels = [];

		Ext.Array.each(a_layoutItems, function(o_layoutItem) {

			var b_layout = false;
			var a_items = o_layoutItem.items || [];

			if (o_layoutItem.type) {
				b_layout = true;
				var o_layout = o_layoutItem.layout || o_layoutItem;
				var i_return = i_self.create(o_layout, a_groups, a_widgets, a_relations, a_affiliations, dataParent);
				i_return.region = o_layoutItem.region;
				i_return.split = o_layoutItem.split;
				a_panels.push(i_return);
			}
			else {
				var a_children = [];

				Ext.Array.each(a_items, function(o_item, n_index) {

					if (Ext.isObject(o_item)) {
						if (o_item.type || o_item.layout) {
							b_layout = true;
							var o_layout = o_item.layout || o_item;
							var i_return = i_self.create(o_layout, a_groups, a_widgets, a_relations, a_affiliations, dataParent);

							a_children.push(i_return);
						}
					}
					else {
						var b_found = false;
						Ext.Array.each(a_groups, function (o_group) {
							if (o_item == o_group.name) {

								Ext.applyIf(o_group, {
									sequence: n_index,
									flex: 1
								});

								a_children.push(o_group);
								b_found = true;
							}
						});
						if (!b_found) {
							Ext.Array.each(a_widgets, function (o_widget) {
								if ("widget-" + o_item == o_widget.itemId) {
									o_widget.inLayout = true;

									Ext.applyIf(o_widget, {
										sequence: n_index,
										flex: 1
									});

									a_children.push(o_widget);
								}
							});
						}
						if (!b_found) {
							Ext.Array.each(a_relations, function (i_relation) {
								if (o_item == i_relation.itemId) {
									i_relation.inLayout = true;

									Ext.applyIf(i_relation, {
										sequence: n_index,
										flex: 1
									});

									a_children.push(i_relation);
								}
							});
						}
						if (!b_found) {
							Ext.Array.each(a_affiliations, function (i_affiliation) {
								if ("affiliation-" + o_item == i_affiliation.itemId) {
									i_affiliation.inLayout = true;

									Ext.applyIf(i_affiliation, {
										sequence: n_index,
										flex: 1
									});

									a_children.push(i_affiliation);
								}
							});
						}
					}
				});

				a_children.sort(function(a, b) {
					var n_a = (Ext.isNumber(a.sequence) ? a.sequence : 0);
					var n_b = (Ext.isNumber(b.sequence) ? b.sequence : 0);

					var n_min = Math.min(n_a, n_b);

					return n_min;
				});

				var i_panel = Ext.widget("panel", {
					title: o_layoutItem.title ? ip2.i18n(o_layoutItem.title) : "",
					flex: o_layoutItem.flex,
					width: o_layoutItem.width ? o_layoutItem.width : undefined,
					height: o_layoutItem.height ? o_layoutItem.height : undefined,
					minWidth: o_layoutItem.minWidth ? o_layoutItem.minWidth : undefined,
					minHeight: o_layoutItem.minHeight ? o_layoutItem.minHeight : undefined,
					maxWidth: o_layoutItem.maxWidth ? o_layoutItem.maxWidth : undefined,
					maxHeight: o_layoutItem.maxHeight ? o_layoutItem.maxHeight : undefined,
					region: o_layoutItem.region,
					layout: {
						type: b_layout ? "fit" : "anchor"
					},
					defaults: o_layoutItem.defaults,
					items: a_children,
					collapsible: o_layoutItem.collapsible ? o_layoutItem.collapsible : false,
					collapsed: o_layoutItem.collapsed ? o_layoutItem.collapsed : false,
					bodyPadding: o_layoutItem.bodyPadding ? o_layoutItem.bodyPadding : 0,
					border: o_layoutItem.border ? o_layoutItem.border : false,
					dataParent: dataParent,
					split: o_layoutItem.split !== undefined ? o_layoutItem.split : true,
					autoScroll: b_layout ? false : true
				});
				a_panels.push(i_panel);
			}
		});

		a_panels.sort(function(a, b) {
			var n_a = (Ext.isNumber(a.sequence) ? a.sequence : 0);
			var n_b = (Ext.isNumber(b.sequence) ? b.sequence : 0);

			return n_a - n_b;
		});

		var i_panel = Ext.widget("panel", {
			title: o_layout.title ? ip2.i18n(o_layout.title) : "",
			collapsible: o_layout.collapsible ? o_layout.collapsible : false,
			collapsed: o_layout.collapsed ? o_layout.collapsed : false,
			bodyPadding: o_layout.bodyPadding ? o_layout.bodyPadding : 0,
			border: o_layout.border ? o_layout.border : false,
			flex: o_layout.flex,
			width: o_layout.width ? o_layout.width : undefined,
			height: o_layout.height ? o_layout.height : undefined,
			minWidth: o_layout.minWidth ? o_layout.minWidth : undefined,
			minHeight: o_layout.minHeight ? o_layout.minHeight : undefined,
			maxWidth: o_layout.maxWidth ? o_layout.maxWidth : undefined,
			maxHeight: o_layout.maxHeight ? o_layout.maxHeight : undefined,
			split: o_layout.hasOwnProperty("split") ? o_layout.split : undefined,
			defaults: o_layout.defaults || {},
			layout: {
				type: o_layout.type
			},
			items: a_panels,
			dataParent: dataParent,
			autoScroll: o_layout.autoScroll ? o_layout.autoScroll : false
		});
		return i_panel;
	},

	hbox: function(o_layout, a_groups, a_widgets, a_relations, a_affiliations, dataParent) {

		var i_self = this;
		var a_items = o_layout.items;
		var a_children = [];
		Ext.Array.each(a_items, function(o_item, n_index) {
			if (Ext.isObject(o_item)) {
				if (o_item.type || o_item.layout) {
					var o_layout = o_item.layout || o_item;
					var i_return = i_self.create(o_layout, a_groups, a_widgets, a_relations, a_affiliations, dataParent);
					a_children.push(i_return);
				}
			}
			else {
				var b_found = false;
				Ext.Array.each(a_groups, function (o_group) {
					if (o_item == o_group.name) {

						Ext.applyIf(o_group, {
							sequence: n_index,
							flex: 1
						});

						a_children.push(o_group);
						b_found = true;
					}
				});
				if (!b_found) {
					Ext.Array.each(a_widgets, function (o_widget) {
						if ("widget-" + o_item == o_widget.itemId) {
							o_widget.inLayout = true;

							Ext.applyIf(o_widget, {
								sequence: n_index,
								flex: 1
							});

							a_children.push(o_widget);
						}
					});
				}
				if (!b_found) {
					Ext.Array.each(a_relations, function (i_relation) {
						if (o_item == i_relation.itemId) {
							i_relation.inLayout = true;

							Ext.applyIf(i_relation, {
								sequence: n_index,
								flex: 1
							});

							a_children.push(i_relation);
						}
					});
				}
				if (!b_found) {
					Ext.Array.each(a_affiliations, function (i_affiliation) {
						if ("affiliation-" + o_item == i_affiliation.itemId) {
							i_affiliation.inLayout = true;

							Ext.applyIf(i_affiliation, {
								sequence: n_index,
								flex: 1
							});

							a_children.push(i_affiliation);
						}
					});
				}
			}
		});

		a_children.sort(function(a, b) {
			var n_a = (Ext.isNumber(a.sequence) ? a.sequence : 0);
			var n_b = (Ext.isNumber(b.sequence) ? b.sequence : 0);

			return n_a - n_b;
		});

		var i_panel = Ext.widget("panel", {
			title: o_layout.title ? ip2.i18n(o_layout.title) : "",
			collapsible: o_layout.collapsible ? o_layout.collapsible : false,
			collapsed: o_layout.collapsed ? o_layout.collapsed : false,
			bodyPadding: o_layout.bodyPadding ? o_layout.bodyPadding : 0,
			border: o_layout.border ? o_layout.border : false,
			flex: o_layout.flex,
			width: o_layout.width ? o_layout.width : undefined,
			height: o_layout.height ? o_layout.height : undefined,
			minWidth: o_layout.minWidth ? o_layout.minWidth : undefined,
			minHeight: o_layout.minHeight ? o_layout.minHeight : undefined,
			maxWidth: o_layout.maxWidth ? o_layout.maxWidth : undefined,
			maxHeight: o_layout.maxHeight ? o_layout.maxHeight : undefined,
			split: o_layout.hasOwnProperty("split") ? o_layout.split : undefined,
			layout: {
				type: o_layout.type,
				align: "stretch"
			},
			defaults: o_layout.defaults || {},
			items: a_children,
			dataParent: dataParent,
			autoScroll: o_layout.autoScroll ? o_layout.autoScroll : false
		});
		return i_panel;
	},

	vbox: function(o_layout, a_groups, a_widgets, a_relations, a_affiliations, dataParent) {

		var i_self = this;
		var a_items = o_layout.items;
		var a_children = [];

		Ext.Array.each(a_items, function(o_item, n_index) {
			if (Ext.isObject(o_item)) {
				if (o_item.type || o_item.layout) {
					var o_layout = o_item.layout || o_item;
					var i_return = i_self.create(o_layout, a_groups, a_widgets, a_relations, a_affiliations, dataParent);
					a_children.push(i_return);
				}
			}
			else {
				var b_found = false;
				Ext.Array.each(a_groups, function(o_group) {
					if (o_item == o_group.name) {

						Ext.applyIf(o_group, {
							sequence: n_index,
							flex: 1
						});

						a_children.push(o_group);
						b_found = true;
					}
				});
				if (!b_found) {
					Ext.Array.each(a_widgets, function(o_widget) {
						if ("widget-" + o_item == o_widget.itemId) {
							o_widget.inLayout = true;

							Ext.applyIf(o_widget, {
								sequence: n_index,
								flex: 1
							});

							a_children.push(o_widget);
						}
					});
				}
				if (!b_found) {
					Ext.Array.each(a_relations, function(i_relation) {
						if (o_item == i_relation.itemId) {
							i_relation.inLayout = true;

							Ext.applyIf(i_relation, {
								sequence: n_index,
								flex: 1
							});

							a_children.push(i_relation);
						}
					});
				}
				if (!b_found) {
					Ext.Array.each(a_affiliations, function(i_affiliation) {
						if ("affiliation-" + o_item == i_affiliation.itemId) {
							i_affiliation.inLayout = true;

							Ext.applyIf(i_affiliation, {
								sequence: n_index,
								flex: 1
							});

							a_children.push(i_affiliation);
						}
					});
				}
			}
		});

		a_children.sort(function(a, b) {
			var n_a = (Ext.isNumber(a.sequence) ? a.sequence : 0);
			var n_b = (Ext.isNumber(b.sequence) ? b.sequence : 0);

			return n_a - n_b;
		});

		var i_panel = Ext.widget("panel", {
			title: o_layout.title ? ip2.i18n(o_layout.title) : "",
			collapsible: o_layout.collapsible ? o_layout.collapsible : false,
			collapsed: o_layout.collapsed ? o_layout.collapsed : false,
			bodyPadding: o_layout.bodyPadding ? o_layout.bodyPadding : 0,
			border: o_layout.border ? o_layout.border : false,
			flex: o_layout.flex,
			width: o_layout.width ? o_layout.width : undefined,
			height: o_layout.height ? o_layout.height : undefined,
			minWidth: o_layout.minWidth ? o_layout.minWidth : undefined,
			minHeight: o_layout.minHeight ? o_layout.minHeight : undefined,
			maxWidth: o_layout.maxWidth ? o_layout.maxWidth : undefined,
			maxHeight: o_layout.maxHeight ? o_layout.maxHeight : undefined,
			split: o_layout.hasOwnProperty("split") ? o_layout.split : undefined,
			layout: {
				type: o_layout.type,
				align: "stretch"
			},
			defaults: o_layout.defaults || {},
			items: a_children,
			dataParent: dataParent,
			autoScroll: o_layout.autoScroll ? o_layout.autoScroll : false
		});
		return i_panel;
	},

	accordion: function(o_layout, a_groups, a_widgets, a_relations, a_affiliations, dataParent) {

		var i_self = this;
		var a_layoutItems = o_layout.items;
		var a_panels = [];
		Ext.Array.each(a_layoutItems, function(o_layoutItem) {

			var a_children = [];
			var b_layout = false;
			var a_items = o_layoutItem.items || [];

			Ext.Array.each(a_items, function(o_item, n_index) {
				if (Ext.isObject(o_item)) {
					if (o_item.type || o_item.layout) {
						var o_layout = o_item.layout || o_item;
						var i_return = i_self.create(o_layout, a_groups, a_widgets, a_relations, a_affiliations, dataParent);
						a_children.push(i_return);
					}
				}
				else {
					var b_found = false;
					Ext.Array.each(a_groups, function (o_group) {
						if (o_item == o_group.name) {

							Ext.applyIf(o_group, {
								sequence: n_index,
								flex: 1
							});

							a_children.push(o_group);
							b_found = true;
						}
					});
					if (!b_found) {
						Ext.Array.each(a_widgets, function (o_widget) {
							if ("widget-" + o_item == o_widget.itemId) {
								o_widget.inLayout = true;

								Ext.applyIf(o_widget, {
									sequence: n_index,
									flex: 1
								});

								a_children.push(o_widget);
							}
						});
					}
					if (!b_found) {
						Ext.Array.each(a_relations, function (i_relation) {
							if (o_item == i_relation.itemId) {
								i_relation.inLayout = true;

								Ext.applyIf(i_relation, {
									sequence: n_index,
									flex: 1
								});

								a_children.push(i_relation);
							}
						});
					}
					if (!b_found) {
						Ext.Array.each(a_affiliations, function (i_affiliation) {
							if ("affiliation-" + o_item == i_affiliation.itemId) {
								i_affiliation.inLayout = true;

								Ext.applyIf(i_affiliation, {
									sequence: n_index,
									flex: 1
								});

								a_children.push(i_affiliation);
							}
						});
					}
				}
			});

			a_children.sort(function(a, b) {
				var n_a = (Ext.isNumber(a.sequence) ? a.sequence : 0);
				var n_b = (Ext.isNumber(b.sequence) ? b.sequence : 0);

				var n_min = Math.min(n_a, n_b);

				return n_min;
			});

			var i_panel = Ext.widget("panel", {
				title: o_layoutItem.title ? ip2.i18n(o_layoutItem.title) : "",
				collapsible: o_layoutItem.collapsible ? o_layoutItem.collapsible : false,
				collapsed: o_layoutItem.collapsed ? o_layoutItem.collapsed : false,
				bodyPadding: o_layoutItem.bodyPadding ? o_layoutItem.bodyPadding : 0,
				border: o_layoutItem.border ? o_layoutItem.border : false,
				flex: o_layoutItem.flex,
				width: o_layout.width ? o_layout.width : undefined,
				height: o_layout.height ? o_layout.height : undefined,
				minWidth: o_layout.minWidth ? o_layout.minWidth : undefined,
				minHeight: o_layout.minHeight ? o_layout.minHeight : undefined,
				maxWidth: o_layout.maxWidth ? o_layout.maxWidth : undefined,
				maxHeight: o_layout.maxHeight ? o_layout.maxHeight : undefined,
				defaults: o_layoutItem.defaults || {},
				layout: {
					type: b_layout ? "fit" : "anchor"
				},
				items: a_children,
				dataParent: dataParent,
				autoScroll: o_layoutItem.autoScroll ? o_layoutItem.autoScroll : false
			});
			a_panels.push(i_panel);
		});

		var i_panel = Ext.widget("panel", {
			title: o_layout.title ? ip2.i18n(o_layout.title) : "",
			collapsible: o_layout.collapsible ? o_layout.collapsible : false,
			collapsed: o_layout.collapsed ? o_layout.collapsed : false,
			bodyPadding: o_layout.bodyPadding ? o_layout.bodyPadding : 0,
			border: o_layout.border ? o_layout.border : false,
			flex: o_layout.flex,
			width: o_layout.width ? o_layout.width : undefined,
			height: o_layout.height ? o_layout.height : undefined,
			minWidth: o_layout.minWidth ? o_layout.minWidth : undefined,
			minHeight: o_layout.minHeight ? o_layout.minHeight : undefined,
			maxWidth: o_layout.maxWidth ? o_layout.maxWidth : undefined,
			maxHeight: o_layout.maxHeight ? o_layout.maxHeight : undefined,
			defaults: o_layout.defaults || {},
			split: o_layout.hasOwnProperty("split") ? o_layout.split : undefined,
			layout: {
				type: o_layout.type
			},
			items: a_panels,
			dataParent: dataParent,
			autoScroll: o_layout.autoScroll ? o_layout.autoScroll : false
		});

		return i_panel;
	},

	fit: function(o_layout, a_groups, a_widgets, a_relations, a_affiliations, dataParent) {

		var i_self = this;
		var a_items = o_layout.items;
		var a_children = [];

		Ext.Array.each(a_items, function(o_item, n_index) {

			if (Ext.isObject(o_item)) {

				if (o_item.type || o_item.layout) {

					var o_layout = o_item.layout || o_item;
					var i_return = i_self.create(o_layout, a_groups, a_widgets, a_relations, a_affiliations, dataParent);
					a_children.push(i_return);
				}
			}
			else {
				var b_found = false;
				Ext.Array.each(a_groups, function (o_group) {
					if (o_item == o_group.name) {

						Ext.applyIf(o_group, {
							sequence: n_index,
							flex: 1
						});

						a_children.push(o_group);
						b_found = true;
					}
				});
				if (!b_found) {
					Ext.Array.each(a_widgets, function (o_widget) {
						if ("widget-" + o_item == o_widget.itemId) {
							o_widget.inLayout = true;

							Ext.applyIf(o_widget, {
								sequence: n_index,
								flex: 1
							});

							a_children.push(o_widget);
						}
					});
				}
				if (!b_found) {
					Ext.Array.each(a_relations, function (i_relation) {
						if (o_item == i_relation.itemId) {
							i_relation.inLayout = true;

							Ext.applyIf(i_relation, {
								sequence: n_index,
								flex: 1
							});

							a_children.push(i_relation);
						}
					});
				}
				if (!b_found) {
					Ext.Array.each(a_affiliations, function (i_affiliation) {
						if ("affiliation-" + o_item == i_affiliation.itemId) {
							i_affiliation.inLayout = true;

							Ext.applyIf(i_affiliation, {
								sequence: n_index,
								flex: 1
							});

							a_children.push(i_affiliation);
						}
					});
				}
			}
		});

		a_children.sort(function(a, b) {
			var n_a = (Ext.isNumber(a.sequence) ? a.sequence : 0);
			var n_b = (Ext.isNumber(b.sequence) ? b.sequence : 0);

			return n_a - n_b;
		});

		var i_panel = Ext.widget("panel", {
			title: o_layout.title ? ip2.i18n(o_layout.title) : "",
			collapsible: o_layout.collapsible ? o_layout.collapsible : false,
			collapsed: o_layout.collapsed ? o_layout.collapsed : false,
			bodyPadding: o_layout.bodyPadding ? o_layout.bodyPadding : 0,
			border: o_layout.border ? o_layout.border : false,
			flex: o_layout.flex,
			width: o_layout.width ? o_layout.width : undefined,
			height: o_layout.height ? o_layout.height : undefined,
			minWidth: o_layout.minWidth ? o_layout.minWidth : undefined,
			minHeight: o_layout.minHeight ? o_layout.minHeight : undefined,
			maxWidth: o_layout.maxWidth ? o_layout.maxWidth : undefined,
			maxHeight: o_layout.maxHeight ? o_layout.maxHeight : undefined,
			defaults: o_layout.defaults || {},
			split: o_layout.hasOwnProperty("split") ? o_layout.split : undefined,
			layout: {
				type: o_layout.type
			},
			items: a_children,
			dataParent: dataParent,
			autoScroll: o_layout.autoScroll ? o_layout.autoScroll : false
		});
		return i_panel;
	},

	anchor: function(o_layout, a_groups, a_widgets, a_relations, a_affiliations, dataParent) {

		var i_self = this;
		var a_items = o_layout.items;
		var a_children = [];

		Ext.Array.each(a_items, function(o_item, n_index) {

			if (Ext.isObject(o_item)) {

				if (o_item.type || o_item.layout) {
					var o_layout = o_item.layout || o_item;
					var i_return = i_self.create(o_layout, a_groups, a_widgets, a_relations, a_affiliations, dataParent);
					a_children.push(i_return);
				}
			}
			else {
				var b_found = false;
				Ext.Array.each(a_groups, function (o_group) {
					if (o_item == o_group.name) {

						Ext.applyIf(o_group, {
							sequence: n_index,
							flex: 1
						});

						a_children.push(o_group);
						b_found = true;
					}
				});
				if (!b_found) {
					Ext.Array.each(a_widgets, function (o_widget) {
						if ("widget-" + o_item == o_widget.itemId) {
							o_widget.inLayout = true;

							Ext.applyIf(o_widget, {
								sequence: n_index,
								flex: 1
							});

							a_children.push(o_widget);
						}
					});
				}
				if (!b_found) {
					Ext.Array.each(a_relations, function (i_relation) {
						if (o_item == i_relation.itemId) {
							i_relation.inLayout = true;

							Ext.applyIf(i_relation, {
								sequence: n_index,
								flex: 1
							});

							a_children.push(i_relation);
						}
					});
				}
				if (!b_found) {
					Ext.Array.each(a_affiliations, function (i_affiliation) {
						if ("affiliation-" + o_item == i_affiliation.itemId) {
							i_affiliation.inLayout = true;

							Ext.applyIf(i_affiliation, {
								sequence: n_index,
								flex: 1
							});

							a_children.push(i_affiliation);
						}
					});
				}
			}
		});

		a_children.sort(function(a, b) {
			var n_a = (Ext.isNumber(a.sequence) ? a.sequence : 0);
			var n_b = (Ext.isNumber(b.sequence) ? b.sequence : 0);

			return n_a - n_b;
		});

		var i_panel = Ext.widget("panel", {
			title: o_layout.title ? ip2.i18n(o_layout.title) : "",
			collapsible: o_layout.collapsible ? o_layout.collapsible : false,
			collapsed: o_layout.collapsed ? o_layout.collapsed : false,
			bodyPadding: o_layout.bodyPadding ? o_layout.bodyPadding : 0,
			border: o_layout.border ? o_layout.border : false,
			flex: o_layout.flex,
			width: o_layout.width ? o_layout.width : undefined,
			height: o_layout.height ? o_layout.height : undefined,
			minWidth: o_layout.minWidth ? o_layout.minWidth : undefined,
			minHeight: o_layout.minHeight ? o_layout.minHeight : undefined,
			maxWidth: o_layout.maxWidth ? o_layout.maxWidth : undefined,
			maxHeight: o_layout.maxHeight ? o_layout.maxHeight : undefined,
			split: o_layout.hasOwnProperty("split") ? o_layout.split : undefined,
			layout: {
				type: o_layout.type
			},
			dataParent: dataParent,
			defaults: o_layout.defaults || {
				"anchor": "100%"
			},
			items: a_children,
			autoScroll: o_layout.autoScroll ? o_layout.autoScroll : true
		});

		return i_panel;
	},

	tabpanel: function(o_layout, a_groups, a_widgets, a_relations, a_affiliations, dataParent) {

		var i_self = this;
		var a_items = o_layout.items;
		var a_children = [];

		Ext.Array.each(a_items, function(o_item, n_index) {

			if (Ext.isObject(o_item)) {

				if (o_item.type || o_item.layout) {
					var o_layout = o_item.layout || o_item;
					var i_return = i_self.create(o_layout, a_groups, a_widgets, a_relations, a_affiliations, dataParent);
					a_children.push(i_return);
				}
			}
			else {
				var b_found = false;
				Ext.Array.each(a_groups, function (o_group) {
					if (o_item == o_group.name) {

						Ext.applyIf(o_group, {
							sequence: n_index,
							flex: 1
						});

						a_children.push(o_group);
						b_found = true;
					}
				});
				if (!b_found) {
					Ext.Array.each(a_widgets, function (o_widget) {
						if ("widget-" + o_item == o_widget.itemId) {
							o_widget.inLayout = true;

							Ext.applyIf(o_widget, {
								sequence: n_index,
								flex: 1
							});

							a_children.push(o_widget);
						}
					});
				}
				if (!b_found) {
					Ext.Array.each(a_relations, function (i_relation) {
						if (o_item == i_relation.itemId) {
							i_relation.inLayout = true;

							Ext.applyIf(i_relation, {
								sequence: n_index,
								flex: 1
							});

							a_children.push(i_relation);
						}
					});
				}
				if (!b_found) {
					Ext.Array.each(a_affiliations, function (i_affiliation) {
						if ("affiliation-" + o_item == i_affiliation.itemId) {
							i_affiliation.inLayout = true;

							Ext.applyIf(i_affiliation, {
								sequence: n_index,
								flex: 1
							});

							a_children.push(i_affiliation);
						}
					});
				}
			}
		});

		a_children.sort(function(a, b) {
			var n_a = (Ext.isNumber(a.sequence) ? a.sequence : 0);
			var n_b = (Ext.isNumber(b.sequence) ? b.sequence : 0);

			return n_a - n_b;
		});

		var i_panel = Ext.widget("tabpanel", {
			title: o_layout.title ? ip2.i18n(o_layout.title) : "",
			collapsible: o_layout.collapsible ? o_layout.collapsible : false,
			collapsed: o_layout.collapsed ? o_layout.collapsed : false,
			bodyPadding: o_layout.bodyPadding ? o_layout.bodyPadding : 0,
			border: o_layout.border ? o_layout.border : false,
			flex: o_layout.flex,
			width: o_layout.width ? o_layout.width : undefined,
			height: o_layout.height ? o_layout.height : undefined,
			minWidth: o_layout.minWidth ? o_layout.minWidth : undefined,
			minHeight: o_layout.minHeight ? o_layout.minHeight : undefined,
			maxWidth: o_layout.maxWidth ? o_layout.maxWidth : undefined,
			maxHeight: o_layout.maxHeight ? o_layout.maxHeight : undefined,
			split: o_layout.hasOwnProperty("split") ? o_layout.split : undefined,
			layout: "fit",
			items: a_children
		});
		return i_panel;
	},

	portal: function(o_layout, a_fields, a_groups, a_widgets, a_relations, dataParent) {

		var i_self = this;
		var a_columnsitems = o_layout.items;
		var a_columns = [];

		Ext.Array.each(a_columnsitems, function(o_column) {

			var a_items = o_column.items;
			var a_children = [];
			var a_portlets = [];

			Ext.Array.each(a_items, function(o_item) {

				if (Ext.isObject(o_item)) {

					if (o_item.type || o_item.layout) {

						var o_layout = o_item.layout || o_item;
						var i_return = i_self.create(o_layout, a_fields, a_groups, a_widgets, a_relations, dataParent);

						var i_portlet = Ext.widget("portlet", {
							title: o_item.title ? ip2.i18n(o_item.title) : "",
							collapsible: o_item.collapsible ? o_item.collapsible : true,
							collapsed: o_item.collapsed ? o_item.collapsed : false,
							closable: o_item.closable ? o_item.closable : true,
							resizable: false,
							bodyPadding: o_item.bodyPadding ? o_item.bodyPadding : 0,
							border: o_item.border ? o_item.border : false,
							dataParent: dataParent,
							items: [
								i_return
							]
						});
						a_portlets.push(i_portlet);
					}
				}
			});

			var i_panel = Ext.widget("portalcolumn", {
				flex: o_column.flex,
				width: o_column.width,
				height: o_column.height,
				items: a_portlets,
				dataParent: dataParent,
				autoScroll: o_column.autoScroll ? o_column.autoScroll : true
			});

			a_columns.push(i_panel);
		});

		var i_panel = Ext.widget("portalpanel", {
			title: o_layout.title ? ip2.i18n(o_layout.title) : "",
			collapsible: o_layout.collapsible ? o_layout.collapsible : false,
			collapsed: o_layout.collapsed ? o_layout.collapsed : false,
			bodyPadding: o_layout.bodyPadding ? o_layout.bodyPadding : 0,
			border: o_layout.border ? o_layout.border : false,
			flex: o_layout.flex,
			width: o_layout.width,
			height: o_layout.height,
			layout: o_layout.type,
			items: a_columns,
			dataParent: dataParent,
			autoScroll: o_layout.autoScroll ? o_layout.autoScroll : false
		});

		return i_panel;
	}
});
