var prefsLoaded = false;
var currentFontType = 1;

function toggleSerif() {
	currentFontType = parseInt(currentFontType);
	switch (currentFontType) {
		case 1: 
			currentFontType = 2; 
			break;
		case 2: 
			currentFontType = 3; 
			break;
		case 3:
			currentFontType = 4;
			break;
		case 4:
			currentFontType = 5;
			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 setFontFace(fontType){
	fontType = parseInt(fontType);
	switch (fontType) {
		default:
			document.body.style.fontFamily = '"Segoe UI", "Lucida Grande", "Lucida Sans Unicode", Tahoma, sans-serif';
			break;
		case 2:
			document.body.style.fontFamily = '"Palatino Linotype", Georgia, serif';
			break;
		case 3:
			document.body.style.fontFamily = '"Frutiger Linotype", "Trebuchet MS", Arial, sans-serif';
			break;
		case 4:
			document.body.style.fontFamily = '"Franklin Gothic Medium", sans-serif';
			break;
		case 5:
			document.body.style.fontFamily = 'Times, "Times New Roman", serif';
			break;
	}
}


/* Helper functions */

function fcreateCookie(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 freadCookie(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 = freadCookie("fontFace");
		currentFontType = cookie ? cookie : 1;
		setFontFace(currentFontType);
		prefsLoaded = true;
	}
}

function saveSettings() {
	fcreateCookie("fontFace", currentFontType, 365);
}

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