<?php
$imgDir = "./images";
$thumbDir = "./thumbnails";
$thOnPage = 5;

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

$count = count($dir);
$pages = ceil($count / $thOnPage);

if(isSet($_GET['iid'])){
  $iId = $_GET['iid'];
  if(!is_numeric($iId) || $iId < 0 || $iId >= $count){
    $iId = 0;
  }
  die(getImgDiv());
}
else if(isSet($_GET['pid'])){
  $pId = $_GET['pid'];
  if(!is_numeric($pId) || $pId < 0 || $pId > $pages){
    $pId = 0;
  }
  die(getThDiv());
}
else{
  $iId = 0;
  $pId = 0;
}
?>

<!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" style="text-align:center;">

<div id="thDiv" style="text-align:center;width:100%;">

<?php
echo getThDiv();
?>

</div>
<div style="text-align:center;width:100%;margin:5px;">
Idź do strony: 
<?php
for($i = 0; $i < $pages; $i++){
  $str = "<span onclick='changeThPage($i)' style='cursor:pointer;";
  $str .= "background-color:#F0F0F0;font-weight:bold;'>$i</span>&nbsp;";
  echo $str;
}
?>

</div>

</div>
</body>
</html>

<?php
function getThDiv()
{
  global $pId, $thOnPage, $count, $dir, $thumbDir;
  $str = "";
  for($i = 0; $i < $thOnPage; $i++){
    $imgNo = $pId * $thOnPage + $i;
    if($imgNo >= $count) break;
    $imgName = $dir[$imgNo];
    $imgTag = "<img src='$thumbDir/$imgName' alt='$imgName' ";
    $imgTag .= "onclick='showImg($imgNo);' style='cursor:pointer;'>";
    $str .= "$imgTag&nbsp;&nbsp;";
  }
  return $str;
}

function getImgDiv()
{
  global $dir, $iId, $imgDir;
  $imgName = $dir[$iId];
  $imgTag = "<img src=\"$imgDir/$imgName\" alt=\"$imgName\" ";
  $imgTag .= "id=\"img1\" onclick=\"closeImg();\">";
  return $imgTag;
}
?>