<?php
include "conn.php";


$page = $_GET["page"];

$upcoming = $_GET["e"];
if (($upcoming != 1))
{
	$upcoming = 0;
}

$today = date("Y-m-d");
$y = date("Y");
$m = date("m");
$d = date("d");

$step = 7;
if (! ($page > 1))
{
	$page = 1;
}

$start = ($page -1)*$step;

echo "<table width=\"100%\">
	<tr><td width=\"100%\">";
links();
echo "</td></tr>
	<tr><td width=\"100%\">";

$query = "select eventid, startdate, title, stopdate, venue, descrip, imgpath from events where";
	
if ($upcoming == 1)
{
	$query .= " startdate>='$today'";
	$query .= " order by startdate asc  limit $start, $step";
	
	$pagequery = "select count(*) from events where startdate>='$today'";
}
else
{
	$query .= " startdate<'$today'";
	$query .= " order by startdate desc  limit $start, $step";
	
	$pagequery = "select count(*) from events where startdate<'$today'";
}
	
//echo $query;
printnews($query);

echo "</td></tr><tr><td width=\"100%\">";
pages($pagequery, $step, $upcoming);
echo "</td></tr></table>";
?>

<?php
function pages($pagequery, $step, $upcoming)
{
	$result = mysql_query($pagequery) or die (mysql_error());
	$row = mysql_fetch_array($result);
	$total = $row[0];
	
	if ($total % $step == 0)
	{
		$pages = $total / $step;
	}                                    
	else
	{
		$pages = ceil($total / $step);
	}
	
	if ($pages > 1)
	{
		echo "<table cellspacing=\"0\" cellpadding=\"2\" border=\"0\"><tr><td>Page</td>";
		
		if ($pages <= 7)
		{
			for ($i = 1; $i <= $pages; $i++)
			{
				echo "<td align=left><a href=$PHP_SELF?e=$upcoming&page=$i>$i</a></td>";
			}
		}
		else
		{
			for ($i = 1; $i <= 7; $i++)
			{
				echo "<td align=left><a href=$PHP_SELF?e=$upcoming&page=$i>$i</a></td>";
			}
			echo "<td align=left>...</td><td align=left><a href=$PHP_SELF?e=$upcoming&page=$pages>$pages</a></td>";
		}
		
		echo "</tr></table>";
	}
	
}

function printnews($query)
{
	//echo $query;
	$result = mysql_query($query) or die (mysql_error());
	while ($row = mysql_fetch_array($result))
	{
		$eventid = $row[0];
		$startdate = $row[1];
		$title = $row[2];
		$stopdate = $row[3];
		$venue = $row[4];
		$descrip = $row[5];
		if ($row[6] != '')
		{
		$imgpath = "events/".$row[6];
		}
		else
		{
		$imgpath = "";
		}
		$date = '';
		
		if ($startdate != "0000-00-00")
		{
			$year = substr($startdate, 0, 4);
			$month = substr($startdate, 5, 2);
			$day = substr($startdate, 8, 2);
			
			$date = date("dS M, Y", mktime(0,0,0,$month, $day, $year));
			
			if ($stopdate != "0000-00-00" && $stopdate != $startdate)
			{
				$year = substr($stopdate, 0, 4);
				$month = substr($stopdate, 5, 2);
				$day = substr($stopdate, 8, 2);
				
				$date .= " - " . date("dS M, Y", mktime(0,0,0,$month, $day, $year));
			}
		}
		
		echo "<div style=\"padding-bottom: 20px; line-height: 150%\">";
		if (!empty($imgpath))
		{
			echo "<img src=\"$imgpath\">";
		}
		if ($date != '')
		{
			echo "<br><span class=text_small>$date</span>";
		}
		if ($venue != '')
		{
			echo "<br>$venue";
		}
		echo "<br><b>$title</b>";
		
		if ($descrip != '')
		{
			echo "<br>$descrip";
		}
		echo "<br><a href=\"events/evd.php?id=$eventid\" onclick=\"openPopWin('events/evd.php?id=$eventid', '500', '375');return false\">more info...</a>";
		echo "</div>";
	}
}

function links()
{
	$e = $_GET["e"];
	echo "<table width=50% cellspacing=0 cellpadding=0 border=0>";
	
	if ($e == 1)
	{
		echo "<tr><td><font color=red>[Current Events]</font></td>";
		echo "<td><a href=" . $_SERVER["PHP_SELF"] . ">[Archived Events]</a></td></tr>";
	}
	else
	{
		echo "<tr><td><a href=" . $_SERVER["PHP_SELF"] . "?e=1>[Current Events]</a></td>";
		echo "<td><font color=red>[Archived Events]</font></td></tr>";
	}
	?>
	<tr><td colspan=2>&nbsp;</td></tr>
	</table>
	<?php
}
?>