function apriPopup(foto){	
	immagine.setAttribute('src','images/'+foto); 
	var altezza = immagine.height;

	var larghezza = immagine.width;
	window.open("popup.html","Dettaglio","width=450,height=360,location=no");
	
}
var immagine = document.createElement('img');

function caricaImmaginiIniziali(){
	var divGallery = document.getElementById('gallery_foto');
	for(i = 11; i <= 16; i++){
		var foto = document.createElement('img');		
		foto.setAttribute('src','images/miniature/esterni/foto'+i+'.jpg');
		foto.setAttribute('alt','Vedi foto '+i);
		foto.setAttribute('title','Vedi foto '+i);
		foto.setAttribute('width','80');
		foto.setAttribute('height','60');
		foto.setAttribute('border','1');
		foto.setAttribute('id','foto' + i);
		foto.onmouseover = function(){document.body.style.cursor='pointer';sostituisciFoto(this.id,'images/rollover/esterni/'+this.id + '.jpg');}
		foto.onmouseout = function(){document.body.style.cursor='default';ripristinaFoto(this.id,'images/miniature/esterni/'+this.id+'.jpg');}
		foto.onclick = function(){caricaFotoGrande(this.id + '.jpg','esterni');}
		//foto.onmouseover = function(){document.body.style.cursor = 'hand';}
		
		if(!document.getElementById('avanti')){
			var par = document.createElement('p');
			var avanti = document.createElement('input');
			avanti.setAttribute("type","button");
			//avanti.setAttribute("href","javascript:cambiaGallery('novecento')");
			avanti.onclick = function(){cambiaGallery('novecento');}
			//avanti.setAttribute("title","Vai alla gallery successiva");
			avanti.setAttribute("id","avanti");
			//avanti.style.fontSize = '14px';
			//avanti.style.textDecoration = 'none';
			avanti.setAttribute("value",">>");
			//var testoAvanti = document.createTextNode('Avanti >>');
			//avanti.appendChild(testoAvanti);
			par.appendChild(avanti);
			divGallery.appendChild(par);
		}
		divGallery.appendChild(foto);		
	}
	
	var bigImg = document.createElement('img');
	bigImg.setAttribute('src','images/grandi/esterni/foto11.jpg');
	bigImg.setAttribute('width','380');
	bigImg.setAttribute('height','285');
	bigImg.setAttribute('alt','Esterni 1');
	bigImg.setAttribute('id','big_foto');
	bigImg.onmouseover = function(){document.body.style.cursor = 'pointer';}
	bigImg.onmouseout = function(){document.body.style.cursor = 'default';} 
	bigImg.onclick = function(){apriPopup('full/esterni/foto11.jpg');}
	document.getElementById('big').appendChild(bigImg);
	
}

// AJAX
var xmlHttp = createXmlHttpRequestObject();

function createXmlHttpRequestObject(){
	var xmlHttp;
	try{
		xmlHttp = new XMLHttpRequest();
	}catch(e){
		var xmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
			"MSXML2.XMLHTTP.5.0",
			"MSXML2.XMLHTTP.4.0",
			"MSXML2.XMLHTTP.3.0",
			"MSXML2.XMLHTTP",
			"Microsoft.XMLHTTP");
		
		for(i= 0; i < xmlHttpVersions.length && !xmlHttp; i++){
			try{
				xmlHttp = new ActiveXObject(xmlHttpVersions[i]);
			}catch(e){}
		}	
	}
	if(!xmlHttp){
		alert('Errore durante la creazione dell\'oggetto XmlHttpRequest');
	}else{
		return xmlHttp;
	}
}

function caricaFotoGrande(foto,cartella){	
	var url = "ajax.php?op=foto_grande&foto=" + foto+ '&cartella=' + cartella;
	try{
		xmlHttp.open("GET",url,true);
		xmlHttp.onreadystatechange = handleFotoGrande;
		xmlHttp.send(null);
	}
	catch(e){
		alert('Errore durante il collegamento al server: ' + e.toString());
	}
	
}

function handleFotoGrande(){
	try{
		if(xmlHttp.readyState == 4){
			if(xmlHttp.status == 200){
				var XmlResponse = xmlHttp.responseXML;
				var srcBig = XmlResponse.getElementsByTagName('src')[0].firstChild.data; 
				visualizzaFotoGrande(srcBig);
			}
		}		
	}
	catch(e){
		alert('Impossibile scaricare i dati dal server: ' + e.toString());
	}

}

function visualizzaFotoGrande(source){
	document.getElementById('big_foto').src = 'images/grandi/' + source;
	document.getElementById('big_foto').onclick = function(){apriPopup('full/' + source);}	
}

function cambiaGallery(tipo){
	var url = "ajax.php?op=gallery&tipo=" + tipo;
	try{
		xmlHttp.open("GET",url,true);
		xmlHttp.onreadystatechange = handleGallery;
		xmlHttp.send(null);
	}
	catch(e){
		alert('Errore durante il collegamento al server: ' + e.toString());
	}
}

function handleGallery(){
	if(xmlHttp.readyState == 4){
		if(xmlHttp.status == 200){
			document.getElementById('gallery_foto').innerHTML = '';
			var parag = document.createElement('p');
			parag.setAttribute('id','par');
			parag.style.display = 'block';
			document.getElementById('gallery_foto').appendChild(parag);
			var XmlResponse = xmlHttp.responseXML;
			var root = XmlResponse.getElementsByTagName('response')[0];
			var cartella = root.getAttribute('tipo');
			for(i = 1; i <= root.childNodes.length; i++ ){
				var source = root.getElementsByTagName('img')[i-1].firstChild.nodeValue;
				var foto = document.createElement('img');
				foto.setAttribute('src',source);
				foto.setAttribute('alt',cartella + ' '+ i);
				foto.setAttribute('title','Vedi foto '+i);
				foto.setAttribute('width','80');
				foto.setAttribute('height','60');
				foto.setAttribute('border','1');
				foto.setAttribute('id',(i < 9)?'foto' + '1'+i:'foto'+(i+10));
				foto.onmouseover = function(){document.body.style.cursor='pointer';sostituisciFoto(this.id,'images/rollover/'+cartella+'/'+this.id + '.jpg');}
				foto.onmouseout = function(){document.body.style.cursor='default';ripristinaFoto(this.id,'images/miniature/'+cartella+'/'+this.id+'.jpg');}
				foto.onclick = function(){caricaFotoGrande(this.id + '.jpg',cartella);}
				
				if((!document.getElementById('indietro')) && (cartella != 'esterni')){
					var par = document.getElementById('par');
					var nome_gallery_indietro = '';
					if(cartella == 'novecento'){
						nome_gallery_indietro = 'esterni';
					}else if(cartella == 'minimal'){
						nome_gallery_indietro = 'novecento';
					}else if(cartella == 'particolari'){
						nome_gallery_indietro = 'minimal';
					}
					var indietro = document.createElement('input');
					indietro.setAttribute("type","button");					
					//indietro.setAttribute("href","javascript:cambiaGallery('esterni')");
					indietro.onclick = function(){cambiaGallery(nome_gallery_indietro);}					
					//indietro.setAttribute("title","Vai alla gallery precedente");
					indietro.setAttribute("id","indietro");
					//indietro.style.fontSize = '14px';
					//indietro.style.textDecoration = 'none';
					
					//var testoIndietro = document.createTextNode('<< Indietro');
					//indietro.appendChild(testoIndietro);
					indietro.setAttribute("value","<<");
					indietro.style.float = 'left';
					
					par.appendChild(indietro);
					//document.getElementById('pulsanti').appendChild('indietro');
				}
				
				if((!document.getElementById('avanti')) && (cartella != 'particolari')){
					var par = document.getElementById('par');
					var nome_gallery_avanti = '';
					if(cartella == 'esterni'){
						nome_gallery_avanti = 'novecento';
					}
					else if(cartella == 'novecento'){
						nome_gallery_avanti = 'minimal';
					}else if(cartella == 'minimal'){
						nome_gallery_avanti = 'particolari';
					}
					var avanti = document.createElement('input');
					avanti.setAttribute("type","button");
					//avanti.setAttribute("href","javascript:cambiaGallery('minimal')");
					avanti.onclick = function(){cambiaGallery(nome_gallery_avanti);}
					//avanti.setAttribute("title","Vai alla gallery successiva");
					avanti.setAttribute("id","avanti");
					//avanti.style.fontSize = '14px';
					//avanti.style.textDecoration = 'none';
					//avanti.style.marginLeft = '90px';
					//var testoAvanti = document.createTextNode('Avanti >>');
					//avanti.appendChild(testoAvanti);
					avanti.setAttribute("value",">>");
					avanti.style.float = 'left';	
					par.appendChild(avanti);		
				}
				document.getElementById('gallery_foto').appendChild(foto);		
				
				
			}
			
			// Carica l'immagine grande iniziale
			document.getElementById('big').innerHTML = '';
			var bigImg = document.createElement('img');
			bigImg.setAttribute('src','images/grandi/' + cartella + '/foto11.jpg');
			bigImg.setAttribute('width','380');
			bigImg.setAttribute('height','285');
			bigImg.setAttribute('alt',cartella + '1');
			bigImg.setAttribute('id','big_foto');
			bigImg.onmouseover = function(){document.body.style.cursor = 'pointer';}
			bigImg.onmouseout = function(){document.body.style.cursor = 'default';}
			bigImg.onclick = function(){apriPopup('full/'+cartella+'/foto11.jpg');}
			document.getElementById('big').appendChild(bigImg);	
		}
	}
	
	
}

function sostituisciFoto(id,newFoto){
	document.getElementById(id).src = newFoto;
}

function ripristinaFoto(id,percorso){	
	document.getElementById(id).src = percorso;
}



