
addEvent(window,'load',addListeners);

var h_guscio, h_contenuti, h_foto, w_guscio, w_contenuti, w_foto, ml_guscio, mt_guscio, chework = 1;
var arr_lavori = new Array();
var hdante, cliccataanteprima = false;
var xmlHttp;

function createXMLHttpRequest() {
    if (window.ActiveXObject) {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    } 
    else if (window.XMLHttpRequest) {
        xmlHttp = new XMLHttpRequest();
    }
}


onload=function(){
if(!document.getElementsByTagName) return;
l=document.getElementsByTagName("a");
for(i=0;i<l.length;i++){
  if(l[i].className.indexOf("aut")!=-1){
    l[i].title="richiesta autorizzazione";
    l[i].onclick=function(){startRequestAut(this.id)};
    }
  else if(l[i].className.indexOf("est")!=-1){
    l[i].title="link esterno, si apre in una nuova finestra";
    l[i].onclick=function(){window.open(this.href);return(false)};
    }
  }
 /*
if(!document.getElementsByClassName) return;
s=document.getElementsByClassName("anteprima");
for(j=0;j<s.length;j++){
	s[j].onclick=function(){chework=j+1;};
 }
*/  
  
  	//setupZoom(); // abilita lo zoom delle immagini
  
	//Effect.toggle('indirizzi', 'slide', { duration: 0.5 }); 
	
	//reimposta le dimensione a seconda dell'altezza dello screen del browser (parte visibile)
	
	h_guscio = parseInt((getBrowserHeight() * 0.9)); // altezza del guscio
	h_contenuti = (h_guscio - 30); // altezza contenuti
	h_foto = (h_contenuti / 2); // altezza foto
	w_guscio = ((h_guscio / 2) * 3); // larghezza guscio
	w_contenuti = w_guscio; // larghezza contenuti
	w_foto = (w_contenuti / 2); // larghezza foto
	ml_guscio = -(w_guscio / 2); // margin-left guscio per centrarlo nello schermo
	mt_guscio = -(h_guscio / 2); // margin-top guscio per centrarlo nello schermo
	adatta_dimensioni();

	appari = setTimeout("appari_guscio()", 1000);

	chework = 1;
		
}
function appari_guscio() {
    new Effect.Appear('guscio', {duration:5});
}
function appari_anteprime() {
    Effect.toggle('anteprime', 'slide', { duration: 0.5 }); 
}

function adatta_dimensioni() {
	document.getElementById('guscio').style.height = h_guscio + 'px';
	document.getElementById('guscio').style.marginLeft = ml_guscio + 'px';
	document.getElementById('guscio').style.marginTop = mt_guscio + 'px';
	document.getElementById('contenuti').style.height = h_contenuti + 'px';
	document.getElementById('fotoa').style.height = h_foto + 'px';
	document.getElementById('fotob').style.height = h_foto + 'px';
	document.getElementById('guscio').style.width = w_guscio + 'px';
	document.getElementById('contenuti').style.width = w_contenuti + 'px';
	document.getElementById('menubasso').style.width = w_contenuti + 'px';
	document.getElementById('fotoa').style.width = w_foto + 'px';
	document.getElementById('fotob').style.width = w_foto + 'px';
	document.getElementById('no').style.width = w_foto + 'px';
	document.getElementById('ne').style.width = w_foto + 'px';
	document.getElementById('so').style.width = w_foto + 'px';
	document.getElementById('se').style.width = w_foto + 'px';
	document.getElementById('no').style.height = h_foto + 'px';
	document.getElementById('ne').style.height = h_foto + 'px';
	document.getElementById('so').style.height = h_foto + 'px';
	document.getElementById('se').style.height = h_foto + 'px';
	if(h_guscio<500) {
		document.getElementById('so').style.fontSize = '8px';
		document.getElementById('ne').style.fontSize = '8px';
		document.getElementById('ne').style.lineHeight = '20px';
		document.getElementById('so').style.lineHeight = '20px';
		document.getElementById('centrato1').style.top = ((h_foto/2)-((20*3)/2))  + 'px';
		document.getElementById('centrato2').style.top = ((h_foto/2)-((20*3)/2))  + 'px';
	}
	else {
		document.getElementById('ne').style.fontSize = '11px';
		document.getElementById('so').style.fontSize = '11px';
		document.getElementById('ne').style.lineHeight = '15px';
		document.getElementById('so').style.lineHeight = '15px';	
		document.getElementById('centrato1').style.top = ((h_foto/2)-((15*3)/2))  + 'px';	
		document.getElementById('centrato2').style.top = ((h_foto/2)-((15*3)/2))  + 'px';			
	}
}
function getBrowserHeight ( ) {
    if ( window.innerHeight ) { return window.innerHeight; }
    else if ( document.documentElement && document.documentElement.clientHeight != 0 ) { return document.documentElement.clientHeight; }
    else if ( document.body ) { return document.body.clientHeight; }
    return 0;
}

function precaricaimmagini(){
	if (document.images){
		var filediimmagini = precaricaimmagini.arguments;
		var matriceprecaricate = new Array();
		for (var i=0; i<filediimmagini.length; i++){
			matriceprecaricate[i] = new Image;
			matriceprecaricate[i].src = filediimmagini[i];
		}
	}
}

//Events
function addEvent(elm, evType, fn) {
	// cross-browser event handling
	if(elm.addEventListener) {
		elm.addEventListener(evType, fn, false);
		return true;
	}
	else if(elm.attachEvent) {
		var r = elm.attachEvent('on'+evType,fn);
		return r;
	}
	else {
		elm['on'+evType] = fn
	}
}
function addListeners(e) {
	var arr_anteprime = new Array();
	
	for(j=1;j < arr_lavori.length; j++) {
		arr_anteprime[j] = document.getElementById('thumb'+j);
		//addEvent(arr_anteprime[i],'mousedown', function() {chework = i});
		//addEvent(arr_anteprime[j],'click', function() {clickanteprima(j)});
	}
	if(arr_lavori.length - 1 > 0)
		addEvent(arr_anteprime[1],'click', function() {clickanteprima(1)});
	if(arr_lavori.length - 1 > 1)
		addEvent(arr_anteprime[2],'click', function() {clickanteprima(2)});
	if(arr_lavori.length - 1 > 2)
		addEvent(arr_anteprime[3],'click', function() {clickanteprima(3)});
	if(arr_lavori.length - 1 > 3)
		addEvent(arr_anteprime[4],'click', function() {clickanteprima(4)});
	if(arr_lavori.length - 1 > 4)
		addEvent(arr_anteprime[5],'click', function() {clickanteprima(5)});
	if(arr_lavori.length - 1 > 5)
		addEvent(arr_anteprime[6],'click', function() {clickanteprima(6)});
	if(arr_lavori.length - 1 > 6) 
		addEvent(arr_anteprime[7],'click', function() {clickanteprima(7)});
	if(arr_lavori.length - 1 > 7)
		addEvent(arr_anteprime[8],'click', function() {clickanteprima(8)});
		

}
function nascondianteprime() {
	if(cliccataanteprima === false) {
	hdante = setTimeout("nascondileanteprime()", 100); 	
	}
	cliccataanteprima = false;
}
function nascondileanteprime() {
	Effect.toggle('anteprime', 'slide', { duration: 0.5 }); 
}
function mostraanteprime() {
	clearTimeout(hdante);
	$('anteprime').show; 
}
function togli_timeout() {
	clearTimeout(hdante);
}
function clickanteprima(quale) {
	chework = quale;
	cliccataanteprima = true;
	Effect.toggle('anteprime', 'slide', { duration: 0.5 }); 
	cambiawork();
	//startRequest(chework);
}

function cambiawork(who) {
	
	if(who == '+') {
		chework = chework + 1;
	}
	else if (who == '-') {
		chework = chework - 1;
	}
	if(chework == arr_lavori.length) {
		//document.getElementById("cmdavanti").innerHTML = '<img src="../img/cmd_dx.jpg" alt="avanti" />';
		chework = 1;
	}
	else {
		//document.getElementById("cmdavanti").innerHTML = '<a class="comandi" onclick="cambiawork(\'+\')"><img src="../img/cmd_dx.jpg" alt="avanti" /></a>';		
	}
	if(chework == 0) {
		//document.getElementById("cmdindietro").innerHTML = '<img src="../img/cmd_sx.jpg" alt="indietro" />';
		chework = 8;
	}
	else {
		//document.getElementById("cmdindietro").innerHTML = '<a class="comandi" onclick="cambiawork(\'-\')"><img src="../img/cmd_sx.jpg" alt="indietro" /></a>';
	}
	startRequest(chework);
}

function startRequest(quale) {
    createXMLHttpRequest();
    xmlHttp.onreadystatechange = handleStateChange;
    xmlHttp.open("GET", "../esterni/cambia_work.php?id=" + quale, true);
    xmlHttp.send(null);
}
    
function handleStateChange() {
	
    if(xmlHttp.readyState == 4) {
        if(xmlHttp.status == 200) {			
			if (!(xmlHttp.responseText)) {
				alert("Si è verificato un problema");				
			}
			else {
				verifica = xmlHttp.responseText;
				if (verifica) {			
					new Effect.Opacity('contenuti', { from: 1.0, to: 0, duration: 2 });
					appari = setTimeout("appari_contenuti(verifica)", 2000);					
				}
				else {
					document.location.href = 'work/';
				}
			}        
        }
    }
}
function appari_contenuti(html) {
	document.getElementById("contenuti").innerHTML = html;
	adatta_dimensioni();
	new Effect.Opacity('contenuti', { from: 0, to: 1, duration: 2 });
	//setupZoom();
}

function zoom_immagine(img) {
	//document.getElementById("contenuti").style.display = 'none';
	//document.getElementById("menubasso").style.display = 'none';
	new Effect.Opacity('contenuti', { from: 1, to: 0, duration: 0.7 });
	new Effect.Opacity('menubasso', { from: 1, to: 0, duration: 0.7 });
	document.getElementById("zoom").style.width = w_guscio;
	document.getElementById("zoom").style.height = h_guscio;
	//document.getElementById("zoom").innerHTML = '<img src="http://www.malbos-bonsignore.com/img/wait.gif" alt="zoom" />';
	document.getElementById("zoom").innerHTML = '<a href="javascript:void(0)" title="back to works" onclick="hide_immagine()"><img src="http://www.malbos-bonsignore.com/img/' + img + '" alt="zoom" width="'+w_guscio+'" height="'+h_guscio+'" /></a>';
	appari = setTimeout("appari_zoom_immagine('"+img+"')", 700);
}

function appari_zoom_immagine(img) {
	document.getElementById("zoom").style.display = 'block';
	
}

function hide_immagine(img) {
	document.getElementById("zoom").style.display = 'none';
	//document.getElementById("contenuti").style.display = 'block';
	//document.getElementById("menubasso").style.display = 'block';
	new Effect.Opacity('contenuti', { from: 0, to: 1, duration: 0.1 });
	new Effect.Opacity('menubasso', { from: 0, to: 1, duration: 0.1 });
}
