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

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

// Variable to store the images we need to set as background
// which also includes some text and url's.
var photos = [ {
		"image" : "luxuryvilla.jpg",
		"url" : "luxury_villa.htm",
		"firstline" : "architettura",
		"secondline" : "Luxury villa",
		"title" : "Roma",
		"testo" : "#ffffff",
		"test1" : "#ffffff",
		"test2" : "#000000",
		"background" : "#90332c"
	}, {
		"image" : "filippinembassy.jpg",
		"url" : "ambasciata_filippine.htm",
		"firstline" : "interior design",
		"secondline" : "Ambasciata delle Filippine",
		"title" : "Roma",
		"testo" : "#4b4c48",
		"test1" : "#ffffff",
		"test2" : "#ffffff",
		"background" : "#ffffff"
	}, {	
		"image" : "store.jpg",
		"url" : "storefront.htm",
		"firstline" : "retail design",
		"secondline" : "Storefront",
		"title" : "Beijing - China",
		"testo" : "#000000",
		"test1" : "#ffffff",
		"test2" : "#000000",
		"background" : "#ffffff"
	}, {
		"image" : "fireplace.jpg",
		"url" : "casa_lemontree.htm",
		"firstline" : "interior design",
		"secondline" : "Lemon Tree house",
		"title" : "Morlupo - Roma",
		"testo" : "#ffffff",
		"test1" : "#ffffff",
		"test2" : "#ffffff",
		"background" : "#f48408"
	}, {
		"image" : "indonesia2.jpg",
		"url" : "ambasciata_indonesia.htm",
		"firstline" : "interior design",
		"secondline" : "Ambasciata d'Indonesia",
		"title" : "Roma",
		"testo" : "#aa2135",
		"background" : "#ffffff"
	}, {
		"image" : "mdh.jpg",
		"url" : "mongolian.htm",
		"firstline" : "architettura",
		"secondline" : "Dream House",
		"title" : "Ulan Bator - Mongolia",
		"testo" : "#ffffff",
		"background" : "#ff8400"
	}, {
		"image" : "cinecitta.jpg",
		"url" : "cinecitta_due.htm",
		"firstline" : "lighting design",
		"secondline" : "Cinecitta' 2",
		"title" : "Roma",
		"testo" : "#ffffff",
		"background" : "#329fca"
	}, {
		"image" : "nuvolari.jpg",
		"url" : "casa_nuvolari.htm",
		"firstline" : "interior design",
		"secondline" : "casa Nuvolari",
		"title" : "Roma",
		"testo" : "#ffffff",
		"test1" : "#90332c",
		"test2" : "#90332c",
		"background" : "#90332c"
	}, {
		"image" : "traiana2.jpg",
		"url" : "colonna_traiana.htm",
		"firstline" : "lighting design",
		"secondline" : "Colonna Traiana",
		"title" : "Roma",
		"testo" : "#ffffff",
		"test1" : "#ffffff",
		"test2" : "#ffffff",
		"background" : "#90332c"
	}, {
		"image" : "tarquinia.jpg",
		"url" : "giardino.htm",
		"firstline" : "garden design",
		"secondline" : "giardino a Tarquinia",
		"title" : "Tarquinia",
		"testo" : "#323f5f",
		"background" : "#ffffff"
	}, {
		"image" : "morena.jpg",
		"url" : "casa_morena.htm",
		"firstline" : "interior design",
		"secondline" : "casa Morena",
		"title" : "Roma",
		"testo" : "#ffffff",
		"test1" : "#ffffff",
		"test2" : "#af243b",
		"background" : "#af243b"
	}, {
		"image" : "sordi.jpg",
		"url" : "galleria_sordi.htm",
		"firstline" : "lighting design",
		"secondline" : "galleria Alberto Sordi",
		"title" : "Roma",
		"testo" : "#151c15",
		"test1" : "#ffffff",
		"test2" : "#ffffff",
		"background" : "#ffffff"
	}, {
		"image" : "marco.jpg",
		"url" : "sanmarco.htm",
		"firstline" : "lighting design",
		"secondline" : "San Marco Evangelista",
		"title" : "Capena",
		"testo" : "#060023",
		"background" : "#efefef"
	}, {		
		"image" : "ischia.jpg",
		"url" : "ischia.htm",
		"firstline" : "lighting design",
		"secondline" : "lighting tests",
		"title" : "Ischia",
		"testo" : "#ffffff",
		"background" : "#2e3f51"
	}, {
		"image" : "ronciglione.jpg",
		"url" : "gardenparty.htm",
		"firstline" : "garden design",
		"secondline" : "Il giardino delle feste",
		"title" : "Ronciglione",
		"testo" : "#ffffff",
		"background" : "#7d1f17"
	},
];




$(document).ready(function() {
		
	// Backwards navigation
	$("#back").click(function() {
		stopAnimation();
		navigate("back");
	});
	
	// Forward navigation
	$("#next").click(function() {
		stopAnimation();
		navigate("next");
	});
	
	var interval;
	$("#control").toggle(function(){
		stopAnimation();
	}, function() {
		// Change the background image to "pause"
		$(this).css({ "background-image" : "url(images/btn_pause.png)" });
		
		// 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
		$("#headerimg" + activeContainer).css({
			"background-image" : "url(sfondi/" + photoObject.image + ")",
			"display" : "block",
			"z-index" : currentZindex
		});


		$("#secondline").css({
			"color" : (photoObject.testo),
			"background-color" : (photoObject.background)
		});
		
		$("#firstline").css({
			"color" : (photoObject.test1)
		});
		
		$("#pictureduri").css({
			"color" : (photoObject.test2)
		});
		
		
		// Hide the header text
		$("#headertxt").css({"display" : "none"});
		
		// Set the new header text
		$("#firstline").html(photoObject.firstline);
		$("#secondline")
			.attr("href", photoObject.url)
			.html(photoObject.secondline);
			
		$("#pictureduri")
			.attr("href", photoObject.url)
			.html(photoObject.title);
		
		
		// Fade out the current container
		// and display the header text when animation is complete
		$("#headerimg" + currentContainer).fadeOut(function() {
			setTimeout(function() {
				$("#headertxt").css({"display" : "block"});
				animating = false;
			}, 500);
		});
	};
	
	var stopAnimation = function() {
		// Change the background image to "play"
		$("#control").css({ "background-image" : "url(images/btn_play.png)" });
		
		// Clear the interval
		clearInterval(interval);
	};
	
	// We should statically set the first image
	navigate("next");
	
	// Start playing the animation
	interval = setInterval(function() {
		navigate("next");
	}, slideshowSpeed);
	
});
