<!doctype html>
<html>
	
	<head>
		<title>Rancid Tomatoes</title>

		<meta charset="utf-8" />
		<link href="movie.css" type="text/css" rel="stylesheet" />
		<link rel="icon" type="image/gif" href="rotten.gif" />
		
		<!-- 
		Katelyn Dempsey
		CPSC 5200
		Movie Review Part 2
		1/29/24
		-->
		
	</head>
	
	<?php
		//Variables
		$movie = $_GET["film"];
		$raw_info = file_get_contents("$movie/info.txt");
		$raw_overview = file_get_contents("$movie/overview.txt");
		$info = explode("\n", $raw_info);
		$overview = explode("\n", $raw_overview);
		$count = 0;
		$freshness = "";
		$freshAlt = "";
		
		//Sets the value for freshness and freshAlt
		if (intval($info[2]) >= 60){
				$freshness = "freshbig.png";
				$freshAlt = "Fresh";
			}
			else{
				$freshness = "rottenbig.png";
				$freshAlt = "Rotten";
			}
	
		//Functions
		
		//This function displays the overview sidebar 
		function sidebar(){
			global $overview;
			foreach($overview as $row){
				$row = explode(":", $row);
				echo "<dt>{$row[0]}</dt><dd>{$row[1]}</dd>";
			}
		}
		
		//This function gets the review information
		function getReviews(){
			global $movie;
			global $count;
			$raw_review = array();
			foreach (glob("$movie/review*.txt") as $filename){
				$raw_review[$count] = file_get_contents("$filename");
				$count++;
			}
			for ($i=0;$i<$count;$i++){
				$review = explode("\n", $raw_review[$i]);
				displayReview($review,$i);
			}
		}
		
		//This function displays the review information
		function displayReview($review,$num){
			global $count;
			$num++;
			$review[1] = strtolower($review[1]);
			echo "<p class='review'>
						<img src='{$review[1]}.gif' alt='{$review[1]}' />
						<q>{$review[0]}</q>
				 </p>
				 <p class='reviewer'>
				 		<img src='critic.gif' alt='Critic' />
				 		{$review[2]}<br />
				 		{$review[3]}
				 </p>";
			if($num == ceil($count/2)){
			echo "</div>
				  <div class='column'>";
			}
		}			
	?>

	<body>
		<div class="banner">
			<img src="banner.png" alt="Rancid Tomatoes" />
		</div>

		<h1 id="title"><?echo "$info[0] ($info[1])";?></h1>
		
		<div id="overall"> <!-- Start of main section -->
		
			<div id="right"> <!-- Start of right section -->
				<div>
					<img id="overview_img" src="<?=$movie?>/overview.png" alt="general overview" />
				</div>

				<dl>
					<?php sidebar();?>
				</dl>

			</div> <!-- End of right section -->

			<div id="left"> <!-- Start of left section -->

				<div id="score">
					<img id="rotten" src="<?=$freshness?>" alt="<?=$freshAlt?>" />
					<?=$info[2]?>%
				</div>
				
				<div class="column"> <!-- Start of Reviews -->
					<?php getReviews();?>				
				</div> <!-- End of reviews -->
				<div id="aftercolumns"></div>

			</div> <!-- End of left section -->

				<p id="footer">(1- <?=$count?>) of <?=$count?></p>
		</div> <!-- End of main section -->
		
	</body>
</html>