// script tratto da http://www.webaccessibile.org/
function getScreen() {
if (screen.width == 800) {

return "r800";
	}
if (screen.width == 1024) {


return "r1024";
	}
if (screen.width == 1280) {

return "r1280";
	}
}



function setActiveColorStyleSheet(color) {
var i, a, main;
// memorizza lo stylesheet del carattere prima di cambiare colore
var currentFont=getActiveFontStyleSheet();
for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("id").indexOf("color")!=-1) {
      a.disabled = true;

	// abilita lo stile colore ''
      if(a.getAttribute("id") == color){a.disabled = false;}
	// abilita lo stile carattere
	if(a.getAttribute("id") == currentFont){a.disabled=false;}
	    }
  }

setActiveImageStyleSheet(getImageStyleSheet() );

var color=color.replace(/color/i,"");
var screenx=getScreen();
var imagecss=document.getElementById(screenx+"im"+color);

imagecss.setAttribute("rel","stylesheet");

}



function setActiveFontStyleSheet(font) {
 var i, a, main;

// memorizza lo stylesheet del colore prima di cambiare carattere
var currentColor=getActiveColorStyleSheet();
 for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("id").indexOf("font")!=-1) {

a.disabled = true;
     // abilita lo stile carattere
	 if(a.getAttribute("id") == font){a.disabled = false;}
	// abilita lo stile colore
	if(a.getAttribute("id") == currentColor)a.disabled=false;	}
  }

 setActiveImageStyleSheet(getImageStyleSheet() );
}


function setActiveImageStyleSheet(image) {
 var i, a, main;
 for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("id").indexOf("im")!=-1) {
      a.disabled = true;

     // abilita lo stile carattere

	 if(a.getAttribute("id") == image){a.disabled = false;}


  }
}
}



function setSimple(){
var i, a, main;
var res = getScreen();
res=res+"base";

 for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("id").indexOf("base")!=-1) {
      a.disabled = true;

	 // abilita lo stile forma
	 if(a.getAttribute("id") == res){a.disabled =false;}

  }
}

}




function getActiveColorStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("id").indexOf("color")!=-1 && !a.disabled){return a.getAttribute("id");}

}

  return null;
}

function getActiveFontStyleSheet() {
var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("id").indexOf("font")!=-1 && !a.disabled){return a.getAttribute("id");}
  }
  return null;
}

function getImageStyleSheet()
{

//alert("6");
var color= getActiveColorStyleSheet();

color= color.replace(/color/i, "");
var screen=getScreen();
var imagestyle=screen+"im"+color;
return imagestyle;

}


function getPreferredColorStyleSheet() {
return "colorautore";
var i, a;
for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
// se il link contiene style,title ma non alt ritorna lo style preferito
if(a.getAttribute("rel").indexOf("style") != -1
              && a.getAttribute("id").indexOf("color")!=-1
       ) {return "colorautore";}
  }
  return null;
}

function getPreferredFontStyleSheet() {
	
return "fontsmall";
var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
// se il link contiene style,title ma non alt ritorna lo style preferito
 if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("id").indexOf("font")!=-1
       ) {return "fontsmall";}
  }
  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) {

//alert("9");
  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 begin(){
	
var screen=getScreen();
var simplecss=document.getElementById(screen+"base");
simplecss.setAttribute("rel","stylesheet");
var color,font;
  var colorcookie= readCookie("##colorstyle");
// se non ci sono cookie si usa lo style colore di default (preferred)
if (colorcookie!=null){color=colorcookie;}
else {color=getPreferredColorStyleSheet();}
setActiveColorStyleSheet(color);
var fontcookie = readCookie("##fontstyle");
// se non ci sono cookie si usa lo style font di default (preferred)
if (fontcookie!=null){font=fontcookie;}
else {font=getPreferredFontStyleSheet();}
setActiveFontStyleSheet(font);
}






window.onunload = function(e) {
	
	var id = getActiveColorStyleSheet();
	createCookie("##colorstyle", id, 365);
  	var id = getActiveFontStyleSheet();
  	createCookie("##fontstyle", id, 365);
}






