$(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 = $(document).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;
		}
		
		
		$('body').height(redim);
		$('#habillage').height(redim);
		$('.fond-bas').height(redim);
		$('.fond-site').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);
		timer = setTimeout(function(){
			
			next();
			
		}, pause_fond);
		
	}
	
};

var next = function(){
	//alert('fd');
	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);
	timer = setTimeout(function(){
			
		next();
		
	}, pause_fond);
	
};

$(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 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);
	timer = setTimeout(function(){
			
		next();
		
	}, pause_fond);
	
};

var timer = Object;

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();
				
			}
			
		});
		
	}
	
};


/*
Copyright (c) 2009 Dimas Begunoff, http://www.farinspace.com

Licensed under the MIT license
http://en.wikipedia.org/wiki/MIT_License
*/
function imgpreload(a,b){if(b instanceof Function){b={all:b}}if(typeof a=="string"){a=[a]}var c=[];var t=a.length;var i=0;for(i;i<t;i++){var d=new Image();d.onload=function(){c.push(this);if(b.each instanceof Function){b.each.call(this)}if(c.length>=t&&b.all instanceof Function){b.all.call(c)}};d.src=a[i]}}if(typeof jQuery!="undefined"){(function($){$.imgpreload=imgpreload;$.fn.imgpreload=function(b){b=$.extend({},$.fn.imgpreload.defaults,(b instanceof Function)?{all:b}:b);this.each(function(){var a=this;imgpreload($(this).attr('src'),function(){if(b.each instanceof Function){b.each.call(a)}})});var c=[];this.each(function(){c.push($(this).attr('src'))});var d=this;imgpreload(c,function(){if(b.all instanceof Function){b.all.call(d)}});return this};$.fn.imgpreload.defaults={each:null,all:null}})(jQuery)}
