<?php
$count = 12;
$imagesDir = "./images";

if(isset($_GET['id'])){
  $id = $_GET['id'];
  if($id < 1) $id = 1;
  if($id > $count) $id = $count;

  if(file_exists("$imagesDir/obraz$id.jpg")){
    $name = "$imagesDir/obraz$id.jpg";
  }
  else{
    $name = "error\nObraz nie jest dostêpny.";
  }
  $str = generateSpanDiv($id, $count);
  die("$str\n$name");
}
else{
  $id = 1;
  $name = "obraz$id.jpg";
}
?>

<!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='$imagesDir/$name' alt='$name'>"; ?>
<div id="spanDiv">
<?php echo generateSpanDiv($id, $count); ?>
</div>
</div>
</div>
</body>
</html>

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

    $firstSpanClass = "class='activeSpan'";
    $onFirstClick = "onclick='startRequest(1);'";
  }
  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;
}
?>