/**
 * @project Overheid.nl
 * @author	Victor Zuydweg, Stichting ICTU
 * @since	19 febr 2009
 * @version	0.1
 */

if (window.addEventListener) window.addEventListener("load", init, false); 
else if (window.attachEvent) window.attachEvent("onload", init);
else window.onload = init;

var fontsizes = ['57.5%','60%','62.5%','65%','67.5%'];
var fontsize;

/**
 * Set the fontsize of the body
 * Set global variable fontsize;
 */
function setFontSize()
{
	fontsize = parseFloat(fontsize);
	var size = 0;
	if(this.size != undefined) size = parseFloat(this.size);

	if(fontsizes[fontsize + size] != undefined)
	{
		document.body.style.fontSize = fontsizes[fontsize += size];
		createCookie('fontsize', fontsize);
	}
	return false;
}

/**
 * Initial loads
 */
function init()
{
	var div = document.getElementsByTagName('div');
	for(var i=0; i<div.length; i++)
	{
		if(div[i].className.search('func_links') >= 0)
		{
			//delExistingFontSizeControls(div[i]);
			if(attachFontSizeControls(div[i]))
			{
				if (!readCookie('fontsize')) createCookie('fontsize', 2);
				fontsize = readCookie('fontsize');
				setFontSize();
			}
		}
	}
}

/**
 * Delete hardcoded fontsize controls
 * @param {HTML divelement} header
 */
function delExistingFontSizeControls(header)
{
	var dl = header.getElementsByTagName('dl');
	if(dl.length > 0) dl[dl.length - 1].parentNode.removeChild(dl[dl.length - 1]);
}

/**
 * Attach fontsize controls tot header div
 * @param {HTML divelement} header
 */
function attachFontSizeControls(header)
{	
	var a_kleiner = document.createElement('a');
	a_kleiner.appendChild(document.createTextNode('-'));
	a_kleiner.size = -1;
	a_kleiner.onclick = setFontSize;
	a_kleiner.href="#";
	a_kleiner.title="Tekst kleiner maken";
	a_kleiner.className="minus imgreplacement";
	
	var a_groter = document.createElement('a');
	a_groter.appendChild(document.createTextNode('+'));
	a_groter.size = 1;
	a_groter.onclick = setFontSize;
	a_groter.href="#";
	a_groter.title="Tekst groter maken";
	a_groter.className="plus imgreplacement";
	
	var li = document.createElement('li');
            li.appendChild(document.createTextNode('Tekstgrootte '));
	li.appendChild(a_kleiner);
            li.appendChild(a_groter);
            li.className="font_size";
	
	var ul  = document.createElement('ul');
	ul.appendChild(li);
	ul.className="left";
	
	if(header.appendChild(ul)){
          return true;
        }
        else
        {
          return false;
        }
}


/**
 * Returns cookie according to name
 * @param {String} name
 */
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;
}

/**
 * Saves local cookie
 * @param {String} name
 * @param {String} value
 */
function createCookie(name, value)
{
    var exdate=new Date();
	exdate.setDate(exdate.getDate()+365);
	var expires = "; expires= " + exdate.toGMTString(); // 1 jaar
	var path = "; path=/";
	
	if(window.location.host.search('overheid.nl') >= 0) var domain = '; domain=overheid.nl'; // productie
	else if(window.location.host.search('overheid.nl') >= 0) var domain = '; domain=asp4all.nl'; // acceptatie
	else var domain = ""; // werkt niet in subdomain

	document.cookie = name + "=" + value + expires + path + domain;
}
