<?php

require_once("include/config.php");
include_once("include/fonctions.php");
include_once("include/fonctions_photos.php");
include("include/compteur.php");

define('PHT_LONGUEUR_MAX_TITRE', 50); // Longueur maximum du titre dans la liste déroulante (en caractères)

function tronquer ($chaine, $taille_max)
{
  if (strlen($chaine) > $taille_max) $chaine = substr($chaine, 0, $taille_max).'...';
  return $chaine;
}

// Initialisation des variables
empty($_GET['id']) ? $phtId = 0 : $phtId = decode_gpc($_GET['id']);

$phtRepPhotos = "photos/";

// Creation de l'index des photos
$phtPhotos = array();
$dossier = opendir($phtRepPhotos);
$i = 0;
while ($fichier = readdir($dossier)) {
  if (is_file($phtRepPhotos.$fichier) && $fichier != "index.php" &&
      strtolower($fichier) != "thumbs.db") {
		$phtPhotos[$i][0] = $fichier;
    $desc = description_photo($fichier);
    if ($desc == "") $desc = $fichier;
    $phtPhotos[$i][1] = $desc;
    $i++;
  }
}
closedir($dossier);

// Trie les photos par description
function comparer_photos($p1, $p2) { return strcoll2($p1[1], $p2[1]); }
setlocale(LC_COLLATE, PHORTAIL_LC_COLLATE);
usort($phtPhotos, "comparer_photos");

if ($phtId < 1) $phtId = 1;
elseif ($phtId > count($phtPhotos)) $phtId = count($phtPhotos);


//=============================================================================
//                          Affichage de la page
//=============================================================================


if (!empty($phtPhotos)) {
  // Les constantes définies ci-dessous sont utilisées par hpage.inc

  define("HPAGE_HEADER", '
  <script type="text/javascript">
    function setfocus()
    {
      if (document.getElementById) {
        document.getElementById("photos_select").focus();
      }
      return;
    }
  </script>');

  define("HPAGE_BODY_ATTRIBUTES", 'onload="setfocus()"');
}

include("include/hpage.inc");


echo '<a id="photo"></a>'."\n";

htable(conv_html($cfgNom[2]), "100%");

if (!empty($phtPhotos)) {

  if (file_exists($phtRepPhotos.$phtPhotos[$phtId - 1][0])) {
    echo '
    <p style="text-align: center"><img src="'.conv_url($phtRepPhotos.$phtPhotos[$phtId - 1][0]).'" alt="'.conv_html($phtPhotos[($phtId - 1)][0]).'" style="border: 0px" /></p>
    <p style="text-align: center"><b>'.conv_html($phtPhotos[$phtId - 1][1]).'</b><br />
    </p>
    ';
  }

  $idp = $phtId - 1;
  $ids = $phtId + 1;
  if ($idp < 1) $idp = count($phtPhotos);
  if ($ids > count($phtPhotos)) $ids = 1;

  echo '
  <hr class="separation_formulaire" />

  <p style="text-align: center">S&eacute;lectionnez une photo dans la liste d&eacute;roulante ci-dessous ou utilisez les fl&egrave;ches de navigation.</p>

  <form action="photos.php" method="get">
  <div class="center">
  <table cellspacing="0" cellpadding="0" border="0">
    <tr>
      <td><p><a href="photos.php?id='.$idp.'#photo">
      <img src="graphics/photo-precedente.png" alt="Pr&eacute;c&eacute;dent" style="border: 0px" /></a></p>
      </td>
      <td>
        <select name="id" id="photos_select" onchange="location = \'photos.php?id=\' + this.options[this.selectedIndex].value + \'#photo\';">
  ';

  for ($i = 0; $i < count($phtPhotos); $i++) {
    echo '<option value="'.($i + 1).'"'.(($i == $phtId - 1) ? ' selected="selected"' : '').'>'.
      conv_html(tronquer($phtPhotos[$i][1], PHT_LONGUEUR_MAX_TITRE)).'</option>'."\n";
  }

  echo '
        </select>
      </td>
      <td><p><a href="photos.php?id='.$ids.'#photo">
      <img src="graphics/photo-suivante.png" alt="Suivant" style="border: 0px" /></a></p>
      </td>
    </tr>
  </table>
  </div>
  </form>
  ';
}

else {
  echo '
  <p>&nbsp;</p>
  <p style="text-align: center">Aucune photo n\'est disponible</p>
  <p>&nbsp;</p>
  ';
}

btable();

include("include/bpage.inc");

?>
