<?php
	$under_title 	= 'Danger with electronics is fun!';
	$page_title	= 'Schedule';

	include('directory.php');
	include($dir_prefix . 'header.html');
	
	echo "<link rel=\"stylesheet\" href=\"" . $dir_prefix . "style.css\">";
?>

<body bgcolor=#000000>
	<?php
		include($dir_prefix . 'top.php');
		include($dir_prefix . 'beginbody.php');
	?>
	<!--	      -
		     | |
		     | |
		 - - | |-      |====| |====| |====| |====|    |=|=| |    | |====| |====|    |    | |====| |====|  |====|
		| |  | | |     |    | |    | |      |           |   |\   | |      |    |    |    | |      |    |  |
		|        |-    |    | |    | |      |           |   | |  | |      |    |    |    | |      |    |  |
		|        | |   |====| |====| |      |====|      |   | \  | |==|   |    |    |====| |====| |====|  |====|
		|        | |   |      |    | |  |=| |           |   |  | | |      |    |    |    | |      |   \   |
		\         /    |      |    | |    | |           |   |   \| |      |    |    |    | |      |    \  |
		 \       /     |      |    | |====| |====|    |=|=| |    | |      |====|    |    | |====| |     \ |====|
			
		 WRITE ALL YOUR IMPORTANT INFORMATION HERE!  BUT LEAVE THIS TAG HERE! BECAUSE IT LOOKS COOL!
	-->
	<font class = "NormalText">
	This page contains my schedules, as you may have guessed by the title.  This could be useful if you're trying to track me down at some particular time of day.  It's also useful if you're just wondering what I do with my life.<br><br>
   At some point I realized this page fell out of date.  I updated it with my
new schedule.  Why did I ever think anyone would care enough me to look at my
schedule?  Oh well.  People post pictures of their breakfast on
Facebook...<br><br>
	<?php
		// our objective here is to read all the files in the schedules/ directory and parse them into working schedules
		// file format can be seen in any of the files
		$sched_dir = @opendir('schedules/') or die('Unable to open schedule directory.');

		$filnum = 0;
		while(false !== ($filename = readdir($sched_dir))) {
			if($filename[0] != ".") { // filter hidden files and '.', '..'
				$files[$filnum] = $filename;	
				$filnum++;
			}
		}

		usort($files, "strcmp");

		for($filnumindex = $filnum - 1; $filnumindex >= 0; $filnumindex--) {
			$file = fopen('schedules/' . $files[$filnumindex], 'r');
			
			$title = "";
			$activities = array();
			$actlongs = array();
			$times = array(array());
			$index = 0;
			$tstart = 0;
			$tend = 0;
			$days = "";
			while(!feof($file)) {
				$line = fgets($file);
				if($line[0] !== '#') { // make sure we're not looking at a comment
					$keyword = strtok($line, " ");

					// which keyword did we get?
					if($keyword == "TITLE") {
						$title = strtok(" ");
						while(false !== ($token = strtok(" ")))
							$title = $title . " " . $token;
					}
					else if($keyword == "SET") {
						$index = strtok(" ");
						$activities[$index] = strtok(" ");
						$actlongs[$index] = strtok(" ");

						while(false !== ($token = strtok(" ")))
							$actlongs[$index] = $actlongs[$index] . " " . $token;
					}
					else if($keyword == "TIME") {
						$index = strtok(" ");
						$days = strtok(" ");
						$tstart = strtok(" ");
						$tend = strtok(" ");

						for($i = 0; $i < strlen($days); $i++) {
							for($tcount = ($tstart / 50); $tcount < ($tend / 50); $tcount++) {
								$times[substr($days, $i, 1)][$tcount] = $index;
							}
						}
					}
				}
			}

			// We have all our information, let's make the table.
			echo "<font class=\"PageTitle\">";
			echo $title;
			echo "</font><br><br>";
			echo "<table border = 1>";
			echo "<thead>";
			echo "<th class=\"TableHead\" width=8%></th>";
			echo "<th class=\"TableHead\" width=12%>Sunday</th>";
			echo "<th class=\"TableHead\" width=12%>Monday</th>";
			echo "<th class=\"TableHead\" width=12%>Tuesday</th>";
			echo "<th class=\"TableHead\" width=12%>Wednesday</th>";
			echo "<th class=\"TableHead\" width=12%>Thursday</th>";
			echo "<th class=\"TableHead\" width=12%>Friday</th>";
			echo "<th class=\"TableHead\" width=12%>Saturday</th>";
			echo "</thead>";

			$hour = 0;
			$min = 0;

         $schedStart = 12;
         $schedEnd = 45;
         if($title == "Now\n") {
            $schedStart = 0;
            $schedEnd = 48;
         }

			for($i = $schedStart; $i < $schedEnd; $i++) {
				$hour = floor($i / 2);
				$min = ($i * 50) % 100;
				if($min == 50)
					$min = 30;

				echo "<tr>";
				echo "<td class=\"TableHead\">" . str_pad($hour, 2, "0", STR_PAD_LEFT) . ":" . str_pad($min, 2, "0", STR_PAD_RIGHT) . "</td>";

				// Put the table items there
				for($j = 0; $j < 7; $j++) {
					echo "<td class=\"ScheduleItem" . $times[$j][$i] . "\">";
					echo $activities[$times[$j][$i]];
					echo "</td>";
				}
			}
			echo "</table><br><br>";

			// Make the key
			echo "<font class=\"ImportantText\">Key:</font><br><br>";
			echo "<table border=1>";
			echo "<thead>";
			echo "<th class=\"TableHead\">Item</th>";
			echo "<th class=\"TableHead\">Description</th>";
			echo "</thead>";

			for($i = 1; $i <= count($activities); $i++) {
				echo "<tr>";
				echo "<td class=\"ScheduleItem" . $i . "\">";
				echo $activities[$i];
				echo "</td>";
				echo "<td class=\"TableBody\">";
				echo $actlongs[$i];
				echo "</td>";
				echo "</tr>";
			}

			echo "</table>";
			echo "<br><br><br>";
		}
	?>
	</font>
	
	<!-- End page! -->
	<?php
		include($dir_prefix . 'counter.php');
		include($dir_prefix . 'footer.php');
	?>
</body>
</html>
