<?php
define("PAGE_USES_FACEBOX",true);
include_once("includes/header.php");

require_once("includes/classes/Commenter.inc.php");
$Commenter['featured_orgs'] = new Commenter(14,$user_data);
$Commenter['census_data'] = new Commenter(15,$user_data);
$Commenter['suggested_news'] = new Commenter(16,$user_data);



//Affiliation Filter
if(isset($_GET['affiliation_ID']) && (int)$_GET['affiliation_ID']>0)
	$affiliation_ID = (int)$_GET['affiliation_ID'];
?>


<h2>What's Happening in Your District</h2>

<?php

	//Key here is 'daysTillEvent' that is a signed integer value corresponding to the days until being_held. Use this for filtering.
	$result = mysql_query("
	
	SELECT CAST( UNIX_TIMESTAMP( Events.being_Held ) - UNIX_TIMESTAMP( ) AS SIGNED INTEGER ) /3600 /24 as 'daysTillEvent', Events.being_held, 
	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() AND Events.being_held<DATE_ADD(CURDATE(),INTERVAL 1 WEEK)
	ORDER BY Events.being_held DESC 
	LIMIT 0 , 30
	
	");

	
	while($event = mysql_fetch_assoc($result)){
		/*
		
			This is a bit complicated...
			The basic idea is to store the events corresponding to a day in an array with that day as the index.
			
			The event information is stored as <li>'s so that later, a <ul> can be wrapped around the event details and 
			that days's events will all be displayed together. This avoids any problems of having to scan through
			two different arrays and find the intersection because the intersection is handily provided in the form
			of an array index. 
		
		*/
		
		
		ob_start();
		?>
			<li>
				<h4>
					<a href="events_browse.php#event<?=$event['ID'];?>"><?=stripslashes($event['title']);?></a>
				</h4>
				In <?=$event['city'];?>, <?=$event['state'];?>
				<br>
				Hosted by: <a href="profile_user.php?ID=<?=$event['user_ID'];?>"><?=$event['name_concat'];?></a>
			</li>
		
		<?php
		@$events[date("l",$event['timestamp'])] .= ob_get_clean();
		
	}
	
	if(isset($events) && is_array($events)){
	?>
		<table width="100%" cellspacing="0" id="calendar">
			<thead>
				<tr>
					<th>
						Today
					</th>
					
					<?php
					for($i=1;$i<7;$i++){
					?>
						<th>
							<?=date("l",time()+($i*60*60*24));?>
						</th>
					<?php
					}
					?>
					
				</tr>
			</thead>
			<tbody>
				<tr>
			
					<?php
					for($i=0;$i<7;$i++){ //Notice how this for loop starts at 0, not 1 like the previous one (today=0)
						$timestamp = time()+($i*60*60*24); //Make the day's timestamp
						?>
						
							<td>
								<?php
								if(isset($events[date("l",$timestamp)]) && strlen($events[date("l",$timestamp)])>0)
									echo "<ul>".$events[date("l",$timestamp)]."</ul>";
								?>
							</td>
						
						<?php
					}
					?>
				</tr>
			
			</tbody>
		</table>
	<?php
	}
	?>
	<script type="text/javascript">
		$('#calendar td:odd').css({'background-color' : '#f2f8ff'});
	</script>
	
<br>

<h3>New People in Your District This Week</h3>
<ul>
<?php
$result = mysql_query("SELECT * FROM Users WHERE district_ID = ".$user_data['district_ID']." AND join_TS>DATE_SUB(CURDATE(),INTERVAL 1 WEEK) LIMIT 30");
while($user = mysql_fetch_assoc($result)){

	//Bold the new user's location if it's the same as the current user
	$location = $user['city'].', '.$user['state'];
	if($location == $user_data['city'].', '.$user_data['state'])
		$location = "<b>".$location ."</b>";
		
		
	?>
	<li style="display:inline">
		<?php 

		$thumb_image_location = 'images/profiles/thumbnail_'.(int)$user_data['ID'].'.'.$user_data['photo_URL'];
		if(file_exists($thumb_image_location))
			print '<img src="'.$thumb_image_location.'" alt="Thumbnail of Profile Image"/>';
		?>
		
		<a href="profile_user.php?ID=<?=$user['ID'];?>" style="font-size:110%"><?=$user['first_name'].' '.$user['last_name'];?></a> in <?=$location;?>
	</li>
	<?php
}

?>
</ul>




<h3>Recently Listed Events</h3>
<?php

	//Key here is 'daysSinceHost' that is a signed integer value corresponding to the days until ts. Use this for filtering.
	$result = mysql_query("
	
	SELECT CAST( UNIX_TIMESTAMP( ) - UNIX_TIMESTAMP( Events.TS ) AS SIGNED INTEGER ) /3600 /24 AS daysSinceHost, 
	Events.being_held, 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.TS>DATE_SUB(CURDATE(),INTERVAL 1 WEEK)
	ORDER BY Events.ID DESC 
	LIMIT 0 , 30

	");
?>

<table width="100%" cellspacing="0" id="events">
	<thead>
		<tr>
			<th style="text-align:left">Date of Event</th>
			<th style="text-align:left;width:50%">Title</th>
			<th>City</th>
			<th>Host</th>
		</tr>
	</thead>
	<tbody>
	<?php
	
	while($event = mysql_fetch_assoc($result)){
		?>
		<tr valign="middle">
			<td style="text-align:left"><?=format_date($event['timestamp']);?></td>
			<td style="text-align:left">
				<a href="events_browse.php#event<?=$event['ID'];?>"><?=stripslashes($event['title']);?></a>
			</td>
			<td><?=$event['city'].', '.$event['state'];?></td>
			<td><a href="profile_user.php?ID=<?=$event['user_ID'];?>"><?=$event['name_concat'];?></a></td>
		</tr>
	<?php
	}
	
	?>

</table>
<br>

</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 class="column-left">
	<h3>Reference Links for <?=$user_data['state'].'-'.$user_data['district'];?></h3>
	<ul class="comments">
		<li>
			<a href="http://projects.washingtonpost.com/elections/keyraces/census/<?=strtolower($user_data['state']);?>/district-<?=$user_data['district'];?>/"><?=$user_data['state'];?> Census Data by the Washington Post</a><?=$Commenter['census_data']->getLink();?><?=$Commenter['census_data']->getSupportingHTML();?>
			<p class='excerpt'>
				Gender, Race/Origin, Age, Language, Employment, Income, Marital Status, Occupations and Private vs. Government Employment.
			</p>
		</li>
	</ul>
</div>

<div class="column-right">
	<h3>Featured Organizations in Your District<?=$Commenter['featured_orgs']->getLink();?></h3><?=$Commenter['featured_orgs']->getSupportingHTML();?>
	<a href="http://www.singlepayeraction.org"><img src="http://www.singlepayeraction.org/images/single_payer_action_logo.jpg"></a>
</div>

<br style="clear:both">


<div class="column-left">

<h3>Recently Suggested News Items<?=$Commenter['suggested_news']->getLink();?></h3><?=$Commenter['suggested_news']->getSupportingHTML();?>
<ul id="news" class="comments">
<?php

	$comment_count = 0;

	$result = mysql_query("SELECT User_News.*,CONCAT(Users.first_name,' ',Users.last_name) as 'name_concat' FROM User_News,Users WHERE Users.ID = User_News.user_ID AND CAST( UNIX_TIMESTAMP( ) - UNIX_TIMESTAMP( TS ) AS SIGNED INTEGER ) < (7 * 3600 * 24) ORDER BY ID DESC LIMIT 20");
	while($news_item = mysql_fetch_assoc($result)){
		$comment_count++;
		?>
		
		<li>
			<span class='number'><?=$comment_count;?></span>
			<h4><a href="<?=$news_item['url'];?>"><?=$news_item['title'];?></a> Posted by <a href="profile_user.php?ID=<?=$news_item['user_ID'];?>"><?=$news_item['name_concat'];?></a></h4>
			<p class='excerpt'>
				<?=$news_item['excerpt'];?>
			</p>
		</li>
		
		<?php
	}

?>
</ul>
</div>

<div class="column-right">
<h3>Recent Forum Posts</h3>

<ul id="forum" class="comments">
	<?php
	
	include_once("includes/classes/Comments.inc.php");
	
	$comment_count = 0;

	$Comments = new Comments(0,0,0);
	
	$comments = $Comments->getComments();
	if($comments != FALSE)
		foreach($comments as $comment){
			$comment_count++;
			?>
			<li>
				<span class='number'><?=$comment_count;?></span>
				<h4>
					<a href="forum_view_topic.php?ID=<?=$comment['forum_topic_ID'];?>#comment<?=$comment['ID'];?>"><?=$comment['topic'];?></a>
					Posted on <?=format_datetime($comment['timestamp']);?>	
				</h4>

				<p class="post">
					<?=stripslashes($comment['body']);?>
				</p>
			</li>
			<?php
		}
	
?>
</ul>
</div>



<script type="text/javascript">
	$('ul.comments li:odd').css({'background-color' : '#f2f8ff'});
</script>



<br style="clear:both">


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