jQuery.noConflict();
jQuery(function($) {

	//INFO BULLE
	$('a.tTip').tinyTips('title');
	$('img.tTip').tinyTips('title');
		
	//ACCORDEON
	$(".accordion p.accordion_titre").click(function(){
		var acc_replie="";
		if($(this).hasClass("accordion_active")){ //si replie 
			acc_replie="oui";
		}
		$(this).addClass("accordion_active").next("div.accordion_texte").slideToggle(300).siblings("div.accordion_texte").slideUp("slow");
		$(this).siblings(".accordion_titre").removeClass("accordion_active");//change puce des autres
		if(acc_replie=="oui"){$(this).removeClass("accordion_active");}
	});	


	// Gestion des partenaires (colonne droite)
	$('#partenaires1').innerfade({ speed: 'slow', timeout: 3000, type: 'sequence'}); 
	$('#partenaires2').innerfade({ speed: 'slow', timeout: 3000, type: 'sequence'}); 
	$('#partenaires3').innerfade({ speed: 'slow', timeout: 3000, type: 'sequence'}); 

	
	// MENU
	var closerdrop;
	var config = {    
		 sensitivity:3,
		 interval: 100,
		 over: openDropdown,
		 timeout: 100,
		 out: closeDropdown
	};

	function openDropdown(){  
		$(this).children().addClass("nav_on");
		$(this).children("div.dropdown").slideToggle("fast");
		clearTimeout(closerdrop);
		$("#bloc_nav").animate({height:'200px'},200);
		
	}
	function closeDropdown(){ 
		$(this).children("div.dropdown").slideToggle(100);
		$(this).children().removeClass("nav_on");
		closerdrop=setTimeout(function() {$("#bloc_nav").animate({height:'40px'},500);}, 400);
	}
	$("#nav_trophees, #nav_infos, #nav_rse, #nav_engage").hoverIntent(config);

	//HOME > Ils s'engagent : selection des videos
	$("#home_engagent_bloc_bleuclair ul li a").click(function(){ 
		$.ajax({
			type: "POST",
			url: "_ajax_home_aff_video.php",
			data: "id="+$(this).attr('href'),
			success: function(html){
				$("#home_engagent_bloc_bleu").empty();
				$("#home_engagent_bloc_bleu").append(html);
			}
		});
		
		return false;
  	});	
  
  	//HOME > A LA UNE
	$("#featured > ul").tabs({fx:{opacity: "toggle", duration:'fast'}}).tabs("rotate",7000, false);
	
	
	//PRIDES > selection des videos
	$("#pridestv_liste ul li a").click(function(){ 
		$.ajax({
			type: "POST",
			url: "_ajax_prides_aff_video.php",
			data: "id="+$(this).attr('href'),
			success: function(html){
				$("#pridestv_video").empty();
				$("#pridestv_video").append(html);
			}
		});
		
		return false;
  	});	
	
	
	
	//PROJETS INNOVANTS > Recevoir les alertes nouveaux projets
	$("#pi_d_suiveznous form").submit(function(){ 
		$.ajax({
			type: "POST",
			url: "_ajax_pi_alerte.php",
			data: "email="+$("#pi_alerte_email").val(),
			success: function(html){
				$("#pi_alerte_email").val(html);
			}
		});
	
		return false;
	});
	
	$("#pi_alerte_email").click(function(){
		$("#pi_alerte_email").val("");
	});
	
	
	
	
	//PROJETS INNOVANTS > FORMULAIRE
	$('.modal').click(function (e) {
		$("#pi_vote_id").val($(this).attr('href'));
	
		e.preventDefault();
		$("#pi_form_vote").modal({
			opacity:80,
			overlayClose:true,
			onClose: function (dialog) {
				dialog.data.fadeOut('fast', function () {
					$.modal.close();
				});
			},
			onOpen: function (dialog) {
				dialog.overlay.fadeIn('fast', function () {
					dialog.container.slideDown('fast', function () {
						dialog.data.fadeIn('fast');
					});
				});
			}
		});
	});
	
	//PROJETS INNOVANTS > SCRIPT
	$("#form_vote").submit(function(){ 
		
		if( ($('#pi_vote_email').val()=="Votre email") || ($('#pi_vote_cp').val()=="Votre code postal")){
			$("#pi_form_erreur").empty();
			$("#pi_form_erreur").append("Votre Email et code postal sont obligatoires.");
			return false;
		}
		
		$.ajax({
			type: "POST",
			url: "_ajax_pi_vote.php",
			data: "pi_vote_email="+$("#pi_vote_email").val()+"&pi_vote_cp="+$("#pi_vote_cp").val()+"&pi_vote_id="+$("#pi_vote_id").val(),
			success: function(response){
				$("#pi_form_vote_content").empty();
				$('#pi_form_vote_content').html(response);
				setTimeout(function() {$.modal.close();}, 4500);
			}
		});
		return false;
	});
	
	
	$('.autoHide').focus(function() {
        if(this.value == this.defaultValue) {
            this.value = "";
        }
    }).blur(function() {
        if(!this.value.length) {
            this.value = this.defaultValue;
        }
    });
	
	// TROPHEES 2011 > ONGLETS
	$(".tab_content").hide(); 
	$("ul.home_tabs li:first").addClass("active").show();
	$(".tab_content:first").show();
	
	$("ul.home_tabs li").click(function() {
		$("ul.home_tabs li").removeClass("active");
		$(this).addClass("active");
		$(".tab_content").hide();

		var activeTab = $(this).find("a").attr("href");
		
		if($(this).find("a").find('img').attr('src')=="images/trophees/ong-exposants.png"){
			$(this).find("a").find('img').attr('src', 'images/trophees/ong-exposants-on.png');
			$("#home_tab2").find('img').attr('src', 'images/trophees/ong-visiteurs.png');
			$("#home_tab3").find('img').attr('src', 'images/trophees/ong-partenaires.png');
			$("#home_tab4").find('img').attr('src', 'images/trophees/ong-candidats.png');
			$("#home_tabs_ligne").css('background-color','#1367a9');
			
		}
		if($(this).find("a").find('img').attr('src')=="images/trophees/ong-visiteurs.png"){
			$("#home_tab1").find('img').attr('src', 'images/trophees/ong-exposants.png');
			$(this).find("a").find('img').attr('src', 'images/trophees/ong-visiteurs-on.png');
			$("#home_tab3").find('img').attr('src', 'images/trophees/ong-partenaires.png');
			$("#home_tab4").find('img').attr('src', 'images/trophees/ong-candidats.png');
			$("#home_tabs_ligne").css('background-color','#a5c6e5');
		}
		if($(this).find("a").find('img').attr('src')=="images/trophees/ong-partenaires.png"){
			$("#home_tab1").find('img').attr('src', 'images/trophees/ong-exposants.png');
			$("#home_tab2").find('img').attr('src', 'images/trophees/ong-visiteurs.png');
			$(this).find("a").find('img').attr('src', 'images/trophees/ong-partenaires-on.png');
			$("#home_tab4").find('img').attr('src', 'images/trophees/ong-candidats.png');
			$("#home_tabs_ligne").css('background-color','#fb8701');
		}
		if($(this).find("a").find('img').attr('src')=="images/trophees/ong-candidats.png"){
			$("#home_tab1").find('img').attr('src', 'images/trophees/ong-exposants.png');
			$("#home_tab2").find('img').attr('src', 'images/trophees/ong-visiteurs.png');
			$("#home_tab3").find('img').attr('src', 'images/trophees/ong-partenaires.png');
			$(this).find("a").find('img').attr('src', 'images/trophees/ong-candidats-on.png');
			$("#home_tabs_ligne").css('background-color','#f49e00');
		}
		
	
		$(activeTab).fadeIn();
		return false;
	});
	
	
	// TROPHEES > ARCHIVES
	$(".archives_tab_content").hide(); 
	$("ul.archives_tabs li:first").addClass("active").show();
	$(".archives_tab_content:first").show();
	
	$("ul.archives_tabs li").click(function() {
		$("ul.archives_tabs li").removeClass("active");
		$(this).addClass("active");
		$(".archives_tab_content").hide();

		var activeTab = $(this).find("a").attr("href");
		
		if($(this).find("a").find('img').attr('src')=="images/trophees/ong-exposants.png"){
			$(this).find("a").find('img').attr('src', 'images/trophees/ong-exposants-on.png');
			$("#archives_ong_tab1").find('img').attr('src', 'images/trophees/ong-laureats.png');
			$("#archives_ong_tab3").find('img').attr('src', 'images/trophees/ong-invites.png');
			$("#archives_ong_tab4").find('img').attr('src', 'images/trophees/ong-images.png');
			$("#archives_tabs_ligne").css('background-color','#1367a9');
			
		}
		if($(this).find("a").find('img').attr('src')=="images/trophees/ong-laureats.png"){
			$("#archives_ong_tab2").find('img').attr('src', 'images/trophees/ong-exposants.png');
			$(this).find("a").find('img').attr('src', 'images/trophees/ong-laureats-on.png');
			$("#archives_ong_tab3").find('img').attr('src', 'images/trophees/ong-invites.png');
			$("#archives_ong_tab4").find('img').attr('src', 'images/trophees/ong-images.png');
			$("#archives_tabs_ligne").css('background-color','#a5c6e5');
		}
		if($(this).find("a").find('img').attr('src')=="images/trophees/ong-invites.png"){
			$("#archives_ong_tab2").find('img').attr('src', 'images/trophees/ong-exposants.png');
			$("#archives_ong_tab1").find('img').attr('src', 'images/trophees/ong-laureats.png');
			$(this).find("a").find('img').attr('src', 'images/trophees/ong-invites-on.png');
			$("#archives_ong_tab4").find('img').attr('src', 'images/trophees/ong-images.png');
			$("#archives_tabs_ligne").css('background-color','#fb8701');
		}
		if($(this).find("a").find('img').attr('src')=="images/trophees/ong-images.png"){
			$("#archives_ong_tab2").find('img').attr('src', 'images/trophees/ong-exposants.png');
			$("#archives_ong_tab1").find('img').attr('src', 'images/trophees/ong-laureats.png');
			$("#archives_ong_tab3").find('img').attr('src', 'images/trophees/ong-invites.png');
			$(this).find("a").find('img').attr('src', 'images/trophees/ong-images-on.png');
			$("#archives_tabs_ligne").css('background-color','#f49e00');
		}
	
		$(activeTab).fadeIn();
		return false;
	});
	
	
	
	// Gestion des carousels
    $('.newCarousel').each(function(){
        var animationActive = false;
        var idCarousel = '#'+$(this).attr('id')+' ';

        var elementWidth = $(idCarousel + '.carousel li').width();
        var numberElements = $(idCarousel + '.carousel li').length;
		var c;
		var d;
        $(idCarousel + '.carousel').css('width', (numberElements*2)*elementWidth).wrap(document.createElement('div')).parent().addClass('jcarousel-clipp');
        
        // Gestion de defilement arriere
        $(idCarousel + '.carouselPrev a').bind('click', function(){
            if(animationActive != true) {
                animationActive = true;
                
                $(idCarousel + '.carousel li:last').clone().insertBefore(idCarousel + '.carousel li:last');
                $(idCarousel + '.carousel li:last').insertBefore(idCarousel + '.carousel li:first');
                
                var marginEnd = ($(idCarousel + '.carousel').css('marginLeft'));
                var marginBegin = marginEnd.replace(/px/, '');
                marginBegin = marginBegin-elementWidth;
                marginBegin = marginBegin+'px';
                
                $(idCarousel + '.carousel').css('marginLeft', marginBegin);
                $(idCarousel + '.carousel').stop().animate({'marginLeft': marginEnd}, 'slow', function() {
                    $(idCarousel + '.carousel li:last').remove();
                    animationActive = false
                });
            }

			$('.navCarousel li.hidden').removeClass('hidden');
			$('.navCarousel ul li:last]').clone().insertBefore('.navCarousel ul li:first');
			$('.navCarousel ul li:last]').insertBefore('.navCarousel ul li:first');			
			$('.navCarousel ul li:last]').addClass('hidden');
            return false;
        });
        
        // Gestion de defilement avant
        $(idCarousel + '.carouselNext a').bind('click', function(){
            if(animationActive != true) {
                animationActive = true;
                
                $(idCarousel + '.carousel li:first').clone().insertAfter(idCarousel + '.carousel li:first');
				$(idCarousel + '.carousel li:first').insertAfter(idCarousel + '.carousel li:last');
                
                var marginEnd = ($(idCarousel + '.carousel').css('marginLeft'));
                var marginBegin = marginEnd.replace(/px/, '');
                marginBegin = marginBegin-elementWidth;
                marginBegin = marginBegin+'px';
                
                $(idCarousel + '.carousel').stop().animate({'marginLeft': marginBegin}, 'slow', function() {
                    $(idCarousel + '.carousel').css('marginLeft', marginEnd);
                    $(idCarousel + '.carousel li:first').remove();
                    animationActive = false
                });
            }
			
            return false;
        });
		
		
		// Gère l'affichage des item de la navigation du carousel
		$('.navCarousel ul li a').click(function(){
			var idNav = $(this).attr('href');
			if($('.navCarousel ul li').hasClass('hidden')){
				$('.navCarousel ul li').removeClass('hidden');
			}
			$(this).parent().addClass('hidden');
			$(this).parent().insertAfter('.navCarousel ul li:last');
			$(idCarousel + ".carousel " + idNav).insertBefore(idCarousel + '.carousel li:first');
			
			return false;
		});	
		
        
        // Autoslide
        if ($(this).is('.autoSlide')){
			var a=0;
			var b;
			var c;
            var interval = setInterval(function() {
                $(idCarousel + '.carouselNext a').click();
				b = $('.newCarousel ul li:eq(1)').attr('id');
				$('.navCarousel li').removeClass('hidden');
				$('.navCarousel ul li a[href$="'+b+'"]').parent().addClass('hidden').insertAfter('.navCarousel ul li:last');
            }, 7000);
			$(idCarousel + '.carousel').bind('mouseover', function(){ clearInterval(interval); });
			$('.navCarousel').bind('mouseover', function(){ clearInterval(interval); });		  
        };		
	});

	function navCarouselUpdate(){
		b = $('.newCarousel ul li:eq(0)').attr('id');
		$('.navCarousel li').removeClass('hidden');
		$('.navCarousel ul li a[href$="'+b+'"]').parent().addClass('hidden').insertAfter('.navCarousel ul li:last');
	}
	
});
