var CPSC_prefsLoader = false;
var CPSC_currentFontSize = CPSC_FONT_SIZE;
var CPSC_currentContainerWidth = CPSC_MEDIUM_WIDTH;
var CPSC_currentColor = CPSC_COLOR;

function changeColor(newColor){
  CPSC_currentColor = parseInt(newColor)
  if (CPSC_currentColor == 0) {
    document.getElementsByTagName('body')[0].innerHTML += '<link rel="stylesheet" type="text/css" href="'+_DOSSIER_SQUELETTES+'/template_css_light_orange.css" />';
  } 
  else if (CPSC_currentColor == 1) {
    document.getElementsByTagName('body')[0].innerHTML += '<link rel="stylesheet" type="text/css" href="'+_DOSSIER_SQUELETTES+'/template_css_light_violet.css" />';
  } 
  else if (CPSC_currentColor == 2) {
    document.getElementsByTagName('body')[0].innerHTML += '<link rel="stylesheet" type="text/css" href="'+_DOSSIER_SQUELETTES+'/template_css_light_green.css" />';
  } 
  else if (CPSC_currentColor == 3) {
    document.getElementsByTagName('body')[0].innerHTML += '<link rel="stylesheet" type="text/css" href="'+_DOSSIER_SQUELETTES+'/template_css_light_blue.css" />';
  } 
  else if (CPSC_currentColor == 4) {
    document.getElementsByTagName('body')[0].innerHTML += '<link rel="stylesheet" type="text/css" href="'+_DOSSIER_SQUELETTES+'/template_css_orange.css" />';
  } 
  else if (CPSC_currentColor == 5) {
    document.getElementsByTagName('body')[0].innerHTML += '<link rel="stylesheet" type="text/css" href="'+_DOSSIER_SQUELETTES+'/template_css_violet.css" />';
  } 
  else if (CPSC_currentColor == 6) {
    document.getElementsByTagName('body')[0].innerHTML += '<link rel="stylesheet" type="text/css" href="'+_DOSSIER_SQUELETTES+'/template_css_green.css" />';
  } 
  else if (CPSC_currentColor == 7) {
    document.getElementsByTagName('body')[0].innerHTML += '<link rel="stylesheet" type="text/css" href="'+_DOSSIER_SQUELETTES+'/template_css_blue.css" />';
  }
  createCookie("color", CPSC_currentColor, 30);
}

function revertStyles(){
  CPSC_currentFontSize = CPSC_FONT_SIZE;
  changeFontSize(0);
}

function changeFontSize(sizeDifference){
  CPSC_currentFontSize = parseInt(CPSC_currentFontSize) + parseInt(sizeDifference);

  if (CPSC_currentFontSize > 18) {
    CPSC_currentFontSize = 18;
  } else if(CPSC_currentFontSize < 6) {
    CPSC_currentFontSize = 6;
  }

  setFontSize(CPSC_currentFontSize);
}

function setFontSize(fontSize){
  document.body.style.fontSize = fontSize + 'px';
  createCookie("fontSize", CPSC_currentFontSize, 30);
}

function changeContainerWidth(newWidth) {
  var ids = new Array("top-margin-out", "top-margin-round-out", "bottom-margin-round-out", "menu-top", "jdmenu-top", "menu-top", "header", "wrapper-content");
  CPSC_currentContainerWidth = parseInt(newWidth);
  for (var i=0; i<ids.length; i++) {
    var obj = document.getElementById(ids[i]);
    if (obj == null)
      continue;
    if (false && CPSC_currentContainerWidth == 0) {
      obj.style.width = '98%';
      /*obj.style.marginLeft = '15px';
      obj.style.marginRight = '15px';*/
    }
    else {
      obj.style.width = CPSC_currentContainerWidth + 'px';
      /*obj.style.marginLeft = 'auto';
      obj.style.marginRight = 'auto';*/
    }
  }

  var id2s = new Array("accueil-actu", "accueil-agenda");
  for (var i=0; i<id2s.length; i++) {
    var obj = document.getElementById(id2s[i]);
    if (obj == null)
      continue;
    obj.style.width = "" + ((CPSC_currentContainerWidth - 320 - 20) / 2) + 'px';
  }
  
  createCookie("containerWidth", CPSC_currentContainerWidth, 30);
}

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;
}

/*
window.onload = setUserOptions;

function setUserOptions() {
  if (!CPSC_prefsLoader){
    userFontSize = readCookie("fontSize");
    userContainerWidth = readCookie("containerWidth");
    userColor = readCookie("color");
    CPSC_currentFontSize = userFontSize ? userFontSize : CPSC_FONT_SIZE;
    CPSC_currentContainerWidth = userContainerWidth ? userContainerWidth : CPSC_MEDIUM_WIDTH;
    CPSC_currentColor = userColor ? userColor : CPSC_COLOR;
    //setFontSize(CPSC_currentFontSize);
    //changeContainerWidth(CPSC_currentContainerWidth);
    //changeColor(CPSC_currentColor);
    CPSC_prefsLoader = true;
  }
}

window.onunload = saveUserOptions;

function saveUserOptions()
{
  createCookie("fontSize", CPSC_currentFontSize, 30);
  createCookie("containerWidth", CPSC_currentContainerWidth, 30);
  createCookie("color", CPSC_currentColor, 30);
}
*/
