
// Set HTML FontSize via DOM, 2010-06-30

var defFontSize = window.g_fontsize;	// Set in head.inc.php from config
var minFontSize = 56;					// Will differ according to Font / Layout
var maxFontSize = 112;

var prefsLoaded = false;
var currentFontSize = defFontSize;


if (window.g_language == 'de') {
	var ttipLarger = 'Schrift vergrößern';
	var ttipNormal = 'Schriftgröße auf Normal zurücksetzen';
	var ttipSmaller = 'Schrift verkleinern';
	var textLarger = 'Größer';
	var textNormal = 'Normal';
	var textSmaller = 'Kleiner';
	// var textPrompt = window.g_fontsizePictyp == '' ? 'Schrift: ' : 'Schriftgröße: ';
	var textPrompt = window.g_fontsizePictyp == '' ? 'Schrift: ' : '';
}
else {
	// Default to english
	var ttipLarger = 'Display text larger';
	var ttipNormal = 'Reset text size to normal';
	var ttipSmaller = 'Display text smaller';
	var textLarger = 'Larger';
	var textNormal = 'Normal';
	var textSmaller = 'Smaller';
	// var textPrompt = window.g_fontsizePictyp == '' ? 'Text: ' : 'Text size: ';
	var textPrompt = window.g_fontsizePictyp == '' ? 'Text: ' : '';
}


var controls = new Array (	// array of objects (object literals)
	{	ttip: ttipLarger,
		actn: 'changeFontSize(2)',
		clss: 'avble fslarger',
		text: textLarger,
		file: 'font-increase.',
		link: null,			// DOM node with link to perform the action
		span: null,			// DOM node without link (control is inactive)
		actv: true
	},{
		ttip: ttipNormal,
		actn: 'resetFontSize()',
		clss: 'avble fsreset',
		text: textNormal,
		file: 'font-reset.',
		link: null,
		span: null,
		actv: true
	},{
		ttip: ttipSmaller,
		actn: 'changeFontSize(-2)',
		clss: 'avble fssmaller',
		text: textSmaller,
		file: 'font-decrease.',
		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
		// intIndex: 0=Increase - 1=Normal - 2=Decrease
		if (fontSize >= maxFontSize &&  controls[0].actv) swapInDom(0); else
		if (fontSize <  maxFontSize && !controls[0].actv) swapInDom(0); else
		if (fontSize == defFontSize &&  controls[1].actv) swapInDom(1); else
		if (fontSize != defFontSize && !controls[1].actv) swapInDom(1); else
		if (fontSize <= minFontSize &&  controls[2].actv) swapInDom(2); else
		if (fontSize >  minFontSize && !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);
}


/*

<span id="fontsize">
	|
	+ <TextNode>: Text size:
	|
	+ <a href="#" class="avble fslarger">  OR  <span>
	|	 |
	|	 |/----- <TextNode>: Größer
	|	 <
	|	  \----- <img src="..." height="16" width="16" alt="">
	|      window.g_pathPrefix + "images/std/font-increase." + window.g_fontsizePictyp
	|
	+ <TextNode>: " | "
	|
	+ <a href="#" class="avble fsreset">  OR  <span>
	|	 |
	|	 |/----- <TextNode>: Normal
	|	 <
	|	  \----- <img src="..." height="16" width="16" alt="">
	|      window.g_pathPrefix + "images/std/font-reset." + window.g_fontsizePictyp
	|
	+ <TextNode>: " | "
	|
	+ <a href="#" class="avble fssmaller">  OR  <span>
		 |
		 |/----- <TextNode>: Kleiner
		 <
		  \----- <img src="..." height="16" width="16" alt="">
	       window.g_pathPrefix + "images/std/font-decrease." + window.g_fontsizePictyp

*/

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.

	// DEBUG
	// textPrompt = ' min ' + minFontSize + ' max ' + maxFontSize +
	// 			 ' def ' + defFontSize + ' cur ' + currentFontSize + ' ';

	var textObj = document.createTextNode(textPrompt);

	fsSpan.appendChild(textObj);

	var mouseOver = "if (typeof window.g_loading == 'boolean') { showtip(this, 0, 0, 0, '";
	var mouseOut  = "if (typeof window.g_loading == 'boolean') { showtip(this, 0, 0, 0, ''); }";

	var l = controls.length, linkObj, spanObj, displayLink, objTextClone,
		prePath = window.g_pathPrefix + 'images/std/';

	// createTextNode escapes ampersand, so:
	// escaped unicode middot \u00B7
	// escaped unicode non-breaking space \u00A0
	// var sepText = window.g_fontsizePictyp == '' ? ' \u00B7 ' : '\u00A0';
	var sepText = window.g_fontsizePictyp == '' ? ' \u00B7 ' : '';

	// 1. Always create all the link and span objects, although
	//    only either the link or the span object will be used
	//    at one time - which of them is, depends on current size.

	for (var i = 0; i < l; i++) {

		// alert(maxFontSize + ' / ' + currentFontSize + ' / ' + minFontSize);

		if (isIE6 || isIE7) {	// Globals set in startup.js
			// MSIE - Grrr... (attributes not applied using W3C-Method)
			// HREF: "javascript:return true;" also possible if problems with "#"
			if (window.g_extraTooltips) {
				// linkObj = document.createElement('<A HREF="#" TITLE="' +
				// 	controls[i].ttip + '" ONCLICK="' + controls[i].actn +
				// 	'; return false;" CLASS="' + controls[i].clss + '">');
				linkObj = document.createElement('<A HREF="#" ONMOUSEOVER="' +
					mouseOver + controls[i].ttip + "'); }" + '" ONMOUSEOUT="' +
					mouseOut  + '" ONCLICK="' + controls[i].actn +
					'; return false;" CLASS="' + controls[i].clss + '">');
			}
			else {
				linkObj = document.createElement('<A HREF="#" ONCLICK="' +
					controls[i].actn + '; return false;" CLASS="' +
					controls[i].clss + '">');
			}
			spanObj = document.createElement('<SPAN CLASS="current">');
		}

		else {
			// W3C-compliant browser
			linkObj = document.createElement('a');
			linkObj.setAttribute('href', '#');
			if (window.g_extraTooltips) {
				// linkObj.setAttribute('title', controls[i].ttip);
				linkObj.setAttribute('onmouseover', mouseOver + controls[i].ttip + "'); }");
				linkObj.setAttribute('onmouseout', mouseOut);
			}
			linkObj.setAttribute('onclick', controls[i].actn + '; return false;');
			linkObj.setAttribute('class', controls[i].clss);
			spanObj = document.createElement('span');
			spanObj.setAttribute('class', 'current');
		}

		if (window.g_fontsizePictyp == '') {

			// Display text
			textObj = document.createTextNode(controls[i].text);
			linkObj.appendChild(textObj);

			textObj = document.createTextNode(controls[i].text);
			spanObj.appendChild(textObj);

		} else {

			// Display images
			fileObj = document.createElement('img');
			fileObj.setAttribute('src', prePath + controls[i].file + window.g_fontsizePictyp);
			fileObj.setAttribute('width', 16);
			fileObj.setAttribute('height', 16);
			fileObj.setAttribute('alt', '');
			linkObj.appendChild(fileObj);

			fileObj = document.createElement('img');
			fileObj.setAttribute('src', prePath + controls[i].file + window.g_fontsizePictyp);
			fileObj.setAttribute('width', 16);
			fileObj.setAttribute('height', 16);
			fileObj.setAttribute('alt', '');
			spanObj.appendChild(fileObj);
		}

		// 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 == 'avble fslarger') {
			controls[i].actv = (currentFontSize  < maxFontSize);
		}
		else
		if (controls[i].clss == 'avble fssmaller') {
			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');
		if (i == (l-1)) spanObj.setAttribute('id', 'fstrailingspace');
		fsSpan.appendChild(spanObj);
		// At the end add more space, between elements just a middot or nbsp
		// createTextNode escapes ampersand, so:
		// escaped unicode non-breaking space \u00A0
		var strText = (i == (l-1)) ? '\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0' : sepText;
		if (strText != '') {
			textObj = document.createTextNode(strText);
			fsSpan.lastChild.appendChild(textObj);
		}

	}	// for

}


function swapInDom(intIndex) {
	// intIndex: 0=Increase - 1=Normal - 2=Decrease
	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;
	}
	// Cancel any own tooltip - we swap the DOM element so there is no onmouseout
	if (window.showtipwork) showtipwork(null, 0, 0, '');
	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();
}

