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

// Speed of the automatic slideshow
var slideshowSpeed1 = 6000;

// Variable to store the images we need to set as background
// which also includes some text and url's.
var photos1 = [ {
		"title" : "Stairs",
		"image" : "mast_mba1.jpg",
		"url" : "http://www.sxc.hu/photo/1271909",
		"firstline" : "Going on",
		"secondline" : "vacation?"
	}, {
		"title" : "Office Appartments",
		"image" : "mast_mba2.jpg",
		"url" : "http://www.sxc.hu/photo/1265695",
		"firstline" : "Or still busy at",
		"secondline" : "work?"
	}, {
		"title" : "Mountainbiking",
		"image" : "mast_mba3.jpg",
		"url" : "http://www.sxc.hu/photo/1221065",
		"firstline" : "Get out and be",
		"secondline" : "active"
	}
];



$(document).ready(function() {
		
	// Backwards navigation
	$("#back").click(function() {
		stopAnimation1();
		navigate1("back");
	});
	
	// Forward navigation
	$("#next").click(function() {
		stopAnimation1();
		navigate1("next");
	});
	
	var interval1;
	$("#control").toggle(function(){
		stopAnimation1();
	}, function() {
		// Change the background image to "pause"
		$(this).css({ "background-image" : "url(images/btn_pause.png)" });
		
		// Show the next image
		navigate1("next");
		
		// Start playing the animation
		interval1 = setInterval(function() {
			navigate1("next");
		}, slideshowSpeed1);
	});
	
	
	var activeContainer1 = 1;	
	var currentImg1 = 0;
	var animating1 = false;
	var navigate1 = function(direction1) {
		// Check if no animation is running. If it is, prevent the action
		if(animating1) {
			return;
		}
		
		// Check which current image we need to show
		if(direction1 == "next") {
			currentImg1++;
			if(currentImg1 == photos1.length + 1) {
				currentImg1 = 1;
			}
		} else {
			currentImg1--;
			if(currentImg1 == 0) {
				currentImg1 = photos1.length;
			}
		}
		
		// Check which container we need to use
		var currentContainer1 = activeContainer1;
		if(activeContainer1 == 1) {
			activeContainer1 = 2;
		} else {
			activeContainer1 = 1;
		}
		
		showImage1(photos1[currentImg1 - 1], currentContainer1, activeContainer1);
		
	};
	
	var currentZindex1 = -1;
	var showImage1 = function(photoObject1, currentContainer1, activeContainer1) {
		animating1 = true;
		
		// Make sure the new container is always on the background
		currentZindex1--;
		
		// Set the background image of the new active container
		$("#mbaheaderimg" + activeContainer1).css({
			"background-image" : "url(images/" + photoObject1.image + ")",
			"display" : "block",
			"z-index" : currentZindex1
		});
		
		// Hide the header text
		$("#mbaheadertxt").css({"display" : "none"});
		
		// Set the new header text
		$("#firstline").html(photoObject1.firstline);
		$("#secondline")
			.attr("href", photoObject1.url)
			.html(photoObject1.secondline);
		$("#pictureduri")
			.attr("href", photoObject1.url)
			.html(photoObject1.title);
		
		
		// Fade out the current container
		// and display the header text when animation is complete
		$("#mbaheaderimg" + currentContainer1).fadeOut(function() {
			setTimeout(function() {
				$("#mbaheadertxt").css({"display" : "block"});
				animating1 = false;
			}, 2500);
		});
	};
	
	var stopAnimation1 = function() {
		// Change the background image to "play"
		$("#control").css({ "background-image" : "url(images/btn_play.png)" });
		
		// Clear the interval
		clearInterval(interval1);
	};
	
	// We should statically set the first image
	navigate1("next");
	
	// Start playing the animation
	interval1 = setInterval(function() {
		navigate1("next");
	}, slideshowSpeed1);
	
});
