// Diese Funktion durchläuft das gesamte DOM einer HTML-Seite und wendet auf alle Knoten eine Funktion an.
// Aufruf z.B.:
// walkTheDOM( document.body, function(node) { ... } );
function walkTheDOM(node, func) {
	func(node); 
	node = node.firstChild; 
	while (node) { 
		walkTheDOM(node, func); 
		node = node.nextSibling; 
	} 
}

// Diese Funktion rundet eine Zahl num auf dig Stellen hinter dem Komma.
// Wird dig nicht angegeben (oder ist gleich 0), so verhält sich diese Funktion
// wie Math.round(). Kann eines der Argumente nicht als Zahl identifiziert
// werden, so ist das Ergebnis gleich NaN.
function roundNum(num, dig) {
	if (arguments.length == 1) dig = 0;
	var mult = Math.pow(10, dig);
	return Math.round(parseFloat(num) * mult) / mult;
}

// Verwendung von lrnFontSizer wie folgt:
// In das BODY-Tag das Attribut onload="lrnFontSizer.init()" einfügen.
// In das HTML dann an irgendeiner Stelle folgendes einfügen:
// <p>
//   <a href="JavaScript: lrnFontSizer.adjust(-10)">A-</a>&nbsp;&nbsp;
//   <a href="JavaScript: lrnFontSizer.reset()">A</a>&nbsp;&nbsp;
//   <a href="JavaScript: lrnFontSizer.adjust(+10)">A+</a>
// </p>
// Die Funktion reset() setzt die Schriftgrößen auf die ursprünglichen Werte zurück.
// Die Funktion adjust erhält einen positiven oder negativen Wert, der als Prozentwert
// interpretiert wird. Z.B. wird durch lrnFontSizer(-10) die Schrift um 10% verkleinert,
// durch lrnFontSizer(10) um 10% vergrößert.
lrnFontSizer = function() {
	var aspect = 1;
	var fontPropArr = [];

	function doResize() {
		var node, fs, fu, lh, lu, bt;
		for (i = 0; i < fontPropArr.length; i += 5) {
			node = fontPropArr[i]; bt = (node.tagName == 'BODY');
			if (fontPropArr[i + 1] == '') fs = ''; else fs = roundNum(fontPropArr[i + 1] * aspect, 1);
			fu = fontPropArr[i + 2];
			if (bt || (fu != '%' && fu != 'em')) node.style.fontSize = ''.concat(fs, fu);
			if (fontPropArr[i + 3] == '') lh = ''; else lh = roundNum(fontPropArr[i + 3] * aspect, 1);
			lu = fontPropArr[i + 4];
			if (bt || (lu != '%' && lu != 'em')) node.style.lineHeight = ''.concat(lh, lu);
		}
	}
	
	return {
		init : function () {
			var fsu, fs, fu, lhu, lh, lu;
			var fSize = lrnGetCookie('lrnFontSizeAspect');
			aspect = !isNaN(roundNum(fSize, 1)) ? roundNum(fSize, 1) : 1;
			fontPropArr = [];
			walkTheDOM(document.body, function(node) {
				if (node.tagName != undefined) {
					if (node.currentStyle) { // IE only
						if (node.currentStyle['fontSize'] != undefined) {
							fsu = node.currentStyle['fontSize'];
							fs = roundNum(fsu, 1); if (isNaN(fs)) fs = '';
							fu = fsu.replace(/[0-9\.]/g, '');
							lhu = node.currentStyle['lineHeight'];
							lh = roundNum(lhu ,1); if (isNaN(lh)) lh = '';
							lu = lhu.replace(/[0-9\.]/g, '');
						}
					} else if (document.defaultView) { // all other browsers except IE
						fsu = document.defaultView.getComputedStyle(node, '').getPropertyValue('font-size');
						fs = roundNum(fsu, 1); if (isNaN(fs)) fs = '';
						fu = fsu.replace(/[0-9\.]/g, '');
						lhu = document.defaultView.getComputedStyle(node, '').getPropertyValue('line-height');
						lh = roundNum(lhu, 1); if (isNaN(lh)) lh = '';
						lu = lhu.replace(/[0-9\.]/g, '');
					}
					fontPropArr.push(node, fs, fu, lh, lu);
				}
			});
			doResize();
		},
		
		adjust : function(percent) { // percent can be plus or minus
			if (percent == 0) return;
			aspect = aspect * (1 + percent / 100);
		    lrnSetCookie('lrnFontSizeAspect', aspect, 180, '/');
			doResize();
		},
		
		reset : function () {
			aspect = 1;
		    lrnDeleteCookie('lrnFontSizeAspect', '/');
			doResize();
		}
	}
}();
