function showPhoto(elt)
{
	var src = elt.attr('src');
	
	if( src == null )
	{
		return;
	}
				
	var idx  = src.indexOf('?i=');
	var idx2 = src.indexOf('&w=');	
	
	src = src.substring(idx + 3, idx2);
	
	var photos = elt.parents('.photos').length > 0 ? elt.parents('.photos') : elt.parents('.photosdroite');
	var main = photos.find('.photo > img').get(0);
				
	main.src = root + src;
				
	photos.find("IMG").removeClass('current');
				
	elt.addClass('current');
}

function handlePagination(elt)
{
	var photos = elt.parents('.photos').length > 0 ? elt.parents('.photos') : elt.parents('.photosdroite');
				
	photos.find('.pagination > span.current').removeClass('current');
	elt.addClass('current');
	
	var name = elt.attr('name');
	
	photos.find('.vignettes > div.page:not(.hidden)').each(function()
	{
		$(this).addClass('hidden');	
		
		photos.find('.vignettes > div[name='+name+']').removeClass('hidden');
	});
}

$(function()
{
	$('.paragraphe').each(function()
	{
		$(this).find('.page > img').each(function()
		{
			var src = $(this).attr('src');
				
			var idx  = src.indexOf('?i=');
			var idx2 = src.indexOf('&w=');	
			
			src = src.substring(idx + 3, idx2);
			
			$(this).click(function()
			{
				showPhoto($(this), src);
			});
			
			$.preloadImages(root + src);
		});
		
		$(this).find('.pagination > span').each(function()
		{
			$(this).click(function()
			{
				handlePagination($(this));
			});
		});
	});
});

jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}
