﻿var LayoutCells = new Class(
{
	initialize: function()
	{
		this.cells = [];  // Is a 2 dimensional array - an item contains an object of an array of cells and the parent element
		this.cellSizes = {};
		this.smartPadding = 8;
		this.typeID = "lc";

		ui.registerResizeHandler(this.resizeCells);
		ui.registerCleanupHandler(this.removeCells);

		uiDialog.registerPreInitHandler(this.typeID, this.preInit);
	},

	getCellElement: function(cell)
	{
		return ui.getNestedControl(cell.DialogID, cell.ControlID);
	},

	preInit: function(control, clientData)
	{
		var lcControl = control;
		var lcParent = null;

		var data = clientData[layoutCells.typeID];

		try
		{
			lcParent = lcControl.getParent();
		}
		catch (e)
		{
			return;
		}

		if (lcControl == null || lcParent == null)
		{
			return;
		}

		var cell =
		{
			"control": lcControl,
			"size": { "x": 0, "y": 0 },
			"subCells": []
		};

		$extend(cell, data);

		lcControl.store("cell", cell);

		var lcParentLC = lcControl.getParent(".layoutCell");

		var cells = [];

		if (lcParentLC != null && lcParentLC.retrieve("cell") != null)
		{
			var parentLCCell = lcParentLC.retrieve("cell");
			cells = parentLCCell.subCells;
		}
		else
		{
			var parentCells = lcParent.retrieve("cells");

			if (parentCells != null)
			{
				cells = parentCells;
			}
			else
			{
				lcParent.store("cells", cells);

				layoutCells.cells.push(
					{
						"cells": cells,
						"parent": lcParent
					}
				);
			}
		}

		cells.push(cell);
	},

	resizeCells: function()
	{
		for (var i = 0; i < layoutCells.cells.length; i++)
		{
			var parentCells = layoutCells.cells[i];

			var parentSize = parentCells.parent.getSize();

			if (parentSize.y == 0)
			{
				var wndHeight = window.getSize().y;
				if (parentSize.y < wndHeight)
				{
					parentSize.y = wndHeight;
				}
			}

			layoutCells.calculateCellSizes(parentCells.cells, parentSize);
			layoutCells.refreshCells(parentCells.cells);
		}
	},

	calculateCellSize: function(cell, parentSize, usedWidth, usedHeight)
	{
		var cellWidth = 0;
		var cellHeight = 0;

		switch (cell.kind)
		{
			// Column          
			case 0:
				switch (cell.unit)
				{
					// Pixel          
					case 0:
						cellWidth = cell.width;
						break;

					// Percent          
					case 1:
						cellWidth = parentSize.x * (cell.width / 100);
						break;

					// Fill          
					case 2:
						cellWidth = (parentSize.x - usedWidth) * cell.width;
						break;
				}

				cellHeight = parentSize.y;

				break;

			// Row          
			case 1:
				switch (cell.unit)
				{
					// Pixel          
					case 0:
						cellHeight = cell.height;
						break;

					// Percent          
					case 1:
						cellHeight = parentSize.y * (cell.height / 100);
						break;

					// Fill          
					case 2:
						cellHeight = (parentSize.y - usedHeight) * cell.height;
						break;
				}

				cellWidth = parentSize.x;

				break;

			// Cell          
			case 2:
				cellWidth = parentSize.x;
				cellHeight = parentSize.y;

				break;
		}

		if (cellWidth < 0)
		{
			cellWidth = 0;
		}

		if (cellHeight < 0)
		{
			cellHeight = 0;
		}

		return { x: cellWidth, y: cellHeight };
	},

	determineCellSizeByContent: function(cell)
	{
		var width = 0;
		var height = 0;

		var lcControl = cell.control;

		if (lcControl != null)
		{
			var sizers = lcControl.getElements(".lcSizer");

			for (var i = 0; i < sizers.length; i++)
			{
				var sizerHeight = sizers[i].getSize().y;

				if (sizerHeight > height)
				{
					height = sizerHeight;
				}
			}
		}

		return { "x": width, "y": height };
	},

	calculateCellSizes: function(cells, parentSize)
	{
		var usedWidth = 0;
		var usedHeight = 0;

		switch (cells[0].kind)
		{
			case 0:
				usedWidth = (cells.length - 1) * layoutCells.smartPadding;
				break;
			case 1:
				usedHeight = (cells.length - 1) * layoutCells.smartPadding;
				break;
		}

		// Get fixed cell sizes
		for (var i = 0; i < cells.length; i++)
		{
			var cell = cells[i];

			if (cell.unit == 2) continue;

			var cellSize = layoutCells.calculateCellSize(cell, parentSize, usedWidth, usedHeight);
			var cellSizeByDialogs = layoutCells.determineCellSizeByContent(cell);

			if (cellSize.x < cellSizeByDialogs.x)
			{
				cellSize.x = cellSizeByDialogs.x;
			}

			if (cellSize.y < cellSizeByDialogs.y)
			{
				cellSize.y = cellSizeByDialogs.y;
			}

			switch (cell.kind)
			{
				case 0:
					usedWidth += cellSize.x;
					break;
				case 1:
					usedHeight += cellSize.y;
					break;
			}

			cell.size = cellSize;
		}

		// Get dynamic cell sizes
		for (var d = 0; d < cells.length; d++)
		{
			var dynamicCell = cells[d];

			if (dynamicCell.unit != 2) continue;

			var dynamicCellSizeByDialogs = layoutCells.determineCellSizeByContent(dynamicCell);
			var dynamicCellSize = layoutCells.calculateCellSize(dynamicCell, parentSize, usedWidth, usedHeight);

			if (dynamicCellSize.x < dynamicCellSizeByDialogs.x)
			{
				dynamicCellSize.x = dynamicCellSizeByDialogs.x;
			}

			if (dynamicCellSize.y < dynamicCellSizeByDialogs.y)
			{
				dynamicCellSize.y = dynamicCellSizeByDialogs.y;
			}

			dynamicCell.size = dynamicCellSize;
		}

		for (var e = 0; e < cells.length; e++)
		{
			if (cells[e].subCells.length > 0)
			{
				layoutCells.calculateCellSizes(cells[e].subCells, cells[e].size);

				var cellHeight = cells[e].size.y;
				var childHeight = 0;

				for (var c = 0; c < cells[e].subCells.length; c++)
				{
					if (cells[e].subCells[c].kind != 1)
					{
						if (childHeight < cells[e].subCells[c].size.y)
						{
							childHeight = cells[e].subCells[c].size.y;
						}
					}
					else
					{
						childHeight += cells[e].subCells[c].size.y;

						if (c < cells[e].subCells.length - 1)
						{
							childHeight += layoutCells.smartPadding;
						}
					}
				}

				if (cellHeight < childHeight)
				{
					cellHeight = childHeight;
				}

				cells[e].size.y = cellHeight;
			}
		}
	},

	removeCells: function(rootElement)
	{
		var filteredCellContainers = [];

		for (var i = 0; i < layoutCells.cells.length; i++)
		{
			var cellContainer = layoutCells.cells[i];

			if (!rootElement.hasChild(cellContainer.parent) && (cellContainer.parent != rootElement))
			{
				filteredCellContainers.push(cellContainer);
			}
		}

		var rootCell = rootElement.retrieve("cell");

		if (rootCell != null)
		{
			rootCell.subCells = [];
		}

		layoutCells.cells = filteredCellContainers;
	},

	refreshCells: function(cells)
	{
		var left = 0;
		var top = 0;

		// Resize and position cells
		for (var c = 0; c < cells.length; c++)
		{
			var cell = cells[c];
			var cellPadding = layoutCells.smartPadding;

			if (c == cell.length - 1)
			{
				cellPadding = 0;
			}

			var cellDiv = cell.control; //layoutCells.getCellElement(cell);

			if (left < 0)
			{
				left = 0;
			}

			cellDiv.setStyle("left", left);
			cellDiv.setStyle("top", top);
			cellDiv.setStyle("width", cell.size.x);
			cellDiv.setStyle("height", cell.size.y);

			switch (cell.kind)
			{
				case 0:
					left += cell.size.x + cellPadding;
					break;
				case 1:
					top += cell.size.y + cellPadding;
					break;
			}

			if (cell.subCells.length > 0)
			{
				layoutCells.refreshCells(cell.subCells);
			}
		}
	}
});

var layoutCells = new LayoutCells();
