// Velocidade do slideshow
var slideshowSpeed = 6000;


// Variável que coloca em cache as imagens que necessitamos de mostrar
// Que pode igualmente conter informação e links
var photos = [ {
		"title" : "",
		"image" : "picFundo.jpg",
		"url" : "",
		"firstline" : "",
		"secondline" : ""
	}, {
		"title" : "",
		"image" : "FundoVerao.jpg",
		"url" : "",
		"firstline" : "",
		"secondline" : ""
	}
];



$(document).ready(function() {
		
	// Navegação (anterior)
	$("#back").click(function() {
		stopAnimation();
		navigate("back");
	});
	
	// Navegação (próximo)
	$("#next").click(function() {
		stopAnimation();
		navigate("next");
	});
	
	var interval;
	$("#control").toggle(function(){
		stopAnimation();
	}, function() {
		// Faz a animação ficar em "pause"
		$(this).css({ "background-image" : "url(images/btnPause.png)" });
		
		// Mostra a próxima imagem
		navigate("next");
		
		// Faz "play" à animação
		interval = setInterval(function() {
			navigate("next");
		}, slideshowSpeed);
	});
	
	
	var activeContainer = 1;	
	var currentImg = 0;
	var animating = false;
	var navigate = function(direction) {
		// Verifica se a animação está a correr. Se sim, previne a acção
		if(animating) {
			return;
		}
		
		// Vê qual a imagem corrente que precisa de mostrar
		if(direction == "next") {
			currentImg++;
			if(currentImg == photos.length + 1) {
				currentImg = 1;
			}
		} else {
			currentImg--;
			if(currentImg == 0) {
				currentImg = photos.length;
			}
		}
		
		// Vê qual o container que precisa de utilizar
		var currentContainer = activeContainer;
		if(activeContainer == 1) {
			activeContainer = 2;
		} else {
			activeContainer = 1;
		}
		
		showImage(photos[currentImg - 1], currentContainer, activeContainer);
		
	};
	
	var currentZindex = -1;
	var showImage = function(photoObject, currentContainer, activeContainer) {
		animating = true;
		
		// Garante que o fundo está sempre por trás da restante informação
		currentZindex--;
		
		// Determina a imagem de fundo do novo container
		$("#headerimg" + activeContainer).css({
			"background-image" : "url(images/" + photoObject.image + ")",
			"display" : "block",
			"z-index" : currentZindex
		});
		
		// Esconde o Título
		$("#headertxt").css({"display" : "none"});
		
		// Determina o novo título
		$("#firstline").html(photoObject.firstline);
		$("#secondline")
			.attr("href", photoObject.url)
			.html(photoObject.secondline);
		$("#pictureduri")
			.attr("href", photoObject.url)
			.html(photoObject.title);
		
		
		// Fade out do presente container
		// Mostra o novo título quando a animação de fade out estiver completa
		$("#headerimg" + currentContainer).fadeOut(function() {
			setTimeout(function() {
				$("#headertxt").css({"display" : "block"});
				animating = false;
			}, 500);
		});
	};
	
	var stopAnimation = function() {
		// Muda a imagem do botão para "play"
		$("#control").css({ "background-image" : "url(images/btnPlay.png)" });
		
		// Limpa o intervalo??? Não sei ao certo, mas tudo indica que seja o intervalo entre animações "fade out" e "fade in" (tem de estar, se não, não corre a animação)
		clearInterval(interval);
	};
	
	// Torna estática (pause) a imagem de forma a tornar o slideshow navegável
	navigate("next");
	
	// Faz "play" da animação
	interval = setInterval(function() {
		navigate("next");
	}, slideshowSpeed);
	
});
