$(document).ready(function() {
	$(document).everyTime(50,refresher,20);
	function refresher() {
		$('h2').each(function(){
			$(this).children('span').css('width','auto');
			var largeur = $(this).children('span').width() + 5;
			var largeurTotale = $(this).width();
			$(this).children('span').css('width',largeur + ((largeurTotale-largeur)%10));
		});
		$('h3').each(function(){
			if($(this).children('span').width()==0){
				var largeurTotale = $(this).width();
				$(this).css('width',largeurTotale-(largeurTotale%5));
			}else{
				$(this).children('span').css('width','auto');
				var largeur = $(this).children('span').width() + 3;
				var largeurTotale = $(this).width();
				$(this).children('span').css('width',largeur + ((largeurTotale-largeur)%5));
			}
		});
	}
	refresher();
	
	
	
	$("a.newsLirePlus").hover(function(){
		$(this).parents('.cadreNews').addClass('cadreNewsHover');
	}, function() {
		$(this).parents('.cadreNews').removeClass('cadreNewsHover');
	});
	
	$(".docs_telechargeable a").hover(function(){
		$(this).parents('li').addClass('actif');
	}, function() {
		$(this).parents('li').removeClass('actif');
	});
	
	
	$('.sect_first a').click(function(){
		ancien = $('.sect_first a.actif').attr('id');
		ancien = ancien.replace("sect_deg_","");
		num = $(this).attr('id');
		num = num.replace("sect_deg_","");
		
		$('#sect_deg_'+ancien).removeClass("actif");
		$('#sect_deg_'+num).addClass("actif");
		
		$('.sect_info_'+ancien).removeClass("actif");
		$('.sect_info_'+num).addClass("actif");
		
		return false;
	})
	
	
	
	
	// --- Galerie Photo News -------------------------------------- Galerie Photo News ---
	if($('.galerie_photo').length != 0){
		var numPict = 1;
		var bottomActif = true;
		var topActif = true;
		var posY = numPict;
		$('.galerie_photo_top').click(function(){
			if(topActif){
				moveUpDown(-1);
			}
		})
		$('.galerie_photo_bottom').click(function(){
			if(bottomActif){
				moveUpDown(1);
			}
		})
		verifFleches();
		var myRows = $('.galerie_photo_small a').click(clickPhoto);
		$('.galerie_photo_right').click(clickPhotoNext);
		$('.galerie_photo_left').click(clickPhotoBefore);
		var totalGalerie = myRows.size();
		openPhoto(numPict);
	}
	function verifFleches(){
		if($('.galerie_photo_small').length<(posY+4)){
			$('.galerie_photo_bottom').addClass('inactif');
			bottomActif = false;
		}else{
			$('.galerie_photo_bottom').removeClass('inactif');
			bottomActif = true;
		}
		if(posY<=1){
			$('.galerie_photo_top').addClass('inactif');
			topActif = false;
		}else{
			$('.galerie_photo_top').removeClass('inactif');
			topActif = true;
		}
		if(numPict>=totalGalerie){
			$('.galerie_photo_right').addClass('inactif');
		}else{
			$('.galerie_photo_right').removeClass('inactif');
		}
		if(numPict<=1){
			$('.galerie_photo_left').addClass('inactif');
		}else{
			$('.galerie_photo_left').removeClass('inactif');
		}
	}
	function moveUpDown(direction){
		posY += 4*direction;
		if($('.galerie_photo_small').length<=(posY+3)){
			posY = ($('.galerie_photo_small').length-3);
		}
		if(posY<1){
			posY = 1;
		}
		moveTo();
	}
	function moveTo(){
		$('.galerie_photo_all').animate({top: '-'+((posY-1)*100)+'px'});
		verifFleches();
	}
	function clickPhoto(){
		openPhoto(myRows.index(this)+1);
		PhotoNext();
		verifFleches();
		return false;
	}
	function clickPhotoNext(){
		if(numPict<totalGalerie){
			openPhoto(numPict+1);
		}
		PhotoNext();
	}
	function clickPhotoBefore(){
		if(numPict>1){
			openPhoto(numPict-1);
		}
		PhotoNext();
	}
	function PhotoNext(){
		var moved = false;
		if(numPict<posY){
			moved = true;
			while(numPict<posY){
				posY--;
			}
		}
		if(numPict>=posY+4){
			moved = true;
			while(numPict>=posY+4){
				posY++;
			}
		}
		if(moved==true){
			moveTo();
		}else{
			verifFleches();
		}
	}
	
	function openPhoto(numPictNow){

		numPict = numPictNow;
		elem = $('.galerie_photo_small a:eq('+(numPict-1)+')');
		adress = elem.attr('href');
		
		$('.galerie_photo_big .loader').show();
		
		var img = new Image();
		$(img).load(function () {
			$(this).css('display', 'none');
			$(this).hide();
			$('.galerie_photo_big .loader').fadeOut();
			$('.galerie_photo_big').append(this);
			$(this).fadeIn();
		}).attr('src', adress);
		$('.galerie_photo_title').html(elem.children('img').attr('alt'));
		$('.galerie_photo_num').html(numPict+'/'+totalGalerie);
		$('.galerie_photo_small a.actif').removeClass('actif');
		elem.addClass('actif');
		return false;
	}
	// --- ------- ----- ---- -------------------------------------- ------- ----- ---- ---
	
	
	// --- Galerie Photo Section -------------------------------- Galerie Photo Section ---
	if($('.section_gallery_small li').length != 0){
		var numPicture = 1;
		$('.section_gallery_big li').click(function(){
			if(numPicture>=totalSectionGalerie){
				numPicture=1;
			}else{
				numPicture++;
			}
			moveSectionTo();
		})
		var mySectionRows = $('.section_gallery_small li img').click(clickPhotoSection);
		var totalSectionGalerie = mySectionRows.size();
		moveSectionTo();
	}
	function moveSectionTo(){
		$('.section_gallery_big').animate({top: '-'+((numPicture-1)*344)+'px'});
	}
	function clickPhotoSection(){
		numPicture = mySectionRows.index(this)+1;
		moveSectionTo();
	}
	// --- ------- ----- ------- -------------------------------- ------- ----- ------- ---
	
});
