﻿var curBook;
var allBooks;

function countBooks(){
	var items = document.getElementsByTagName('div');
	var current = items.length;
	var all = 0;
	for(d=0;d<current;d+=1){
		if(items[d].className=='bookbrowser'){
			all++;
		}
	}
	return all;
}

function iniBooks(){
	// Count books
	var all = countBooks();
	
	// Determine previous / next buttons
	var navi_prev_active = document.getElementById('navi_prev_active');
	var navi_prev_inactive = document.getElementById('navi_prev_inactive');
	var navi_next_active = document.getElementById('navi_next_active');
	var navi_next_inactive = document.getElementById('navi_next_inactive');
	
	if(all==0 || all==1){
		navi_prev_inactive.style.display = 'block';
		navi_next_inactive.style.display = 'block';
	}else{
		navi_prev_inactive.style.display = 'block';
		navi_next_active.style.display = 'block';
	}
	
	// Set current book
	curBook = 1;
	
	// Update all books variable
	allBooks = all;
}

function removeBooks(){
	var items = document.getElementsByTagName('div');
	var current = items.length;
	for(d=0;d<current;d+=1){
		if(items[d].className=='bookbrowser'){
			items[d].style.display = 'none';
		}
	}
}

function toogleNavi(){
	var navi_prev_active = document.getElementById('navi_prev_active');
	var navi_prev_inactive = document.getElementById('navi_prev_inactive');
	var navi_next_active = document.getElementById('navi_next_active');
	var navi_next_inactive = document.getElementById('navi_next_inactive');
	
	if(curBook>1){
		navi_prev_active.style.display = 'block';
		navi_prev_inactive.style.display = 'none';
	}else{
		navi_prev_active.style.display = 'none';
		navi_prev_inactive.style.display = 'block';
	}
	if(curBook==allBooks){
		navi_next_active.style.display = 'none';
		navi_next_inactive.style.display = 'block';
	}else{
		navi_next_active.style.display = 'block';
		navi_next_inactive.style.display = 'none';
	}
}

function toogleBook(f){
	if(f=='back'){
		if(curBook>1){
			removeBooks();
			curBook--;
			document.getElementById('bookbrowser'+curBook).style.display = 'block';
		}
	}
	if(f=='forward'){
		if(curBook<allBooks){
			removeBooks();
			curBook++;
			document.getElementById('bookbrowser'+curBook).style.display = 'block';
		}
	}
	toogleNavi();
}