
<?php
//
// BBWA
// thumb.php
// 画像一覧を別ウィンドウで表示する
// Teppei KAKU <teppei@sat.co.jp>
//

include("variables.php");
?>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>BBWA : ThumbView</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">
<!--
tWidth = ((${thumbSize} + 15) * 5) + 60;
sWidth = Math.min(tWidth, screen.width * 0.8);
sHeight = screen.height * 0.8;
window.resizeTo(sWidth, sHeight);
EOD;

?>


function sortSelect(sf) {
  if (sf.ss.selectedIndex == 0) {
    location.href = 'thumb.php?order=0';
  } else if (sf.ss.selectedIndex == 1) {
    location.href = 'thumb.php?order=1';
  } else if (sf.ss.selectedIndex == 2) {
    location.href = 'thumb.php?order=2';
  } else if (sf.ss.selectedIndex == 3) {
    location.href = 'thumb.php?order=3';
  }
}
// -->
</script>
</head>
<body class="top">
<table border="0" width="100%" class="top"
  cellspacing="0" cellpadding="0">
<tr><td nowrap>


<?php
// インデックスを読み込み並べ替える
$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);
    $selected = array("", "selected", "", "");
    break;
  // 画像順(古い順)
  case 2:
    while ($pizza = fgets($filePtr, 1024)) {
      $piece = explode("|||", $pizza);
      $tempArray[$piece[0]] = $piece[1];
    }
    ksort($tempArray);
    $selected = array("", "", "selected", "");
    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);
    $selected = array("", "", "", "selected");
    break;
  // コメント順
  default:
    while ($pizza = fgets($filePtr, 1024)) {
      $piece = explode("|||", $pizza);
      $tempArray[$piece[0]] = $piece[1];
    }
    arsort($tempArray);
    $selected = array("selected", "", "", "");
}
$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));

$imgStart = 0;
$imgEnd = count($fileArray);
?>


<form class="top" name="sf">
並べ替え
<select class="top" name="ss" align="absmiddle"
  onChange="sortSelect(this.form)">


<?php

echo <<<EOD
<option ${selected[0]}>コメント優先
<option ${selected[1]}>画像優先
<option ${selected[2]}>画像優先(古い順)
<option ${selected[3]}>スコア順
EOD;

?>


</select>
&nbsp;&nbsp;
<a href="JavaScript:window.close()">
<img src="Icons/icon_close.png" border="0" align="absmiddle">閉じる</a>
&nbsp;&nbsp;
<a href="JavaScript:opener.focus()">
<img src="Icons/icon_main.png" border="0" align="absmiddle">メインへ</a>
&nbsp;&nbsp;


<?php
print("イメージ数: " . count($fileArray));
?>


</form>
</td></tr></table>
<table border="0" cellspacing="5" align="center" style="margin:5px;">


<?php
if (count($fileArray) > 0) {
  $counter = 0;
  for ($i = $imgStart; $i < $imgEnd; $i++) {
    if ($counter % 5 == 0) {
      print("<tr><td align=\"center\" class=\"photoframe\">");
    } else {
      print("<td align=\"center\" class=\"photoframe\">");
    }

    // 画像の説明を読み込む
    $message = file("${dirName}Messages/${fileArray[$i]}_mes.txt");
    $pizza = htmlspecialchars($message[0]);
    $piece = explode("|||", $pizza);

    if (mb_strlen($piece[2]) > 10) {
      $piece[2] = mb_substr($piece[2], 0, 9) . "…";
    }
    if (mb_strlen($piece[3]) > 10) {
      $piece[3] = mb_substr($piece[3], 0, 9) . "…";
    }

    // サムネイルを表示する
    print("<a href=\"right.php?img=${fileArray[$i]}\"" .
       "target=\"RIGHT\" onClick=\"JavaScript:opener.focus()\">");
    if (eregi("\.gif$", $fileArray[$i])) {
      print("<img src=\"${picUrl}${fileArray[$i]}\"" .
        "border=\"0\" width=\"${piece[0]}\" height=\"${piece[1]}\"><br>");
    } else {
      print("<img src=\"${picUrl}Thumbnails/${fileArray[$i]}_thumb.jpg\"" .
        "border=\"0\" width=\"${piece[0]}\" height=\"${piece[1]}\"><br>");
    }
    print("<div class=\"thumb\">題名: ${piece[2]}<br>" .
      "名前: ${piece[3]}<br>" .
      "コメント数: " . (count($message) - 1));
    if (time() - filemtime("${dirName}Messages/${fileArray[$i]}_mes.txt") <
      86400) {
      print(" <img src=\"Icons/icon_fresh.png\" border=\"0\"
        align=\"absmiddle\" alt=\"NEW\">");
    }
    print("</div>");
    print("</a>");

    if ($counter % 5 == 4) {
      print("</td></tr>\n");
    } else {
      print("</td>\n");
    }
    $counter++;
  }
} else {
  print("<tr><td align=\"center\">NO IMAGE!</td></tr>");
}
?>


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