// JavaScript Document



function templateDefault()
	{
		//alert(readCookie('myTemplate'));
		//alert(readCookie('myFontSize'));
		
		
		//start chiamata defaul nel body
		if(!readCookie('myTemplate'))
		{
			writeCookie("myTemplate", "black", 24);
			templateChangeCookie("black")
		}
		else
		{
			templateChangeCookie(readCookie('myTemplate'))
		}
		
		
		//start chiamata defaul nel body
		if(!readCookie('myFontSize'))
		{
			writeCookie("myFontSize", "small", 24);
			templateChangeCookie2("small")
		}
		else
		{
			templateChangeCookie2(readCookie('myFontSize'))
		}	
		
	}

function templateChange(mySelectTemplate)
{
		//cambio e scrivo cookie
		templateChangeCookie(mySelectTemplate);
		writeCookie("myTemplate", mySelectTemplate, 24);
		
	}

function templateSizeChange(mySizeTemplate)
	{
		
		//cambio e scrivo cookie
		templateChangeCookie2(mySizeTemplate);
		writeCookie("myFontSize", mySizeTemplate, 24);
		
		//templateChangeCookie(readCookie('myTemplate'))
		
	}


// La funzione per cambiare i fogli di stile
function templateChangeCookie(){
	
	//controllo browser
	if(!document.styleSheets){
		var ss = getAllSheets() //Opera
	}else{
		var ss = document.styleSheets; //Dom
	}
	
	numStyles = $("link[rel='STYLESHEET']").length;
	
	for( var x = 0; x < numStyles; x++ ) {
	
		pippo = $(ss[x]);
		
		urlCss = $(pippo).attr('href');
		if((urlCss.indexOf('white')>0) || (urlCss.indexOf('black')>0)) {
			$(pippo).attr('disabled',true);
		}
		for( var y = 0; y < arguments.length; y++ ) {
			//controlla ogni titolo ...
			if((urlCss.indexOf(arguments[y].toLowerCase())>0)){
				//e riabilita il foglio di stile se ha il titolo scelto
				$(pippo).attr('disabled',false);
			}
		}
		
	
	// disabilita tutti i fogli di stile con un titolo 
	// tranne quello passato per argomento alla funzione
	/*for( var x = 0; x < ss.length; x++ ) {
		if( ss[x].title == 'Stile1' || ss[x].title == 'Default' ) {
			ss[x].disabled=true;
		}
		for( var y = 0; y < arguments.length; y++ ) {
			//controlla ogni titolo ...
			if(ss[x].title == arguments[y]){
				//e riabilita il foglio di stile se ha il titolo scelto
				ss[x].disabled=false;
				
				
			}
		}*/
	}
	if( !ss.length ) { 
		alert( 'Il tuo browser non è abilitato a cambiare i fogli di stile CSS' );
	}
}


// La funzione per cambiare i fogli di stile (solo size font)
function templateChangeCookie2(){
	
	//controllo browser
	if(!document.styleSheets){
		var ss = getAllSheets() //Opera
		
	}else{
		var ss = document.styleSheets; //Dom
		
	}
	
	numStyles = $("link[rel='STYLESHEET']").length;

	// disabilita tutti i fogli di stile con un titolo 
	// tranne quello passato per argomento alla funzione
	//alert(arguments[arguments.length-1])
	
	for( var x = 0; x < numStyles; x++ ) {
	
		pippo = $(ss[x]);
		urlCss = $(pippo).attr('href');
		
		if((urlCss.indexOf('small')>0) || (urlCss.indexOf('medium')>0) || (urlCss.indexOf('large')>0)) {
			$(pippo).attr('disabled',true);
		}
		
		for( var y = 0; y < arguments.length; y++ ) {
			//controlla ogni titolo ...
			if((urlCss.indexOf(arguments[y].toLowerCase())>0)){
				//e riabilita il foglio di stile se ha il titolo scelto
				$(pippo).attr('disabled',false);
			}
		}
		
		
		
		
		
		/*
		if(ss[x].title == 'Small' || ss[x].title == 'Medium' || ss[x].title == 'Large' ) {
			ss[x].disabled=true;
		}
		for( var y = 0; y < arguments.length; y++ ) {
			//controlla ogni titolo ...
			if(ss[x].title == arguments[y]){
				//e riabilita il foglio di stile se ha il titolo scelto
				ss[x].disabled=false;
			
			}
		}*/
	}
	if( !ss.length ) { 
		alert( 'Il tuo browser non è abilitato a cambiare i fogli di stile CSS' );
	}
}


// ---------------------------------------------------------------------------------

// Funzione per Opera
function getAllSheets(){
	if( document.getElementsByTagName ) {
		var Lt = document.getElementsByTagName('LINK');
		var St = document.getElementsByTagName('STYLE');
	} else {
		// browser minori - restituisce array vuoto
		return []; 
	}
	//per tutti i tag link ...
	for( var x = 0, os = []; Lt[x]; x++ ) {
		//controlla l'attributo rel per vedere se contiene 'style'
		if( Lt[x].rel ) {
			var rel = Lt[x].rel;
		} else if( Lt[x].getAttribute ) {
			var rel = Lt[x].getAttribute('rel');
		} else {
			var rel = '';
		}
		if(typeof(rel)=='string'&&rel.toLowerCase().indexOf('style')+1){
			//riempe la variabile os con i stylesheets linkati
			os[os.length] = Lt[x];
		}
	}
	//include anche tutti i tags style e restituisce l'array
	for( var x = 0; St[x]; x++ ) {
		os[os.length] = St[x];
	}
	return os;
}

// ---------------------------------------------------------------------------------

// Funzione per leggere il cookie
// es. alert( readCookie("myTemplate") );
function readCookie(name)
{
	var cookieValue = "";
  var search = name + "=";
  if(document.cookie.length > 0)
  { 
    offset = document.cookie.indexOf(search);
    if (offset != -1)
    { 
      offset += search.length;
      end = document.cookie.indexOf(";", offset);
      if (end == -1) end = document.cookie.length;
      cookieValue = unescape(document.cookie.substring(offset, end))
    }
  }
  return cookieValue;
}

// writeCookie("myCookie", "my name", 24);
// Stores the string "my name" in the cookie "myCookie" which expires after 24 hours.
function writeCookie(name, value, hours)
{
  var expire = "";
  if(hours != null)
  {
    expire = new Date((new Date()).getTime() + hours * 3600000);
    expire = "; expires=" + expire.toGMTString();
  }
  document.cookie = name + "=" + escape(value) + expire +'; path=/';	
		
}


