// JavaScript Document - Author: Guido

var IMG_MAX = 6; 		// slides number
var DIR = "img/0"; 	// images URL 
var EXT = ".jpg";		// file extention
var ITV = 2000; 		// delay in milleseconds

var images = new Array(IMG_MAX);
var frame = 0;
var timeout_id = null;
var auto = false;


function aggiornaIndice()
{
	var txt; 

	txt = 'foto ' + (frame + 1) + ' di ' + IMG_MAX;
	document.getElementById("index").firstChild.nodeValue = txt;
}

function caricaImmagini ()
{
	for (i = 0; i < IMG_MAX; i++)
	{
		images[i] = new Image();
	  	images[i].src = DIR + (i + 1) + EXT;
	}
	aggiornaIndice();
	//document.slideshow.play.focus();
}

function scorriImmagini ()
{
	frame++;
    if (frame == IMG_MAX) frame = 0;
    document.immagine.src = images[frame].src;
	aggiornaIndice();
	timeout_id = setTimeout("scorriImmagini()", ITV);
}

function avvia ()
{
	if (timeout_id == null)
	{
		auto = true;
		document.slideshow.play.value = "stop";
		scorriImmagini();
	}
	else
	{
		clearTimeout(timeout_id);
		timeout_id = null;
		document.slideshow.play.value = "start";
	}
}

function avanti (last)
{
	if (auto)
	{
    	clearTimeout(timeout_id);
		timeout_id = null;
		document.slideshow.play.value = "start";
        auto = false;
   	}

    if (last)
	{
		frame = IMG_MAX - 1;
	}
	else
	{
		frame++;
	}
	
	if (frame == IMG_MAX) frame = 0;
	document.immagine.src = images[frame].src;
	aggiornaIndice();
}

function indietro (first)
{
    if (auto)
	{
    	clearTimeout(timeout_id);
		timeout_id = null;
 		document.slideshow.play.value = "start";
        auto = false;
    }
	
	if (first)
	{
		frame = 0;
	}
	else
	{
		frame--;
	}
	
    if (frame < 0) frame = IMG_MAX - 1;
	document.immagine.src = images[frame].src;
	aggiornaIndice();

}