<?php
$imgDir = "./images";

if(!$dir = @scandir($imgDir)){
  die("error\nBłąd serwera. Galeria nie jest dostępna.");
}
array_shift($dir);
array_shift($dir);

$count = count($dir);

if(isset($_GET['id'])){
  $id = $_GET['id'];

  if(!is_Numeric($id) || $id < 0 || $id >= $count){
    $id = 0;
  }

  $imgName = $dir["$id"];

  if(!file_exists("$imgDir/$imgName")){
    die("error\nObraz nie jest dostępny.");
  }
  $spanDivTxt = generateSpanDiv($id, $count);
  $nr = $id + 1;
  $descDivTxt = "Plik $imgName ($nr z $count)";
  die("$descDivTxt\n$spanDivTxt\n$imgDir/$imgName");
}
else{
  $id = 0;
  $imgName = $dir["$id"];
}
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Ajax</title>
<link href="ajaxcss.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="ajaxcore.js"></script>
<script type="text/javascript" src="ajax.js"></script>
</head>
<body>
<div id="mainDiv" class="mainDiv" style="text-align:center;">
<div
 style="width:45%;margin:0 auto;border:1px solid #000000;text-align:center;padding:5px;">
<?php echo "<img id='img1' src='$imgDir/$imgName' alt='$imgName'>"; ?>
<div id="descDiv">
<?php $nr = $id + 1; echo "Plik $imgName ($nr z $count)"; ?>
</div>
<div id="spanDiv">
<?php echo generateSpanDiv($id, $count); ?>
</div>
</div>
</div>
</body>
</html>

<?php
function generateSpanDiv($id, $count){
  $count--;
  if($id > 0){
    $prev = $id - 1;
    $prevSpanClass = "class='activeSpan'";
    $onPrevClick = "onclick='startRequest($prev);'";

    $firstSpanClass = "class='activeSpan'";
    $onFirstClick = "onclick='startRequest(0);'";
  }
  else{
    $prevSpanClass = "class='inactiveSpan'";
    $onPrevClick = "";

    $firstSpanClass = "class='inactiveSpan'";
    $onFirstClick = "";
  }
  if($id < $count){
    $next = $id + 1;
    $nextSpanClass = "class='activeSpan'";
    $onNextClick = "onclick='startRequest($next);'";

    $lastSpanClass = "class='activeSpan'";
    $onLastClick = "onclick='startRequest($count);'";
  }
  else{
    $next = $count;
    $nextSpanClass = "class='inactiveSpan'";
    $onNextClick = "";

    $lastSpanClass = "class='inactiveSpan'";
    $onLastClick = "";
  }
  $str = "<span $firstSpanClass $onFirstClick>Pierwszy</span> ";
  $str .= "<span $prevSpanClass $onPrevClick>Poprzedni</span> ";
  $str .= "<span $nextSpanClass $onNextClick>Następny</span> ";
  $str .= "<span $lastSpanClass $onLastClick>Ostatni</span> ";
  return $str;
}
?>