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

// Speed of the automatic slideshow
var slideshowSpeed = 5000;

// Variable to store the images we need to set as background
// which also includes some text and url's.
var photos = [ {
		"title" : "After the WOD",
		"image" : "home_after-the-wod.jpg"
	}, {
		"title" : "Barbells",
		"image" : "home_barbells.jpg"
	},  {
		"title" : "Bloed zweet en tranen",
		"image" : "home_blood-sweat-tears.jpg"
	},  {
		"title" : "Chalk",
		"image" : "home_chalk.jpg"
	},  {
		"title" : "Deadlift",
		"image" : "home_deadlift.jpg"
	},  {
		"title" : "Dennis van Beek",
		"image" : "home_dennis.jpg"
	},  {
		"title" : "Gouden Kettlebells",
		"image" : "home_golden-kb.jpg"
	},  {
		"title" : "Jan van Delden",
		"image" : "home_jan.jpg"
	},  {
		"title" : "Joris Trooster",
		"image" : "home_joris.jpg"
	},  {
		"title" : "Kettlebell",
		"image" : "home_kettlebell.jpg"
	},  {
		"title" : "Kettlebells",
		"image" : "home_kettlebells.jpg"
	},  {
		"title" : "Kettlebells Volkspark",
		"image" : "home_kettlebells-volkspark.jpg"
	},  {
		"title" : "CrossFit Level I Certification Course",
		"image" : "home_level-1-certification-course.jpg"
	},  {
		"title" : "Lilly",
		"image" : "home_lilly.jpg"
	},  {
		"title" : "Muscle Up",
		"image" : "home_muscle-ups.jpg"
	},  {
		"title" : "Whiteboard",
		"image" : "home_whiteboard.jpg"
	}
];

$(document).ready(function() {

	//randomly reorder photos array
	photos.sort(function() {return 0.5 - Math.random()})
	photos = photos.slice(0,5); //Maak de array niet langer dan 5 items om te voorkomen dat ie enorme hoeveelheden dataverkeer trekt als de pagina open blijft staan.
	//alert(photos);

	var gotime = photos.length;
 
	//preload
	$.each(photos,function(e) {
		$(new Image()).load().attr('src','/wp-content/themes/crossfittwente/images/header/'+this.image);
	});

	var interval;
	var activeContainer = 1;	
	var currentImg = 0;
	var animating = false;
	var navigate = function(direction) {
		// Check if no animation is running. If it is, prevent the action
		if(animating) {
			return;
		}
		
		// Check which current image we need to show
		if(direction == "next") {
			currentImg++;
			if(currentImg == photos.length + 1) {
				currentImg = 1;
			}
		} else {
			currentImg--;
			if(currentImg == 0) {
				currentImg = photos.length;
			}
		}
		
		// Check which container we need to use
		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;
		
		// Make sure the new container is always on the background
		currentZindex--;
		
		// Set the background image of the new active container
		$("#headerimg" + activeContainer).css({
			"background-image" : "url('/wp-content/themes/crossfittwente/images/header/" + photoObject.image + "')",
			"display" : "block",
			"z-index" : currentZindex
		});
		
		// Fade out the current container
		// and display the header text when animation is complete
		$("#headerimg" + currentContainer).fadeOut(function() {
			setTimeout(function() {
				animating = false;
			}, 1000);
		});
	};
	
	// We should statically set the first image
	navigate("next");
	
	// Start playing the animation
	interval = setInterval(function() {
		navigate("next");
	}, slideshowSpeed);
	
});
