/* Gallery for Bendigo Exhibition Centre */

var crntPage = 0;

$(document).ready(function() {
	$('#thumbnails li').click(loadImage);
	$('#controls a.prev').click(prevPage);
	$('#controls a.next').click(nextPage);
	
	setPage(0);
	$('#thumbnails li:first').click();
});

function loadImage(event) {
	var img = document.createElement('img');
	
	$('#thumbnails li').removeClass('selected');
	$(this).addClass('selected');

	img.rel = $('img', this).attr('rel');
	img.alt = $('img', this).attr('alt');

	img.onload = setImage;
	img.src = $('img', this).attr('src').replace('_thumb', '');
	
	event.preventDefault();
}

function setImage() {
	var url = this.src;
	
	// put top image onto bottom
	$('#image .bottom').attr('src', $('#image .top').hide().attr('src'));
	
	$('#image .top').attr('src', url).fadeIn(500);
	$('#image p').html('<strong>Photo ' + this.rel + ':</strong> ' + this.alt);
}

function setPage(num) {
	var pages = $('#thumbnails ul').length;
	
	if (num < 0 || num >= pages) {
		return;
	}
	
	$('#thumbnails .wrapper').animate({ 'margin-top': -1 * num * 288}, 500);
	
	if (pages == 1) {
		$('#controls').hide();
	} else {
		$('#controls').show();
		
		if (num == 0) {
			$('#controls .prev img').attr('src', 'images/btn-previous-off.gif');
		} else {
			$('#controls .prev img').attr('src', 'images/btn-previous-on.gif');
		}

		if (num >= (pages - 1)) {
			$('#controls .next img').attr('src', 'images/btn-next-off.gif');
		} else {
			$('#controls .next img').attr('src', 'images/btn-next-on.gif');
		}
	}
	
	crntPage = num;
}

function prevPage(event) {
	setPage(crntPage - 1);
	event.preventDefault();
}

function nextPage(event) {
	setPage(crntPage + 1);
	event.preventDefault();
}
