// Codice per utilizzare le funzionalitą AJAX
var xmlhttp=false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
 try {
  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 } catch (e) {
  try {
   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (E) {
   xmlhttp = false;
  }
 }
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	try {
		xmlhttp = new XMLHttpRequest();
	} catch (e) {
		xmlhttp=false;
	}
}
if (!xmlhttp && window.createRequest) {
	try {
		xmlhttp = window.createRequest();
	} catch (e) {
		xmlhttp=false;
	}
}

window.addEventListener?window.addEventListener('load',so_init,false):window.attachEvent('onload',so_init);

var d=document, imgs = new Array(), zInterval = null, current=0, zoomcurrent="", pause=false, Loop, xOpacity, hide;
var img, img1 = null, stop = 0, zoom = null;

function so_init()
{
	if(!d.getElementById || !d.createElement)return;

	imgs = d.getElementById('miniature').getElementsByTagName('a');
	current = 0;
	if (imgs[current]) {
		zoomcurrent = 'zoom' + imgs[current].id.substring(3);
		imgs[current].style.background = '#08316F';

		xOpacity = .99;
		hide = 1;
		img = document.getElementById('ospitalita_img');
		stop = 1;
	
		start_animation();
	}
}


function so_xfade()
{

	if (hide == 1) {

		xOpacity-=.05;

		if(xOpacity<=0)
		{
			img.src = img1;
			img1 = null;
			hide = 0;
			xOpacity = 0;
			Loop = setTimeout(so_xfade,50);
		}
		else
		{	
//			var loader = document.getElementById('switch_img');
//			loader.src = '/i/blank.gif';
			if (img1 == null) {
				nextfoto(imgs[current].id);
				if (stop == 0) {
					Loop = setTimeout(so_xfade,500);
				}
			} else
				if (stop == 0) {
					Loop = setTimeout(so_xfade,50);
				}
		}
		if (img1 != null) {
			setOpacity(img, xOpacity);
		}
	
	} else {
		
		xOpacity+=.05;
	
		if(xOpacity>=.99)
		{
			hide = 1;
			xOpacity = .99;
			if (stop == 0) {			
				Loop = setTimeout(so_xfade,2000);
			}
		}
		else
		{
			if ((current - 1) >= 0)
				imgs[current-1].style.background = 'none';
			else
				imgs[imgs.length - 1].style.background = 'none';
			imgs[current].style.background = '#08316F';
			zoomcurrent = 'zoom' + imgs[current].id.substring(3);

			if (stop == 0) {
				Loop = setTimeout(so_xfade,50);
			}
		}

		setOpacity(img, xOpacity);
	}
	
	
	function setOpacity(obj, opacity)
	{
		if(opacity>.99)
		{
			opacity = .99;
			return;
		}

		obj.style.opacity = opacity;
		obj.style.MozOpacity = opacity;
		obj.style.filter = 'alpha(opacity=' + (opacity*100) + ')';
	}
}

function nextfoto(id) {

  for(i=0;i<imgs.length;i++) {
	//imgs[i].style.background = 'none';
	if (imgs[i].id == id) {
		current = imgs[i+1]?i+1:0;
	}
  }
  
  xmlhttp.open("GET", imgs[current].rel, true);
  xmlhttp.onreadystatechange=function() {
		switch (xmlhttp.readyState) {
		//case 0:
		//	alert('Error!');
		//break;
		case 1:
		break;
		case 2:
		break;
		case 3:
		case 4:
			//var loader = document.getElementById('switch_img');
			//loader.src = '/i/loader_small.gif';
			img1 = imgs[current].rel;
			hide = 0;
		break;
		} // end switch
	}
	xmlhttp.send(null);	

	//if ( (stop == 0) || (hide == 0) ) {
	//	Loop = setTimeout(so_xfade, 50);
	//}
}

function stop_animation() {
	clearInterval(Loop);
	var loader = document.getElementById('switch_img');
	loader.src = '/i/blank.gif';
	stop = 1;
}

function start_animation() {
	if (stop == 1) {
		var loader = document.getElementById('switch_img');
		loader.src = '/i/loader_small.gif';
		stop = 0;
		if (xOpacity != .99)
			so_xfade();
		else
			Loop = setTimeout(so_xfade, 1000);
	}
}

// Funzione per creare la lista delle miniature
function changefoto(path, id) {

	var loader = document.getElementById('switch_img');
	loader.src = '/i/loader_small.gif';
			
	var imgs = new Array();
	var img = document.getElementById('ospitalita_img');
	imgs = document.getElementById('miniature').getElementsByTagName('a');
	for(i=0;i<imgs.length;i++) {
		var anchor = imgs[i];
		anchor.style.background = 'none';
		if (imgs[i].id == id) {
			current = i;
		}
	}
	
  xmlhttp.open("GET", path, true);
  xmlhttp.onreadystatechange=function() {
		switch (xmlhttp.readyState) {
		//case 0:
		//	alert('Error!');
		//break;
		case 1:
		break;
		case 2:
		break;
		case 3:
		case 4:
			stop_animation();
			var loader = document.getElementById('switch_img');
			loader.src = '/i/blank.gif';
			img.src = path;
			var anchor = document.getElementById(id);
			anchor.style.background = '#08316F';
			zoomcurrent = 'zoom' + imgs[current].id.substring(3);
			//img1.style.display = 'visible';
		break;
		} // end switch
 };
 
 xmlhttp.send(null);
 
	xOpacity = .99;
	setOpacity(img, xOpacity);
	
	function setOpacity(obj, opacity)
	{
		if(opacity>.99)
		{
			opacity = .99;
			return;
		}

		obj.style.opacity = opacity;
		obj.style.MozOpacity = opacity;
		obj.style.filter = 'alpha(opacity=' + (opacity*100) + ')';
	}
	
}
