var currentDiapo = 0;
var nbDiapo = 0;
$(document).ready(function()	{
	nbDiapo = $('#bg .ImgFond, #bg .mediaFond').length;
	
	$('img.ImgFond').maxImage({
		multiple: true,
		overflow: 'auto'
	});
			
	
	
	$('#bottom-menu .bouton-toggle').click(function(){
		// $('#bottom-menu .content').toggle( 'drop', {}, 500 );
	});
	
	$('#bottom-menu .bouton-selection').click(function(){
		// alert('ok');
		// $('#bottom-menu .liste').slideToggle("slow");
		$('#bottom-menu .menu-liste .liste').toggle( 'blind', {}, 500 );
	});
	
	$('#bottom-menu .boutton-diapo').click(function(){
		showDiapo($(this).dataset('id'));
	});
	
	if(nbDiapo > 1){
		$('#bg .ImgFond, #bg .mediaFond .bg').click(function(){
			showNextDiapo();
		});
	}
	$(document).keydown(function(event) {
		// 'left' : 37
		// 'up'   : 38
		// 'right': 39
		// 'down' : 40
		switch(event.keyCode){
			case 37 : showPreviousDiapo(); break;
			case 39 : showNextDiapo(); break;
			case 38 : document.location=nextProjectUrl; break;
			case 40 : document.location=previousProjectUrl; break;
		}
	});
	
	resizeVideo();
	$(window).resize(function(){
		resizeVideo();
	});
})

function resizeVideo(){
	var pageWidth = $(window).width()-20;
    var pageHeight = $(window).height()-5;
	
	$('.mediaFond').each(function(){
		var videoWidth = $('.mediaFond .video').width();
		var videoHeight = $('.mediaFond .video').height();
		
		var conWidth = pageWidth;
		var conHeight = pageHeight;
		
		if(pageWidth<videoWidth){
			conWidth = videoWidth;
		}
		
		if(pageHeight<videoHeight){
			conHeight = videoHeight;
		}
		
		$('.mediaFond .video').css({
			'top':      ((conHeight-videoHeight)/2),
			'left':     ((conWidth-videoWidth)/2)
		});
		
		$('.mediaFond .bg').css({
		  'overflow':   'hidden',
		  'width':      conWidth,
		  'height':     conHeight
		});
	});
	
}

function showDiapo(id_diapo){
	$('#media-'+currentDiapo).hide();
	incDiapo(id_diapo);
	$('#media-'+currentDiapo).show();
}


function showNextDiapo(){
	$('#media-'+currentDiapo).hide();
	incDiapo();
	$('#media-'+currentDiapo).show();
}

function showPreviousDiapo(){
	$('#media-'+currentDiapo).hide();
	decDiapo();
	$('#media-'+currentDiapo).show();
}

function incDiapo(id_diapo){
	if(id_diapo != undefined)
		currentDiapo = id_diapo;
	else
		currentDiapo++;
		
	if(currentDiapo == nbDiapo){
		currentDiapo = 0;
	}
}

function decDiapo(){
	currentDiapo--;
		
	if(currentDiapo < 0){
		currentDiapo = nbDiapo-1;
	}
}
