// Vitesse de changement du background
var slideshowSpeed = 4000;

// Configuration du background
var photos = ["slideshow_img01.jpg","slideshow_img02.jpg","slideshow_img03.jpg","slideshow_img04.jpg","slideshow_img05.jpg"];
var txt = ["maîtriser les flux","évolutivité permanente","club utilisateurs","disponibilité totale","intégration optimale"];



function blinkpic(id)
{
if(id>11) return;
	$('#picto' + id).animate({opacity: 0.4}, 100, function(){
		$(this).animate({opacity : 0}, 100, function(){
			id2=id+1;
			para_id=id+5;
			if(id2>3 && id2<8)
			{
				blinkpic(id2);
				blinkpic(para_id);
			}else if(id2<8){
				blinkpic(id2);
			}
		});
	});
}



// Le plugin !
$(document).ready(function() {


	var interval;
	
	var activeContainer = 1;	
	var currentImg = 0;
	var animating = false;
	var navigate = function(direction) {
		
		// Empécher l'action s'il y en a une en cour
		if(animating) return;
		
		// On passe à l'image suivante
		currentImg++;
		
		// Si on est à la derniere image, on passe à la premiere
		if(currentImg == photos.length + 1)
			currentImg = 1;
		
		// Modification du conteneur actif
		var currentContainer = activeContainer;
		activeContainer = (activeContainer == 1)?2:1;

		// On change l'image en Fadding !
		showImage(photos[currentImg - 1], currentContainer, activeContainer);
		showTxt(txt[currentImg - 1],currentContainer, activeContainer)
	};
	
	
	var currentZindex = -1;
	var showImage = function(photoObject, currentContainer, activeContainer) {
		animating = true;
		
		// On met le nouveau conteneur derniere pour l'instant
		currentZindex--;
		
		// On place le nouveau background
		$("#headerimg" + activeContainer).css({
			"background-image" : "url(/wp-content/themes/Inconcept/img_head/" + photoObject + ")",
			"display" : "block",
			"z-index" : currentZindex
		});
		
		
		// Fade out the le conteneur actuel
		// and display the header text when animation is complete
		$("#headerimg" + currentContainer).fadeOut(function() {
		blinkpic(1);
			setTimeout(function() {
				animating = false;
			}, 1500);
		});
	};
	
	
	var showTxt = function(newtxt, currentContainer, activeContainer) {
		animating = true;
		
		// On met le nouveau conteneur derniere pour l'instant
		currentZindex--;
		
		
		
		
		// Fade out the le conteneur actuel
		// and display the header text when animation is complete
		$("#headertxt" + currentContainer).fadeOut(function() {
		
			// On place le nouveau background
			$("#headertxt" + activeContainer).css({
				"display" : "block",
				"z-index" : currentZindex+20
			}).html(newtxt);
			
			setTimeout(function() {
				animating = false;
			}, 1500);
		});
	};

	
	// We should statically set the first image
	navigate("next");
	
	// Start playing the animation
	interval = setInterval(function() {
		navigate("next");
	}, slideshowSpeed);
	
});
