/* usage
	chapters = new Chapters();
	onload = chapters.init(chapter_number);

	onclick = chapters.next();
*/
function Chapters() {
var chapterList = new Array();
var columnLength = 10;
var columns = 2;
var chapterDIV = "chapters-list";

var firstCol = 0;
var firstChap = 0;
var currentChap = 0;
var activeChap = 1;

this.init = function(chapter) {
	if(chapter!=null) activeChap = chapter;
	this.addChapter("ch01.php", "Sangacha");
	this.addChapter("ch02.php", "CSI");
	this.addChapter("ch03.php", "Visitors");
	this.addChapter("ch04.php", "Witness?");
	this.addChapter("ch05.php", "Sex Cells");
	this.addChapter("ch06.php", "Domestic Affair");
	this.addChapter("ch07.php", "Low Profile");
	this.addChapter("ch08.php", "Cat & Mouse");
	this.addChapter("ch09.php", "Regrettable");
	this.addChapter("ch10.php", "I.D. Please");
	this.addChapter("ch11.php", "Manuel");
	this.addChapter("ch12.php", "Frank Talk");
	this.addChapter("ch13.php", "Scary Monsters");
	this.addChapter("ch14.php", "Call Waiting");
	this.addChapter("ch15.php", "Emancipation");
	this.addChapter("ch16.php", "Hot &amp; Bothered");
	this.addChapter("ch17.php", "Brain Teasers");
	this.addChapter("ch18.php", "Clan Living");
	this.addChapter("ch19.php", "Televangelist");
	this.addChapter("ch20.php", "Flight Departing");
	this.addChapter("ch21.php", "eFibrillation");
	this.addChapter("ch22.php", "Porcelain Reverie");
	this.addChapter("ch23.php", "Meet &amp; Greet");
	this.addChapter("ch24.php", "Twenty Four");
	this.addChapter("ch25.php", "Vid Sadicious");
	this.addChapter("ch26.php", "Precipice");
	this.addChapter("ch27.php", "Office Visit");
	this.addChapter("ch28.php", "Grill 'n Chill");
	this.addChapter("ch29.php", "Dump Struck");
	this.addChapter("ch30.php", "Tea &amp; Sympathy");
	this.addChapter("ch31.php", "Evasive Arts");
	this.seek(activeChap);
}

this.chapters = function() { return chapterList;
}

this.addChapter = function(link, name) {
	chapter = new Chapter(link, name);
	chapterList.push(chapter);
}

this.seek = function(chapter) {
	currentChap = chapter-columnLength/2;
	currentChap = Math.min(currentChap, chapterList.length-columnLength);
	this.next();
}

this.begin = function() {
	firstCol = 0;
	firstChap = 0;

	this.showChapters();
}

this.end = function() {
	firstCol = Math.ceil(chapterList.length / columnLength);

	this.showChapters();
}

this.next = function() {
	firstChap = currentChap + columnLength;
	firstCol = Math.ceil(firstChap / columnLength);
	if(firstCol>Math.ceil(chapterList.length/columnLength)) firstCol = 0;
	
	this.showChapters();
	if(firstChap>=chapterList.length) firstChap = 0;
	
}

this.prev = function() {
	firstChap = currentChap - 1*columnLength;
	if(firstChap<=columnLength) firstChap = chapterList.length;
	firstCol = Math.ceil(firstChap / columnLength);

	this.showChapters();
}

this.showChapters = function showChapters() {
	firstCol = Math.max(0, firstCol-2);
	firstChap = firstCol*columnLength;
	firstChap = Math.max(0, firstChap);
	currentChap = firstChap;

	chapDiv = document.getElementById(chapterDIV);
	chapDiv.innerHTML = this.chapHTML();
	firstChap = currentChap;

	return;
}

this.chapHTML = function() {
	chapString = "\n<div class='list-left'>\n<ol class='num' start='" + (1+currentChap) + "'>";
	chapString += this.listItems();
	chapString += "\n</ol></div>";

	chapString += "\n<div class='list-right'>\n<ol class='num' start='" + (1+currentChap) + "'>";
	chapString += this.listItems();
	chapString += "\n</ol>\n</div>\n";

	return chapString;
}

this.listItems = function() {
	itemString = "";
	endList = Math.min(currentChap+columnLength, chapterList.length);
	for( ; currentChap<endList ; currentChap++) {
		itemString += chapterList[currentChap].chapItem();
	}
	return itemString;
}

function Chapter(link, name) {
this.pageName = link;
this.chapName = name;

this.chapItem = function() {
	itemString = "\n\t<li><a href='" + this.pageName + "'>" + this.chapName + "</a></li>";
	return itemString;
}
}	// Chapter
}	// Chapters

