/* Script qui applique le thème sélectionné par le visiteur ou celui présent lors de sa dernière visite */

function appliquerFeuilleStyleActive(titre){
  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") == titre) a.disabled = false;
    }
  }
}

function recupererFeuilleStyleActive(){
  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 recupererFeuilleStylePreferee(){
  var date = new Date();
  var i, a;
  var mois = date.getMonth();

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

    if(mois >= 2 && mois <= 4){
      return "printemps";
    }else if(mois >= 5 && mois <= 7){
      return "ete";
    }else if(mois >= 8 && mois <= 10){
      return "automne";
    }else{
      return "hiver";
    }
  }
  return null;
}

function creerCookie(nom,valeur,jours){
  if (jours){
    var date = new Date();
    date.setTime(date.getTime()+(jours*24*60*60*1000));
    var expire = "; expires="+date.toGMTString();
  }
  else expire = "";
  document.cookie = nom+"="+valeur+expire+"; path=/";
}

function lireCookie(nom){
  var nomEQ = nom + "=";
  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(nomEQ) == 0) return c.substring(nomEQ.length,c.length);
  }
  return null;
}

window.onload = function(e){
  var cookie = lireCookie("style");
  var titre = cookie ? cookie : recupererFeuilleStylePreferee();
  appliquerFeuilleStyleActive(titre);
}

window.onunload = function(e){
  var titre = recupererFeuilleStyleActive();
  creerCookie("style", titre, 365);
}

var cookie = lireCookie("style");
var titre = cookie ? cookie : recupererFeuilleStylePreferee();
appliquerFeuilleStyleActive(titre);