function writeMini() {
  output = "";
  for (i=1; i<= Math.min(imgNumber, maxImages); i++) {
	if (i<10) {
		idStr = "0" + i;
	} else {
		idStr = i;
	}


    fileName = imgDir + imgPrefix + idStr + imgMiniSuffix;
    output += "<a href=\"#\" onclick=\"changeImg(" + i + ")\">";	
    output += "<img class=\"foto_mini\" id=\"mini_" + i + "\" src=\"" + fileName + "\" alt=\"\">";
	output += "</a>";
	
	if (i == maxImages && imgNumber > maxImages) { // se dopo l'ultima immagine ce ne sono ancora visualizzo la freccia
	    output += "<a href=\"#\" onclick=\"scrollImg()\">";	
	    output += "<img class=\"foto_mini\" src=\"immagini/freccina.gif\" alt=\"Next\">";
		output += "</a>";
	} else { // altrimenti un quadratino vuoto
		output += "<img class=\"spacer\" src=\"immagini/spacer.gif\">";
	}
  }

  for (i=1; i<= (maxImages - imgNumber) * 2; i++) { // blocchi vuoti per riempire
	output += "<img class=\"spacer\" src=\"immagini/spacer.gif\">";
  }
  document.write(output);
}




function changeImg(id) {
	var imgObj = document.getElementById("foto");

	if (imgObj) { // in caso non esista non fa niente

      idImg = getImageId(id);

	  if (idImg > 0) {
			if (idImg<10) {
				idStr = "0" + idImg;
			} else {
				idStr = idImg;
			}

			fileName = imgDir + imgPrefix + idStr + imgSuffix;
			imgObj.src = fileName;
	  }
	}
}



function scrollImg() {

	idImg = 5;
	newImgId = -1;

	for (i=1; i<= maxImages; i++) {
		
	var miniObj = document.getElementById("mini_" + i);
		if (miniObj) {


			idImg = getImageId(i);

			altraRiga = (idImg + maxImages - i <= imgNumber);

			if (idImg + maxImages <= imgNumber && altraRiga) {
				newImgId = idImg + maxImages;
			}

			if (idImg + maxImages > imgNumber && !altraRiga) {
				newImgId = i;
			}

			if (idImg + maxImages > imgNumber && altraRiga) {
				newImgId = -1;
			}

			if (idImg == -1) {
				newImgId = i;
			}


		
			if (newImgId < 10 && newImgId > 0) {
				newImgId = "0" + newImgId;
				newFileName = imgDir + imgPrefix + newImgId + imgMiniSuffix;
			} 
			if (newImgId == -1) {
				newFileName = "immagini/spacer.gif";
			} 
			if (newImgId >= 10) {
				newFileName = imgDir + imgPrefix + newImgId + imgMiniSuffix;
			} 


			miniObj.src = newFileName;
		
		}

	}
}


// Dato un id html ritorna il nome dell'immagine visualizzata
function getImageId(id) {

	idImg = -1;
	var miniObj = document.getElementById("mini_" + id);
	if (miniObj) {
			oldFileName = new String(miniObj.src);
			if (oldFileName.indexOf("spacer") == -1) {
				oldFileName=oldFileName.substring(oldFileName.lastIndexOf('/'),oldFileName.length);
				idImg = parseInt( oldFileName.substring(oldFileName.indexOf('_') + 1, oldFileName.length - 6), 10);
			}
	}
	return idImg;
}



direction = 1;
ALTEZZA_BOX = 535;


function scrollLoop(dir) {
	direction = dir;
	timer = setInterval("scrollLayer()", 1);
}


function stopScroll() {
	clearInterval(timer);
}

function scrollLayer() {

	if(document.getElementById){
		elemento = document.getElementById("testo");
	}
	else if (document.all) {
		elemento = document.all["testo"];
	}
	else if (document.layers) {
		elemento = document.layers["testo"];
	}

	if (isNaN(parseInt(elemento.style.top))) {
		elemento.style.top = 0;
	}

	elemento_top = parseInt(elemento.style.top);

	top_reached = elemento_top > 0;
	bottom_reached = Math.abs(elemento_top) >= ( elemento.offsetHeight - ALTEZZA_BOX);


	if (!top_reached && direction < 0) {
        elemento.style.top = parseInt(elemento.style.top) - direction;
	}

	if (!bottom_reached && direction > 0) {
        elemento.style.top = parseInt(elemento.style.top) - direction;
	}
}


