var cookieFontSizeName = "WP4_FontSize";

ok = (document.styleSheets) ? 1 : 0;

IE = ((ind1 = navigator.appVersion.indexOf("MSIE")) > -1) ? 1 : 0;
IE4 = (IE && document.all) ? 1:0;

NN = (navigator.appName.indexOf("Netscape")>-1) ? 1 : 0;
NN4 = (NN && document.layers) ? 1 : 0;
NN6 = (NN && document.getElementById) ? 1:0;

var SAFARI = ((window.navigator.userAgent).indexOf("Safari") > -1) ? 1 : 0;

var stile='';
var cssCaricati = '';
if(ok) {
  cssCaricati = document.styleSheets;
} 

//per tener traccia di quante volte si è incrementato/diminuito il carattere
var MAXcounter = 4;
var MINcounter = -2;
var step = 0.1;
var counter = 0;


function _font_setSize(newsize) {

	// change the style of the 'HTML' top tag
	document.documentElement.style.fontSize = newsize;


// old style, browser-dependent
//	if (document.all)
//		document.all.body.style.fontSize = newsize;
//	else
//		document.getElementById('body').style.fontSize = newsize;

}

function aumenta(comandoDaUtente) {
	if(ok) {
		
		var precounter = counter;	
								
		if(comandoDaUtente){
			//alert("comando venuto dall'utente");
			counter++;
		}
	
		if(counter > MAXcounter){
			//alert(precounter + " > "+ counter+"\nimpossibile aumentare ulteriormente\nla grandezza dei caratteri!");
			alert("impossibile aumentare ulteriormente\nla grandezza dei caratteri!");
			counter=MAXcounter;
			createCookie(cookieFontSizeName, counter, 31);
			
		}else{

			var newsize = ""+((1+step*counter)*100)+"%";

			_font_setSize(newsize);
				
		}//end elseif (counter<=MAXcounter)
		
		if(comandoDaUtente){
			createCookie(cookieFontSizeName, counter, 31);
		}

	} else {
		alert("l'oggetto styleSheets\nNON\nè supportato");
	}
}






function diminuisci(comandoDaUtente) {
	if(ok) {
		
			var precounter = counter;
		
			if(comandoDaUtente){
				//da '1' pare passare direttamente a '-1', quindi...
				if(counter==1){
					counter=0;
				}else{
					counter--;
				}
			}
		
			if(counter < MINcounter){
				//alert(counter + " < " + MINcounter + "\nimpossibile diminuire ulteriormente\nla grandezza dei caratteri!");
				alert("impossibile diminuire ulteriormente\nla grandezza dei caratteri!");
				counter = MINcounter;
				createCookie(cookieFontSizeName, counter, 31);
				
			}else{

				var newsize = ""+((1+step*counter)*100)+"%";
	
				_font_setSize(newsize);
						
						
				if(comandoDaUtente){
				  createCookie(cookieFontSizeName, counter, 31);
				}
			}
	}else{
	  alert("l'oggetto styleSheets\nNON\nè supportato")
	}	  
}




function normalizza(){
	if(ok) {
			
			var norma ='';

			if(counter>0){
				//se i caratteri erano stati ingranditi
				//bisogna rimpicciolirli
				norma = "diminuzione: ";
				var valoreAssoluto = Math.abs(counter);
				var i=0;
				for(i=0; i < valoreAssoluto; i++){
					//counter--;
					norma += ' <- ' + i;
					diminuisci(true);
				}

			}else if (counter < 0){
				//se è stato rimpicciolito il carattere, 
				//bisogna ingrandirlo N volte
				norma = "aumentare: counter["+counter+"] ";
				var valoreAssoluto = Math.abs(counter);
				var i=0;
				for(i = 0; i<valoreAssoluto; i++){
					norma += ' -> ' + i ;

					aumenta(true);
				}

			}

		 //createCookie(counter, 31);

	} else {
		alert("l'oggetto styleSheets\nNON\nè supportato");
	}
	
}



function increase(){
	var x=0;
	if(counter>0){
		
		for(x=1; x<=counter; x++){
			aumenta(false);
		}
	}
}



function decrease(){
	var valoreAssoluto = Math.abs(counter);
	var x=0;
	for(x=0; x < valoreAssoluto; x++){
		diminuisci(false);
	}
}


function showCookieCounter(){
//  window.status= counter;
}

function setCookieCounter(x){
	counter = x;
}

//legge il cookie, se presente
function readFontSizeCookie() {
	//try{
		cookies=document.cookie;
		posStile=cookies.indexOf(cookieFontSizeName+"="); 
		var value=0;
		if (posStile > -1) {
			
			fineStile = posStile+ (cookieFontSizeName.length + 1); 
			//+1, perché bisogna considerare anche l'uguale ('=')
		
		
			//prendiamo il valore specificato nel cookie
			//: +2: segno e valore (-1, 
			value = cookies.slice(fineStile,fineStile+1); 

			if(value == '-'){
				value = cookies.slice(fineStile,fineStile+2); 
			}
				
			if(value < 0){
				var stringValue = ""+value;

				setCookieCounter(value);
				decrease();

			}else if(value > 0){
				setCookieCounter(value);
				increase();

			}else{
				value=0;
				setCookieCounter(value);
				//normalizza();
			}
			
			
			
			
		}else{
			value=0;
			//alert("problems during cookie elaboration");
			setCookieCounter(value);
		}
		
		
		
	//}catch(e){
		//alert("eccezione\n" +e);
	//}
	
}


// esegui immediatamente per allineare la dimensione a quella registrata
readFontSizeCookie();
