//Numero di secondi prima di aggiornare
var nsec_agg = 60;

//Aggiornamento webcam
var n = nsec_agg;
function aggFoto(img)
{
	var t = new Date();
	document.getElementById(img).src='img/webcam_w500.jpg?t='+t.getTime();
}
function aggNumSec(img, msg, ultime24)
{
	n--;
	document.getElementById(msg).innerHTML = "Aggiornamento tra " + n + " secondi...";
	if (n == 0)
	{
		//aggiornamento webcam
		aggFoto(img);
		//aggiornamento riquadro ultime 24h tramite chiamata asincrona
		if (ultime24)
			aggUltime24(ultime24);
		n = nsec_agg;
		document.getElementById(msg).innerHTML = "Aggiornamento tra " + n + " secondi...";
	}
}


//Lancia un popup immagine centrato
function PopupImg(immagine, w, h)
{
   var l = Math.floor((screen.width-w)/2);
   var t = Math.floor((screen.height-h)/2);
   finestra = window.open(immagine,"","width="+w+",height="+h+",top="+t+",left="+l);
   finestra.document.write("<html>");
   finestra.document.write("<body style='margin:0'>");
   finestra.document.write("<img src='"+immagine+"' border='0'>");
   finestra.document.write("</body>");
   finestra.document.write("</html>");
}

//Lancia un popup pagina centrato
function Popup(pagina, w, h)
{
   var l = Math.floor((screen.width-w)/2);
   var t = Math.floor((screen.height-h)/2);
   window.open(pagina,"","width="+w+",height="+h+",top="+t+",left="+l);
}

//Per AJAX
function request(url, readyFunct)
{
	//Make the request
	var http_request = null;
	if (window.XMLHttpRequest)	//modern browsers
		http_request = new XMLHttpRequest();
	else	//IE 5 and 6
		http_request = new ActiveXObject("Microsoft.XMLHTTP");
	//Prepare to receive response
	http_request.onreadystatechange = function () {
		 if ( http_request.readyState == 4 )
		 {
			  if ( http_request.status == 200 )
					readyFunct(http_request.responseText);
			  else
					readyFunct(null);
		 }
	};
	//Send request
	http_request.open("GET", url, true);
	http_request.send(null);
}

//Aggiornamento area webcam ultime 24h
function aggUltime24(ultime24)
{
	request(basedir+"/htmlultime24.php", function(respTxt) { 
			if (respTxt != null)
				document.getElementById(ultime24).innerHTML = respTxt;
		} 
	);
}
