<?php
require_once("config.php");
require_once("defs.php");
require_once("modules/Event.php");
require_once("header.php");

require_once("nav.php");
?>
<div id="content">
<div id="events_intro">
<span>
<h4>Events</h4>
<p>Upcoming events in the Blackguard calendar.</p>
</span>
</div>
<?php require_once("event_search_box.php")?>
<?php $month=$_GET['month'];
$year=$_GET['year'];
if(!$month){
	$month=date('m');
}
if(!$year){
	$year=date('Y');
}

$today=time();
$dispmonth=mktime(0,0,0,$month,1,$year);

$lastyear=$year;
$lastmonth=$month-1;
if($lastmonth<1){
	if($lastyear>2008){
		$lastyear--; $lastmonth=12;
	}else{
		$lastmonth=1;
	}
}
$lasttime=mktime(0,0,0,$lastmonth,1,$lastyear);

$nextyear=$year;
$nextmonth=$month+1;
if($nextmonth>12){
	if($nextyear<2014){ 
		$nextyear++;
		$nextmonth=1;
	}else{
		$nextmonth=12;
	}
}
$nexttime=mktime(0,0,0,$nextmonth,1,$nextyear);
$month=0+$month;
if($month<10) $month="0".$month;

$startblanks=date("w",$dispmonth);
$endblanks=7-date("w",$nexttime);
//if($endblanks<1) $endblanks+=6;

$days_in_month=date("t",$dispmonth);
?>

<div id="events">
	<div id="month_bar">
		<a href="events.php?month=<?php print urlencode($lastmonth)?>&year=<?php print urlencode($lastyear)?>"><img src="images/prev_month.jpg" alt="Previous" id="prev_month"/></a>
		<img src="images/month/month_<?php print urlencode($month)?>.jpg" alt="<?php print date("F",$dispmonth);?>" /> 
		<?php if($year<2015){?>
			<img src="images/year/year_<?php print urlencode($year)?>.jpg" alt="<?php print htmlentities($year)?>" />
		<?php }else{?>
			<?php print htmlentities($year)?>
		<?php }?> 
		<a href="events.php?month=<?php print urlencode($nextmonth)?>&year=<?php print urlencode($nextyear)?>"><img src="images/next_month.jpg" alt="Next" id="next_month"/></a>
	</div>		
	
	<div id="day_bar">
		<div class="day_header"><img src="images/day/day_0.jpg" alt="Sunday" /></div>
		<div class="day_header"><img src="images/day/day_1.jpg" alt="Monday" /></div>
		<div class="day_header"><img src="images/day/day_2.jpg" alt="Tuesday" /></div>
		<div class="day_header"><img src="images/day/day_3.jpg" alt="Wednesday" /></div>				
		<div class="day_header"><img src="images/day/day_4.jpg" alt="Thursday" /></div>
		<div class="day_header"><img src="images/day/day_5.jpg" alt="Friday" /></div>
		<div class="day_header"><img src="images/day/day_6.jpg" alt="Saturday" /></div>
	</div>
	<div id="calendar">
	<?php for($i=0;$i<$startblanks;$i++){?>
		<div class="day day_inactive">
		</div>
	<?php }?>
	<script>
	eventDays=[];
	monthStr='<?php print date("F Y",$dispmonth)?>';
	</script>
	<?php
	for($i=1;$i<=$days_in_month;$i++){
		$dayStr=date("l jS",mktime(0,0,0,$month,$i,$year)); 
	?>
		<div class="day">
			<img class="date_img" src="images/day/daynum_<?php print $i?>.jpg" alt="<?php print $i?>" />
			<?php $dayEvents=Event::getEvents($i,$month,$year);
			  foreach($dayEvents as $ev){
			 	?>			
				<script>addEvent('<?php print $dayStr?>','<h4><?php print $ev->getEventTime()?> UK time - <?php print htmlentities($ev->title)?></h4><p><?php print str_replace("\r","\\r",str_replace("\n","\\n",htmlentities($ev->description)))?></p><p class="poster">Posted by <?php print $ev->getAuthorName()?> on <?php print date("jS F Y",$ev->createDate)?></p>');</script>
				<a class="day_event" onClick="showDay('<?php print $dayStr?>'); return false;" href="#"><?php print $ev->getEventTime()?>: <?php print htmlentities($ev->title)?></a>
				<?php if($userdata['isAdmin'] || $ev->idAuthor==$userdata['user_id']){?>(<a href="addEvent.php?id=<?php print $ev->id?>">Edit</a>)<?php }?>							
				<?php
			   }			
			 ?>
				
		</div>
	<?php }?>
	
	<?php for($i=0;$i<$endblanks;$i++){?>
		<div class="day day_inactive">
		</div>
	<?php }?>
	</div>
</div>
<?php
require_once("footer.php");
?>