var busy = false;
	var timer;
	var imgNext;
	var altBackground;
	$(document).ready(function() {
		
		var Navigateur = navigator.appName;
		
		slideBg('');
				
		$('#miniature img').click(function() {
			slideBg($(this));
		});
		
		function activeMini(img) {
			$('#miniature img').removeClass('mini_active');
			$(img).addClass('mini_active');
		}
		
		function slideBg(imgNext) {
			stopTimer();
			
			$('#background').css('display','none');
			$('#categorie_title span').html('');
			$('#spinner').css('display','inline');
			$('#background_old').css('display','inline');

			if(imgNext == '') {
				imgNext = $('img.mini_active').parent().next().children('img');
			}
			if(imgNext.attr('src') == undefined) imgNext = $('#miniature tr td:first-child').children('img');
			
			altBackground = imgNext.attr('alt');
			$('#categorie_title span').html(altBackground);
			$('#acces_portfolio a').each(function() { $(this).attr('href',imgNext.attr('rel')); $(this).attr('title','Visitez le portfolio '+imgNext.attr('alt')); });
			intro($(imgNext).attr('src').replace('/mini',''));
			activeMini(imgNext);
						
		}
		
		function preload_img(source){
			var img = new Image();
			img.src = source;			
			return img;
		}//fin function preload_img
			
		function intro(source){
			var img = preload_img(source);
			if(Navigateur != 'Microsoft Internet Explorer') {
				img.onload = function(){				
					initImage(img);
					if($('#background_old').attr('src') != '') {
						$('#background_old').fadeOut('slow');
					}
					$('#categorie_title span').html($('#background').attr('alt'));
					$('#background_old').css({width:$(window).width(),height:backgroundHeight});
					
					$('#categorie_title').css('top',(parseInt($('#slider').css('height'))/2)-70);
					$('#acces_portfolio').css('top',(parseInt($('#slider').css('height'))/2)-15);
					
					$("#background").fadeIn('slow',function() { 				
						$('#background_old').attr('src',$('#background').attr('src'));
						$('#spinner').css('display','none');
						$('#categorie_title').css('display','inline');
					});
					startTimer(4000);
				}
			}
			else {
				initImage(img);
					if($('#background_old').attr('src') != '') {
						$('#background_old').fadeOut('slow');
					}
					
					
					$('#background_old').css({width:$(window).width(),height:backgroundHeight});
					
					$('#categorie_title').css('top',(parseInt($('#slider').css('height'))/2)-70);
					$('#acces_portfolio').css('top',(parseInt($('#slider').css('height'))/2)-15);
					
					$("#background").fadeIn('slow',function() { 				
						
						$('#background_old').attr('src',$('#background').attr('src'));
						$('#spinner').css('display','none');
						$('#categorie_title').css('display','inline');
					});
					startTimer(4000);
			}
				
		}//fin function intro
		
		function initImage(img) {
			// Calcul de la hauteur de l'image
			var ratioWidth = parseInt($(document).width()) / parseInt(img.width);
			
			backgroundHeight = Math.round(ratioWidth * img.height);
			
			$('#slider').css({width:$(window).width(),height:backgroundHeight});
			
			// Au chargement de la page (une fois qu'on connait la taille de l'image), on affiche le bottom
			if($('#background').attr('src') == undefined) {
				$('#slider').append('<img src="" id="background" alt="" class="fullBg" style="z-index:50;" />');
				$('#slider').append('<img src="" id="background_old" alt="" class="fullBg" style="z-index:50;" />');
			}
			$('#background').css({width:$(window).width(),height:backgroundHeight});
			if($('#bottom').css('display') == 'none') {
				$('#logo').css('display','block');
				$('#bottom').css('display','block');
			}					
			$('#background').attr('src',img.src);
			$('#background').attr('alt',altBackground);
			
		}
		
		function startTimer(delay) {
			timer = setTimeout(function() { slideBg('') },delay);		
		}
		
		function stopTimer() {
			clearTimeout(timer);
		}
		
		$('#slider').hover(function() { $('#acces_portfolio').css('display','inline'); stopTimer(); },
								function() {$('#acces_portfolio').css('display','none'); startTimer(4000); });
		
		$(window).resize(function() {
			slideBg($('.mini_active'));
		});
		
	});
