//style changer script for http://www.invisibledanger.co.uk/


var stylesloaded = false;
var isWide;
var prefcss;
var normalWidth;

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;
        prefcss = title;
        normalWidth = document.getElementById("wrapper").style.width;
        //saveStyleSettings();
        createCookie("id_site_style", prefcss , 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=/";
}

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;
}

function toggleWidth()
{
 isWide = (isWide == 1) ? 0 : 1;
 setWidth(isWide);
}

function setWidth(wide)
{
 var wrapper_div = document.getElementById("wrapper");
 wrapper_div.style.width = (wide == 1) ? "100%" : normalWidth;
 isWide = wide;
 createCookie("id_site_width", isWide, 365);
}

window.onload = loadStyleSettings;

function loadStyleSettings()
{
  normalWidth = document.getElementById("wrapper").style.width;

  if (!stylesloaded)
  {  
    
    //cookie = readCookie("id_site_style");
    prefcss = getPreferredStyleSheet();
    var cookie = readCookie("id_site_width");
    isWide = (cookie == 1) ? 1 : 0;
    //setActiveStyleSheet(prefcss);
    setWidth(isWide);
    stylesloaded = true;
  }
}

function saveStyleSettings()
{  
  createCookie("id_site_style", prefcss , 365);
  createCookie("id_site_width", isWide, 365);
}
