<?php
include("admin/db.php");
login();

//LATEST LISTEN
$latestresult = SelectLatestListen();
$latestcount = mysql_num_rows($latestresult);

//EPISODES AKA LISTEN
$AllEpisodesResult = SelectListen();
$AllEpisodesCount = mysql_num_rows($AllEpisodesResult);

// number of rows to show per page
$rowsperpage = 25;

// find out total pages
$totalpages = ceil($AllEpisodesCount / $rowsperpage);

// get the current page or set a default
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage']))
   $currentpage = (int) $_GET['currentpage']; 
else 
   $currentpage = 1; // default page num

// if current page is greater than total pages...
if ($currentpage > $totalpages)
   $currentpage = $totalpages; // set current page to last page
   
// if current page is less than first page...
if ($currentpage < 1)
	$currentpage = 1; // set current page to first page

// the offset of the list, based on current page 
$offset = ($currentpage - 1) * $rowsperpage;

//FAVS
$ListenResult = SelectListenPaged($offset, $rowsperpage);
$ListenCount = mysql_num_rows($ListenResult);
?>
<!doctype html>
<html lang="en" class="no-js">
<head>
	<title>Episodes | On The Menu Podcast</title>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
	<meta http-equiv="description" content="Download or listen to all the On The Menu episodes.  Also available on iTunes and Google Play.">
	<meta name="keywords" content="fine dining,food,restaurants,food reviews,restaurant reviews,Ann Haigh,Peter Haigh,food podcast,on the menu" />
	<link href="https://fonts.googleapis.com/css?family=Oswald:300,400%7CKarla:400,700" rel="stylesheet">
	<link rel="shortcut icon" href="images/favicon.ico">
	<link rel="stylesheet" href="assets/css/bootstrap-reboot.css">
	<link rel="stylesheet" href="assets/css/bootstrap-grid.css">
	<link rel="stylesheet" href="assets/css/material-design-iconic-font.css">
	<link rel="stylesheet" href="assets/css/style.css">
</head>
<body class="home">
	<header id="top" class="navbar navbar-sticky">
		<div class="container">
			<div class="row align-items-center">
				<? 
				include("site-title.php");
				?>
				<? 
				include("nav.php");
				?>
				<nav id="social-links-menu" style="padding-top:10px" class="col-lg-4 col-md-auto order-4 order-md-1 order-lg-3 col-sm-12">
				  <div class="row">
				   <div class="col-lg-4 col-md-4" style="padding-bottom: 10px;">
				   
					<? 
					include("sm-top.php");
					?>
					</div>
					<div class="col-lg-8 col-md-8">
					<!--START TOP PLAYER-->
					<?php
						//LATEST LISTEN
					$latestresult = SelectLatestListen();
					$latestcount = mysql_num_rows($latestresult);
					 if($latestcount > 0)
					  { 
						while($latestrow = mysql_fetch_object($latestresult))
						{
							$latestURLVar = stripslashes($latestrow->URL);
						
						}//end while
					}
					?>
						<p style="font-size: 14px; margin: 0px; padding-bottom:2px; text-align: center">Listen To The Latest Episode</p>
						<audio class="wp-audio-shortcode" preload="none" style="width: 100%;" controls="controls">
						<source src="<?=$latestURLVar;?>" type="audio/mpeg" />
						</audio>
					<!--END TOP PLAYER-->
					</div>	
				</nav>
				
			
				<div class="site-menu-toggle col-auto order-2 order-sm-3">
					<a href="#site-menu">
						<span class="screen-reader-text">Toggle navigation</span>
					</a>
				</div>
			</div>
		</div>
	</header>

	<header id="featured" class="featured-content2 fade-background-50 padding-top-bottom">
		<div class="container">
			<div class="row align-items-center">
				<div class="col-12 col-md">
					<h1>OTM EPISODES</h1>
				</div>
			</div>
		</div>
	</header>

   <main id="content" class="has-sidebar padding-top-bottom">
		<div class="container">
			<div class="row">
				<div class="col-12 col-md-8 col-lg-9">
					<div class="episodes-listing">
					
						<?php
						  if($ListenCount > 0)
						{ 
							while($ListenRow = mysql_fetch_object($ListenResult))
								{
								
									$ListenID = $ListenRow->ID;
									$ListenTitle = stripslashes($ListenRow->tbxTitle);
									$ListenURL = stripslashes($ListenRow->URL);
									$oDate = new DateTime($ListenRow->Date);
									$ListenDate = $oDate->format("F j, Y");
									$ListenDuration = stripslashes($ListenRow->tbxDuration);
									$ListenBody = stripslashes($ListenRow->Body);
							
						?>
					
						<!--START EPISODE-->
						<article class="entry entry-episode">
							<div class="row align-items-xl-center">
								<div class="col-12 col-lg-4">
									<div class="entry-media entry-image multiply-effect">
											<img class="first" src="images/ep-art.jpg" alt="">
									</div>
								</div>
								<div class="col-12 col-lg-8">
									<header class="entry-header">
										<div class="entry-meta">
											<time class="entry-date published"><?=$ListenDate;?></time>
										</div>
										<h5><?=$ListenTitle;?></h5>
									</header>
									<div class="entry-audio">
										<div class="podcast-episode">
											<div class="podcast-episode-player" data-episode-duration="<?=$ListenDuration;?>">
												<audio class="wp-audio-shortcode" preload="none" style="width: 100%;" controls="controls">
													<source src="<?=$ListenURL;?>" type="audio/mpeg" />
												</audio>
											</div>
										</div>
									</div>
									<div class="entry-content">
										<p><?=$ListenBody;?></p>
									</div>
								</div>
							</div>
						</article>
						<!--END EPISODE-->
						<?php
							} //end while
						} //end if for count
						else
						{
							$ListenTitle = "There are no episodes to display!";

						    echo "<h4>$ListenTitle</h4>\n";
							echo "<p>&nbsp;</p>\n";
						}							
						?>
						<div class="pagination">More Episodes:<br>
							<?php
					/******  build the pagination links ******/
						// range of num links to show
						$range = 3;

						// if not on page 1, don't show back links
						if ($currentpage > 1) {
						   // show << link to go back to page 1
						   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1' class=\"next page-numbers\"><<</a> ";
						   // get previous page num
						   $prevpage = $currentpage - 1;
						   // show < link to go back to 1 page
						   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage' class=\"next page-numbers\"><span class=\"zmdi zmdi-chevron-left\"></span></a> ";
						} // end if 

						// loop to show links to range of pages around current page
						for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
						   // if it's a valid page number...
						   if (($x > 0) && ($x <= $totalpages)) {
							  // if we're on current page...
							  if ($x == $currentpage) {
								 // 'highlight' it but don't make a link
								 echo " <span aria-current=\"page\" class=\"page-numbers current\"><b>$x</b></span> ";
							  // if not current page...
							  } else {
								 // make it a link
								   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x' class=\"next page-numbers\">$x</a> ";
							  } // end else
						   } // end if 
						} // end for

						// if not on last page, show forward and last page links        
						if ($currentpage != $totalpages) {
						   // get next page
						   $nextpage = $currentpage + 1;
							// echo forward link for next page 
						   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage' class=\"next page-numbers\"><span class=\"zmdi zmdi-chevron-right\"></span></a> ";
						   // echo forward link for lastpage
						   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages' class=\"next page-numbers\">>></a> ";
						} // end if
						/****** end build pagination links ******/
						?>
						</div>
					</div>
				</div>
				<div id="sidebar" class="col-12 col-md-4 col-lg-3">
					<? 
					include("search-inc.php");
					?>
					<div class="widget widget_categories">
						<h5 class="add-separator"><span>Site Categories</span></h5>
						<ul>
							<li class="cat-item"><a href="about.php">About</a></li>
							<li class="cat-item current-cat"><a href="episodes.php">Episodes</a></li>
							<li class="cat-item"><a href="news.php">News</a>
							<li class="cat-item"><a href="articles.php">Articles</a></li>
							<li class="cat-item"><a href="bookshelf.php">Bookshelf</a></li>
							<li class="cat-item"><a href="favorites.php">Favorites</a></li>
							<li class="cat-item"><a href="photos.php">Photos</a></li>
							<li class="cat-item"><a href="index.php">Home</a></li>
						</ul>
					</div>
					<? 
						include("podcast-sub.php");
					?>
				</div>
			</div>
		</div>
	</main>

	<? 
		include("footer.php");
	?>

	<script src="assets/js/jquery-3.2.1.min.js"></script>
	<script src="assets/js/modernizr-custom.js"></script>
	<script src="assets/js/functions.js"></script>

	<!-- MediaElement.js files -->
	<link rel="stylesheet" id="mediaelement-css"  href="assets/mediaelement/mediaelementplayer-legacy.css">
	<link rel="stylesheet" id="wp-mediaelement-css"  href="assets/mediaelement/wp-mediaelement.css">
	<link rel="stylesheet" id="castilo-additional-mediaelement-css"  href="assets/css/mediaelement-castilo.css">
	<script src="assets/mediaelement/mediaelement-and-player.js"></script>
	<script src="assets/mediaelement/mediaelement-migrate.js"></script>
	<script src="assets/mediaelement/wp-mediaelement.js"></script>
	<script src="assets/js/mediaelement-castilo.js"></script>
	<script src="assets/js/index.js"></script>
</body>
</html>