<?php
   @include_once("functions.php");
   authenticate();
   $config = getConfig();
?>
<html>
   <head>
      <meta http-equiv="Content-type" content="text/html; charset=utf-8">
      <title><?php echo $config['galleryName'];?> | upload</title>
      <link rel="stylesheet" href="admin.css" type="text/css" />
      <link rel="stylesheet" href="uploader.css" type="text/css" />
      <script type="text/javascript" src="jquery.js"></script>
      <script type="text/javascript" src="uploader_client.js"></script>
      <script>
         $(document).ready(function () {
            createUploader();
         });
      </script>

   </head>
   <body>
      <div id="page">
      <hgroup><h1><?php echo $config['galleryName'];?></h1><h2> | upload-o-matic</h2></hgroup>
   <?php
      $albumDir = '';
      if (isset($_POST['albumname'])) {
         $albumDir = $_POST['albumname'];
         $albumPath = 'albums/' . $albumDir;
      }
	  $validInput = true;
      if ($albumDir == '') $validInput = false;
      if (!array_key_exists('timestamp', $_GET) || $_GET['timestamp'] == "") $validInput = false;
	  if (!$validInput) {
         echo '<div class="errorText">Something is wrong... please <a href="uploader.php">restart the upload process</a></div>';
         exit;
      }
      $newAlbum = false;
      if ($albumDir == 'createnewalbum') {
         $newAlbum = true;
         $albumName = $_POST["newalbumname"];
         // create album dir from album name
         $albumDir = $_GET['timestamp'] . '_' . preg_replace('/[^A-Za-z0-9]/', '_', $albumName);
         $albumPath = 'albums/' . $albumDir;
         echo 'Creating a new album "' . $albumName . '" (directory name: ' . $albumDir . ')';
		 if (!file_exists('albums')) { @mkdir('albums'); }
         @mkdir('albums/' . $albumDir, 0755);
         @mkdir('albums/' . $albumDir . '/small', 0755);
         @mkdir('albums/' . $albumDir . '/thumbs', 0755);
         @mkdir('albums/' . $albumDir . '/featured', 0755);
         $jsonFileName = 'albums/' . $albumDir . '/album.json';
         $jsonContent = array('title' => $albumName, 'month' => date('F'), 'year' => date('Y'), 'captions' => array());
		 writeJSON($jsonFileName, $jsonContent);
      } else {
         echo '<div class="textTitle">Adding images to existing album: <span class="textTitleHighlight">' . $albumDir . '</span></div>';
      }
   ?>
      <div id="file-uploader">       
         <noscript>          
            <p>Please enable JavaScript to use file uploader.</p>
            <!-- or put a simple form for upload here -->
         </noscript>         
      </div>
    <script>
        function createUploader(){            
            var uploader = new qq.FileUploader({
                element: document.getElementById('file-uploader'),
                action: 'uploader_server.php?dir=<?php echo htmlspecialchars($albumPath); ?>',
                allowedExtensions: ['jpg','JPG','png','PNG'],
                debug: true
            });           
        }
    </script>
      <div id="gotocaptions">
      <form name="uploader_page2" action="editalbum.php?album=<?php echo $albumDir; ?>" method="post">
      <input class="hugebutton" type="submit" value="Wait until upload completes, then click here to open the caption editor"/>
      </form>
      </div>
   </div>
   </body>
</html>
