// JavaScript Document
$(document).ready(function() {			   
	
	$('a[rel=externe]').attr('target','_blank'); 
	

	$('.faqs dd').hide(); // Hide all DDs inside .faqs
		$('.faqs dt').hover(function(){$(this).addClass('hover')},function(){$(this).removeClass('hover')}).click(function(){ // Add class "hover" on dt when hover
		$(this).next().slideToggle('normal'); // Toggle dd when the respective dt is clicked
	});
		
	
	//EFFET BOX NAV
	$("#main_nav li:not(.active) img.pic").fadeTo(0, 0.3);
    $("#main_nav li:not(.active) img.pic").hover(function () {
        $(this).fadeTo("fast", 1);
    }, function () {
        $(this).fadeTo("fast", 0.3);
    });
	
	//$("#main_nav li:not(.active) img.legend").hover(function () {
//        $(this).prev().fadeTo("fast", 1);
//    }, function () {
//        $(this).prev().fadeTo("fast", 0.3);
//    });
	
	
	
	var interval;
	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[Math.floor(Math.random()*photos.length)], 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(webimages/background/" + photoObject.image + ")",
			"display" : "block",
			"z-index" : currentZindex
		});
		// Hide the header text
		$("#headertxt").css({
			"display" : "none"
		});
		$("#caption").css({
			"marginLeft" : photoObject.margin,
			"width" : photoObject.width
		});
		// Set the new header text
		$("#titre").html(photoObject.title);
		$("#firstline").html(photoObject.firstline);
	
		// Fade out the current container
		// and display the header text when animation is complete
		$("#headerimg" + currentContainer).fadeOut(800,function() {
			setTimeout(function() {
				$("#headertxt").css({"display" : "block","paddingTop" : "128px"});
				$("#headertxt").animate({
					paddingTop: '0'
				});
				setTimeout(function() {
					//$("#headertxt").fadeOut(1800);
					
					$("#headertxt").animate({
						paddingTop: '138px'
					});
					
					
				}, 10000);
				
				animating = false;
			}, 200);
		});
	};
	
	// We should statically set the first image
	navigate("next");
	
	// Start playing the animation
	interval = setInterval(function() {
		navigate("next");
	}, slideshowSpeed);
	
});



 // Fade out the suggestions box when not active
$("input").blur(function(){
	 	$('#suggestions').fadeOut();
});

function lookup(inputString) {
	if(inputString.length < 3) {
		$('#suggestions').fadeOut(); // Hide the suggestions box
	} else {
		$.post("contenu/resultats_recherche.php", {queryString: ""+inputString+""}, function(data) { // Do an AJAX call
			$('#suggestions').fadeIn(); // Show the suggestions box
			$('#suggestions').html(data); // Fill the suggestions box
		});
	}
}




var slideshowSpeed = 12000;
//1er
//"margin" : "0px",
//"width" : "204px"

//2eme
//"margin" : "234px",
//"width" : "209px"

//3eme
//"margin" : "472px",
//"width" : "207px"

//4eme
//"margin" : "708px",
//"width" : "206px"
var photos = [ {
		"title" : "Avec le gaz naturel, imaginons maintenant notre futur !", //feuille verte goute eau 3
		"image" : "1.jpg",
		"firstline" : "Parce qu'un futur en accord avec son environnement, ça se construit dès aujourd'hui.",
		"margin" : "472px",
		"width" : "207px"
	}, {
		"title" : "Le gaz naturel, l'énergie qu'on aime !", //miss velo 2
		"image" : "2.jpg",
		"firstline" : "La nature remercie ceux qui chauffent leur logement en pensant à l'environnement.",
		"margin" : "234px",
		"width" : "209px"
	}, {
		"title" : "Le gaz naturel, la source d'énergie verte !", //arbre gauche 2 ou 3
		"image" : "4.jpg",
		"firstline" : "Des valeurs d'émissions faibles pour une contribution active à la protection de l'environnement.",
		"margin" : "234px",
		"width" : "209px"
	}, {
		"title" : "Le gaz naturel, l'agent énergétique innovant !", //deux pers face champ 1 ou 4
		"image" : "5.jpg",
		"firstline" : "Une solution propre et avantageuse avec une multitude d'applications innovantes.",
		"margin" : "0px",
		"width" : "204px"
		
	}, {
		"title" : "Le gaz naturel, l'énergie qu'on apprécie !", //motte
		"image" : "6.jpg",
		"firstline" : "Car utiliser un agent énergétique naturel et respectueux de l'environnement, c'est contribuer au bien-être de chacun.",
		"margin" : "234px",
		"width" : "209px"
		
	}, {
		"title" : "Avec le biogaz, c'est la nature qui vous remercie !", //champ all
		"image" : "7.jpg",
		"firstline" : "Qui traite bien la nature, reçoit du positif en retour, par exemple le biogaz.",
		"margin" : "708px",
		"width" : "206px"
		
	}, {
		"title" : "Le biogaz, une énergie en étroite collaboration avec la nature !", //coccinelle 2
		"image" : "8.jpg",
		"firstline" : "Que ce soit pour rouler ou pour chauffer, le biogaz est toujours plus apprécié.",
		"margin" : "234px",
		"width" : "209px"
		
	}, {
		"title" : "Le biogaz, parce que la nature vous le rend bien !", //lac 3
		"image" : "9.jpg",
		"firstline" : "La nature remercie ceux qui roulent en pensant à l'environnement.",
		"margin" : "472px",
		"width" : "207px"
		
	}, {
		"title" : "Le gaz naturel et le biogaz, naturellement !", //fleur pétales 2
		"image" : "10.jpg",
		"firstline" : "Ce que la nature a de mieux à offrir en terme d'énergie renouvelable.",
		"margin" : "234px",
		"width" : "209px"
		
	}, {
		"title" : "Le gaz naturel, moins d'argent pour plus d'énergie !", //deux pers champ 2
		"image" : "11.jpg",
		"firstline" : "Des économies à tous les niveaux pour un bilan énergétique et écologique sans concurrence.",
		"margin" : "234px",
		"width" : "209px"
	}, {
		"title" : "Le biogaz, une énergie en étroite collaboration avec la nature !", //lac 2
		"image" : "12.jpg",
		"firstline" : "Que ce soit pour rouler ou pour chauffer, le biogaz est toujours plus apprécié.",
		"margin" : "234px",
		"width" : "209px"
	}
];


function mail(){
var e = document.getElementById('mail');
var x = "inf" + "o@" + "cosveg" + "az.ch";
e.innerHTML = x;
e.href = "mailto:" + x;}
