// Changes the image being displayed
function changeimg(newimg) {

	// Update main image
	$('#mainimage img').attr('src', newimg.src);
	$('#mainimage img').attr('alt', newimg.alt);
	$('#mainimage').attr('href', unescape(jQuery.url.setUrl(newimg.src).param("target")));
	$('#mainimage').attr('title', newimg.alt);

	// Set active thumbnail
	$('#thumbnails img').removeClass('active');
	newimg.className = 'active';

}

// Called when the main image is clicked
function mainimageclicked() {
	
	// Loop through each thumbnail
	$('#thumbnails a').each(function() {
		// Fire the click event for the corresponding thumbnail
		if (jQuery.url.setUrl(this.href).attr("relative") == $('#mainimage').attr('href')) $(this).click();
	});
	
}

// Adds events etc. to html
$(document).ready(function() {
	
	// Apply hover event to thumbnails
	$('#thumbnails img').mouseover(function(){changeimg(this);});
	
	// Apply lightbox to all links with rel="lightbox" set
	$('a[rel*=lightbox]').lightBox();
	
	// Apply click event to main image
	$('#mainimage').click(function(){mainimageclicked(); return false;});
 
});