<?
	require_once('control/include/user.php');
	require_once('control/include/mysql.php');
	require_once('control/include/functions.php');
	require_once('control/include/fb.php');
	
	ob_start();
	
	// SETUP MYSQL HANDLERS
	$mysqldb1 = new mysql($host, $user, $pswd, $db); $mysqldb1->connect(); $mysqldb1->select();
	$mysqldb2 = new mysql($host, $user, $pswd, $db); $mysqldb2->connect(); $mysqldb2->select();
	
	$limitSQL = '';
	if ($_GET['limit'] > 0) {
		$limitSQL = "LIMIT ".$_GET['limit'];
	}
	
	// DEAL WITH OLD EVENTS
	$query = "SELECT id FROM event WHERE DATEDIFF(CURRENT_DATE(), start_date) > 0";
	$mysqldb1->query($query);
	while ($event = $mysqldb1->fetchArray()) {
		$query = "UPDATE event SET status='disabled' WHERE id=".$event['id'];
		$mysqldb2->query($query);
	}
	
?>
<?
$query = "
SELECT 
	t1.*, 
	DATE_FORMAT(t1.start_date, '%D %M %Y') AS start_date, 
	DATE_FORMAT(t1.end_date, '%D %M %Y') AS end_date, 
	(SELECT name FROM file WHERE source='event' AND record_id=t1.id AND type='image' AND status='enabled' ORDER BY sequence ASC LIMIT 1) AS image 
FROM 
	event AS t1 
WHERE 
	t1.start_date!='0000-00-00' AND 
	t1.hidden=0 AND 
	t1.status='enabled'
ORDER BY 
	t1.start_date ASC
".$limitSQL;
?>
<table width="100%" border="0" cellpadding="0" cellspacing="5">
	<tr>
	<?
    //fb($query);
	$columns = 1;
    $counter = 0;
    $mysqldb1->query($query);
    while ($listing = $mysqldb1->fetchArray()) {
		
		if ($listing['content'] != '') {
			$href = '/event/'.$listing['link_name'];
			$class = '';
			$rel = '';
			$listing_heading = '<a href="'.$href.'" target="_self">'.$listing['name'].'</a>';
		} else {
			$href = '/'.$largeDIR.$listing['image']; 
			$class = 'popup';
			//$rel = 'shadowbox['.$listing['id'].']';
			$listing_heading = $listing['name'];
		}
		
        $counter++;
        if ($counter % 2 != 0) { $bgcolor = ''; } else { $bgcolor = ''; }
    ?>
        <td align="left" valign="top" class="listing" style="background-color:<? print $bgcolor; ?>;">
        	
            <table width="100%" border="0" cellpadding="5" cellspacing="0">
                <tr>
                    <td width="80%" align="left" valign="top"><div class="listing_heading"><? print $listing_heading; ?></div></td>
                    <td width="20%" align="right" valign="top"><? if ($listing['content'] != '') { ?><a href="<? print $href; ?>" target="_self"><img src="/website/styles/default/more_small.png" alt="more" width="60" height="20" border="0" /></a><? } ?></td>
                </tr>
            </table>
            <table width="100%" border="0" cellpadding="0" cellspacing="0">
                <tr>
                    <? if ($listing['image'] != '') { ?>
                    <td align="left" valign="top" style="width:<? print $tiny_image_size; ?>px;">
                        <div class="listing_image">
                        	<a href="<? print $href; ?>" class="<? print $class; ?>" rel="<? print $rel; ?>" target="_self"><img src="/<? print $tinyDIR.$listing['image']; ?>" alt="<? print $listing['heading']; ?>" border="0" /></a>
                        </div>
                    </td>
					<? } ?>
                    <td align="left" valign="top">
                        <div class="listing_description"><? print $listing['short_description']; ?></div>
                    </td>
                </tr>
            </table>
            <table width="100%" border="0" cellpadding="5" cellspacing="0">
                <tr>
                    <td align="right" valign="top">
                        <em><? print $listing['start_date']; ?><? if ($listing['end_date'] != '') { print ' to '.$listing['end_date']; } ?></em>
                    </td>
                </tr>
            </table>
            
        </td>
        
        <? if ($counter % $columns == 0) { ?>
        </tr></tr>
		<? } ?>
	<?
    }
    ?>
    <? if ($mysqldb1->affectedRows() < 1) { ?>
    <td>
    There are currenly no events.
    </td>
    <? } ?>
	</tr>
</table>
<?
	$mysqldb1->close();
	$mysqldb2->close();
?>