var qtdImagensDisponiveis = 11;
var semaf = "aberto";
var tempo = 4000;
var indice_img00 = 1;
var indice_img01 = 1;
var indice_img02 = 2;
var indice_img03 = 3;
var indice_img04 = 4;
var indice_img05 = 5;
var indice_img06 = 6;
var indice_img07 = 7;
var indice_img08 = 8;
var indice_img09 = 9;

function indiceImagem(ind) {

	i = recuperaIndice();

	if (ind == 'soma') {
		if (indice_img08 < qtdImagensDisponiveis){
			if (indice_img00 != indice_img01){
				indice_img00++;
			}
			indice_img01++;
			indice_img02++;
			indice_img03++;
			indice_img04++;
			indice_img05++;
			indice_img06++;
			indice_img07++;
			indice_img08++;
			if(indice_img08 < qtdImagensDisponiveis){
				indice_img09++;
			}
		}else{
			indice_img00 = qtdImagensDisponiveis-8;
			indice_img01 = qtdImagensDisponiveis-7;
			indice_img02 = qtdImagensDisponiveis-6;
			indice_img03 = qtdImagensDisponiveis-5;
			indice_img04 = qtdImagensDisponiveis-4;
			indice_img05 = qtdImagensDisponiveis-3;
			indice_img06 = qtdImagensDisponiveis-2;
			indice_img07 = qtdImagensDisponiveis-1;
			indice_img08 = qtdImagensDisponiveis;
			indice_img09 = qtdImagensDisponiveis;
		}
	}

	if (ind == 'subtrai'){
		if (indice_img01 > 1){

			if (indice_img09 != indice_img08){
				indice_img09--;
			}

			indice_img08--;
			indice_img07--;
			indice_img06--;
			indice_img05--;
			indice_img04--;
			indice_img03--;
			indice_img02--;
			indice_img01--;
			indice_img00--;
			if (indice_img00 == 0) {
				indice_img00=1;
			}
		}
	}

	if (ind == 'reset'){
		indice_img00 = 1;
		indice_img01 = 1;
		indice_img02 = 2;
		indice_img03 = 3;
		indice_img04 = 4;
		indice_img05 = 5;
		indice_img06 = 6;
		indice_img07 = 7;
		indice_img08 = 8;
		indice_img09 = 9;
	}

	
	escreveIndice();
	
	switch(i){
		case 1:
		  atualizaIndice(indice_img01);
		  break;    
		case 2:
		  atualizaIndice(indice_img02);
		  break;
		case 3:
		  atualizaIndice(indice_img03);
		  break;
		case 4:
		  atualizaIndice(indice_img04);
		  break;
		case 5:
		  atualizaIndice(indice_img05);
		  break;
		case 6:
		  atualizaIndice(indice_img06);
		  break;	  
		case 7:
		  atualizaIndice(indice_img07);
		  break;	
		case 8:
		  atualizaIndice(indice_img08);
		  break;	
	}	
}

function escreveIndice(){
	document.getElementById("ind01").innerHTML = showFilled(indice_img01);
	document.getElementById("ind02").innerHTML = showFilled(indice_img02);
	document.getElementById("ind03").innerHTML = showFilled(indice_img03);
	document.getElementById("ind04").innerHTML = showFilled(indice_img04);
	document.getElementById("ind05").innerHTML = showFilled(indice_img05);
	document.getElementById("ind06").innerHTML = showFilled(indice_img06);
	document.getElementById("ind07").innerHTML = showFilled(indice_img07);
	document.getElementById("ind08").innerHTML = showFilled(indice_img08);
}


function showFilled(Value)
{
	return (Value > 9) ? "" + Value : "0" + Value;
}


function slidePictures(indice)
{
	if (semaf=="aberto"){

		escreveIndice();
		atualizaIndice(indice);

		if(indice < qtdImagensDisponiveis ){

			if(indice == 1){
				if(!procuraIndice(indice)){
					indiceImagem('reset');
				}
			}

			MM_swapImage('MainImage','','images/fotos/img_'+ showFilled(indice) + '.jpg',1);
			
			if(procuraIndice(indice) == 0){
				indiceImagem('soma');
			}
			atualizaIndice(indice);
			
			indice++;
			setTimeout("slidePictures("+ indice +")",tempo);

		}else{
			if(procuraIndice(indice) == 0){
				indiceImagem('soma');
			}
			atualizaIndice(indice);
			
			MM_swapImage('MainImage','','images/fotos/img_'+ showFilled(indice) + '.jpg',1);
			setTimeout("slidePictures(1)",tempo);
		}
	}
	
}


function procuraIndice(indice){

	if (document.getElementById("ind01").innerHTML == showFilled(indice) ||
	    document.getElementById("ind01").innerHTML == "<U>"+showFilled(indice)+"</U>" ||
	    document.getElementById("ind01").innerHTML == "<u>"+showFilled(indice)+"</u>"){
		return 1;
	}
	if (document.getElementById("ind02").innerHTML == showFilled(indice) ||
	    document.getElementById("ind02").innerHTML == "<U>"+showFilled(indice)+"</U>" ||
	    document.getElementById("ind02").innerHTML == "<u>"+showFilled(indice)+"</u>"){
		return 2;
	}
	if (document.getElementById("ind03").innerHTML == showFilled(indice) ||
	    document.getElementById("ind03").innerHTML == "<U>"+showFilled(indice)+"</U>" ||
	    document.getElementById("ind03").innerHTML == "<u>"+showFilled(indice)+"</u>"){
		return 3;
	}
	if (document.getElementById("ind04").innerHTML == showFilled(indice) ||
	    document.getElementById("ind04").innerHTML == "<U>"+showFilled(indice)+"</U>" ||
	    document.getElementById("ind04").innerHTML == "<u>"+showFilled(indice)+"</u>"){
		return 4;
	}
	if (document.getElementById("ind05").innerHTML == showFilled(indice) ||
	    document.getElementById("ind05").innerHTML == "<U>"+showFilled(indice)+"</U>" ||
	    document.getElementById("ind05").innerHTML == "<u>"+showFilled(indice)+"</u>"){
		return 5;
	}
	if (document.getElementById("ind06").innerHTML == showFilled(indice) ||
	    document.getElementById("ind06").innerHTML == "<U>"+showFilled(indice)+"</U>" ||
	    document.getElementById("ind06").innerHTML == "<u>"+showFilled(indice)+"</u>"){
		return 6;
	}
	if (document.getElementById("ind07").innerHTML == showFilled(indice) ||
	    document.getElementById("ind07").innerHTML == "<U>"+showFilled(indice)+"</U>" ||
	    document.getElementById("ind07").innerHTML == "<u>"+showFilled(indice)+"</u>"){
		return 7;
	}
	if (document.getElementById("ind08").innerHTML == showFilled(indice) ||
	    document.getElementById("ind08").innerHTML == "<U>"+showFilled(indice)+"</U>" ||
	    document.getElementById("ind08").innerHTML == "<u>"+showFilled(indice)+"</u>"){
		return 8;
	}
	return 0;
}


function recuperaIndice(){

	if (document.getElementById("ind01").innerHTML == "<U>"+showFilled(indice_img01)+"</U>" ||
	    document.getElementById("ind01").innerHTML == "<u>"+showFilled(indice_img01)+"</u>")
	{
		return 1;
	}
	if (document.getElementById("ind02").innerHTML == "<U>"+showFilled(indice_img02)+"</U>" ||
	    document.getElementById("ind02").innerHTML == "<u>"+showFilled(indice_img02)+"</u>")
	{
		return 2;
	}
	if (document.getElementById("ind03").innerHTML == "<U>"+showFilled(indice_img03)+"</U>" ||
	    document.getElementById("ind03").innerHTML == "<u>"+showFilled(indice_img03)+"</u>")
	{
		return 3;
	}
	if (document.getElementById("ind04").innerHTML == "<U>"+showFilled(indice_img04)+"</U>" ||
	    document.getElementById("ind04").innerHTML == "<u>"+showFilled(indice_img04)+"</u>")
	{
		return 4;
	}
	if (document.getElementById("ind05").innerHTML == "<U>"+showFilled(indice_img05)+"</U>" ||
	    document.getElementById("ind05").innerHTML == "<u>"+showFilled(indice_img05)+"</u>")
	{
		return 5;
	}
	if (document.getElementById("ind06").innerHTML == "<U>"+showFilled(indice_img06)+"</U>" ||
	    document.getElementById("ind06").innerHTML == "<u>"+showFilled(indice_img06)+"</u>")
	{
		return 6;
	}
	if (document.getElementById("ind07").innerHTML == "<U>"+showFilled(indice_img07)+"</U>" ||
	    document.getElementById("ind07").innerHTML == "<u>"+showFilled(indice_img07)+"</u>")
	{
		return 7;
	}
	if (document.getElementById("ind08").innerHTML == "<U>"+showFilled(indice_img08)+"</U>" ||
	    document.getElementById("ind08").innerHTML == "<u>"+showFilled(indice_img08)+"</u>")
	{
		return 8;
	}
	return 0;
}


function atualizaIndice(indice){
			
	if (document.getElementById("ind01").innerHTML == showFilled(indice)){
		document.getElementById("ind01").innerHTML = "<u>"+showFilled(indice)+"</u>";

		document.getElementById("ind08").innerHTML = showFilled(indice+7);
		document.getElementById("ind07").innerHTML = showFilled(indice+6);
		document.getElementById("ind06").innerHTML = showFilled(indice+5);
		document.getElementById("ind05").innerHTML = showFilled(indice+4);
		document.getElementById("ind04").innerHTML = showFilled(indice+3);
		document.getElementById("ind03").innerHTML = showFilled(indice+2);
		document.getElementById("ind02").innerHTML = showFilled(indice+1);		
	}

	if (document.getElementById("ind02").innerHTML == showFilled(indice)){
		document.getElementById("ind02").innerHTML = "<u>"+showFilled(indice)+"</u>";

		document.getElementById("ind08").innerHTML = showFilled(indice+6);
		document.getElementById("ind07").innerHTML = showFilled(indice+5);
		document.getElementById("ind06").innerHTML = showFilled(indice+4);
		document.getElementById("ind05").innerHTML = showFilled(indice+3);
		document.getElementById("ind04").innerHTML = showFilled(indice+2);
		document.getElementById("ind03").innerHTML = showFilled(indice+1);
		document.getElementById("ind01").innerHTML = showFilled(indice-1);		
	}

	if (document.getElementById("ind03").innerHTML == showFilled(indice)){
		document.getElementById("ind03").innerHTML = "<u>"+showFilled(indice)+"</u>";

		document.getElementById("ind08").innerHTML = showFilled(indice+5);
		document.getElementById("ind07").innerHTML = showFilled(indice+4);
		document.getElementById("ind06").innerHTML = showFilled(indice+3);
		document.getElementById("ind05").innerHTML = showFilled(indice+2);
		document.getElementById("ind04").innerHTML = showFilled(indice+1);
		document.getElementById("ind02").innerHTML = showFilled(indice-1);
		document.getElementById("ind01").innerHTML = showFilled(indice-2);		
	}

	if (document.getElementById("ind04").innerHTML == showFilled(indice)){
		document.getElementById("ind04").innerHTML = "<u>"+showFilled(indice)+"</u>";

		document.getElementById("ind08").innerHTML = showFilled(indice+4);
		document.getElementById("ind07").innerHTML = showFilled(indice+3);
		document.getElementById("ind06").innerHTML = showFilled(indice+2);
		document.getElementById("ind05").innerHTML = showFilled(indice+1);
		document.getElementById("ind03").innerHTML = showFilled(indice-1);
		document.getElementById("ind02").innerHTML = showFilled(indice-2);
		document.getElementById("ind01").innerHTML = showFilled(indice-3);
	}

	if (document.getElementById("ind05").innerHTML == showFilled(indice)){
		document.getElementById("ind05").innerHTML = "<u>"+showFilled(indice)+"</u>";

		document.getElementById("ind08").innerHTML = showFilled(indice+3);
		document.getElementById("ind07").innerHTML = showFilled(indice+2);
		document.getElementById("ind06").innerHTML = showFilled(indice+1);
		document.getElementById("ind04").innerHTML = showFilled(indice-1);
		document.getElementById("ind03").innerHTML = showFilled(indice-2);
		document.getElementById("ind02").innerHTML = showFilled(indice-3);
		document.getElementById("ind01").innerHTML = showFilled(indice-4);
	}

	if (document.getElementById("ind06").innerHTML == showFilled(indice)){
		document.getElementById("ind06").innerHTML = "<u>"+showFilled(indice)+"</u>";
		
		document.getElementById("ind08").innerHTML = showFilled(indice+2);
		document.getElementById("ind07").innerHTML = showFilled(indice+1);
		document.getElementById("ind05").innerHTML = showFilled(indice-1);
		document.getElementById("ind04").innerHTML = showFilled(indice-2);
		document.getElementById("ind03").innerHTML = showFilled(indice-3);
		document.getElementById("ind02").innerHTML = showFilled(indice-4);
		document.getElementById("ind01").innerHTML = showFilled(indice-5);		
	}

	if (document.getElementById("ind07").innerHTML == showFilled(indice)){
		document.getElementById("ind07").innerHTML = "<u>"+showFilled(indice)+"</u>";
	
		document.getElementById("ind08").innerHTML = showFilled(indice+1);
		document.getElementById("ind06").innerHTML = showFilled(indice-1);
		document.getElementById("ind05").innerHTML = showFilled(indice-2);
		document.getElementById("ind04").innerHTML = showFilled(indice-3);
		document.getElementById("ind03").innerHTML = showFilled(indice-4);
		document.getElementById("ind02").innerHTML = showFilled(indice-5);
		document.getElementById("ind01").innerHTML = showFilled(indice-6);
	}

	if (document.getElementById("ind08").innerHTML == showFilled(indice)){
		document.getElementById("ind08").innerHTML = "<u>"+showFilled(indice)+"</u>";
		document.getElementById("ind07").innerHTML = showFilled(indice-1);
		document.getElementById("ind06").innerHTML = showFilled(indice-2);
		document.getElementById("ind05").innerHTML = showFilled(indice-3);
		document.getElementById("ind04").innerHTML = showFilled(indice-4);
		document.getElementById("ind03").innerHTML = showFilled(indice-5);
		document.getElementById("ind02").innerHTML = showFilled(indice-6);
		document.getElementById("ind01").innerHTML = showFilled(indice-7);
	}	
}

function semaforo()
{
	if(semaf=="aberto"){
		semaf = "fechado";
		document.getElementById("semafor").innerHTML = "Continuar";
	} else {
		semaf = "aberto";
		
		switch(recuperaIndice()){
			case 1:
			  slidePictures(indice_img01);
			  break;    
			case 2:
			  slidePictures(indice_img02);
			  break;
			case 3:
			  slidePictures(indice_img03);
			  break;
			case 4:
			  slidePictures(indice_img04);
			  break;
			case 5:
			  slidePictures(indice_img05);
			  break;
			case 6:
			  slidePictures(indice_img06);
			  break;	  
			case 7:
			  slidePictures(indice_img07);
			  break;	
			case 8:
			  slidePictures(indice_img08);
			  break;	
		}
		document.getElementById("semafor").innerHTML = "Parar";
	}
}

function pararSemaforo(indice){
	semaf = "fechado";
	document.getElementById("semafor").innerHTML = "Continuar";
}


function preDownloadPictures(){
	MM_preloadImages('images/fotos/img_01.jpg','images/fotos/img_02.jpg','images/fotos/img_03.jpg','images/fotos/img_04.jpg','images/fotos/img_05.jpg','images/fotos/img_06.jpg','images/fotos/img_07.jpg','images/fotos/img_08.jpg','images/fotos/img_09.jpg','images/fotos/img_10.jpg','images/fotos/img_11.jpg','images/fotos/img_12.jpg','images/fotos/img_13.jpg');
}

