/* ===================================================================================================
 * pushButtonControl
 * ===================================================================================================
 */
// Dieses Control implementiert die Auswahlleiste der aktuell anwaehlbaren SubForms

/// jsonParameters
/// { fullControlName: <string>,  optional
///   pageName: <string>
/// }
tabControl_selectPage = function(jsonParameters)
{
	var P = parseParameters(jsonParameters);
	var elem = (typeof P.fullControlName != 'undefined') ? getControl(P.fullControlName) : spaceAreas.space4tabs;
	if (! elem) return;
	
	var childs = elem.children;
	var countChilds = childs.length;
	if (countChilds <= 0) return;

	for (var i = 0; i < countChilds; i++)
	{
		var control = childs[i];
		if (control.id.split('$')[1] == P.pageName)
			control.setAttribute("selected", "true");
		else
			control.removeAttribute("selected");
	}
};

tabControl_update = function(elem)
{
	elem.innerHTML = "";
	metrics.subFormNames.split(';').forEach((tabName) =>
	{
		var nameParts = tabName.split('=');
		var tabText = nameParts.length > 1 ? nameParts[1] : nameParts[0];

		var tabDiv = xmlCreateElement(elem, "div");
		tabDiv.id = "tab$" + nameParts[0];
		tabDiv.setAttribute("title", tabText);
		tabDiv.setAttribute("onclick", "tabControl_onTabClick(this);");

		var tabSpan = xmlCreateElement(tabDiv, "span");
		tabSpan.innerText = tabText;
	});
};

tabControl_onTabClick = function(cell)
{
	raiseControlEvent({ msg: "onTabClick", control: cell.id, pageName: cell.id.split('$')[1] });
};

tabControl_onResize = function(elem)
{
	var childs = elem.children;
	var countChilds = childs.length;
	if (countChilds <= 0) return;

	var cxParent = elem.offsetWidth - 8;
	var cyParent = elem.offsetHeight;
	var unitWidth = metrics.unitWidth;
	var x = 0;
	var y = unitWidth;
	var cy = cyParent - y;
	var cx = metrics.tabItemWidth * unitWidth;
	if (cx > (cxParent / countChilds))
		cx = cxParent / countChilds;
	
	for (var i = 0; i < countChilds; i++)
	{
		var control = childs[i];
		control.style.position = "absolute";
		control.style.left = x.toString() + "px";
		control.style.top = y.toString() + "px";
		control.style.width = cx.toString() + "px";
		control.style.minWidth = cx.toString() + "px";
		control.style.maxWidth = cx.toString() + "px";
		control.style.height = cy.toString() + "px";
		control.style.minHeight = cy.toString() + "px";
		control.style.maxHeight = cy.toString() + "px";
		x += cx;
	}
};
