<?php
require_once('global.php');
require_once(ABSOLUTE_PATH.'/class/NewsManager.php');
require_once(ABSOLUTE_PATH.'/class/Paginator.php');

$nm = NewsManager::getInstance();

$handle = ((isset($_GET['handle']))&&($_GET['handle']!='')) ? trim(rawurldecode($_GET['handle'])) : '';
$news = $nm->getIdByHandle($handle);

$template_body_class.= 'background-global';

if($news!=null){
	// Specific blog entry
	$template_content = $am->getLeaderboard();
	$template_content.= '		<section id="blog">
				<div class="container">';
	$template_content.= $nm->getPrintableNews($news, true);
	$template_content.= $nm->getCommentSection($news);
	$template_content.= '			</div>
			</section>';
	$template_content.= $am->getLeaderboard();

}else{
	// Blog overview
	$page = ((isset($_GET['p']))&&($_GET['p']!='')) ? intval($_GET['p']) : 1;
	$limit = 6;
	$offset = ($page * $limit) - $limit;
	$paginator = new Paginator();
	$paginator->setTotalPages(ceil($nm->getTotal() / $limit));
	$paginator->setCurrentPage($page);
	$paginator->setPaginationVariance(5);
	$paginator->setLink('blog/page-%p');
	$paginator->setShowFirst(true);
	$paginator->setShowPrevious(true);
	$paginator->setShowNext(true);
	$paginator->setShowLast(true);
	$pagination = $paginator->paginate();

	$template_content = $am->getLeaderboard();
	$template_content.= '		<section id="blog">
			<div class="container">
				<h2 class="headline">Blog</h2>';
	$template_content.= $pagination;
	$template_content.= '				<div class="row">';
	foreach($nm->getLatestIds($limit, $offset) as $news){
		$template_content.= $nm->getPrintableNewsPreview($news, true);
	}
	$template_content.= '				</div>
				'.$pagination.'
			</div>
		</section>';
	$template_content.= $am->getLeaderboard();
}

include_once(ABSOLUTE_PATH.'/template.php');
?>