
//
// Look for style sheets entitled "PREFIX_NAME" and activate the one that matches "name"
// The idea is to allow multiple sheets to activated/deactivated independently 
// 

function activateStyleSheet(prefix, title) 
{
  var i, a, main, fullTitle, testTitle;
  fullTitle = prefix + "_" + title
  //alert(fullTitle);
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) 
  {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) 
    {
      testTitle = a.getAttribute("title");
      if (testTitle.indexOf(prefix) == 0)
      {
         a.disabled = true;
         if(testTitle == fullTitle) 
         {
            //alert(fullTitle + " found");
            a.disabled = false;
         }
      }
    }
  }
}

function getActiveStyleSheet(prefix) 
{
  var i, a;
  var testTitle, suffix;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) 
  {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title"))
    {
      testTitle = a.getAttribute("title");
      if ((testTitle.indexOf(prefix) == 0) && !a.disabled) 
      {
         suffix = testTitle.substr(prefix.length+1);
         //alert(suffix);
         return suffix;
      }
    }
  }
  return null;
}

function getPreferredStyleSheet() {
  return ('A-');
}

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;
}

//
// Site-specific stuff
//
var defaultColorStyle = "styles";
var defaultSizeStyle  = "large";

window.onload = function(e) 
{
 
  var clrCookie = readCookie("clr");
  var clrTitle = clrCookie ? clrCookie : defaultColorStyle;
  activateStyleSheet("clr",clrTitle);  
  
  var sizeCookie = readCookie("sz");
  var szTitle = sizeCookie ? sizeCookie : defaultSizeStyle;
  activateStyleSheet("sz",szTitle);
  
}

window.onunload = function(e) 
{
  var sizeStyle = getActiveStyleSheet("sz");
  createCookie("sz", sizeStyle, 365);
  
  var colorStyle = getActiveStyleSheet("clr");
  createCookie("clr", colorStyle, 365);  
}

//
// This should just call load...
//
// or is it even needed?

//var sizeCookie = readCookie("sz");
//var szTitle = sizeCookie ? sizeCookie : defaultSizeStyle;
//activateStyleSheet("sz",szTitle);

//var clrCookie = readCookie("clr");
//var clrTitle = clrCookie ? clrCookie : defaultColorStyle;
//activateStyleSheet("clr",clrTitle);
  