/*
 * Copyright (C) 1995-2011 by 4system Polska Sp. z o.o.
 * All Rights Reserved. v4
 */
 
__componentText = function(name)
{
	//inherited	
	this.componentElement = componentElement;
	this.componentElement(name);

	this.allowGF = true;
	this.font = {};
	this.font.family = 'Arial';
	this.font.size = 2;
	this.font.color = "#000000";
	this.font.bold = false;
	this.font.italic = false;
	this.font.underline = false;
	this.wrap = true;
	this.padding = 3;
	this.rtl = false;
	this.scrollOffset = 0;
	this.lineHeight = "100%";
	this.orgText = null;
};

__componentText.prototype = new componentElement;
__componentText.prototype.type = "__componentText";

__componentText.prototype.onCreate = function(obj)
{
	this.contentObj = this.findContentObj();

	if (this.allowGF && project.globalFormating && isDefaultFont(this.font) && !isDefaultFont(project.font)) {
		this.font.family = project.font.family;
		this.font.size = project.font.size;
		this.font.color = project.font.color;
	}
	
	if (is.ie9) {
		this.contentObj.style.letterSpacing = "-0.2px";
		this.contentObj.style.wordSpacing = "-0.2px";
	}
};

__componentText.prototype.onPaint = function(rPngFix)
{
	if (typeof(rPngFix) != "boolean") rPngFix = false;

	this.reinitText();
	this.text = this.reformatText();

	if (this.execution) {
		if (rPngFix) this._setHTMLDef(this.text, true);
	} else {
		this._setHTMLDef(this.text, false);
	}
	
	if (this.execution) {
		this.reinit();
	}
};

__componentText.prototype.reinitText = function()
{
	if (this.orgText == null) {
		this.orgText = this.text;
	} else {
		this.text = this.orgText;
	}
}

__componentText.prototype.reformatText = function()
{
	var text = this.initText(this.text);	
	
	this.contentObj = this.findContentObj();
	if (this.contentObj) {
		this.contentObj.innerHTML = text;
	}		
	return text;
};

__componentText.prototype.onRepaint = function(obj)
{
	this.onPaint(obj, true);
};

__componentText.prototype._setHTMLDef = function(text, rPngFix)
{
	text = this._getHTMLDef(text);
	this.setHTML(text, rPngFix);
};

__componentText.prototype.setFontStyle = function(text)
{
	if (this.font.bold  == true) text = '<b>' + text + '</b>';
	if (this.font.italic == true) text = '<i>' + text + '</i>';
	if (this.font.underline == true) text = '<u>' + text + '</u>';		
	
	return text;
}

__componentText.prototype.replaceTextMarkers = function(text)
{
	var arr = PlayerInterface.GetValue("getTextMarkers");

	if (arr) {
		for (var key in arr) {
			var value = arr[key];
			while (text.indexOf("{=%" + key + "}") > -1) {
				res = true;
				text = text.replace("{=%" + key + "}", value);
			}
		}
	}
	return text;	
}
__componentText.prototype.replaceMarkers = function(text)
{
	if (this.p_checkMarkers) {
		for (var key in me) {
			var value = me[key];
			if (value != "") {
				while (text.indexOf("{=" + key + "}") > -1) {
					text = text.replace("{=" + key + "}", value);
				}
			}
		}
	}
	return text;	
}

__componentText.prototype.initDynamicContent = function(text)
{
	if (config.allowDynamicContent) {
		var properties = PlayerInterface.GetValue("dynamicContent");
		if (properties) {
			for (var key in properties) {
				var value = properties[key];
				if (value != "") {
					while (text.indexOf("*{" + key + "}*") > -1) {
						text = text.replace("*{" + key + "}*", value);
					}
				}
			}
		}
	}
	return text;
}

__componentText.prototype.initText = function(text)
{
	if (PageInterface.GetValue("state") == psDesigning) return text;

	text = text || "";
	
	if (text != "") {
		text = this.replaceMarkers(text);
		text = this.replaceTextMarkers(text);
		text = this.initDynamicContent(text);
		text = this.setFontStyle(text);

		while (text.indexOf('"../resources/') > -1) {
			text = text.replace('"../resources/', '"wbtres://');
		}
		
		while (text.indexOf("'../resources/") > -1) {
			text = text.replace("'../resources/", "'wbtres://");
		}
		
		while (text.indexOf('wbtres://') > -1) {
			text = text.replace('wbtres://', resourcesURL);
		}
	};
	
	return text;
};

__componentText.prototype._getHTMLDef = function(text)
{
	text = this.initText(text);
	
	this.txtw = this.w - ((is.ns4) ? 0 : (this.borderWidth * 2))-this.scrollOffset;
	this.txth = this.h - ((is.ns4) ? 0 : (this.borderWidth * 2));
	
	var styled = '<font id="' + this.id + '_contentObj" style="font-family:' + this.font.family
		+ ';font-size:' + getFontSize(this.font.size, "px") + 'px;color:' + this.font.color + ';'
		+ 'line-height:' + this.lineHeight + ';"'
		+ ((this.allowGF && (project.rtl || this.rtl)) ? ' dir="rtl" align="right" ' : '') + '>'
		+ text + '</font>';
	
	if (this.font.bold == true) styled = '<b>' + styled + '</b>';
	if (this.font.italic == true) styled = '<i>' + styled + '</i>';
	if (this.font.underline == true) styled = '<u>' + styled + '</u>';
	var t = "";
	t += '<table id="' + this.id + 'Table"';
	t += ' dir="' + this.dir + '"';
	t += ' width="' + this.txtw + '"';
	t += ' height="' + this.txth + '"';
	t += ' border="0"';
	t += ' cellpadding="' + this.padding + '" cellspacing="0"';
	t += '>';
	t += '<tr>';
	t += '<td ';
	t += ' valign="' + this.valign + '"';

	t += ' style="text-align:'
	if (this.allowGF && (project.rtl || this.rtl))  {
		t += 'right';
	} else {
		t += this.align;
	}
	t += '"';
	
	t += ' ' + (this.wrap ? '' : 'nowrap');
	t += '>' + styled + '</td></tr></table>'; 
	if (this.allowGF && (project.rtl || this.rtl))  t = t.replace(/<p /gim, '<p dir="rtl" align="right" ');
	return t;
};
