var currentFontSize;

function readCookie(name)
{
	name = name + '=';
	var cookies = document.cookie.split(';');
  
	for(var i = 0; i < cookies.length; ++i)
	{
		var c = cookies[i];
		while(c.charAt(0) == ' ')
		{
			c = c.substring(1, c.length);
		}
		
		if (c.indexOf(name) == 0)
		{
			return c.substring(name.length, c.length);
		}
	}
	
	return null;
}


function createCookie(name, value, days)
{
	var expires = '';

	if(days)
	{
		var date = new Date();
		date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
		expires = '; expires=' + date.toGMTString();
	}

	document.cookie = name + '=' + value + expires + '; path=/';
}


function changeFontSize(sizeDifference)
{
	setFontSize(parseInt(currentFontSize) + parseInt(sizeDifference), 'centre');
}


function setFontSize(fontSize, objId)
{
	if(fontSize < 10 || fontSize > 13) return;

	var obj = document.getElementById(objId);

	if(obj)
	{
		obj.style.fontSize = fontSize + 'px';
		currentFontSize = fontSize;
	}
}


function loadUserSettings()
{
	var cookie = readCookie('newsFontSize');
	var initSize = (cookie && isFinite(parseInt(cookie))) ? parseInt(cookie) : 11;

	setFontSize(initSize, 'centre');
}


function saveUserSettings()
{
	createCookie('newsFontSize', currentFontSize, 365);
}


window.onload   = loadUserSettings;
window.onunload = saveUserSettings;