/*
* Author:      Lars-Olof "Lollo" Allerhed 
*/

// Speed of the automatic slideshow
var slideshowSpeed = 6000;

// Variable to store the images we need to set as background
// which also includes some text and url's.
var photos = [ 
	{
		"title" : "B&ouml;rja tr&auml;na nu",
		"image" : "/wp-content/themes/balstadojo30/images/bdcarusell1.jpg",
		"url" : "/bli-medlem-idag/",
		"firstline" : "Bli Stark Smidig och &ouml;ka din Sj&auml;lvk&auml;nnsla, tr&auml;na hos oss",
		"secondline" : "B&aring;lsta Dojo"
	}, {
		"title" : "B&ouml;rja tr&auml;na nu",
		"image" : "/wp-content/themes/balstadojo30/images/bdcarusell2.jpg",
		"url" : "/bli-medlem-idag/",
		"firstline" : "Just nu bjuder vi p&aring; din f&ouml;rsta tr&auml;ningsm&aring;nad",
		"secondline" : "B&aring;lsta Dojo"
	}, {
		"title" : "L&auml;s mera",
		"image" : "/wp-content/themes/balstadojo30/images/bdcarusell3.jpg",
		"url" : "/traningsformer/mixed-martial-arts/",
		"firstline" : "Designad f&ouml;r sparring och t&auml;vling, men utm&auml;rkt som motionsform",
		"secondline" : "Mixed Martial Arts"
	}, {
		"title" : "L&auml;s mera",
		"image" : "/wp-content/themes/balstadojo30/images/bdcarusell4.jpg",
		"url" : "/",
		"firstline" : "En av de snabbast v&auml;xande kampsporterna i v&auml;rlden",
		"secondline" : "Brazilian Jiu-jitsu"
	}, {
		"title" : "L&auml;s mera",
		"image" : "/wp-content/themes/balstadojo30/images/bdcarusell5.jpg",
		"url" : "/traningsformer/ju-jutsu-kai/",
		"firstline" : "Effektiva och enkla sj&auml;lvf&ouml;rsvarstekniker",
		"secondline" : "Ju-jutsu Kai"
	}, {
		"title" : "L&auml;s mera",
		"image" : "/wp-content/themes/balstadojo30/images/bdcarusell6.jpg",
		"url" : "/traningsformer/submission-wrestling/",
		"firstline" : "En modern hybridkampsport",
		"secondline" : "Submission Wrestling"
	}, {
		"title" : "L&auml;s mera",
		"image" : "/wp-content/themes/balstadojo30/images/bdcarusell7.jpg", 
		"url" : "/traningsformer/muay-thai/",
		"firstline" : "Fysik och tekniska finesser med hj&auml;lp av mitts och sparring",
		"secondline" : "Muay Thai"
	}, {
		"title" : "L&auml;s mera",
		"image" : "/wp-content/themes/balstadojo30/images/bdcarusell8.jpg",
		"url" : "/traningsformer/crossfit-inspirerad-traning/",
		"firstline" : "En kombination av styrke-och konditionstr&auml;ning under h&ouml;g intensitet",
		"secondline" : "Crossfit"
	},{
		"title" : "L&auml;s mera",
		"image" : "/wp-content/themes/balstadojo30/images/bdcarusell9.jpg",
		"url" : "/traningsformer/ungdoms-grupper/",
		"firstline" : "Ett lekfullt s&auml;tt att tr&auml;na sin kordination och kroppskontroll",
		"secondline" : "Ungdomstr&auml;ning"
	},  
];



jQuery(document).ready(function() {
		
	var interval;
	jQuery("#control").toggle(function(){
		stopAnimation();
	}, function() {
		
		// Show the next image
		navigate("next");
		
		// Start playing the animation
		interval = setInterval(function() {
			navigate("next");
		}, slideshowSpeed);
	});
	
	
	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
		jQuery("#carusellimg" + activeContainer).css({
			"background-image" : "url(" + photoObject.image + ")",
			"display" : "block",
			"z-index" : currentZindex
		});
		
		// Hide the header text
		//jQuery("#headertxt").css({"display" : "none"}); 
		
		// Set the new header text
		jQuery("#firstline").html(photoObject.firstline);
		jQuery("#secondline")
			.attr("href", photoObject.url)
			.html(photoObject.secondline);
		jQuery("#thirdlinea")
			.attr("href", photoObject.url)
			.html(photoObject.title);

		// Fade out the current container
		// and display the header text when animation is complete
		jQuery("#carusellimg" + currentContainer).fadeOut(function() {
			setTimeout(function() {
				animating = false;
			}, 500);
		});
	};
	
	
	// We should statically set the first image
	navigate("next");
	
	// Start playing the animation
	interval = setInterval(function() {
		navigate("next");
	}, slideshowSpeed);
	
});
