
function nextImage() {
	if (currentPage >= numPages) return;
	currentPage = currentPage + 1;

	updatePage();
	
}

function previousImage() {
	if (currentPage == 1) return;
	currentPage = currentPage - 1;

	updatePage();


}

function updatePage() {
	//description
	var descElement = document.getElementById('desc');
	descElement.innerHTML = descriptions[currentPage - 1];
	//page
	var pageElement = document.getElementById('page');
	pageElement.innerHTML = getVirtualPage(currentPage) + ' / ' + getBookPages(getBook(currentPage));
	//image
	var imgElement = document.getElementById('picFrame');
	imgElement.innerHTML = '<img src="images/impression' + currentPage + '.jpg" alt="' + descriptions[currentPage - 1] + '">';
	
	//books
	var tdElement;
	var i;
	for (i = 1; i < 6; i++) {
		tdElement = document.getElementById('selected_book' + i);
		if (getBook(currentPage) == i) {
			tdElement.style.backgroundImage = "url(images/sample_menu_bar.png)";
		} else {
			tdElement.style.backgroundImage = "";	
		}	
	}
}

function getBook(page) {
	var book = 1;
	if (page > 0 && page <= descriptions.length) {
		if (page < bookToPictureStart[1]) {
			book = 1;
		}
		if (page >= bookToPictureStart[1] && page < bookToPictureStart[2]) {
			book = 2;
		}
		if (page >= bookToPictureStart[2] && page < bookToPictureStart[3]) {
			book = 3;
		}
		if (page >= bookToPictureStart[3] && page < bookToPictureStart[4]) {
			book = 4;
		}
		if (page >= bookToPictureStart[4]) {
			book = 5;	
		}
	}
	return book;
}

function getVirtualPage(page) {
	var book = 1;
	if (page > 0 && page <= descriptions.length) {
		if (page < bookToPictureStart[1]) {
			book = page;
		}
		if (page > bookToPictureStart[1] && page < bookToPictureStart[2]) {
			book = page - bookToPictureStart[1] + 1;
		}
		if (page > bookToPictureStart[2] && page < bookToPictureStart[3]) {
			book = page - bookToPictureStart[2] + 1;
		}
		if (page > bookToPictureStart[3] && page < bookToPictureStart[4]) {
			book = page - bookToPictureStart[3] + 1;
		}
		if (page > bookToPictureStart[4]) {
			book = page - bookToPictureStart[4] + 1;
		}
	}
	return book;
}

function getBookPages(book) {
	var pages = 0;
	if (book == 5) {
		pages = 5;
	} else if (book == 4) {
		pages = 5;
	} else if (book == 3) {
		pages = 5;
	} else if (book == 2) {
		pages = 6;
	} else if (book == 1) {
		pages = 6;
	}
	
	return pages;
}
