<?
	require_once("includes/config.php");
	
	// retrieve categories for the post using a sub select query
	$category_result = mysql_query("SELECT category FROM categories WHERE id IN (SELECT category_id FROM posts2categories WHERE post_id = $post->id);");
	if($category_result === FALSE)
	{
		exit( "Could not query database: " . mysql_error() );
	}
	$numCategories = mysql_num_rows($category_result);
	
	// retrieve post comments
	$comment_result = mysql_query("SELECT * FROM comments where post_id = $post->id ORDER BY date asc");
	if($comment_result === FALSE)
	{
		exit( "Could not query database: " . mysql_error() );
	}
	$numComments = mysql_num_rows($comment_result);
?>

<div class="post">

	<? blog_displayfeedback($feedback, Feedback::PostAdded); ?>

	<div class="header">
		<? if($post_ShowTitleAsLink === TRUE) { ?>
				<h3><a href="<? echo blog_createpostlink($post->id) ?>"><? echo $post->title; ?></a></h3>
		<? } else { ?>
				<h3><? echo $post->title; ?></h3>
		<? } ?>
		<div class="date">Posted <? echo blog_formatdate(DATE_FORMAT, $post->date); ?> by <? echo $post->author; ?></div>
	</div>
	
	<div class="content">
		<? echo str_replace(array("\r\n", "\r", "\n"), "<br/>", htmlspecialchars($post->content)); ?>
	</div>

	<div class="footer">
		<ul>
			<?
				// Categories:  categorya, categoryb
				if($numCategories > 0)
				{
					echo "<li class=\"categories\">Categories: ";
					$categoryIndex = 0;
					while($category = mysql_fetch_object($category_result)) 
					{ 
						echo "<a href=\"#\">$category->category</a>";
						if($categoryIndex + 1 < $numCategories) echo ", ";
						$categoryIndex++;
					}
					echo "</li>";
				}
			?>
			<li class="comments"><a href="<? echo blog_createpostlink($post->id, "#comments"); ?>">Comments (<? echo $numComments; ?>)</a></li>
		</ul>
	</div>
	
	<? if($post_ShowComments === TRUE) { ?>
	
	<a name="comments"></a>
	<div class="comments">
		<h4>(<? echo $numComments; ?>) Comments on "<? echo $post->title; ?>"</h4>

		<? blog_displayfeedback($feedback, Feedback::CommentAdded); ?>
		
		<? while($comment = mysql_fetch_object($comment_result)) { ?>
		<div class="comment">
			<h5><? echo $comment->author; ?></h5>
			<div class="date"><? echo blog_formatdate(DATE_TIME_FORMAT, $comment->date); ?></div>
			<div class="content">
				<? echo str_replace(array("\r\n", "\r", "\n"), "<br/>", htmlspecialchars($comment->content)); ?>
			</div>
		</div>
		<? } ?>

		<a name="leavecomment"></a>
		<h4>Leave a comment</h4>

		<? blog_displayfeedback($feedback, Feedback::FAILURE); ?>
		
		<p>To leave a comment, enter your name, email, and a comment.</p>
		<form id="addComment" action="addComment.php" method="post">
			<input type="hidden" name="id" value="<? echo $post->id; ?>"/>
			<p><input type="text" id="author" name="author" size="25" maxlength="75" tabindex="10" value="<? if(isset($feedback)) { echo $feedback->getValue('author'); } ?>" /> <label for="author">Name</label></p>
			<p><input type="text" id="email" name="email" size="25" maxlength="256" tabindex="11" value="<? if(isset($feedback)) { echo $feedback->getValue('email'); } ?>" /> <label for="email">Email (will not be published)</label></p>
			<p><textarea name="content" cols="5" rows="10" tabindex="13"><? if(isset($feedback)) { echo $feedback->getValue('content'); } ?></textarea></p>
			<p><input type="submit" value="Submit Comment" tabindex="14" /></p>
		</form>
	</div>
	
	<? } // end if(post_ShowComments)  ?>
</div>