/* ===================================================================================================
 * labelControl
 * ===================================================================================================
 */
 
/// jsonParameters
/// { fullControlName: <string>,
///   text: <string>,
///   visible: "0" | "1"
///   description: <columnDescriptor>
/// }
function labelControl_Initialize(jsonParameters)
{
	//alert(typeof jsonParameters);
	var P = parseParameters(jsonParameters);
	var c = getControl(P.fullControlName);
	c.description = P.description;
	c.style.visibility = P.visible && parseIntEx(P.visible) ? "inherit" : "hidden";
	labelControl_setup(c, P.text);
}

function labelControl_Update(jsonParameters)
{
	var P = parseParameters(jsonParameters);
	var c = getControl(P.fullControlName);
	c.style.visibility = P.visible && parseIntEx(P.visible) ? "inherit" : "hidden";
	labelControl_setText(c, P.text);
}

/// cell: html-div-Element
/// descr: <columnDescriptor>
function labelControl_setup(cell, txt)
{
	var descr = cell.description;
	switch(descr.inputType)
	{
		case "Label_Right":
		case "Amount_SH":
			cell.style.justifyContent = "end";
			break;
		default:
			cell.style.justifyContent = "start";
			break;
	}

	var baseClass = cell.getAttribute("class");
	if (baseClass.indexOf("grid") == 0)
	{
		cell.enabled = false;
		cell.setAttribute("contenteditable", "true");
		cell.setAttribute("class", baseClass + " static");
		addControlEvent(cell, "focus", function(event) { labelControl_onFocus(cell, event); });
		addControlEvent(cell, "blur", function(event) { labelControl_onBlur(cell, event); });
		addControlEvent(cell, "paste", function(event) { labelControl_onPaste(cell, event); });
		addControlEvent(cell, "beforeinput", function(event) { labelControl_onBeforeInput(cell, event); });
		addControlEvent(cell, "contextmenu", function(event) { control_onContextMenu(cell, event); });
	}

	labelControl_setText(cell, txt);
};

function labelControl_setText(cell, txt)
{
	if (cell.description.inputType == "Amount_SH")
	{
		var d = parseFloatEx(txt);
		if (d < 0)
			txt = (-d).formatDE(2, true, true) + "S";
		else if (d > 0)
			txt = d.formatDE(2, true, true) + "H";
		else
			txt = "";
	}
	cell.innerText = txt;
	cell.title = txt;
};

function labelControl_onFocus(cell, event)
{
	if (getBaseClassName(cell).startsWith("grid"))
		gridControl_cellNotify(cell, "onSetFocus");
};

function labelControl_onBlur(cell, event)
{
	if (getBaseClassName(cell).startsWith("grid"))
		gridControl_cellNotify(cell, "onKillFocus");
};

function labelControl_onBeforeInput(cell, event)
{
	event.preventDefault();
	switch(event.inputType)
	{
		case "insertParagraph":
			editControl_selectNext(cell);
			break;

		case "insertLineBreak":
			editControl_selectPrev(cell);
			break;
	}
};

function labelControl_onPaste(cell, event)
{
	event.preventDefault();
};
