// Javascript file to switch CSS stylesheets across all browsers

// Accept input from HTML or user in form of variable 'title' to identify the selected stylesheet
function setCSSActive(title) {
  var i, a, main;
  // loop through the document, check all links to find the stylesheet links
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    // Check links, find if this link is to one of the stylesheets by checking if it has both the rel and title properties. This shows it is both a stylesheet and that it is either a preferred or alternative stylesheet. 
	if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
		// disable stylesheets which should not be active. 
      a.disabled = true;
	  	// if the stylesheet is the one designated in the title variable, then keep it enabled. 
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}

// activate a function to store the current stylesheet and return it, designating the title variable. 
function getActiveCSS() {
  var i, a;
  	// once again, loop through the file seeking out links.
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
	  // find which of the links are to the CSS files and not disabled, if we can find that link, that is the active stylesheet
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

// find the preferred (default) CSS in a way compatable with Firefox and with IE. 
function getPreferredCSS() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
	  // Insure that there is a preferred and alternate stylesheet and that the values are correct to be handled by the script and browser.
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

// create a cookie that stores 
function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
	//set date and time
    date.setTime(date.getTime()+(days*24*60*60*1000));
	//set cookie expiration
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

//function to read the cookie and pull the required value for the these scripts to run.  
function readCookie(name) {
  var nameVL = 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(nameVL) == 0) return c.substring(nameVL.length,c.length);
  }
  return null;
}

//Check to see if the user has a selected stylesheet in a cookie.
window.onload = function(e) {
  var cookie = readCookie("style");
  // if title is equal to cookie then assign it the value contained in the cookie, otherwise get a new value.
  var title = cookie ? cookie : getPreferredCSS();
  setCSSActive(title);
}

//on unloading the page, give user a cookie with the stylesheet they selected. 
window.onunload = function(e) {
  var title = getActiveCSS();
  createCookie("style", title, 365);
}

//designate a cookie variable and set it's value to the stylesheet last selected
var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredCSS();
//activate setCSSActive
setCSSActive(title);
