// skins.js
// These functions support skins on the USConstitution.net site

// Scan the list of link elements. Disable all stylesheets in the list.
// If one is found that matches the given style name, enable it.
function setActiveStyleSheet(stynm)
{
  var i, a, t;
  for (i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    t = a.getAttribute("title");
    if (t && a.getAttribute("rel").indexOf("style") != -1) {
      a.disabled = true;
      if (t == stynm) a.disabled = false;
    }
  }
}

// Basic cookie lookup function
function read_cookie(key)
{
  var c, k, i;
  var d = null;
  c = document.cookie.split("; ");
  for (i=0; i<c.length; i++) {
    k = c[i].split("=");
    if (k[0] == key) {
      d = unescape(k[1]);
      break;
    }
  }
  return(d);
}

// Get the usc_sheet cookie value, if any. If the user's skin is not the
// default skin, set the active skin to the user's choice.
function skinLoader()
{
  var c = read_cookie("usc_sheet");
  if (c != null && c != "Default") setActiveStyleSheet(c);
}

// Call the skin loader as soon as possible. This will basically force the
// user's skin choice to be used before the browser displays anything -
// that's the hope, at least.
// Note that the stylesheet links must come before this call, else the
// whole thing won't work.
skinLoader();


