$(function(){
	
	/* activer le slide de la home page */
	if($('.slide_home')!=0){
		slideHome();
		/* resizer les blocs */
		var $maxHeight = $('.actu_small_list div').height();
		/* résoud le problème pour Chrome et Safari qui mettent la hauteur de l'img à 0px */
		var $tmHeight = $('.tm_small div').height()-$('.tm_small div img').height()+162;
		if($maxHeight < $tmHeight) $maxHeight=$tmHeight;
		if($maxHeight < $('.newsletter div').height()) $maxHeight=$('.newsletter div').height();
		$('.home .info_bloc > div').attr("style","height:"+$maxHeight+"px");
	}
		
	/* enlever les labels en les mettre en valeurs des inputs */
	$('#mc_signup_form label.mc_var_label').each(function(i){
		var label = $(this).html();
		$(this).next().next().attr('value',label);
		$(this).hide();
	});
	
	/* enlever la value par défaut des input */
	$('input, textarea').click(function(){
		$(this).attr('value',"");
	});
	
	/* cacher les sous-menu du header */
	$('#header .nav ul').hide();
	/* affichage des sous-menus */
	$('#header .nav li.sub-nav').hover(function(){
			$(this).children('.rubrique').addClass('hover');
			$(this).children('ul').show();
	},function(){
		if($(this).children('.rubrique').hasClass('hover')){
			$(this).children('.rubrique').removeClass('hover');
			$(this).children('ul').hide();
		}
	});
	
	/* sous menu pour un détail d'un stage */
	$('.inner_content ul.menu-liste').hide();
	$('.inner_content .link-list span').toggle(
		function(){
			$('.inner_content ul.menu-liste').show();
		},function(){
			$('.inner_content ul.menu-liste').hide();
	});
	
	/* affichage stage étranger */
	displayStage();
	$('.ancre_stage a').click(function(){
		$('.ancre_stage a.active').removeClass('active');
		$(this).addClass('active');
		displayStage();
		return false;
	});
	
	/* resizer les thumbs et l'image principale de la galeries */
	$('.thumbs li img').load(function(){
		resizeImg(45,45,$(this));
	});
	$('.big_image_normal img').load(function(){
		resizeImg(350,232,$(this));
	});
	$('.big_image_grande img').load(function(){
		resizeImg(645,232,$(this));
	});
	
	/* changement d'image au clic de la thumbs */
	$('.thumbs li img').click(function(){
		$('.big_image img').attr('src',$(this).attr('src'));
		$('.thumbs .active').removeClass('active');
		$(this).parent().addClass('active');
	});
	
});
function displayStage(){
	var $active = $('.ancre_stage .active').attr('id');
	$('.stage_etranger').hide();
	$('#'+$active+'se').show()
}

function resizeImg(maxWidth, maxHeight, $this){
	if($this.width() < $this.height()){
		$this.width(maxWidth);
	}else {
		$this.height(maxHeight);
	}
}

function slideHome(){
    /* position of the <li> that is currently shown */
    var current = 0;
    
	$('#header .nav .rubrique').mouseover(function(e){
		var $this = $(this);
		/* if we hover the current one, then don't do anything */
		if($this.parent().index() == current) return;
		/* item is bg1 or bg2 or bg3, depending where we are hovering */
		var item = e.target.id;
		
		if(item == 'bg1' || current == 2){
			/* if we hover the first <li> or if we come from the last one, then the images should move left -> right */
			$('.slide_home > li').animate({backgroundPosition:"-800px 0"},0).removeClass('bg1 bg2 bg3 bg4 bg5 bg6 bg7').addClass(item);
			move(1,item);
		}
		
		else{
			/* if we hover the first <li> or if we come from the last one, then the images should move right -> left */
			$('.slide_home > li').animate({backgroundPosition:"800px 0"},0).removeClass('bg1 bg2 bg3 bg4 bg5 bg6 bg7').addClass(item);
			move(0,item);
		}
		/* change the current element */
		current = $this.parent().index();
	});
				
    /*
    dir:1 - move left->right
    dir:0 - move right->left
     */
	function move(dir,item){
        if(dir){
            $('#first').stop().animate({backgroundPosition: '0px 0px'},200)
            $('#second').stop().animate({backgroundPosition:"-207px 0px"},300);
            $('#third').stop().animate({backgroundPosition:"-420px 0px"},400);
        }
        else{
            $('#first').stop().animate({backgroundPosition:'0px 0px'},400);
            $('#second').stop().animate({backgroundPosition:'-207px 0px'},300);
            $('#third').stop().animate({backgroundPosition:'-420px 0px'},200);
        }
    }
}; 

