<?php	
	require_once('../config/config.php');
	require_once('../libs/dgsutil/cgi.php');
	require_once('../libs/util.php');

	global $config;

	$buffer_time = 15;

	$db = mysql_connect($config['databaseServer'], $config['databaseUsername'], $config['databasePassword']);
	mysql_select_db($config['databaseName'], $db);

	$sql = sprintf("SELECT id, UNIX_TIMESTAMP(start) as start, UNIX_TIMESTAMP(end) AS end, summary, detail FROM gui_event_message WHERE (start = 0 AND end = 0) OR (start = 0 AND NOW() < (end + INTERVAL %d MINUTE)) OR ((NOW() > start - INTERVAL %d MINUTE) AND end = 0) OR ((NOW() > start - INTERVAL %d MINUTE) AND (NOW() < end + INTERVAL %d MINUTE)) ORDER BY start DESC, end, id", $buffer_time, $buffer_time, $buffer_time, $buffer_time);

	$result = mysql_query($sql);

	for ($i = 0; $row = mysql_fetch_array($result); $i++) {
		$data[$i]['state'] = 'informational';

		$data[$i]['id'] = $row['id'];	
		$data[$i]['summary'] = $row['summary'];	
		$data[$i]['detail'] = $row['detail'];
		$data[$i]['start'] = $row['start'] != 0 ? date('M j H:i', $row['start']) : 'Unspecified';
		$data[$i]['end'] = $row['end'] != 0 ? date('M j H:i', $row['end']) : 'Unspecified';
	}

	mysql_close($db);
?>

<TABLE CELLSPACING="1" CELLPADDING="0" BORDER="0" WIDTH="100%">
	<TR CLASS="tbltitle">
		<TD COLSPAN="2" NOWRAP>
			<?php printf('%s: %d', $event_title, count($data)); ?> <B>[<A CLASS="tbltitle" HREF="">Snapshot</A>]</B>
		</TD>
	</TR>

	<TR CLASS="tblhdr">
		<TD NOWRAP>
			Time Stamp
		</TD>
		<TD WIDTH="100%" NOWRAP>
			Summary
		</TD>
	</TR>
	<?php for ($i = 0; $i < count($data); $i++) { ?>
	<TR CLASS="<?php printf('%s', $event_new_color[$data[$i]['state']][$i % 2]); ?>">
		<TD NOWRAP>
			<?php
				printf('%s', $data[$i]['start']);
			?>
		</TD>
		<TD NOWRAP>
			<A HREF="../detail/messagedetail_template.php?<?php printf('id=%d&listing=%s', $data[$i]['id'], $_SERVER['PHP_SELF']); ?>"><?php printf('%s', $data[$i]['summary']); ?></A>
		</TD>
	</TR>
	<?php } ?>
</TABLE>
