var idPagina = "destraCopertina";
var idBoxContenuti = "boxContenuti";
var idContenuti = "contenuti";
var myRequest = null;
var daHome=true;

/*window.onresize = resize;

function resize(){setBody();}*/

function CreateXmlHttpReq(handler) {
  //casini vari per renderlo crossbrowser
  var xmlhttp = null;
  try {
    xmlhttp = new XMLHttpRequest();
  } catch(e) {
    try {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
  xmlhttp.onreadystatechange = setContenuto;
  return xmlhttp;

}

function setContenuto() {
    if (myRequest.readyState == 4 && myRequest.status == 200) {
        document.getElementById(idContenuti).innerHTML = myRequest.responseText;
    }
}

function getContenuto(nomepagina) {
    myRequest = CreateXmlHttpReq(setContenuto);
    //myRequest.open("GET",nomepagina);
    myRequest.open("GET",nomepagina+"?rand="+escape(Math.random()));
    myRequest.send(null);
}



function setBody(){
	document.getElementById("pageBody").style.width = document.body.offsetWidth+"px";
	document.getElementById("pageBody").style.height = document.body.offsetHeight+"px";
	if(window.screen.width/window.screen.height > 1.4){
		//16/9		
		document.getElementById("corpo").style.width = eval(window.screen.width/100*72)+"px";
		document.getElementById("corpo").style.marginLeft = "-"+eval(window.screen.width/100*36)+"px";
	}else{
		//4/3
		document.getElementById("corpo").style.width = window.screen.width/100*92+"px";
		document.getElementById("corpo").style.marginLeft = "-"+window.screen.width/100*46+"px";
	}
	//alert(document.getElementById("corpo").style.width);
}

function cambiaPagina(nomepagina,daHomeAInterna){
	document.getElementById(idContenuti).innerHTML = "";
	if(daHomeAInterna){
		document.getElementById("box").style.width="98%";	
		var valueFade = 0;
		obj = document.getElementById(idBoxContenuti);
		obj.style.opacity = valueFade/10;
		obj.style.filter = 'alpha(opacity=' + valueFade*10 + ')';
		document.getElementById(idBoxContenuti).style.background = "#ffffff";
		setTimeout("fadeIn("+valueFade+",0.3,100)",10);
		setTimeout("cambioPaginaGrafico(2.0,0.4)",50);
		getContenuto(nomepagina);
		daHome = false;
	}else{
		nomepagina = "home.php";
		document.getElementById("box").style.width="98%";	
		var valueFade = 10;
		obj = document.getElementById(idBoxContenuti);
		obj.style.opacity = valueFade/10;
		obj.style.filter = 'alpha(opacity=' + valueFade*10 + ')';		
		setTimeout("fadeOut('"+nomepagina+"',"+valueFade+",0.4,100)",30);
		setTimeout("cambioPaginaGrafico(2.0,0.4)",50);
		daHome = true;		
	}
	
}

function cambioPaginaGrafico(value,offset){
	if(value > 2)
		return false;
	if(value <= 0)
		offset = offset*-1;
	value = value - offset;
	document.getElementById("box").style.marginLeft = value +"%";
	setTimeout("cambioPaginaGrafico("+value+","+offset+")",100);
}

function fadeIn(value,passo,tempo){
	if(value > 10 || value < 0)
		return false;
	value = value + passo;
	obj = document.getElementById(idBoxContenuti);
	obj.style.opacity = value/10;
	obj.style.filter = 'alpha(opacity=' + value*10 + ')';
	setTimeout("fadeIn("+value+","+passo+")",tempo+(tempo*2));
}

function fadeOut(nomepagina,value,passo,tempo){
	if(value > 10 || value < 0){
		obj.style.opacity = 10;
		obj.style.filter = 'alpha(opacity=100)';
		document.getElementById(idBoxContenuti).style.background = "#6eb2fb";
		getContenuto(nomepagina);
		return false;
	}
	value = value - passo;
	obj = document.getElementById(idBoxContenuti);
	obj.style.opacity = value/10;
	obj.style.filter = 'alpha(opacity=' + value*10 + ')';
	setTimeout("fadeOut('"+nomepagina+"',"+value+","+passo+")",tempo+(tempo*2));
}

function visualizzaScheda(nScheda){
	var scheda = document.getElementById("scheda"+nScheda);
	scheda.className = scheda.className=="schedaChiusa"?"schedaAperta":"schedaChiusa";
}
