
$(document).ready(function() {
	var slideLwidth = $('.slideL').width();

	$('.slideL_cont, .slideR_cont').hover(
			function(event) {
				$(this).children('div').stop(false, true).animate(
							{width: 180}, // what we are animating
							'fast', // how fast we are animating
							'swing',function(){$(this).find('img').stop(false, true).fadeIn('fast')});
			},function(event){
				$(this).find('img').stop(false, true).hide();
				$(this).children('div').stop(false, true).animate(
					{width: slideLwidth},
					'fast',
					'swing',function(){
						$(this).find('img').stop(false, true).hide();
					});
			}
	);
	$('.slideL_cont').click(
		function(){
			 api.prevSlide();
			 $('.time_line a').removeClass('selected');
				$('.time_line a:nth-child('+(vars.current_slide+1)+')').addClass('selected');
		}
	);
	$('.slideR_cont').click(
		function(){
			  api.nextSlide();
			  $('.time_line a').removeClass('selected');
				$('.time_line a:nth-child('+(vars.current_slide+1)+')').addClass('selected');
		}
	);

	//bouton de la time line
		$('.time_line a:nth-child(1)').click(function(event) {	 event.preventDefault();	 api.goTo(1);	});
		$('.time_line a:nth-child(2)').click(function(event) {	 event.preventDefault();	 api.goTo(2);	});
		$('.time_line a:nth-child(3)').click(function(event) {	 event.preventDefault();	 api.goTo(3);	});
		$('.time_line a:nth-child(4)').click(function(event) {	 event.preventDefault();	 api.goTo(4);	});
		$('.time_line a').click(
			function(event) {
				event.preventDefault();
				$('.time_line a').removeClass('selected');
				$('.time_line a:nth-child('+(vars.current_slide+1)+')').addClass('selected');
			}
		);

	//redimensionne la zone .actuR au chargement de la page
	var actuRwidth = $('.actu_cont').width() - ($('.actuL').width() +40);
	$('.actuR').width(actuRwidth - 40); //-40 de padding

	var nb_actu = $(".une_actu_cont").children().length;
	var une_actu_cont_width = nb_actu * 320;
	$('.une_actu_cont').width(une_actu_cont_width);

	var btn_actuL_hover = false;
	var btn_actuR_hover = false;

	//bouton pour bouger les actus
	$('.btn_actuL').hover(
		function(event) {
			btn_actuL_hover = true;
		  if(parseFloat($('.une_actu_cont').css('left')) <  0){
			  bougeActuL();
			  compteur_appel_bougeActuL=0;
		  }
		}, function(event) { // sorti du hover
			btn_actuL_hover = false;
		}
	);
	$('.btn_actuR').hover(
		function(event){
			btn_actuR_hover = true;

			if(parseFloat($('.une_actu_cont').css('left')) >  (une_actu_cont_width +320) * -1){
				bougeActuR();
				compteur_appel_bougeActuR=0;
			}

		}, function(event) { // sorti du hover
			btn_actuR_hover = false;
		}
	);


	var compteur_appel_bougeActuL = 0;
    // slide les actu vers la gauche
	function bougeActuL(){
		//alert('bougeActuL');
		compteur_appel_bougeActuL++;
		if(btn_actuL_hover && compteur_appel_bougeActuL < nb_actu) {
			if(parseFloat($('.une_actu_cont').css('left')) < 0){
					$('.une_actu_cont').animate(
								{'left': '+=320'},
								'slow',
								'swing',function(){
									//alert(btn_actuL_hover+'-'+compteur_appel);
									bougeActuL();
									compteur_appel_bougeActuL=0;
								}
							);
				}
		}
		else{
			btn_actuL_hover = false;
		}

	}



	var compteur_appel_bougeActuR = 0;
	// slide les actu vers la droite
	function bougeActuR(){
		//alert('bougeActuL');
		compteur_appel_bougeActuR++;
		if(btn_actuR_hover && compteur_appel_bougeActuR < nb_actu) {
			if(parseFloat($('.une_actu_cont').css('left')) >  (une_actu_cont_width - 320) * -1){
					$('.une_actu_cont').animate(
								{'left': '-=320'},
								'slow',
								'swing',function(){
									//alert(btn_actuL_hover+'-'+compteur_appel);
									bougeActuR();
								}
							);
				}
		}
		else{
			btn_actuR_hover = false;
		}

	}


	// défilement breve initialisation
	nb_breve = $(".une_breve_cont").children().length;
	unTimer = setTimeout('breveSlider()', vitesse_diapo);
	$('.actuL').hover(
			function(event) {
				stop_timer = true;
				//alert('stop');
				clearTimeout(unTimer);
			},function(event) {
				stop_timer = false;
				breveSlider();
			}
	);

	// redim le contenu
	$('#contenu').height( $('.trame').height() );

    supersized_height = $("#supersized").height();
    //Vraie trame height, celle calculée au moment du resize_viewport, qui n'est pas encore appellé a ce moment là
    trame_height = supersized_height - $('#footer').height() - $('.actu_cont:first').height() - $('#menu_top').height() - $('#menu_top').offset().top;
	$('#contenu').height( trame_height );

	timeleft = ( $('#contenu').width()/2 ) - ( $('.time_line').width()/2 );

	$('.time_line').animate(
								{'left' : timeleft},
								'normal',
								'swing',function(){
								}
							);
	// repositionne les fleches
	flecheTop = ($('#contenu').height() / 2) - 50;
	$('.slideL_cont, .slideR_cont').css('top' , flecheTop);
	$('#slidecaption').css('top' , flecheTop-82);


});

//redimensionne la zone .actuR au resize de la fenetre
$(window).resize(function(){
	var actuRwidth = $('.actu_cont').width() - ($('.actuL').width() +40);
	$('.actuR').width(actuRwidth - 40); //-40 de padding

	// redim le contenu
	$('#contenu').height( $('.trame').height() );

	timeleft = ( $('#contenu').width()/2 ) - ( $('.time_line').width()/2 );

	$('.time_line').animate(
								{'left' : timeleft},
								'fast',
								'swing',function(){
								}
							);

	// repositionne les fleches
	flecheTop = ($('#contenu').height() / 2) - 50;
	$('.slideL_cont, .slideR_cont').css('top' , flecheTop);
	$('#slidecaption').css('top' , flecheTop-82);

	return false;
});




	// Défilement automatique des brèves
var stop_timer = false;
var vitesse_diapo = 3000;
var compteur_breveSlider = 0;

function breveSlider(){
	//alert(nb_breve);
	compteur_breveSlider++;
	if(compteur_breveSlider < nb_breve ){
		$('.une_breve_cont').stop(false, true).animate({
			'marginTop': '-=72'
		}, 700, 'swing', function(){
			//alert('relance');
			if(stop_timer == false){
				unTimer = setTimeout('breveSlider()', vitesse_diapo);
			}
		});
	}
	else{
		compteur_breveSlider = 0;
		$('.une_breve_cont').stop(false, true).animate({
			'marginTop': '0'
		}, 700, 'swing', function(){
			//alert('relance');
			if(stop_timer == false){
				unTimer = setTimeout('breveSlider()', vitesse_diapo);
			}
		});
	}
}



