/*
 * Stylesheet-Switcher
 * Base from alistapart.com
 * Adapted by D.Pisek
 * 
 */

//Sets the font-size
function setFontsize(size)
{
    document.body.style.fontSize = size;
}

//Switches between the alternate stylesheets
function setAlternateStylesheet(stylesheet)
{
    switch (stylesheet) {
        case 'C' : 
          setActiveStyleSheet('C');
          break;
        case 'C+' : 
          setActiveStyleSheet('C+');
          break;
        default :
          setActiveStyleSheet('C');
          break;
    }
}

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;
    }
  }
}

//Returns the selected font-size
function getSelectedFontsize()
{
    return (document.body.style.fontSize);
}

//Returns the active Stylesheet
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() 
{
  return ('A');
}

function getPreferredSize() 
{
  return ('12');
}

function getProperty(type)
{
    if (type == 'style') {
        return ('A');
    } else {
        return '12';
    }
}

//Creates a cookie for storing the stylesheet, font-size value
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;
}

//Switches either to the user set style / font-size or the default values
window.onload = function(e) 
{
  var cookieStyle = readCookie("style");
  var cookieSize   = readCookie("size");
  
  var titleStyle = cookieStyle ? cookieStyle : getPreferredStyleSheet();
  var selSize   = cookieSize ? cookieSize :  getPreferredSize(); 
  
  setActiveStyleSheet(titleStyle);
  setFontsize(selSize);
  setContrastSelection(titleStyle);
}

//Stores the cookies when leaving the page
window.onunload = function(e) 
{
  var title = getActiveStyleSheet();
  var size = getSelectedFontsize();
  
  createCookie("style", title, 365);
  createCookie("size", size, 365);
}

//If window.onload does not work ?
function setProperties(type)
{
    var cookie = readCookie(type);
    var property = cookie ? cookie : getPropertyt(type);
    
    if (type == 'style') {
        setActiveStyleSheet(property);
    }  else {
        setFontsize(property);
    }  
}


function setContrastSelection(contrast)
{        
        var contrastSelection = document.getElementById('contrast');
        var i;
        
        for (i = 0; i < contrastSelection.options.length; i++) {
            if(contrastSelection.options[i].value == contrast) {
                contrastSelection.options[i].selected = 'selected';
            }
        }
}

