var total_photos = 0;
var current_photo = 0;
var thumbnails;

$(document).ready(function()
{
	thumbnails = $('div.photos a.img:not(.main)');
	total_photos = thumbnails.length;
	
	var standard = $('div.photos a.img.main');
	var caption = $('div.photos dd.caption');
	
	thumbnails.click(function()
	{	
		var img = standard.children('img');
		current_photo = thumbnails.index(this); 

		
		// Hide the image to show the loading icon behind it
		img.css('visibility', 'hidden');

		// The zoom to size is the href of the display image
		var zoom_to_image = $(this).attr('href');
		
		// The display size is taken from the classname of the photos div
		var display_size = $('div.photos').attr('class').match(/thumb|stamped|large/) || '';

		// Build the url for the new src display size
		var display_image = '';
		var image_clicked_src = $(this).children('img').attr('src');
		var current_size = image_clicked_src.match(/\/listings\/(tiny|thumb|[0-9]+)/)[1];
		current_size = (parseInt(current_size)) ? '' : current_size;
		var regex = (current_size) ? new RegExp('\/listings\/'+current_size+'\/') : new RegExp('\/listings\/')
		if (display_size) display_size += '/';		
		display_image = image_clicked_src.replace(regex, '/listings/'+display_size);
	
		img.attr('src', display_image);			// Change the display image
		standard.attr('href', zoom_to_image);	// Make the display image link to a larger version
		
		var new_caption = $(this).attr('title');
		
		// Show the image once it has loaded
		img.load(function()
		{
			img.css('visibility', 'visible');		
			caption.text(new_caption);
		});
		
		return false;
	});	
});