<?
	$EnforceSession = false;
	$SiteRoot = "../";
	include $SiteRoot."includes/includes.php";
	include "functions.php";
		
	// Get the name
	$name = request("name");
	$chapter = intval(request("chapter"));
	$verse = intval(request("verse"));
	$toverse = intval(request("toverse"));
	$pagetitle = "The Holy Bible - " . $name;

	// Open it
	$dbx = fopen("bible.dbx", "rb");

	// Load it
	$chapter_loc = GetChapterLoc($dbx, $name, $chapter);
	
	// Validate
	if($chapter_loc <= 0)
	{
		echo "<h1>Page not found.</h1>";
		exit;
	}
	
	// Run the header
	include $SiteRoot."includes/header.php";
	
	// Begin Template
	$tp = new PhpTemplate;
	for($tp->StartFromFile("chapter.template"); $tp->NextAndEchoOutput();)
	{
		if($tp->IsEscape("NAME")) echo $name;
		else if($tp->IsEscape("NAME-URI")) echo rawurlencode($name);
		else if($tp->IsEscape("CHAPTER")) echo $chapter;
		else if($tp->IsEscape("VERSES")) 
		{
			// Create the segment array
			$verse_starts = array();
			$verse_lengths = array();
			
			// Seek to the location
			fseek($dbx, $chapter_loc, SEEK_SET);
			
			// Load the verse count
			$verse_count = ReadInt($dbx);
			
			// Loop each verse
			for($Vi = 1; $Vi <= $verse_count; $Vi++)
			{
				// Load the locations and size
				$verse_starts[$Vi] = ReadInt($dbx);
				$verse_lengths[$Vi] = ReadInt($dbx);
			}
			
			// Load the length of the compressed chapter
			$compressed_length = ReadInt($dbx);
			
			// Load the compressed text
			$compressed = fread($dbx, $compressed_length);
			
			// Decompress
			$text = gzuncompress($compressed);
			
			// Loop each verse
			for($Vi = 1; $Vi <= $verse_count; $Vi++)
			{
				// Are we hilighting
				$hilight = false;
				if($Vi == $verse) $hilight = true;
				else if($Vi >= $verse && $Vi <= $toverse) $hilight = true;
				
				// Execute the template
				ExVerseTemplate($tp->GetEscapeArgument(), $name, $chapter, $Vi, substr($text, $verse_starts[$Vi], $verse_lengths[$Vi]), $verse_count, $hilight);
			}
		}
	}

	// Close the file
	fclose($dbx);
	
	// Footer
	include $SiteRoot."includes/footer.php";

	function ExVerseTemplate($template, $name, $chapter_number, $verse_number, $text, $verse_count, $hilight)
	{
		// 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("CHAPTER")) echo $chapter_number;
			else if($tp->IsEscape("VERSE")) echo $verse_number;
			else if($tp->IsEscape("TEXT")) echo $text;
			else if($tp->IsEscape("IF-NOT-LAST") && $verse_number < $verse_count) $tp->PushArgumentToStream();
			else if($tp->IsEscape("IF-HILIGHT") && $hilight) $tp->PushArgumentToStream();
		}
	}
	
	function GetChapterLoc($dbx, $name, $chapter)
	{
		// 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
			$candidate_name = strtolower(trim(fread($dbx, 64)));

			// Load the chapter count
			$chapter_count = ReadInt($dbx);

			// If this is not our book
			if(strtolower($name) != $candidate_name) 
			{	
				fseek($dbx, $chapter_count * 4, SEEK_CUR);
				continue;
			}

			// Validate
			if($chapter < 1 || $chapter > $chapter_count)
				return 0;
				
			// Seek to our chapter
			fseek($dbx, ($chapter - 1) * 4, SEEK_CUR);

			// Load the index location
			return ReadInt($dbx);
		}
		
		// Book not found
		return 0;
	}
	
?>