var tb = new Array();
tb[' '] =  1.0124722222;tb['a'] = 1.72930255556;tb['b'] = 2.19503977778;tb['c'] = 1.73335244444;tb['d'] = 2.19503977778;tb['e'] = 1.72930255556;tb['f'] = 1.30811411111;tb['g'] = 2.03304422222;tb['h'] = 2.19503977778;tb['i'] = 1.09751988889;tb['j'] = 1.09751988889;tb['k'] = 2.19503977778;tb['l'] = 1.09751988889;tb['m'] = 3.03741666667;tb['n'] = 2.19503977778;tb['o'] = 1.93989677778;tb['p'] = 2.19503977778;tb['q'] = 2.19503977778;tb['r'] = 1.51870833333;tb['s'] = 1.51870833333;tb['t'] = 1.30811411111;tb['u'] = 2.19503977778;tb['v'] = 2.19503977778;tb['w'] = 3.03741666667;tb['x'] = 2.19503977778;tb['y'] = 2.19503977778;tb['z'] = 1.72930255556;tb['A'] = 2.83087233333;tb['B'] = 2.19503977778;tb['C'] = 2.21528922222;tb['D'] = 2.61622822222;tb['E'] = 2.19503977778;tb['F'] = 1.93989677778;tb['G'] = 2.61622822222;tb['H'] = 2.82682244444;tb['I'] = 1.30811411111;tb['J'] = 1.72930255556;tb['K'] = 2.405634;tb['L'] = 2.19503977778;tb['M'] = 3.50315388889;tb['N'] = 2.82682244444;tb['O'] = 2.61622822222;tb['P'] = 2.19503977778;tb['Q'] = 2.62837788889;tb['R'] = 2.405634;tb['S'] = 1.93989677778;tb['T'] = 2.405634;tb['U'] = 2.61622822222;tb['V'] = 2.61622822222;tb['W'] = 3.92434233333;tb['X'] = 2.405634;tb['Y'] = 2.405634;tb['Z'] = 1.93989677778;tb['1'] = 1.93989677778;tb['2'] = 1.93989677778;tb['3'] = 1.93989677778;tb['4'] = 1.93989677778;tb['5'] = 1.93989677778;tb['6'] = 1.93989677778;tb['7'] = 1.93989677778;tb['8'] = 1.93989677778;tb['9'] = 1.93989677778;tb['0'] = 1.93989677778;tb[','] = 1.11371944444;tb['.'] = 1.09751988889;tb[';'] = 1.10156977778;tb[':'] = 1.09751988889;tb['!'] = 1.10966955556;tb['?'] = 1.74145222222;tb['"'] = 1.65235466667;tb["'"] = 0.72898;

$(function()
{
	if (document.getElementById('inhoud_toelichting') != null)
	{
		setInterval('doUpdateKroniekStats()', 500);
		document.getElementById('inhoud_toelichting').innerHTML =
			'Huidig aantal regels: <span id="aantalregels">0</span><br />' +
			'<i>(Er is ruimte voor ongeveer 98 regels, afhankelijk van je foto)</i>';
	}
});

function textWidth(text)
{
	var l = 0;
	letters = text.split('');
	for (i=0; i<letters.length; ++i)
	{
		l += ((tb[letters[i]] == undefined)?2:tb[letters[i]]);
	}
	return l;
}

function countRows(text)
{
	lines = 1;
	linewidth = 0;

	// Breaks isoleren voor de telling
	text = text.replace(/\r/g, "", 'g');
	text = text.replace(/\n/g, " \n ", 'g');
	words = text.split(' ');
	for (var w=0; w<words.length; ++w)
	{
		if (words[w] == '')
		{
			// Lege woorden (dubbele spaties) worden overgeslagen
			continue;
		}

		if (words[w] == "\n")
		{
			// Een enter gaat direct naar een nieuwe zin
			lines++;
			linewidth = 0;
			continue;
		}

		// Maximale regelbreedte bepalen. Regel 20 tot ca 34 vallen naast het plaatje en zijn smaller.
		maxlinewidth = (lines > 19) && (lines < 35)?32.9:87.9;

		// Bepaal de breedte van het woord
		l = textWidth(words[w]);
		if (l > maxlinewidth)
		{
			// Woord is langer dan de regel. Laat het de resterende ruimte invullen
			// Dit kan eventueel nog iets nauwkeuriger.
			lines++;
			l = l - (maxlinewidth - linewidth);
			linewidth = l + 2; // + 2mm = 1 teken 'loss'.
		}
		else if (l + tb[' '] + linewidth > maxlinewidth)
		{
			// Woord is kleiner dan een regel, maar past niet meer achter de huidige regel.
			// Woord geheel naar de volgende regel.
			lines++;
			linewidth = l;
		}
		else
		{
			// Woord past erachter
			linewidth += tb[' '] + l;
		}

	}

	return lines + ' regel' + (lines!=1?'s':''); //, ' + parseInt(linewidth) + 'mm';
}

function doUpdateKroniekStats()
{
	document.getElementById('aantalregels').innerHTML = countRows(document.getElementById('inhoud').value);
}

var kroniektimer = 0; kroniekinterval = 0;

function updateKroniekStats()
{
	clearTimeout(kroniektimer);
	kroniektimer = setTimeout('doUpdateKroniekStats()', 1000);
}

//alert(tb['a']);
//alert(tb['^']);
//alert(tb['b']);

