/* ===================================================================================================
 * textBoxControl
 * ===================================================================================================
 */
/// jsonParameters
/// { fullControlName: <string>,
///   text: <string>,
///   visible: "0" | "1"
///   enabled: "0" | "1"
///   tabStop: "0" | "1"
///   backColor: <integer>
///   textLimit: <integer>
///   textWidth: <integer>
/// }

textBoxControl = function(param)
{
	var control = typeof param == "string" ? getControl(param) : param;
	if (! control)	return null;
	if (control.textBoxControl) return control.textBoxControl;
	
	this.element = control;

	this.contentSpace = xmlCreateElement(this.element, "div", null, "textBoxContent");
	this.textLimit = 2000;
	this.textWidth = 50;
	this.changed = false;
	
	this.calcContentWidth = function()
	{
		return this.textWidth / 10;
	};
	this.contentWidth = this.calcContentWidth();

	this.justify = function()
	{
		var myCy = this.element.offsetHeight;
		var myCx = this.element.offsetWidth;
		
		// p96 pixel per inc + 12px Padding
		var requiredCx = parseIntEx(this.contentWidth * 96) + 12;  
		this.contentSpace.style.left = "0px";
		this.contentSpace.style.width = requiredCx.toString() + "px";
		//this.contentSpace.style.top = "0px";
		//this.contentSpace.style.height = myCy.toString() + "px";
		var cxDiff = requiredCx - this.contentSpace.clientWidth;
		if (cxDiff != 0)
		{
			requiredCx += cxDiff;
			this.contentSpace.style.width = requiredCx.toString() + "px";
		}
	};

	this.update = function(P)
	{
		var needResize = false;
		
		if (typeof P.visible != "undefined")
		{
			visible = parseIntEx(P.visible);
			this.element.style.visibility = visible ? "inherit" : "hidden";
		}

		if (typeof P.enabled != "undefined")
		{
			enabled = parseIntEx(P.enabled);
			this.contentSpace.setAttribute("contenteditable", enabled ? "true" : "false");
		}
		
		if (typeof P.backColor != "undefined")
		{
			var styleColor = enabled ? metrics.editControl_enabled : metrics.editControl_disabled;
			var winColor = parseIntEx(P.backColor);
			if ((winColor > 0) && (winColor != 16777215)) // rgb(255,255,255)
			{
				var r = winColor & 255;
				var g = (winColor >> 8) & 255;
				var b = (winColor >> 16) & 255;
				styleColor = "rgb(" + r.toString() + "," + g.toString() + "," + b.toString() + ")";
			}
			this.element.style.backgroundColor = styleColor;
		}

		if ((typeof P.textWidth != "undefined") && (parseIntEx(P.textWidth) >= 20))
		{
			this.textWidth = parseIntEx(P.textWidth);
			this.contentWidth = this.calcContentWidth();
			needResize = true;
		}

		if (typeof P.textLimit != "undefined")
		{
			this.textLimit = parseIntEx(P.textLimit);
		}

		if (typeof P.text != "undefined")
		{
			this.contentSpace.innerText = P.text;
			this.changed = false;
		}
		
		//alert(JSON.stringify(P));
		if (needResize) this.justify();
	};
	
	this.element.textBoxControl = this;
	this.element.onAfterResize = function(e)
	{
		textBoxControl(e).justify();
	};
	this.element.onAfterResize(this.element);

	addControlEvent(this.contentSpace, "focus", function(event) { textBoxControl_onFocus(control, event); });
	addControlEvent(this.contentSpace, "blur", function(event) { textBoxControl_onBlur(control, event); });
	addControlEvent(this.contentSpace, "paste", function(event) { textBoxControl_onPaste(control, event); });
	addControlEvent(this.contentSpace, "beforeinput", function(event) { textBoxControl_onBeforeInput(control, event); });
	addControlEvent(this.contentSpace, "input", function(event) { textBoxControl_onInput(control, event); });
	return this;
};
 
textBoxControl_Initialize = function(jsonParameters)
{
	var P = parseParameters(jsonParameters);
	new textBoxControl(P.fullControlName).update(P);
};

textBoxControl_Update = function(jsonParameters)
{
	var P = parseParameters(jsonParameters);
	textBoxControl(P.fullControlName).update(P);
};

textBoxControl_onFocus = function(cell, event)
{
	var tb = textBoxControl(cell);
	tb.changed = false;
	textBoxControl_notify(tb, "onSetFocus");
};

textBoxControl_onBlur = function(cell, event)
{
	var tb = textBoxControl(cell);
	textBoxControl_notify(tb, "onKillFocus", tb.changed, tb.contentSpace.innerText);
	tb.changed = false;
};

textBoxControl_onBeforeInput = function(cell, event)
{
	var tb = textBoxControl(cell);

	switch(event.inputType)
	{
		case "insertText":
			var totalLength = tb.contentSpace.innerText.length + event.data.length;
			if (totalLength > tb.textLimit)
			{
				event.preventDefault();
				return false;
			}
			break;

		case "deleteContentForward":
		case "deleteContentBackward":
			break;

		case "insertParagraph":
			event.preventDefault();
			textBoxControl_insertParagraph(tb);
			return false;

		case "insertLineBreak":
			return false;
			
		default:
			alert("textBoxControl_onBeforeInput " + event.inputType);
			event.preventDefault();
			return false;
	}

	//cell.changed = true;
	return true;
};

textBoxControl_insertParagraph = function(tb)
{
	var selection = window.getSelection();
	if ((selection.rangeCount == 1) && (selection.toString().length == 0))
	{
		control_replaceSelection(tb.contentSpace, "\n", tb.textLimit);
		tb.changed = true;
	}
};

textBoxControl_onInput = function(cell, event)
{
	var tb = textBoxControl(cell);
	tb.changed = true;
	return true;
};

textBoxControl_onPaste = function(cell, event)
{
	// paste plain only
	var tb = textBoxControl(cell);
	var pastedText = (event.clipboardData || window.clipboardData).getData('text');
	if (control_replaceSelection(tb.contentSpace, pastedText, tb.textLimit))
		tb.changed = true;
	event.preventDefault();
};

textBoxControl_notify = function(textBox, eventName, changed, text)
{
	var msg = 
	{ msg:     eventName, 
	  control: textBox.element.id, 
	};
	if ((typeof changed != "undefined") && changed)
		msg.changed = "1";
	if (typeof text != "undefined")
		msg.text = text;
	console.log(JSON.stringify(msg));
	raiseControlEvent(msg);
};
