$(function() {
	// Control du diaporama d'images
	var diaporama = {
		size: 0,
		queue: [],
		started: false,
		init:function(queue, options) {
			var loader = this, k;
			this.size = queue.length;
			for (k in options) {
				this[k] = options[k];
			}						
			if (typeof loader.start == 'function') {
				loader.start();
			}
			$.each(queue, function() {
				loader.load(this);
			});
		},
		load:function(href) {
			var loader = this;
			if (!loader.started) {
				loader.started = true;
			}
			var img = $(new Image());
			img.load(function() {
				loader.queue.push(img);
				if (loader.queue.length == loader.size && typeof loader.complete == 'function') {
					loader.complete();
				}
			}).error(function(event) {
				if (typeof loader.error == 'function') {
					loader.error(event);
				}
			});
			img.attr('src', href);
		},
		start:null,
		complete:null
	};
	
	// Initialise le diaporama d'images
	diaporama.init([
		'_images/_accueil/_diaporama/image1.jpg',
		'_images/_accueil/_diaporama/image2.jpg',
		'_images/_accueil/_diaporama/image3.jpg',
		'_images/_accueil/_diaporama/image5.jpg',
		'_images/_accueil/_diaporama/image7.jpg'
	], {
		complete:function() {
			var cur, transition;
			$.each(this.queue, function() {
				$('#diaporama').append(this);					
			});
			
			transition = function() {
				var next, current;
				if (cur == null) {
					cur = 0;
				} else if (cur == diaporama.size-1) {
					current = diaporama.queue[cur];
					cur = 0;
				} else {
					current = diaporama.queue[cur++];
				}
				next = diaporama.queue[cur];
				
				if (current) {
					current.css('z-index', '2');
				} else {
					next.css('opacity', 0);
				}
				next.css('z-index', '1');
				next.show();
				if (current) {
					current.animate({
						opacity:0
					}, 1000, function() {
						$(this).hide();
						$(this).css('opacity', '');
						$(this).css('z-index', '');
					});
				} else {
					next.animate({
						opacity:1
					}, 1000, function() {
						$(this).css('opacity', '');
						$(this).css('z-index', '');
					});
				}
				
				/*
				if (current) {
					current.css('z-index', '1');
				}
				next.css('left', next.width());
				next.css('z-index', '2');
				next.show();
				next.animate({
					left:0
				}, 1000, function() {
					if (current) {
						current.css('z-index', '');
						current.hide();
					}
					$(this).css('left', '');
					$(this).css('z-index', '');
				});
				*/
				
			};
			transition();
			setInterval(transition, 5000)
		}
	});
});