/*
* Author:      Marco Kuiper (http://www.marcofolio.net/)
*/

// 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" : "",
		"image" : "header1.jpg",
		"url" : "#",
		"firstline" : "Creativit&agrave; &amp; Fantasia",
		"secondline" : "Ci distinguiamo per la nostra creativit&agrave; e seguiamo <br> la vostra azienda con competenza e professionalit&agrave;"
	}, {
		"title" : "",
		"image" : "header2.jpg",
		"url" : "#",
		"firstline" : "L'immagine vincente",
		"secondline" : "L'apparenza conta! Affidatevi a dei veri specialisti per ottenere un Corporate Identity che esca dall'anonimato."
	}, {
		"title" : "",
		"image" : "header3.jpg",
		"url" : "#",
		"firstline" : "Puntate in alto",
		"secondline" : "Essere presenti sul web è importante ma essere primi nei risultati di ricerca è fondamentale!"
	}
];



$(document).ready(function() {
		
    $("#firstline, #secondline, #item1, #item2, #item3, #item4, #ProjectStep, #Fundamentals h2").css("display","none");
    
	// 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(images/" + photoObject.image + ")",
			"display" : "block",
			"z-index" : currentZindex
		});
		
		// Hide the header text
		//$("#HeadLine").css({"display" : "none"});
		$("#firstline, #secondline").slideUp(function(){
        
            // Set the new header text
            $("#firstline").html(photoObject.firstline);
            $("#secondline").html(photoObject.secondline);
			//$("#item1").html(photoObject.item1);
			//$("#item2").html(photoObject.item2);
			//$("#item3").html(photoObject.item3);
			//$("#item4").html(photoObject.item4);
		
            // Fade out the current container
            // and display the header text when animation is complete
        
            $("#headerimg" + currentContainer).fadeOut(function() {
                $("#firstline, #secondline").slideDown(function(){animating=false;});
				$("#ProjectStep, #Fundamentals h2").fadeIn(1000);
				$("#item1, #item2, #item3, #item4").animate({width:'400','padding-left':'60','opacity':'1'}, 800);

				
            });
        });
	};
	
	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);
	
});
