<?php
define("ANS_CORE", "1.0");
include("data.php");

// Capture variables
$requestedCategory = addslashes($_GET['cat']);
$requestedAnswer = addslashes($_GET['ans']);

if ($requestedAnswer == "") {
	$requestedAnswer = -1;
}

if ($requestedCategory == "") {
	$requestedCategory = -1;
}

// Declare variables
// helps me think straight - I realize you don't need to in PHP
$isOnHomeLandingPage = false;
$category_nav_list = "";

// No selection, show home landing page
if ($requestedCategory < 0) {
	$content = base64_decode($homeLandingPage);
	$isOnHomeLandingPage = true;
}

// Not answering a question, show category landing page.
if ($requestedCategory > -1 && $requestedAnswer < 0) {
	$content = base64_decode($categoryLandingPages[$requestedCategory]) . "\n<p><ul class=\"questionList\">\n";
	$appendage = "";
	$answersOffset = 0;
	$max = 0;
	if ($requestedCategory > 0) {
		$answersOffset = $categories[$requestedCategory][1];
	}
	if ($requestedCategory == count($categories)-1) {
		$max = count($questionsAndAnswers);
	} else {
		$max = $categories[$requestedCategory+1][1];
	}
	
		for ($currentIterationCounter = 0 + $answersOffset; $currentIterationCounter < $max; $currentIterationCounter++) {
			$appendage .= "<li class=\"questionLine\"><a href=\"index.php?cat=$requestedCategory&ans=$currentIterationCounter\" class=\"questionLink\">" . base64_decode($questionsAndAnswers[$currentIterationCounter][0]) . "</a></li>";
		}
	$content .= $appendage . "</ul></p>";

}

// Answering a question!
if ($requestedAnswer > -1) {
	$content = "<h2>" . base64_decode($questionsAndAnswers[$requestedAnswer][0]) . "</h2>\n<div class=\"answer\">\n" . base64_decode($questionsAndAnswers[$requestedAnswer][1]) . "\n</div>";
}

// Generate nav list
$currentIterationCounter = 0;
foreach ($categories as $currentIteration) {
	$selectedCategoryVar = "";
	
	// Select the category?
	if ($currentIterationCounter == $requestedCategory) {
		$selectedCategoryVar = " class=\"current\"";
	}
	
	// Append to navigation list
	$category_nav_list .= "<li><a href=\"index.php?cat=$currentIterationCounter\"$selectedCategoryVar>" . base64_decode($currentIteration[0]) . "</a></li>";
	
	$currentIterationCounter++;
}


// Show home link
if ($showHomeLink) {	
	// Select home link?
	$selectedCategoryVar = "";
	if ($isOnHomeLandingPage) {
		$selectedCategoryVar = " current";
	}
	
	// Append to navigation list
	$category_nav_list = "<li><a href=\"index.php\" class=\"landingPageNavLink" . $selectedCategoryVar . "\">Home</a></li>" . $category_nav_list;
	
}

?>