<?
	$pagetitle = "The Holy Bible";
	$EnforceSession = false;
	$SiteRoot = "../";
	include $SiteRoot."includes/includes.php";
	include "functions.php";
	include $SiteRoot."includes/header.php";

	// Begin Template
	$tp = new PhpTemplate;
	for($tp->StartFromFile("index.template"); $tp->NextAndEchoOutput();)
	{
		if($tp->IsEscape("BOOKS")) 
		{
			// Open it
			$dbx = fopen("bible.dbx", "rb");
			
			// Load the index location
			$index_loc = ReadInt($dbx);
			
			// Seek
			fseek($dbx, $index_loc, SEEK_SET);
			
			// Load the number of books
			$book_count = ReadInt($dbx);
			
			// Loop each book
			for($Bi = 0; $Bi < $book_count; $Bi++)
			{
				// Load the name
				$name = trim(fread($dbx, 64));
				
				// Load the chapter count
				$chapter_count = ReadInt($dbx);
				
				// Advance
				fseek($dbx, $chapter_count * 4, SEEK_CUR);
				
				// Run the name
				ExBookTemplate($tp->GetEscapeArgument(), $name, $Bi + 1, $book_count);
			}
			
			// Close it
			fclose($dbx);
		}
	}

	// Footer
	include $SiteRoot."includes/footer.php";


	function ExBookTemplate($template, $name, $onRow, $book_count)
	{
		// Begin Template
		$tp = new PhpTemplate;
		for($tp->Start($template); $tp->NextAndEchoOutput();)
		{
			if($tp->IsEscape("NAME")) echo $name;
			else if($tp->IsEscape("NAME-URI")) echo rawurlencode($name);
			else if($tp->IsEscape("IF-NOT-LAST") && $onRow < $book_count) $tp->PushArgumentToStream();
			else if($tp->IsEscape("IF-NAME") && strtolower($tp->GetParameter()) == strtolower($name)) $tp->PushArgumentToStream();
		}
	}
	
?>