function img_loader(urlimg){
	if ( typeof urlimg !== 'undefined' ){
	 	var newsrc = urlimg.replace(/-100/, '');
		jQuery('.main-photo').addClass('loading');
		jQuery('.main-photo img').fadeOut('fast', function (){
			jQuery('.main-photo img').remove();
			var img = new Image();
			jQuery(img).load(function () {
			  jQuery(this).css('display','none');
			  jQuery('.main-photo').append(this);
			  var xmax = 550;
			  var ymax = 400;
				if ( jQuery('.main-photo img').width() > xmax || jQuery('.main-photo img').height() > ymax ){
					var R = jQuery('.main-photo img').width()/jQuery('.main-photo img').height();
					if (( jQuery('.main-photo img').width()-xmax ) > ( jQuery('.main-photo img').height()-ymax )){
					  jQuery('.main-photo img').width( xmax );
						jQuery('.main-photo img').height( xmax/R );
					}
					else{
					  jQuery('.main-photo img').height( ymax );
					  jQuery('.main-photo img').width( ymax*R );
					}
				}
				jQuery(this).width(jQuery('.main-photo img').width());
				jQuery(this).height(jQuery('.main-photo img').height());
				var margin = 0.5*(xmax - jQuery('.main-photo img').width());
				jQuery('.main-photo').animate({
					width:jQuery('.main-photo img').width(),
					height:jQuery('.main-photo img').height(),
					marginLeft:margin
					}, 100 );
			  jQuery(this).fadeIn('fast');
			  jQuery('.main-photo').removeClass('loading');
			  jQuery('.main-photo').attr('title', jQuery('strong.titre').html());
		  }).attr('src', newsrc );
		});
		jQuery('.vignette').children().each(function(){
		  if ( jQuery(this).attr('src') == urlimg )
				jQuery(this).addClass('img-select');
			else
			  jQuery(this).removeClass('img-select');
		});
	}
}
function start_diapo(){
  Intdiapo = setInterval( 'diapo()', 4000 );
	jQuery('.btn-diapo').html('diaporama<br />OFF').addClass('off');
  return Intdiapo;
}
function stop_diapo(){
	if ( diapo_on )
  	clearInterval( Intdiapo );
  jQuery('.btn-diapo').html('diaporama<br />ON').removeClass('off');
  jQuery( '.img-select' ).removeClass( 'img-select' );
  return false;
}
function diapo(){
  var nb = jQuery('.vignette').children().size();
  var regex = new RegExp( 'rang\-([0-9])' ) ;
  var match = regex.exec( jQuery('.img-select').attr('class') );
  var current = 0;
  if ( match )
  	var current = parseInt( match[1] );
  var next = current + 1;
	if( current == nb )
	  next = 1;
  img_loader( jQuery( '.rang-' + next ).attr('src') );
}

jQuery(document).ready(function(){
	img_loader( jQuery('img.sec-photo').attr('src') );
  jQuery('img.sec-photo').click(function(event){
    diapo_on = stop_diapo();
    img_loader( jQuery(this).attr('src') );
  });
  if( jQuery('.vignette').children().size() > 1 ){
    jQuery('.photo_contener').prepend('<span class="btn-diapo">diaporama<br />OFF</span>');
    diapo_on = start_diapo();
	}
	else
	  diapo_on = false;
	jQuery('.btn-diapo').click(function(){
	  if ( diapo_on )
	  	diapo_on = stop_diapo();
		else
		  diapo_on = start_diapo();
	});
});
