function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
  createCookie("style", title, 365);
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

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;
}

window.onload = function(e) {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);


function renderReadability () {

	var typefaces = new Array('Lucida Grande', 'Verdana', 'Arial', 'Gill Sans', 'Trebuchet', 'Georgia', 'Times');
	document.write('<form>');
	document.write('<span id="fontsizemenu">Size:<select name="fontsize" style="font-size: 10px; margin: 2px 0 2px 3px" onChange="changeFontSize(this.options[selectedIndex].value);">');
	for ( var x=9; x <= 24; x++) {
		if (Number(x) == Number(fontsize)) {
			document.write('<option value="'+x+'" selected="selected">'+x+'<\/option>');
		} else {
			document.write('<option value="'+x+'">'+x+'<\/option>');
		}
	}
	document.write('<\/select><\/span>');
	document.write('<span id="fontfamilymenu"><br />Font:<select name="fontfamily" style="font-size: 10px; margin: 2px 0 2px 3px" onChange="changeFontFamily(this.options[selectedIndex].value);">');
	for (i=0; i<typefaces.length; i++) {
		if (fontfamily == typefaces[i]) {
			var notcustom = true;
			document.write('<option value="'+ typefaces[i]+'" selected="selected">'+typefaces[i]+'<\/option>');
		} else {
			document.write('<option value="'+ typefaces[i]+'">'+typefaces[i]+'<\/option>');
		}
	}
	if (!notcustom) {
		document.write('<option value="'+ fontfamily+'" selected="selected">'+fontfamily+'<\/option>');
	}
	document.write('<\/select><\/span><\/form>');
	document.write('<br \/><form onsubmit="changeFontFamily(escape(document.getElementById(\'customfontfield\').value)); return false;"><a href="#" onclick="document.getElementById(\'customfont\').style.display = \'block\'; document.getElementById(\'fontfamilymenu\').style.display = \'none\'; return false" style="text-decoration: underline"><\/a>');
	document.write('<div id="customfont" style="display: none; margin: 0; padding: 0">Font: <input id="customfontfield" name="customfontfield" size="8" style="width: 50px; font-size: 10px; margin: 6px 6px 0 0" /><input type="submit" value="Set" style="font-size: 10px" /><\/div>');
	document.write('<\/form>');
}

var fontfamily = readCookie('fontfamily');
var fontsize = readCookie('fontsize');

if (!fontfamily && navigator.userAgent.toLowerCase().indexOf("mac") > -1) {
	var fontfamily = 'Lucida Grande';
} else if (!fontfamily) {
	var fontfamily = 'Verdana';
}
if (!fontsize) {
	var fontsize = 11;
}



function changeFontFamily (fontfamily) {
	createCookie('fontfamily', fontfamily.replace(/%20/g,unescape('%20')),365);
	self.location.href = self.location.href;
}

function changeFontSize (fontsize) {
  createCookie('fontsize', fontsize,365);
  self.location.href = self.location.href;
}

var fontfamilyadjusted;

	if (fontfamily == 'Times') {
		fontfamilyadjusted = '"Times New Roman", Times';
	} else if (fontfamily == 'Trebuchet') {
		fontfamilyadjusted = '"Trebuchet MS", Trebuchet';
	} else if (fontfamily == 'Gill Sans') {
		fontfamilyadjusted = '"Gill Sans MT", Gill Sans';
	} else {
		fontfamilyadjusted = '"'+fontfamily+'"';
}

document.write('<style type="text/css">');
document.write('#content {font-family:'+fontfamilyadjusted+'; font-size:'+fontsize+'px;}');
document.write('<\/style>');
