/*
* Author:      Marco Kuiper (http://www.marcofolio.net/)
*/

var slideshowSpeed = 8000;

var photos = [ {
		"title" : "",
		"image" : "slide1.jpg",
		"url" : "",
		"firstline" : "",
		"secondline" : ""
	}, {
		"title" : "",
		"image" : "slide2.jpg",
		"url" : "",
		"firstline" : "",
		"secondline" : ""
	}
];



$(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(theme/default/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(theme/default/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(3000, function() {
			setTimeout(function() {
				$("#headertxt").css({ "display": "block" });
				animating = false;
			});
		});
	};
	var stopAnimation = function() {
		$("#control").css({ "background-image" : "url(theme/default/images/btn_play.png)" });
		clearInterval(interval);
	};
	navigate("next");
	interval = setInterval(function() {
		navigate("next");
	}, slideshowSpeed);
	
});
