//
// Module:	string_width.js
//
// Version:	1.0
//
// Autor:	(c) B. Tyumnev, 2002
//
// History:
//
//	1.0,	25.07.2002
//		Create module.
//
//
//	This module provide some functions for determine string's width in pixels.
//	Before use SW_GetStringPixelWidth() you must run SW_Init().
//
//	This module is working only in IE.
//
//

var SW_nAccuracy = 250;
var SW_KoefChange = 1.0;

var pLetterWidth = new Array; // Catche system

function SW_Init(strStyle)
// Initial function
// 	strStyle - font style name
{
	if(typeof(SW_TextField) == "object") return;
	
	document.writeln("<TABLE border='0' cellpadding='0' cellspacing='0' style='position: absolute; top: -1000px; left: 0px; width: "+SW_nAccuracy+"px'>");
	document.writeln("<TR><TD id='SW_TextField' class='"+strStyle+"'></TD></TR>");
	document.writeln("<TR><TD align='right'><DIV id='SW_SignalField' style='background-color: red; width: "+SW_nAccuracy+"px'>!</DIV></TD></TR>");
	document.writeln("</TABLE>");
}

function SW_GetLetterPixelWidth(strLetter)
// Function return letter width in pixel.
//	strLetter - letter
// If failure, return zero.
{
	if(strLetter.length != 1) return 0;
	if(pLetterWidth[strLetter] != null) return pLetterWidth[strLetter];

	var i;
	SW_TextField.innerText = "";
	for(i = 0; (i < SW_nAccuracy) && (SW_SignalField.offsetLeft <= 1); i++) {
		SW_TextField.innerText += strLetter;
	}
		
	pLetterWidth[strLetter] = SW_KoefChange * (SW_nAccuracy + SW_SignalField.offsetLeft) / i; // catche system
	
	return pLetterWidth[strLetter];
}

function SW_GetStringPixelWidth(strPhrase)
// Function return letter width in pixel.
//	strPhrase - string
{
	var fResult = 0, i;
	for(i = 0; i < strPhrase.length; i++) fResult += SW_GetLetterPixelWidth(strPhrase.charAt(i));
	return Math.ceil(fResult);
}