<?php
   @include_once("functions.php");
   $config = getConfig();
?>
<!DOCTYPE html>
<html>
   <head>
      <meta http-equiv="content-type" content="text/html;charset=utf-8" />
      <title><?php echo $config['galleryName'];?></title>
      <link rel="stylesheet" href="index.css" type="text/css" />
   </head>
   <body>
      <div id="page">
         <hgroup>
            <h1><?php echo $config['galleryName'];?></h1>
         </hgroup>
         <ul id="mainmenu">
<?php
if (isset($config['menuItems'])) {
	foreach ($config['menuItems'] as $itemName => $itemPath) {
	   echo '            <li><a href="' . $itemPath . '">' . $itemName . '</a></li>' . "\n";
	}
}
?>
            <li class="adminbutton"><a href="admin.php">Admin</a></li>
         </ul>
<?php
$albums = array();
if (file_exists('albums')) {
   foreach (new DirectoryIterator('albums') as $file) {
      if (!$file->isDot() && $file->isDir()) {
         $albumDir = $file->getFilename();
	     $jsonPath = 'albums/' . $albumDir . '/album.json';
         if (file_exists($jsonPath) && filesize($jsonPath) > 0) {
            $albums[] = $file->getFileName();
         }
      }
   }
}
if (empty($albums)) {
   echo '<div class="defaultText">No albums yet, stay tuned...</div>';
}
echo '         <ul id="albums">' . "\n";
rsort($albums);
foreach ($albums as $album) {
   $albumDir = 'albums/' . $album;
   $jsonPath = $albumDir . '/album.json';
   $albumInfo = readJSON($jsonPath);
   $albumName = $albumInfo['title'];
   $albumMonth = $albumInfo['month'];
   $albumYear = $albumInfo['year'];
   $featuredImage = $featuredResized = null;
   if (array_key_exists('featured', $albumInfo)) {
      $featuredImage = $albumInfo['featured'];
	  $featuredResized = $albumDir . '/featured/featured_' . $featuredImage;
   }
   
   echo '<li class="album">
            <div class="albumContent">
               <a href="showalbum.php?album=' . $album . '"><img class="featuredImage" alt="" src="' . $featuredResized . '"/></a>
               <a href="showalbum.php?album=' . $album . '"><div class="albumname">' . $albumName . '</div></a>
              <div class="albumdate">' . $albumMonth . ' ' . $albumYear . '</div>
            </div>
         </li>';
}
if (count($albums) % 2 == 1) {
   echo '<li class="album blankalbum"/>';
}
?>            
         </ul>
      </div>
<?php
if (!empty($albums)) {
   echo '      <div id="footer"><a href="http://raspberryalbums.sf.net">Raspberry Albums</a></div>';
}
?>
   </body>
</html>