<?php
require_once("includes/SQLConnect.php");
require_once("includes/user_data.php");

if(isset($_GET['remove'])){
	$event_ID = (int)$_GET['ID'];
	$user_ID = $user_data['ID'];
	
	if($event_ID>0){
		if(mysql_query("DELETE FROM Events_RSVP WHERE event_ID = ".$event_ID." AND user_ID = ".$user_ID." LIMIT 1"))
			$success = "You are no longer listed as attending.";
		else
			$error = "You are not attending this event. Possibly you hit cancel twice.";
	}//Fail silently
}

include_once("includes/header.php");

?>
<?php
//Maybe consider plotting the events
?>
<form action="event_host.php" method="GET" class='action'>
	<input type=submit class="submit" value="Host an Event">
</form>

<h2>Events</h2>
<?php
//Once this goes live/big enough it should actually just display in district events
$result = mysql_query("SELECT Events.*,UNIX_TIMESTAMP(Events.being_held) as 'timestamp', CONCAT(Users.first_name,' ',Users.last_name) as 'name_concat' FROM Events,Users WHERE Users.ID = Events.user_ID AND Events.being_held >= CURDATE() ORDER BY Events.ID DESC");
?>

	<table cellspacing="0" id="events">
		<thead>
			<tr>
				<th>Date of Event</th>
				<th>Title</th>
				<th>Place</th>
				<th>Address</th>
				<th>Host</th>
				<th>RSVP</th>
			</tr>
		</thead>
		<tbody>
		<?php
		
		while($event = mysql_fetch_assoc($result)){
		
		$rsvp_flag = mysql_num_rows(mysql_query("SELECT ID FROM Events_RSVP WHERE event_ID = ".$event['ID']." AND user_ID = ".$user_data['ID'])) > 0 ? true : false;
		?>
			<tr <?php if($rsvp_flag){echo 'class="rsvp"';}?> valign="middle">
				<td>
					<a name="event<?=$event['ID'];?>"></a>
					<?=format_datetime($event['timestamp']);?>
				</td>
				<td>
					<?=stripslashes($event['title']);?>
					<p class='description'>
						<?=stripslashes($event['description']);?>
					</p>
				</td>
				<td><?=$event['place'];?></td>
				<td><?=$event['address'].'<br>'.$event['city'].', '.$event['state'];?></td>
				<td class="from" style="text-align:center">
				<?php 
				$query = sprintf("SELECT photo_URL FROM `Users` WHERE `ID`='%s'",
				mysql_real_escape_string($event['user_ID']));
				$extension = mysql_result(mysql_query($query),0);
				$thumb_image_location = 'images/profiles/thumbnail_'.$event['user_ID'].'.'.$extension;
				if(file_exists($thumb_image_location))
	
					print '<img src="'.$thumb_image_location.'" alt="Thumbnail Image" class="thumbnail"/>';
				?>
				<p><a href="profile_user.php?ID=<?=$event['user_ID'];?>"><?=$event['name_concat'];?></a></p></td>
				<td style="text-align:center">
					<b>
					<?php
					$number_attendees = (int)mysql_num_rows(mysql_query("SELECT ID FROM Events_RSVP WHERE event_ID = ".(int)$event['ID']));
					if($number_attendees>0){
						echo $number_attendees;
						if($number_attendees>1)
							echo " people";
						else
							echo " person";
					}
					?> attending.</b>
					<br>
					
					<?php
					if(!$rsvp_flag){
					?>
					<form action="event_rsvp.php" method="POST" id="action-rsvp" class='tiny'>
						<input type="hidden" class="hidden" name="ID" value="<?=$event['ID'];?>">
						<input type=submit class="submit" value="RSVP">
					</form>
					<?php
					}
					else{
						?>
						<form action="events_browse.php" method="GET" id="action-rsvp-delete" class='tiny'>
							<input type="hidden" class="hidden" name="ID" value="<?=$event['ID'];?>">
							<input type="hidden" class='hidden' name='remove' value="true">
							<input type=submit class="submit" value="Cancel">
						</form>
						<?php
					}
					?>
				</td>
			</tr>
		<?php
		}
		
		?>
	
	</table>

	<script type="text/javascript">
	$('table#events tr:odd').css({'background-color' : '#f2f8ff'});
	$('table#events tr.rsvp').css({'border-top' : '3px solid #FFFF66','border-bottom' : '3px solid #FFFF66','background-color' : '#ffffa9'});
	</script>	



	<br>
	
	<div id="socialactions">
		<h2>Other Events You Might Be Interested In</h2>
		<img src="images/jquery_facebox/loading.gif">
	</div>
	<script type="text/javascript">
		//Inject events to avoid lag
		$.ajax({
		   type: "GET",
		   url: "/includes/events/social_actions.php",
		   success: function(msg){
			 if(msg.length>25)
				$("#socialactions").html(msg);

		   }
		});
	</script>

<?php
include_once("includes/footer.php");
?>
