function domready_conteneur() {
	$('conteneur').fade('hide');
	
	var dim = window.getSize();
	var d = $('conteneur').getSize();
	var diff_height = Math.floor((dim.y - d.y)/2);
	var diff_width = Math.floor((dim.x - d.x)/2);
	if(diff_height > 0) {
		$('conteneur').setStyles({
			'top': diff_height + 'px',
			'margin-top': '0px'
		});
	} else {
		$('conteneur').setStyles({
			'top': '0px',
			'margin-top': '0px'
		});
	}
	if(diff_width > 0) {
		$('conteneur').setStyles({
			'left': diff_width + 'px',
			'margin-left': '0px'
		});
	} else {
		$('conteneur').setStyles({
			'left': '0px',
			'margin-left': '0px'
		});
	}
	resize_cache(diff_width);
}

window.addEvent('domready', domready_conteneur);

function load_conteneur() {
	$('conteneur').setStyle('visibility', 'visible');
	$('conteneur').fade('in');
}

function unload_conteneur() {
	$('conteneur').fade('out');
}

window.addEvent('load', function() {
	setTimeout(load_conteneur, 500);
});

window.addEvent('unload', function() {
	setTimeout(unload_conteneur, 500);
});

function resize_conteneur() {
	var dim = window.getSize();
	var d = $('conteneur').getSize();
	var diff_height = Math.floor((dim.y - d.y)/2);
	var diff_width = Math.floor((dim.x - d.x)/2);
	var fx = new Fx.Morph('conteneur', {
		duration: 1000,
		transition: Fx.Transitions.Quint.easeOut
	});
	if(diff_height > 0 && diff_width > 0) {
		fx.start({
			'top': diff_height + 'px',
			'margin-top': '0px',
			'left': diff_width + 'px',
			'margin-left': '0px'
		});
	} else if(diff_height <= 0 && diff_width <= 0) {
		fx.start({
			'top': '0px',
			'margin-top': '0px',
			'left': '0px',
			'margin-left': '0px'
		});
	} else if(diff_height <= 0 && diff_width > 0) {
		fx.start({
			'top': '0px',
			'margin-top': '0px',
			'left': diff_width + 'px',
			'margin-left': '0px'
		});
	} else if(diff_height > 0 && diff_width <= 0) {
		fx.start({
			'top': diff_height + 'px',
			'margin-top': '0px',
			'left': '0px',
			'margin-left': '0px'
		});
	}
	resize_cache(diff_width);
}
window.addEvent('resize', resize_conteneur);

function click_lien(ref) {
	$$(ref).each(function(item, index) {
		var href = item.href;
		item.href = 'javascript:void(0)';
		item.addEvent('click', function() {
			unload_conteneur();
			setTimeout(function() {
				location.href = href;
			}, 500);
		});
	});
}

function resize_cache(diff_width) {
	var leftcache = $('cache').getStyle('left').toInt();
	var widthcache = $('cache').getStyle('width').toInt();
	
	$('cache').setStyle('left', leftcache-diff_width);
	$('cache').setStyle('width', widthcache+diff_width);
}

var temps = 0;
var mousex = 0;
var divx = 0;
var diffx = 0;

function cache_vignette(limite) {
	var vignettes = $$('.vignette');
	vignettes.each(function(vignette){
		var id = vignette.getProperty('id');
		//var id = 'vignette11';
		
		var x = $(id).getPosition().x;
		if (x < limite) {
			//$(id).setStyle('display', 'hidden');
			$(id).setStyle('visibility', 'hidden');
		} else if (x >= limite) {
			//$(id).setStyle('display', 'inline');
			$(id).setStyle('visibility', 'visible');
		}
		//$('cache').set('html', x+" "+limite);
	});
}

function move_div(div) {
	temps = 0;
	var divactif = 'ligne_'+div;
	var margin = $(divactif).getStyle('margin-left').toInt();
	var widthdiv = $(divactif).getStyle('width').toInt();
	var diffwidth = 0-(widthdiv-550);
	
	if (diffx <= 200) {
		//droite
		if (margin < 0) {
			$(divactif).setStyle('margin-left', margin+3);
		}
	} else if (diffx > 350) {
		// gauche
		if (margin > diffwidth) {
			$(divactif).setStyle('margin-left', margin-3);
		}
	}
	/*
	var limite = divx - 400;
	if (margin + limite > 0) {
		cache_vignette(limite);
	}
	*/
	//$('cache').set('html', divx+" "+mousex+" "+diffx);
	temps = setTimeout("move_div("+div+");", 20);
}

function scroll_ready() {
	if ($$('.ligne_images')) {
		var lignes = $$('.ligne_images');
		lignes.each(function(ligne){
			ligne.addEvents({
				'mouseenter': function() {
					var temp = ligne.getProperty('id');
					temp = temp.split('_');
					nb = temp[1];
					move_div(nb);
				},
				'mouseleave': function() {
					clearTimeout(temps);
				},
				'mousemove': function(event) {
					mousex = event.page.x;
					divx = $('page').getPosition().x;
					diffx = mousex-divx;
				}
			});
		});
	}
}

window.addEvent('domready', scroll_ready);

/*
var temps = 0;
function move_div(div, sens) {
	temps = 0;
	var divactif = 'ligne'+div;
	//divactif = divactif.toString();
	var margin = $(divactif).getStyle('margin-left').toInt();
	if(sens == "g") {
		$(divactif).setStyle('margin-left', margin-2);
		temps = setTimeout("move_div("+div+", 'g');", 10);
	} else if(sens == "d") {
		if(margin < 0) {
			$(divactif).setStyle('margin-left', margin+2);
			temps = setTimeout("move_div("+div+", 'd');", 10);
		}
	}
}

function scroll_ready() {
	if ($$('.move_div')) {
		var lignes = $$('.move_div');
		lignes.each(function(ligne){
			ligne.addEvents({
				'mouseenter': function() {
					var temp = ligne.getProperty('id');
					temp = temp.split('_');
					nb = temp[1];
					sens = temp[2];
					move_div(nb, sens);
				},
				'mouseleave': function() {
					clearTimeout(temps);
				}
			});
		});
	}
}

window.addEvent('domready', scroll_ready);
*/

	