$(document).ready(function(){
	
	redimfond();
	
	rotation();
	
	vignettes();
	
	if($('#bas-de-page').length > 0 && $('#habillage').length > 0){
		
		//routine de redimentionement
		$(window).resize(function(){
			
			redimfond();
			
		});
		
	}
	
	
});



var redimfond = function (){
	
	if($('#bas-de-page').length > 0 && $('#habillage').length > 0){
		
		var hauteur_contenu = $('#bas-de-page').offset().top;
		var largeur_page = $(window).width()
		var hauteur_page = $(document).height();
		
		var redim = 0;
		//si le contenu est plus petit que la fenetre, on redimensionne a la taille de la fenetre
		if(hauteur_page > hauteur_contenu){
			redim = hauteur_page;
		}
		else{
			redim = hauteur_contenu;
		}
		
		//redimensionnement
		$('#habillage').height(redim);
		//bidouille pour IE
		var decal = 0;
		if($.browser.msie){
			decal = 1;
		}
		$('#habillage').width(largeur_page - decal);
		
	}
	
};



var rotation = function(){
	
	if($('.habillage').length > 1){
		
		//on flag le premier actif et first
		$('.habillage:first').addClass('active first');
		
		activevignette();
		
		//contenu
		$('#espace-'+$('.habillage:first').attr('id')).fadeIn(transition_fond);
		
		//on flag le dernier
		$('.habillage:last').addClass('last');
		
		clearTimeout(timer);
		// {{{ COR : Correction de l'erreur js quand il y a qu'un seul slideShow
	 	if($('.habillage').length > 1){
			timer = setTimeout(function(){
					
				next();
				
			}, pause_fond);
		}
		// COR }}}
		
	}
	else{
		
		var _esp = $('.espace');
		
		if(_esp.length > 0){
			
			$('#espace-'+$('.habillage:first').attr('id')).show();
			
		}
		
	}
	
};

var next = function(){
	var _active = $('.habillage.active');
	
	if(_active.hasClass('last')){
		var _next = $('.habillage:first');
	}
	else{	
		var _next = _active.next();
	}
	//masquage de la courante et affichage de la prochaine
	_active.removeClass('active').fadeOut(transition_fond);
	_next.addClass('active').fadeIn(transition_fond);
	
	//contenu
	$('.espace').fadeOut(transition_fond);
	$('#espace-'+_next.attr('id')).fadeIn(transition_fond);
	
	activevignette();
	clearTimeout(timer);
	// {{{ COR : Correction de l'erreur js quand il y a qu'un seul slideShow
 	if($('.habillage').length > 1){
		timer = setTimeout(function(){
				
			next();
			
		}, pause_fond);
	}
	// COR }}}
};

var activate = function(){
	
	var _active = $('.habillage.active');
	
	var _next = $('#'+$('.ap-vignette.active').attr('rel'));
	
	//masquage de la courante et affichage de la prochaine
	_active.removeClass('active').fadeOut(transition_fond);
	_next.addClass('active').fadeIn(transition_fond);
	
	//contenu
	$('.espace').fadeOut(transition_fond);
	$('#espace-'+_next.attr('id')).fadeIn(transition_fond);
	
	activevignette();
	clearTimeout(timer);
	// {{{ COR : Correction de l'erreur js quand il y a qu'un seul slideShow
	if($('#habillage').length > 1){
	  timer = setTimeout(function(){
	
			next();
			
		}, pause_fond);
	}
	// COR }}}	
	
};

var timer = Object;

$(document).blur(function(){clearTimeout(timer);});
$(document).focus(function(){
 // {{{ COR : Correction de l'erreur js quand il y a qu'un seul slideShow
 if($('#habillage').length > 1){	
  timer = setTimeout(function(){
			
		next();
		
	}, pause_fond);
 }
 // COR }}}
});

var activevignette = function(){
	
	var id = $('.habillage.active').attr('id');
	
	$('.ap-vignette').removeClass('active');
	
	//activation de la premiere vignette
	$('.ap-vignette[rel="'+id+'"]').addClass('active');
	
};

var vignettes = function(){
	
	if($('.ap-vignette').length > 0){
		
		$('.ap-vignette').click(function(){
			
			//si deja actif on ne fait rien
			if(!$(this).hasClass('active')){
				
				$('.habillage.active').removeClass('.active');
			
				//activation de la vignette
				$('.ap-vignette').removeClass('active');
				$(this).addClass('active');
				
				activate();
				
			}
			
		});
		
	}
	
};

