﻿/// <reference path="jquery-1.2.6.js" />

// Initialise after page has loaded
$(document).ready(function() {
	initialiseImageViewer();
});

function initialiseImageViewer() {
	var id = 0;
	
	// Hide 'previous' link and bind click event
	$('#link-previous').hide().bind('click', function(event) {
		id = id - 1;
		displayImage(id);
		event.preventDefault();
	});

	// Show 'next' link and bind click event
	$('#link-next').show().bind('click', function(event) {
		id = id + 1;
		displayImage(id);
		event.preventDefault();
	});

	// Preload next image
	var img = new Image();
	img.src = images[id + 1].src;
}
function displayImage(id) {
	//console.log('image: ' + (id + 1) + ' of ' + images.length);

	// Hide links
	$('#link-previous, #link-next').hide();

	// Show links if image next/previous exists
	if (id >= 1) {
		// Show previous link
		$('#link-previous').show();
	}
	if (id + 1 < images.length) {
		// Show next link
		$('#link-next').show();

		// Preload next image
		var img = new Image();
		img.src = images[id + 1].src;
	}

	// Fade out image and title
	$('#image, #title').fadeOut(function() {
		// Set new image attributes
		$('#image').attr({
			src: images[id].src,
			alt: images[id].title,
			title: images[id].title
		});

		// Set new title text
		$('#title').text(images[id].title);

		// Fade in new image and title
		$('#image, #title').fadeIn();
	});
}
