// functions lifted from http://www.heartsofoakfoundation.org/

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;
    }
  }
}

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, 0);
}

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);
// RESIZING TEXT -------------------------------------------------------

// Set Body text size
function setSize(iSize) {
	if (!document.getElementsByTagName) return false;
	var bodyTag = document.getElementsByTagName ("body");
	for (var i=0; i < bodyTag.length; i++) {
		if (!bodyTag[i].style.fontSize) {
		if (!readCookie("resize")) {
		bodyTag[i].style.fontSize = "62.5%";
		}
		else {
		bodyTag[i].style.fontSize = ((parseFloat(readCookie("resize"))) + "%");
		}
		}
		else {
		if (!readCookie("resize")) {
		bodyTag[i].style.fontSize = "62.5%";
		}
		else {
		bodyTag[i].style.fontSize = (((parseFloat(iSize)) + (parseFloat(readCookie("resize")))) + "%");
		}
		}
		cookieValue = bodyTag[i].style.fontSize;
		writeCookie("resize", cookieValue, 0);
	}
}


// Attaches the onclick event to the correct ids to allow resizing
function resizeT(iID, iDir) {
	if (!document.getElementById) return false;
	var incLink = document.getElementById (iID);
	if (iDir == "decrease") {
	incLink.onclick = function() {
		setSize("-5%");
		return false;
	}
	}
	if (iDir == "increase") {
	incLink.onclick = function() {
		setSize("5%");
		return false;
	}
	}
}

// Writes cookie
// writeCookie("myCookie", "my name", 24);
// Stores the string "my name" in the cookie "myCookie" which expires after 24 hours.

function writeCookie(name,value,hours) {
  if (hours) {
    var date = new Date();
    date.setTime(date.getTime()+(hours*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}


// Read Cookie

function readCookie(name) {
  var cookieValue = 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(cookieValue) == 0) return c.substring(cookieValue.length,c.length);
  }
  return null;
}

window.onload=function(){
resizeT("stIncrease", "increase");
resizeT("stDecrease", "decrease");
setSize("62.5%");
}
