var img1 = new Image();
img1.src = 'icones/setaeover.jpg';
var img2 = new Image();
img2.src = 'icones/setadover.jpg';
var img4 = new Image();
img4.src = 'icones/mais.jpg';
var img5 = new Image();
img5.src = 'icones/menos.jpg';
var img6 = new Image();
img6.src = 'icones/maisover.jpg';
var img7 = new Image();
img7.src = 'icones/menosover.jpg';

var slideon = false;
var foco = 'exibe1';
var idatual = 1;
var liberaimg = false;
var liberatmp = false;
var tmpslide = 0;
var tempoi = 4000;
var tmpespera = 0;
var esperaload = 0;
var liberaprox = true;

function Amplia(id){
	Reposiciona();
	document.getElementById('exibe1').innerHTML = '';
	document.getElementById('exibe2').innerHTML = '';

	Exibe(id,1);

	document.getElementById('fundo').style.visibility = 'visible';
	document.getElementById('bordae').style.visibility = 'visible';
}

function TempoLoad(){
	document.getElementById('loader').style.visibility = 'visible';
}

function Min(id){
	var mostra = '';
	
	id = parseInt(id);
	var total = document.getElementById('total').value;
	total = parseInt(total);
	
	if(id > 2){
		var limite = 3;
	}else{
		var limite = (6 - id);
	}
	
	for(i = (id - 2); i < (id + limite); i++){
		if((i > 0) && (i <= total)){
			foto = document.getElementById('f' +i).value;
			mostra += '<div style="float: left; width: 110px; padding: 0px; height: 80px; margin-top: 5px;" align="center">';
			mostra += '<a href="javascript:Exibe(' +i+ ',1)" title="Visualizar imagem"><img src="' +dir+pasta+ 'mini/' +foto+ '" border="0" height="75"></a></div>';
		}
	}
	document.getElementById('min').innerHTML = mostra;
}

function ExibeMin(tipo){
	idatual = parseInt(idatual);
	
	if(parseInt(tipo) == 1){
		idatual = idatual - 5;
		if(idatual < 0){ idatual = 1; }
		Min(idatual);
	}else{
		var total = document.getElementById('total').value;
		total = parseInt(total);
		idatual = idatual + 5;
		if(idatual > total){ idatual = total; }
		Min(idatual);
	}	
}

function Exibe(id,pre){
	if(liberaprox == true){
		if(parseInt(pre) == 1){
			ParaSlide();
			esperaload = setTimeout('TempoLoad()',50);
		}
		var foto = document.getElementById('f' +id).value;
		document.getElementById('atual').value = id;
		idatual = id;
		document.getElementById(foco).innerHTML = '<img src="' +dir+pasta+foto+ '" border="0" onload="Fade(1)">';
		var total = document.getElementById('total').value;
		document.getElementById('info').innerHTML = id +' / '+ total;
		liberaprox = false;
		Min(id);
	}
}

function Fade(tempo){
	tempo = parseInt(tempo);
	if(tempo == 1){
		clearTimeout(esperaload);
		document.getElementById('loader').style.visibility = 'hidden';
		document.getElementById(foco).style.zIndex = 17;
	}
	if(tempo < 5){
		tempo++;
		document.getElementById(foco).className = 'fade' +tempo;
		setTimeout('Fade( '+tempo+')',40);
	}else{
		AlteraFoco();
		liberaprox = true;
		//Precarrega(); //observar ------------------------------------------------------------
	}
}

function Precarrega(){
	var id = document.getElementById('atual').value;
	var total = document.getElementById('total').value;
	id = parseInt(id);
	total = parseInt(total);
	
	if(id < total){
		id = id + 1;
	}else{
		id = 1;
	} 
	var foto = document.getElementById('f' +id).value;
	var precarrega = new Image();
	precarrega.src = dir+pasta+foto;
}

function AlteraFoco(){
	if(foco == 'exibe1'){
		document.getElementById(foco).style.zIndex = 16;
		foco = 'exibe2';
		document.getElementById(foco).style.zIndex = 15;
		document.getElementById(foco).className = 'fade1';
		document.getElementById(foco).innerHTML = '';	
	}else{
		document.getElementById(foco).style.zIndex = 16;
		foco = 'exibe1';
		document.getElementById(foco).style.zIndex = 15;
		document.getElementById(foco).className = 'fade1';
		document.getElementById(foco).innerHTML = '';
	}
}

function Proxima(tipo){
	if(liberaprox == true){
		var id = document.getElementById('atual').value;
		var total = document.getElementById('total').value;
		
		id = parseInt(id);
		total = parseInt(total);
		
		if(parseInt(tipo) == 0){
			if(id > 1){
				id = id - 1;
			}else{
				id = total;
			}
		}else{
			if(id < total){
				id = id + 1;
			}else{
				id = 1;
			} 
		}
		
		Exibe(id,1);
		liberaprox = false;
	}
}
function Reposiciona(){
	var tamy = document.body.clientHeight;
	var tamx = document.body.clientWidth;
	var topo = document.body.scrollTop;
	var sch = document.body.scrollHeight;
	var scw = document.body.scrollWidth;
	
	if(tamy > sch){ sch = tamy; }
	if(tamx > scw){ scw = tamx; }
	
	document.getElementById('fundo').style.height = sch;
	document.getElementById('fundo').style.width = scw;
	
	//janela exibição
	document.getElementById('bordae').style.left = (tamx - 550) / 2;
	document.getElementById('bordae').style.top = ((tamy - 553) / 2) + topo;	
	
}

function Rolar(){
	var tamy = document.body.clientHeight;
	if(tamy > 540){
		var topo = document.body.scrollTop;
		document.getElementById('bordae').style.top = ((tamy - 553) / 2) + topo;
	}
}

function FechaFundo(){
	ParaSlide();
	document.getElementById('fundo').style.visibility = 'hidden';
	document.getElementById('bordae').style.visibility = 'hidden';
	document.getElementById('loader').style.visibility = 'hidden';
}

function ImgOver(id){
	id.className = 'galimgover';
	id.onmouseout = function(){
		id.className = 'galimg';	
	}
}

function SetaOver(id,tipo){
	if(parseInt(tipo) == 1){
		id.src = 'icones/setadover.jpg';
		id.onmouseout = function(){
			id.src = 'icones/setad.jpg';	
		}
	}else{
		id.src = 'icones/setaeover.jpg';
		id.onmouseout = function(){
			id.src = 'icones/setae.jpg';	
		}
	}
}

function TempoOver(id,tipo){
	if(parseInt(tipo) == 1){
		id.src = 'icones/maisover.jpg';
		id.onmouseout = function(){
			id.src = 'icones/mais.jpg';	
		}
	}else{
		id.src = 'icones/menosover.jpg';
		id.onmouseout = function(){
			id.src = 'icones/menos.jpg';	
		}
	}
}

function Teclap(e){
  var tecla = 0;
  if(window.event) // IE
    tecla = window.event.keyCode;
  else if(e.keyCode) // Netscape/Firefox/Opera
    tecla = e.keyCode;

	if(document.getElementById('fundo').style.visibility == 'visible'){
		if(tecla == 37){
			Proxima(0);
		}else if(tecla == 39){
			Proxima(1);
		}
	}
}
// Slide -------------------
function IniciaSlide(){
	if(slideon == false){
		document.getElementById('linkslide').innerHTML = 'Parar';
		document.getElementById('linkslide').title = 'Parar apresentação';
		slideon = true;
		tmpslide = setTimeout('EsperaTempo()',tempoi);
		EsperaImg();
	}else{
		ParaSlide();
	}
}

function Espera(){
	if((liberatmp == false) || (liberaimg == false)){
		tmpespera = setTimeout('Espera()',50);
	}else{
		liberatmp = false;
		liberaimg = false;
		
		var id = document.getElementById('atual').value;
		var total = document.getElementById('total').value;
		id = parseInt(id);
		total = parseInt(total);
		
		if(id < total){
			id = id + 1;
		}else{
			id = 1;
		} 
		
		Exibe(id,0);
		tmpslide = setTimeout('EsperaTempo()',tempoi);
		EsperaImg();	
	}
}

function EsperaTempo(){
	liberatmp = true;
	Espera();	
}

function LiberaImg(){
	liberaimg = true;
}

function EsperaImg(){
	var id = document.getElementById('atual').value;
	var total = document.getElementById('total').value;
	id = parseInt(id);
	total = parseInt(total);
	
	if(id < total){
		id = id + 1;
	}else{
		id = 1;
	} 
		
	var foto = document.getElementById('f' +id).value;
	document.getElementById('precarrega').innerHTML = '<img src="' +dir+pasta+foto+ '" border="0" height="1" width="1" onload="LiberaImg()">';
}

function ParaSlide(){
	clearTimeout(tmpslide);
	clearTimeout(tmpespera);
	slideon = false;
	liberatmp = false;
	liberaimg = false;
	document.getElementById('linkslide').innerHTML = 'Slide';
	document.getElementById('linkslide').title = 'Iniciar apresentação em slides';
}

window.onresize = Reposiciona;
window.onscroll = Rolar;
document.onkeydown = Teclap;
