/*****************************************************************************************************************
	Project Name: Cambridge Department of Public - The Works
	Project URL: http://www.cambridgema.gov/TheWorks/
	Description: styleswitcher - changes css styles that control text size within the site for accessibility
	Date: 1 July, 2009  
	Author: Peter-Paul Koch (http://www.xs4all.nl/~ppk/js/index.html?/~ppk/js/intro.html)
*****************************************************************************************************************/

function setActiveStyleSheet(title, isTombStoneClickedByUser, isDefaultFontFromSystem) {
  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;
    }
  }
  if (isDefaultFontFromSystem == true) {
      if (readCookie("tombstoneclicked") == true) {
          // rather than just aborting the request from the system
          // to set the default font we will put the font from
          // the already existing font cookie called "style"
          // because a user has already clicked on their tombstone
          // image to set their own font size and the user knows better what they want now.
          // Jeff Lee 9/18/2008 11:07:00 AM
          var cookie = readCookie("style");
          var title = cookie;
      }
  }
  else {
      if (isTombStoneClickedByUser == 'true') {
          createCookie("tombstoneclicked", true, 365);
      }      
  }
      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=/";
  //document.cookie = "tombstoneclicked=" + 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, false, false);
}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
//var tombstonecookie = readCookie("tombstoneclicked");
//var tombstoneclicked = tombstonecookie ? tombstonecookie : 'false';
setActiveStyleSheet(title, false, false);

