var slideshowSpeed = 6000;
var photos = [ {
		"title" : "Lucena Academia",
		"image" : "1.jpg",
		"url" : "#",
		"secondline" : "Seja bem-vindo ao nosso site!"
	}, {
		"title" : "Office Appartments",
		"image" : "2.jpg",
		"url" : "#",
		"secondline" : "Os melhores alunos!"
	}, {
		"title" : "Mountainbiking",
		"image" : "3.jpg",
		"url" : "#",
		"secondline" : "Os melhores aparelhos!"
	}, {
		"title" : "Mountains Landscape",
		"image" : "4.jpg",
		"url" : "#",
		"secondline" : "A melhor sala de Musculação!"
	}, {
		"title" : "Italian pizza",
		"image" : "5.jpg",
		"url" : "#",
		"secondline" : "O melhor restaurante!"
	}, {
		"title" : "Italian pizza",
		"image" : "6.jpg",
		"url" : "#",
		"secondline" : "As melhores piscinas!"
	}
];
$(document).ready(function() {
	$("#back").click(function() {
		stopAnimation();
		navigate("back");
	});
	$("#next").click(function() {
		stopAnimation();
		navigate("next");
	});
	var interval;
	$("#control").toggle(function(){
		stopAnimation();
	}, function() {
	$(this).css({ "background-image" : "url(images/btn_pause.png)" });
	navigate("next");
	interval = setInterval(function() {
			navigate("next");
		}, slideshowSpeed);
	});
	var activeContainer = 1;	
	var currentImg = 0;
	var animating = false;
	var navigate = function(direction) {
		if(animating) {
			return;
		}
		if(direction == "next") {
			currentImg++;
			if(currentImg == photos.length + 1) {
				currentImg = 1;
			}
		} else {
			currentImg--;
			if(currentImg == 0) {
				currentImg = photos.length;
			}
		}
		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;
		currentZindex--;
		$("#headerimg" + activeContainer).css({
			"background-image" : "url(images/" + photoObject.image + ")",
			"display" : "block",
			"z-index" : currentZindex
		});
		$("#headertxt").css({"display" : "none"});
		$("#firstline").html(photoObject.firstline);
		$("#secondline")
			.attr("href", photoObject.url)
			.html(photoObject.secondline);
		$("#pictureduri")
			.attr("href", photoObject.url)
			.html(photoObject.title);
		$("#headerimg" + currentContainer).fadeOut(function() {
			setTimeout(function() {
				$("#headertxt").css({"display" : "block"});
				animating = false;
			}, 500);
		});
	};
	var stopAnimation = function() {
		$("#control").css({ "background-image" : "url(images/btn_play.png)" });
	clearInterval(interval);
	};
	navigate("next");
	interval = setInterval(function() {
		navigate("next");
	}, slideshowSpeed);
});
