function createLoader() 
{
	var _loader = new Image();
	_loader.src = '../images/album-ajax-loader.gif';
	return _loader;
}

function init() 
{
	// Preload the loader
	MM_preloadImages('../images/album-ajax-loader.gif');
	
	// Turn to page 1
	changePage(1);
}

function changePage(pageid)
{
	// Ensure the pageid is an integer
	pageid = parseInt(pageid, 10);

	// Make sure it is a valid page id
	if (pageid < 1 || pageid > numPages) return;

	// Show / Hide the left arrow
	if (pageid == 1) {
		$("#album-left-arrow").css({"display":"none"});
	} else {
		$("#album-left-arrow").css({"display":""});
	}

	// Show / Hide the right arrow
	if (pageid == numPages) {
		$("#album-right-arrow").css({"display":"none"});
	} else {
		$("#album-right-arrow").css({"display":""});
	}

	// Renew the page number
	for (i=1; i<=18; i++) {
		var pagenum = ((pageid - 1) * 18) + i;
		if (pagenum <= numPhotos) {
			document.getElementById("page-number-" + i).innerHTML = (pagenum < 10) ? "0" + pagenum : pagenum;
		} else {
			document.getElementById("page-number-" + i).innerHTML = "";
		}
	}

	// Update current_page
	current_page = pageid;

	// Display the 1st photo on this page
	displayPhoto((pageid - 1) * 18);
}

function prevPage() 
{
	changePage(current_page - 1);
}

function nextPage() 
	{
	changePage(current_page + 1);
}

function displayPhoto(photoid)
{
	// Clear the timer
	if (timer != null) {
		clearTimeout(timer);
		timer = null;
	}

	// Show the loader
	MM_swapImage('photo','','../images/album-ajax-loader.gif',1);

	// Configuration
	var img = links[photoid];
	var baseurl = 'http://projects.opencreative.com/lighthouse/';
	var thumbnail = '../thumb.php?src=' + img + '&w=500&h=340&zc=1&q=70';
	var previous_photo = current_photo;
	current_photo = photoid;

	// Bold the current page number
	$("#page-number-" + (previous_photo % 18 + 1)).css({"font-weight":""});
	$("#page-number-" + (current_photo % 18 + 1)).css({"font-weight":"bold"});

	// Show photo
	$("#photo_link").attr({"href":pathToRoot + img});
	$("#photo").attr({"src":thumbnail});

	// Start timer
	if (timer == null) {
		timer = setTimeout("play()", 5000);
	}
}

function displayPhotoByElement(e)
{
	var photoid = parseInt(e.innerHTML,10) - 1;
	displayPhoto(photoid);
}

function play() {
	var current_page_last_photo = (current_page * 18) - 1;
	if (current_photo == numPhotos - 1) {
		changePage(1);
	} else if (current_photo >= current_page_last_photo) {
		nextPage();
	} else {
		displayPhoto(current_photo + 1);
	}
}
