// Set HTML FontSize via DOM, 2009-06-20

var defFontSize = 85;	// In %, applied to <body>
var minFontSize = 56;	// Will differ according to Font / Layout
var maxFontSize = 112;

var prefsLoaded = false;
var currentFontSize = defFontSize;

var controls = new Array (	// array of objects (object literals)
	{	ttip: "Schrift vergrößern",
		actn: 'changeFontSize(2)',
		clss: 'larger',
		text: "Größer",
		link: null,			// DOM node with link
		span: null,			// DOM node without link
		actv: true
	},{
		ttip: "Schrift Verkleinern",
		actn: 'changeFontSize(-2)',
		clss: 'smaller',
		text: "Kleiner",
		link: null,
		span: null,
		actv: true
	},{
		ttip: "Schriftgröße auf Standard zurücksetzen",
		actn: 'resetFontSize()',
		clss: 'reset',
		text: "Standard",
		link: null,
		span: null,
		actv: true
	}
);

function changeFontSize(sizeDifference){
	currentFontSize = currentFontSize + sizeDifference;
	if (currentFontSize > maxFontSize) {
		currentFontSize = maxFontSize;
	}
	else
	if (currentFontSize < minFontSize) {
		currentFontSize = minFontSize;
	}
	setFontSize(currentFontSize);
	// AdMuncher defaults to preventing onunload actions, so do this here
	createCookie("fontSize", currentFontSize, 365);
};

function resetFontSize() {
	currentFontSize = defFontSize;
	changeFontSize(0);
}

function setFontSize(fontSize) {
	document.body.style.fontSize = fontSize + '%';
	// alert(document.body.style.fontSize);
	if (prefsLoaded) {
		// alert(fontSize + ' / ' + defFontSize  + ' / ' + (controls[2].actv ? 'true' : 'false'));
		// Maybe need to activate / deactivate a link
		if (fontSize >= maxFontSize &&  controls[0].actv) swapInDom(0); else
		if (fontSize <  maxFontSize && !controls[0].actv) swapInDom(0); else
		if (fontSize <= minFontSize &&  controls[1].actv) swapInDom(1); else
		if (fontSize >  minFontSize && !controls[1].actv) swapInDom(1); else
		if (fontSize == defFontSize &&  controls[2].actv) swapInDom(2); else
		if (fontSize != defFontSize && !controls[2].actv) swapInDom(2);
	}
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
};

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for (var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
};

function saveSettings()
{
	// alert('saveSettings');
	createCookie("fontSize", currentFontSize, 365);
}

function displayControls() {
	var fsSpan = (document.getElementById) ? document.getElementById('fontsize') : document.all('fontsize');
	// Old MSIEs don't like it when the text node is appended
	// before its parent is inserted into the DOM.
	var textObj = document.createTextNode("Schrift: ");
	fsSpan.appendChild(textObj);
	var l = controls.length, linkObj, spanObj, displayLink, objTextClone;
	var ua = navigator.userAgent.toLowerCase();
	var isOpera = ua.indexOf("opera") > -1;
	var isIE = !isOpera && ua.indexOf("msie") > -1;
	// 1. Always create all the link and span elements
	for (var i = 0; i < l; i++) {
		// alert(maxFontSize + ' / ' + currentFontSize + ' / ' + minFontSize);
		if (isIE) {
			// MSIE -  Grrr.. (attributes not applied using W3C-Method)
			linkObj = document.createElement('<A HREF="index.php" TITLE="' +
				controls[i].ttip + '" ONCLICK="' + controls[i].actn +
				'; return false;" CLASS="' + controls[i].clss + '">');
			spanObj = document.createElement('<SPAN CLASS="current">');
			}
		else {
			linkObj = document.createElement('a');
			linkObj.setAttribute('href', 'index.php');
			linkObj.setAttribute('title', controls[i].ttip);
			linkObj.setAttribute('onclick', controls[i].actn + '; return false;');
			linkObj.setAttribute('class', controls[i].clss);
			spanObj = document.createElement('span');
			spanObj.setAttribute('class', 'current');
			}
		textObj = document.createTextNode(controls[i].text);
		linkObj.appendChild(textObj);
		textObj = document.createTextNode(controls[i].text);
		spanObj.appendChild(textObj);
		// 2. Note them in the global object
		controls[i].link = linkObj;
		controls[i].span = spanObj;
		// 3. Add the appropriate one to the DOM
		if (controls[i].clss == 'larger') {
			controls[i].actv = (currentFontSize  < maxFontSize);
		}
		else
		if (controls[i].clss == 'smaller') {
			controls[i].actv = (currentFontSize  > minFontSize);
		}
		else {	// Reset to Standard
			controls[i].actv = (currentFontSize != defFontSize);
		}
		if (controls[i].actv) {
			fsSpan.appendChild(linkObj);
		}
		else {
			fsSpan.appendChild(spanObj);
		}
		// 4. Add some space
		spanObj = document.createElement('span');
		fsSpan.appendChild(spanObj);
		// escaped unicode non-breaking space - createTextNode escapes ampersand
		var strText = (i == (l-1)) ? '\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0' : '\u00A0';
		textObj = document.createTextNode(strText);
		fsSpan.lastChild.appendChild(textObj);
	}	// for
}

function swapInDom(intIndex) {
	if (controls[intIndex].actv) {
		controls[intIndex].link.parentNode.replaceChild(
			controls[intIndex].span, controls[intIndex].link);
		controls[intIndex].actv = false;
	}
	else {
		controls[intIndex].span.parentNode.replaceChild(
			controls[intIndex].link, controls[intIndex].span);
		controls[intIndex].actv = true;
	}
	return;
}

function setUserOptions() {
	// First get any saved value from a cookie and apply it
	if (!prefsLoaded) {
		var cookie = readCookie("fontSize");
		currentFontSize = cookie ? parseInt(cookie) : defFontSize;
		setFontSize(currentFontSize);
		prefsLoaded = true;
	}
	// Then display user controls (one may be initially inactive)
	displayControls();
}
