function diaporama(divId, vitesse){
	// on part juste d'une div avec un format d�fini
	JQobj = $('#'+divId);
	JQobj.css('overflow', 'hidden');
	
	// redim des images
	nbimg = 0;
	JQobj.find('img').each(function(){
		$(this).css('float', 'left');
		$(this).css('visibility', 'hidden');
		$(this).load(function(){
			diaporama_img_resize($(this));
			$(this).css('visibility', 'visible');
		});
		nbimg++;
	});
	
	// redim
	JQobj.find('.conteneur').css('width', (nbimg*JQobj.width()+10)+'px');
	
	// interactivite
	if(vitesse == undefined){
		vitesse = 500;
	}
	JQobj.click(function(){
		diaporama_scrolleBy(1, $(this), vitesse);
	});
}

function diaporama_img_resize(JQimg){
	L = JQimg.width();
	H = JQimg.height();	
	Lmax = JQimg.parent().parent().parent().width();
	Hmax = JQimg.parent().parent().parent().height();
	
	//alert(L+' / '+JQimg.parent().parent().parent().attr('id')+' : '+JQimg.parent().parent().parent().width());
	
	// no cropping
	if(Hmax/Lmax < H/L){
		JQimg.width(Hmax*L/H);
		JQimg.height(Hmax);
		// marge auto
		//alert('margin right auto : Lmax='+Lmax+' / imgWidth='+JQimg.width()+' > '+(Lmax-JQimg.width())+'px');
		JQimg.css({'margin-top': '0px'});	
		JQimg.css({'margin-left': '0px'});	
		JQimg.css({'margin-right': ((Lmax-JQimg.width()))+'px'});	
	} else {
		JQimg.width(Lmax);
		JQimg.height(Lmax*H/L);
		// marge auto
		JQimg.css({'margin-top': ((Hmax-JQimg.height())/2)+'px'});	
		JQimg.css({'margin-left': '0px'});		
		JQimg.css({'margin-right': '0px'});	
	}
}

function diaporama_scrolleBy(coef, JQobj, vitesse){
	if(typeof JQobj == 'string'){
		JQobj = $('#'+JQobj);
	}
	if(vitesse == undefined){
		vitesse = 500;
	}
	if(!JQobj.find('.conteneur').is(':animated')){	
		pas = JQobj.width();
		curmargin = parseFloat(JQobj.find('.conteneur').css('margin-left'));
		nbimg = diaporama_nbimg(JQobj);
		//curpos = diaporama_getpos(JQobj);
		next = curmargin-coef*pas;
		limite = -1*(nbimg-1)*pas;
		if(next<limite){
			next = 0;
		}
		if(next>0){
			next = limite;
		}	
		JQobj.find('.conteneur').animate({
			'margin-left': next+'px'
		}, vitesse);
	}
}

function diaporama_scrolleTo(index, JQobj, vitesse){
	if(typeof JQobj == 'string'){
		JQobj = $('#'+JQobj);
	}
	if(vitesse == undefined){
		vitesse = 500;
	}
	if(!JQobj.find('.conteneur').is(':animated')){	
		pas = JQobj.width();
		next = -1*(index-1)*pas;
		nbimg = diaporama_nbimg(JQobj);
		limite = -1*(nbimg-1)*pas;
		if(next<limite){
			next = 0;
		}
		if(next>0){
			next = limite;
		}	
		JQobj.find('.conteneur').animate({
			'margin-left': next+'px'
		}, vitesse);
	}
}

function diaporama_auto(JQobj, delai, vitesse){
	if(typeof JQobj == 'string'){
		JQobj = $('#'+JQobj);
	}
	if(delai == undefined){
		delai = 4000;
	}
	if(vitesse == undefined){
		vitesse = 500;
	}
	autoplay = setTimeout(function(){
		diaporama_scrolleBy(1, JQobj, vitesse);
		diaporama_auto(JQobj, delai, vitesse);
		}, delai
	);
}
function diaporama_stop(JQobj){
	if(typeof JQobj == 'string'){
		JQobj = $('#'+JQobj);
	}
	clearTimeout(autoplay);
}


function diaporama_getpos(JQobj){
	if(typeof JQobj == 'string'){
		JQobj = $('#'+JQobj);
	}
	// position avant
	pas = JQobj.width();
	curpos = parseFloat(JQobj.find('.conteneur').css('margin-left'))/pas;
	return curpos;
}
function diaporama_nbimg(JQobj){
	if(typeof JQobj == 'string'){
		JQobj = $('#'+JQobj);
	}
	nbimg = 0;
	JQobj.find('img').each(function(){
		diaporama_img_resize($(this));
		nbimg++;
	});
	return nbimg;
}

function diaporama_resize(JQobj, curpos){
	if(typeof JQobj == 'string'){
		JQobj = $('#'+JQobj);
	}
	JQobj.find('.conteneur').css('width', (nbimg*JQobj.width()+30)+'px');
	JQobj.find('.conteneur').css('height', JQobj.height()+'px');
	// on corrige le scroll...
	pas = JQobj.width();
	JQobj.find('.conteneur').css('margin-left', curpos*pas);
	
	nbimg = 0;
	JQobj.find('img').each(function(){
		diaporama_img_resize($(this));
		nbimg++;
	});
}
function diaporama_fullscreen_update(){
	curpos = diaporama_getpos(JQobj);	
	// sauvegarde de l'état inital
	//diaporama_nofullscreen = {'position':JQobj.css('position'), 'z-index':JQobj.css('z-index'), 'top':JQobj.css('top'), 'left':JQobj.css('left'), 'width':JQobj.css('width'), 'height':JQobj.css('height')}
	// redim du conteneur
	JQobj.css({'position':'fixed', 'z-index':101, 'top':0, 'left':0, 'width':$(window).width(), 'height':$(window).height()});		
	// redim des images
	diaporama_resize(JQobj, curpos);
	// redim du fond
	$('#fullscreenbkg').css({'position':'fixed', 'z-index':100, 'left':0, 'top':0, 'width':$(window).width(), 'height':$(window).height(), 'background-color':'#000000'});
}


function diaporama_fullscreen(JQobj){
	if(typeof JQobj == 'string'){
		JQobj = $('#'+JQobj);
	}
	
	curpos = diaporama_getpos(JQobj);
	
	// sauvegarde de l'état inital
	diaporama_nofullscreen = {'position':JQobj.css('position'), 'z-index':JQobj.css('z-index'), 'top':JQobj.css('top'), 'left':JQobj.css('left'), 'width':JQobj.css('width'), 'height':JQobj.css('height')}
	
	// redim du conteneur
	JQobj.css({'position':'fixed', 'z-index':101, 'top':0, 'left':0, 'width':$(window).width(), 'height':$(window).height()});
	// rustine si mauvaise imbrication
	JQobj.find('.diaporama').css({'width':$(window).width(), 'height':$(window).height()});
	
	// redim des images
	diaporama_resize(JQobj, curpos);
	
	// fond noir
	$('body').append('<div id="fullscreenbkg"></div>');
	$('#fullscreenbkg').css({'position':'fixed', 'z-index':100, 'left':0, 'top':0, 'width':$(window).width(), 'height':$(window).height(), 'background-color':'#000000'});
	
	// instructions de sortie
	$('body').append('<div id="fullscreenbkg"></div><div id="fullscreentmp"><img src="plugins/diaporama/img/fullscreen-instruction.png" /></div>');
	$('#fullscreentmp').css({'position':'fixed', 'z-index':102, 'left':($(window).width()/2-473/2), 'top':($(window).height()/2-50/2)});
	$('#fullscreentmp').delay(3000).fadeOut(1000, function(){
		$(this).remove();	
	});
	
	// esc pour quitter
	diaporama_fullscreen_JQobj = JQobj;
	$(window).keydown(function(event) {
		event.preventDefault();
		
		if (event.which == 27) {	
			$(window).unbind('resize', diaporama_fullscreen_update);
			$(window).unbind('keydown');
			//
			curpos = diaporama_getpos(diaporama_fullscreen_JQobj);
			JQobj.css(diaporama_nofullscreen);
			diaporama_resize(diaporama_fullscreen_JQobj, curpos);
			// auto-destruction
			$('#fullscreenbkg').remove();
			$('#fullscreentmp').remove();
		}
	});
	
	// onresize
	$(window).bind('resize', diaporama_fullscreen_update);

}
