<? 

#	define("FG_COL", "#ffffff");
#	define("BG_COL", "#000000");

#	define("FG_COL", "#000000");
#	define("BG_COL", "#ffffff");

	define("FG_COL", "#990000");
	define("BG_COL", "#ffffff");

// Pick from optional descriptive text
function get_text($idx) {
	$descr_name = "descr.txt";
	if(file_exists($descr_name)) { // Go get it
		$lines = file($descr_name);
		$ret = chop($lines[$idx]);
	} else {
		$ret = "";
	}

	return $ret;

}

// Format nav nicely
function fmt_ptn($prev, $text, $next) {
	$ret = "<tr><td align=left width=120>";
	$ret .= "<font color=\"".FG_COL."\">";
	if($prev != "")
		$ret .= "<a href=\"index.php3?img=$prev\">&lt;&lt;&lt; (F&ouml;reg&aring;ende)</a>";
	else
		$ret .= "&lt;&lt;&lt; (F&ouml;reg&aring;ende)";
	$ret .= "</font>";
	$ret .= "</td>";
	global $max_run_x;
	$ret .= "<td align=center width=".(($max_run_x)-240)."><font color=\"".FG_COL."\">$text</font></td>";
	$ret .= "<td align=right width=120>";
	$ret .= "<font color=\"".FG_COL."\">";
	if($next != "")
		$ret .= "<a href=\"index.php3?img=$next\">(N&auml;sta) &gt;&gt;&gt;</a>";
	else
		$ret .= "(N&auml;sta) &gt;&gt;&gt;";
	$ret .= "</font>";
	$ret .= "</td></tr>";

	return $ret;
}

?>
<html>
<head>
<title>Bildspel - <? echo get_text(0); ?></title>
</head>
<body bgcolor="<?=BG_COL;?>" link="<?=FG_COL;?>" vlink="<?=FG_COL;?>" onload="init_auto()">
<img src="http://www.sydostran.se/images/logoLong.gif"><br>
<font color="<?=FG_COL;?>"><? echo get_text(0); ?></font>
<br>
<? 

	// Get image files in current dir
	$imgs = array();

	$d = dir("./");
	while($entry=$d->read()) {
		if(ereg("^[0-9]+\.jpg$", $entry)) {
			$imgs[$entry] = $entry;
		}
	}
	$d->close();
	ksort($imgs);

	// Some vals
	$max_thumb_x = 100;
	$max_thumb_y = 70;

	$pad = 2;

	$max_run_x = 650;
	$run_x = 0;

	// Check for cached thumbnails
	reset($imgs);
	while(list($key, $val) = each($imgs)) {
		$img_num = substr($key, 0, -4);
		$thumbname = $img_num."_".$max_thumb_x."_".$max_thumb_y.".jpg";
		if(!file_exists($thumbname)) { // Go create the cache copy
			$convert = "/usr/bin/convert";
			$cmd = $convert." -geometry ".$max_thumb_x."x".$max_thumb_y." $key $thumbname";
#DEB			echo $cmd;
			exec(escapeshellcmd($cmd));
			flush();
		}
	}

	// Which image do we show?
	if(!isset($img)) { // use first
		reset($imgs);
		list($key, $val) = each($imgs);
		$use_img = $key;
	} else {
		$use_img = $img;
	}

	// Show thumbnail gallery
	echo "<table><tr height=".($max_thumb_y+$pad*2).">\r\n";

	$prev = $next = "";
	$cnt = 0;

	reset($imgs);
	while(list($key, $val) = each($imgs)) {
		$cnt++;
		if($last == $use_img) { // Previous was the img selected.
			$next = $key; // Use this as next img
		}

		if($key == $use_img) { // This is the img selected
			$prev = $last;
			$idx = $cnt;
		}

		$last = $key;

		$run_x += $max_thumb_x + $pad * 2;
		if($run_x > $max_run_x) { // Break rows when wide enough
			echo "</tr><tr height=".($max_thumb_y+$pad*2).">\r\n";
			$run_x = $max_thumb_x + $pad * 2;
		}

		$img_num = substr($key, 0, -4);
		$thumbname = $img_num."_".$max_thumb_x."_".$max_thumb_y.".jpg";

		$thumb = getimagesize($thumbname);
		$wh = $thumb[3];

		// Td with thumbnail picture
		echo "<td cellpadding=".$pad." align=center width=".($max_thumb_x+$pad*2);
		if($key == $use_img)
			echo " bgcolor=\"".FG_COL."\"";
		echo "><a href=\"index.php3?img=$key\"><img src=\"$thumbname\" $wh border=0 alt=\"$key\"></a></td>\r\n";
	}

	echo "</tr></table>\r\n";

	echo "<INPUT TYPE=\"checkbox\" VALUE=1 ID=\"auto_next\" NAME=\"auto_next\"";
	if(isset($_REQUEST["auto_next"]) || !isset($_REQUEST["img"]))
		echo " checked";
	echo " onClick='init_auto()'";	
	echo "><font color=\"".FG_COL."\">Automatiskt bildbyte</font>";

	// Top nav
	echo "<table width=".$max_run_x.">";
	$text = "Bild $idx/".count($imgs)." - ".get_text($idx); 
	$prev_text_next = fmt_ptn($prev, $text, $next);
	echo $prev_text_next;
	echo "</table>\r\n";
	

	// Show image
	if($next)
		echo "<a href=\"index.php3?img=$next\">";

	$imgsize = getimagesize($use_img);
	echo "<img src=\"$use_img\" border=0 ".$imgsize[3]." alt=\"$text\">";
	if($next)
		echo "</a>";
	echo "\r\n";

	// Bottom nav
	echo "<table width=".$max_run_x.">";
	echo $prev_text_next;
	echo "</table>\r\n";

	if($next)
	{ 

?>
<script>


	function next_img()
	{
		// Only advance automatically if auto is true 
		var auto = document.getElementById('auto_next');
//		alert(auto.checked);
		if(auto.checked)
		{
			window.location = '<?="index.php3?auto_next=1&img=".$next;?>';
		}

	}

	function init_auto()
	{
		var auto = document.getElementById('auto_next');
		if(auto.checked)
		{
			setTimeout("next_img()", 2500);
		}
	}

</script>

<?
	} else {
?>

<script>
	function init_auto()
	{
		;
	}
</script>
<?
}
?>

</body>
</html>