<? 

// 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;

}

// Static or dynamic links
function img_link($img) {
	global $dl;

	if($dl == 1) {
		return "index.".str_replace(".jpg", "", $img).".html";
	} else {
		return "index.php3?img=$img";
	}
}


// Format nav nicely
function fmt_ptn($prev, $text, $next, $snd) {
	global $dl;
	$ret = "<tr><td align=left width=120>";
	$ret .= "<font color=\"#ffffff\">";
	if($prev != "") {
		$ret .= "<a href=\"".img_link($prev);
		$ret .= "\">&lt;&lt;&lt; (F&ouml;reg&aring;ende)</a>";
	} else
		$ret .= "&lt;&lt;&lt; (F&ouml;reg&aring;ende)";
	$ret .= "</font>";
	$ret .= "</td>";
	if($snd != "") {
		$ret.= "<td><a href=\"$snd\"><img src=\"snd.gif\" width=20 height=20 border=0></a></td>";
	}


	global $max_run_x;
	$ret .= "<td align=center width=".(($max_run_x)-240)."><font color=\"#ffffff\">";
	$ret .= $text."</font></td>";
	$ret .= "<td align=right width=120>";
	$ret .= "<font color=\"#ffffff\">";
	if($next != "") {
		$ret .= "<a href=\"";
		$ret .= img_link($next);
		$ret .= "\">(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>Album - <? echo get_text(0); ?></title>
</head>
<body bgcolor="#000000" link="#ffffff" vlink="#ffffff">
<? 

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

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

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

	$pad = 2;

	$max_run_x = 800;
	$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;
	}

	if($dir == 1) {
		// Show directory of images in wget-able format

		reset($imgs);
		while(list($key, $val) = each($imgs)) {
			$dl = "";
			echo "<a href=\"".img_link($key);
			$dl = 1;
			echo "&dl=1\">".img_link($key)."</a>\r\n";
		}


	} else { // Show in browsable format

		// 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=\"#ffffff\"";
			echo "><a href=\"".img_link($key);
			echo "\"><img src=\"$thumbname\" $wh border=0 alt=\"$key\"></a></td>\r\n";
		}

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

		// Top nav
		echo "<table width=".$max_run_x.">";
		$text = get_text($idx);

		$snd = $snds[substr($use_img, 0, -4).".wav"];

		$prev_text_next = fmt_ptn($prev, $text, $next, $snd);
		echo $prev_text_next;
		echo "</table>\r\n";
	

		// Show image
		if($next) {
			echo "<a href=\"".img_link($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";
	}

?>
</body>
</html>