<?php
//
// BBWA
// left.php
// 画像一覧を表示する
// Teppei KAKU <teppei@sat.co.jp>
//

include("variables.php");
?>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>BBWA</title>
<meta http-equiv="Content-Type" content="text/html; charset=euc-jp">


<?php

echo <<<EOD
<link rel="stylesheet" type="text/css" href="Themes/${theme}.css">
<script language="JavaScript">
<!--
function scrollEnd() {
  order = ${_GET['order']};
  page = "${_GET['page']}";
  if (order == 2 && page == "END") {
    window.scrollTo(0, 10000);
  }
}
//-->
</script>
EOD;

?>


</head>
<body class="left" onLoad="scrollEnd()">
<table border="0" align="center">


<?php
// ページ上下部のアイコンを表示する
function printIcons($current, $pages) {
  print("<tr><td nowrap align=\"center\" class=\"button\">\n");
  if ($current > 1) {
    $prev = $current - 1;

    echo <<<EOD
<a href="left.php?page=1&order=${_GET['order']}"
  target="LEFT"><img src="Icons/icon_first.png" align="absmiddle"
  alt="先頭のページ" border="0"></a>
<a href="left.php?page=${prev}&order=${_GET['order']}"
  target="LEFT"><img src="Icons/icon_left.png" align="absmiddle"
  alt="前のページ" border="0"></a>
EOD;

  } else {
    print("<img src=\"Icons/icon_first_d.png\" align=\"absmiddle\" " .
      "border=\"0\">\n");
    print("<img src=\"Icons/icon_left_d.png\" align=\"absmiddle\" " .
      "border=\"0\">");
  }
  if ($current < $pages) {
    $next = $current + 1;

    echo <<<EOD
<a href="left.php?page=${next}&order=${_GET['order']}"
  target="LEFT"><img src="Icons/icon_right.png" align="absmiddle"
  alt="次のページ" border="0"></a>
<a href="left.php?page=${pages}&order=${_GET['order']}"
  target="LEFT"><img src="Icons/icon_last.png" align="absmiddle"
  alt="最後のページ" border="0"></a>
EOD;

  } else {
    print("<img src=\"Icons/icon_right_d.png\" align=\"absmiddle\" " .
      "border=\"0\">\n");
    print("<img src=\"Icons/icon_last_d.png\" align=\"absmiddle\" " .
      "border=\"0\">");
  }
  print("</td></tr>");
}

// インデックスを読み込み並べ替える
$tempArray = array();
$filePtr = fopen("${dirName}INDEX", "r");
switch ($_GET['order']) {
  // 画像順
  case 1:
    while ($pizza = fgets($filePtr, 1024)) {
      $piece = explode("|||", $pizza);
      $tempArray[$piece[0]] = $piece[1];
    }
    krsort($tempArray);
    break;
  // 画像順(古い順)
  case 2:
    while ($pizza = fgets($filePtr, 1024)) {
      $piece = explode("|||", $pizza);
      $tempArray[$piece[0]] = $piece[1];
    }
    ksort($tempArray);
    break;
  // スコア順
  case 3:
    while ($pizza = fgets($filePtr, 1024)) {
      $piece = explode("|||", $pizza);
      if ($piece[2] == 0) {
        $score = 0;
      } else {
        $score = $piece[2] / $piece[3];
      }
      $tempArray[$piece[0]] = $score;
    }
    arsort($tempArray);
    break;
  // コメント順
  default:
    while ($pizza = fgets($filePtr, 1024)) {
      $piece = explode("|||", $pizza);
      $tempArray[$piece[0]] = $piece[1];
    }
    arsort($tempArray);
}
$fileArray = array_keys($tempArray);
fclose($filePtr);

// ページ数を計算
$pages = (int) ((count($fileArray) - 1) / $perPage + 1);
if ($_GET['page'] == "END") {
  $current = $pages;
} else {
  $current = min($_GET['page'], $pages);
}

$imgStart = ($current - 1) * $perPage;
$imgEnd = min(($imgStart + $perPage), count($fileArray));

if (count($fileArray) > 0) {
  print("<tr><td align=\"center\">" .
    "<strong>${current}/${pages}</strong> ページ</td></tr>");
  printIcons($current, $pages);

  for ($i = $imgStart; $i < $imgEnd; $i++) {
    print("<tr><td align=\"center\" class=\"photoframe\">");

    // 画像の説明を読み込む
    $filePtr = fopen("${dirName}Messages/${fileArray[$i]}_mes.txt", "r");
    $pizza = fgets($filePtr, 1024);
    $pizza = htmlspecialchars($pizza);
    $piece = explode("|||", $pizza);
    fclose($filePtr);

    // サムネイルを表示する
    print("<a href=\"right.php?img=${fileArray[$i]}\"" .
       "target=\"RIGHT\">");
    if (eregi("\.gif$", $fileArray[$i])) {
      print("<img src=\"${picUrl}${fileArray[$i]}\"" .
        "border=\"0\" width=\"${piece[0]}\" height=\"${piece[1]}\"" .
        "alt=\"${piece[2]}\n${piece[3]}\n${piece[5]}\">");
    } else {
      print("<img src=\"${picUrl}Thumbnails/${fileArray[$i]}_thumb.jpg\"" .
        "border=\"0\" alt=\"${piece[2]}\n${piece[3]}\n${piece[5]}\">");
    }
    print("</a>");
    print("</td></tr>\n");
  }

  print("<a name=\"bt\">");
  printIcons($current, $pages);
  print("</a>");
} else {
  print("<tr><td align=\"center\">NO IMAGE!</td></tr>");
}
?>


</table>
</body>
</html>
