// JavaScript Document

// global flag
var isIE = false;
var firstTime=true

// global request and XML document objects
var req;
var xml;
var cavalloAttuale=0

// handle onreadystatechange event of req object
function processReqChange() {
	//alert('step9')
    // only if req shows "loaded"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
			xml = req.responseXML.getElementsByTagName('cavallo')
			/* FUNZIONE DA METTERE */
			popolaFoto()
         } else {
            alert("There was a problem retrieving the XML data:\n" +
                req.statusText);
         }
    }
}


// FUNZIONE PER INTERAGIRE CON IL XML
function loadXMLDoc(url) {
	//alert('step8')
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open("GET", url, true);
        req.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        isIE = true;
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChange;
            req.open("GET", url, true);
            req.send();
        }
    }
}

function popolaFoto(){
	var nome = xml[cavalloAttuale].getAttribute('nome');
	var foto = xml[cavalloAttuale].getAttribute('foto');
	document.getElementById('nome_cavallo').innerHTML=nome
	document.getElementById('foto_unica').src='immagine.php?id='+foto+'&w=301&h=185'
}

function avantiCavallo(){
	document.getElementById('foto_unica').style.visibility='hidden';
	cavalloAttuale++;
	if(cavalloAttuale==xml.length){
		//alert(cavalloAttuale)
		cavalloAttuale=0
	}
	popolaFoto()
}
function indietroCavallo(){
	document.getElementById('foto_unica').style.visibility='hidden';
	cavalloAttuale--;
	if(cavalloAttuale < 0){
		//alert(cavalloAttuale)
		cavalloAttuale=xml.length-1
	}
	popolaFoto()
}

function visualizzaFoto(){
	document.getElementById('foto_unica').style.visibility='visible';
}

function vediCavallo(){
	window.location = "cavallo.php?c="+xml[cavalloAttuale].getAttribute('id');	
}