/*
 created on 28 may 2007
 by JT - Descom Oy
 -
 unobstructive javascript
 set default font size for body element
*/

/* cookies handling */
function setCookie(name, value, expire) {
	deleteCookie(name);
	document.cookie = name + "=" + escape(value) + ((expire === null) ? "" : ("; expires=" + expire.toGMTString()))
}

function deleteCookie(name) {
    if (getCookie(name)) {
        document.cookie = name + "=" + ";expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

function getCookie(Name) {
	var search = Name + "=";
	if (document.cookie.length > 0) { // if there are any cookies
		offset = document.cookie.indexOf(search);
		if (offset != -1) { // if cookie exists
			offset += search.length;   // set index of beginning of value
			end = document.cookie.indexOf(";", offset);   // set index of end of cookie value
			if (end == -1) {
				end = document.cookie.length;
			}
			return unescape(document.cookie.substring(offset, end))
		}
	}
}

/* Font size handling */
function setFontSize(vSize) {
	var	isMSIE = /*@cc_on!@*/false;
		
	document.body.style.fontSize = vSize;
	
	if (isMSIE) {
		setTableFontSize(vSize);
	}
	
}

function setTableFontSize(vSize) {
	var	contenttables = null,
		maincontent = null;

	// get tables from content area
	maincontent = document.getElementById('cont_main');
	contenttables = maincontent.getElementsByTagName('table');

	// loop all tables and change font size
	for (i = 0; i < contenttables.length; i++) {
		contenttables[i].style.fontSize = vSize;
	}
}

function changeBodyFontSize(changeInt) {
	// created on 24 May 2007 by Descom (Tuomala)
	var defaultSize = 12,
		limitLow = 8,
		limitMax = 16,
		currentSize = (document.body.style.fontSize === "") ? defaultSize : parseInt(document.body.style.fontSize),
		updatedSize = currentSize + changeInt,
		img = null,
		reduceid = "reduceimg",
		enlargeid = "enlargeimg",
		expires = new Date(),
		isMSIE = /*@cc_on!@*/false,
		fontsize = (updatedSize > limitLow && updatedSize < limitMax) ? updatedSize + "px" : (updatedSize <= limitLow) ? limitLow + "px" : (updatedSize >= limitMax) ? limitMax + "px" : defaultSize + "px",
		i = null;

	// update body font size
	document.body.style.fontSize = fontsize;

	if (isMSIE) {
		setTableFontSize(fontsize);
	}
	
	// save default font size value to cookie
	// cookie expires after 1 hour
	expires.setTime(expires.getTime() + 60*60*1000);
	setCookie("defaultfontsize", document.body.style.fontSize, expires);
	// update minus image to active/passive
	img = (document.getElementById) ? document.getElementById(reduceid) : null;
	if (img) {
		img.src = (updatedSize === limitLow) ? img.src.replace(/miinus/,"miinus_passiivinen") : (updatedSize > limitLow) ? img.src.replace(/miinus_passiivinen/,"miinus") : img.src;
	}
	// update plus image to active/passive
	img = (document.getElementById) ? document.getElementById(enlargeid) : null;
	if (img) {
		img.src = (updatedSize === limitMax) ? img.src.replace(/plus/,"plus_passiivinen") : (updatedSize < limitMax) ? img.src.replace(/plus_passiivinen/,"plus") : img.src;
	}
}

/* Add function to onload event of page */
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

/* read default font size in cookies */
function setDefaultFontSize() {
	var deffontsize = getCookie("defaultfontsize");
	if (deffontsize) {
		setFontSize(deffontsize);
	}
}

addLoadEvent(setDefaultFontSize);