
/* Shamelessly adapted from Ars Technica 
   I hope to get around to create something better soon */

var prefsLoaded = false;
var currentFontType = 1;
var currentFontSize = 0.7;


/* Interactive functions */

function changeFontSize(sizeDifference){
	
	setFontOffset (sizeDifference);
	
	if (window.opera) {
		saveSettings();

		re = /Opera (\d)\.(\d)/
		matches = re.exec(navigator.userAgent);
		
		if (parseInt(matches[1]) < 7 || 
		   (parseInt(matches[1]) == 7 && parseInt(matches[2]) < 5)) 
		{
			location.reload();
		}
	}
}

function toggleSerif() {

	currentFontType = parseInt(currentFontType);
	
	switch (currentFontType) {
		case 1: 
			currentFontType = 2; 
			break;
			
		case 2: 
			currentFontType = 3; 
			break;
			
		default: 
			currentFontType = 1; 
			break;
	}
	
	setFontFace(currentFontType);

	if (window.opera) {
		saveSettings();

		re = /Opera (\d)\.(\d)/
		matches = re.exec(navigator.userAgent);
		
		if (parseInt(matches[1]) < 7 || 
		   (parseInt(matches[1]) == 7 && parseInt(matches[2]) < 5)) 
		{
			location.reload();
		}
	}
}




function setFontOffset (sizeDifference) {
	
	currentFontSize = ((currentFontSize * 10) + parseInt(sizeDifference)) / 10;

	if (currentFontType == 1)
	{
		if(currentFontSize > 0.9)
		{
			currentFontSize = 0.9;
		}
		else if(currentFontSize < 0.7)
		{
			currentFontSize = 0.7;
		}
	}
	else
	{
		if(currentFontSize > 1.0)
		{
			currentFontSize = 1.0;
		}
		else if(currentFontSize < 0.7)
		{
			currentFontSize = 0.7;
		}
	}
	
	setFontSize(currentFontSize);
}

function setFontSize(fontSize){
	document.body.style.fontSize = fontSize + 'em';
}

function setFontFace(fontType){
	
	fontType = parseInt(fontType);
	
	switch (fontType) {
		case 3:
			document.body.style.fontFamily = '"Lucida Sans Unicode", "Lucida Sans", Arial, sans-serif';
			break;
			
		case 2:
			document.body.style.fontFamily = 'Georgia, Times, "Times New Roman", serif';
			setFontOffset(1);
			break;
			
		default:
			document.body.style.fontFamily = 'Verdana, Geneva, Arial, Helvetica, sans-serif';
			setFontOffset(-1);
			break;
	}
}

/* Helper functions */

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 setUserOptions()
{	
	if (!prefsLoaded)
	{
		cookie = readCookie("fontFace");
		currentFontType = cookie ? cookie : 2;
		setFontFace(currentFontType);

		cookie = readCookie("fontSize");
		currentFontSize = cookie ? cookie : 0.8;
		setFontSize(currentFontSize);
		
		prefsLoaded = true;
	}
}

function saveSettings()
{
	createCookie("fontSize", currentFontSize, 365);
	createCookie("fontFace", currentFontType, 365);
}

window.onunload = saveSettings;
window.onload = setUserOptions;

