<?php defined( '_BIGNIGHT_EXEC' ) or die( 'Restricted access' );?>

<script type="text/javascript" src="js/attend.js"></script>
<script type="text/javascript" src="js/comment.js"></script>

<?php
	if(!isset($_GET["id"])) echo "No event selected.";
	else {
		$DB = DB::Open();
		global $user;
		
		$id = intval($_GET["id"]);
		$event = $DB->qry("SELECT e.*,u.permissions FROM events e LEFT JOIN users u ON e.username=u.username WHERE id={$id};",4);
		if ($DB->numrows()===0){
			echo "Invalid event id";	
		} else {
			if(isset($_POST["delete-submit"]) && isset($_POST["delete"])){
				if($user["username"]==$event["username"] || ($user["permissions"]>=1 && $user["permissions"]>$event["permissions"])){
					delete_event($id);
					echo "Event deleted successfully.<br>\n";
					echo "<a href=\"index.php\">back to home</a>";
				} else {
					$error = "Access Denied.";
					echo "You have no permissions to delete this event";
				}
			} else if(isset($_GET["delete"])){
				echo "<form action=\"index.php?view=event&id={$id}\" method=\"post\">\n";
				echo "<font color=\"#FF0000\">Are you sure you want to delete event <b>{$event["title"]}</b>?</font><br>\n";
				echo "<input type=\"hidden\" name=\"delete\" value=\"Delete\">";
				echo "<input type=\"submit\" name=\"delete-submit\" value=\"Delete permanently\"> <a href=\"index.php?view=event&id={$id}\">Cancel</a>\n";
				echo "</form>";
			} else {
				if(isset($_POST["submit"])){
					
					$minutes = intval($_POST["minutes"]);
					if ($minutes == -1) $minutes = 0;
					else if ($minutes >= 60 || $minutes <0) die("Error! Incorrect minutes value.");
					$hours = intval($_POST["hours"]);
					if ($hours == -1) $hours = 12;	// default time 12:00 - noon
					else if ($hours > 12 || $hours <= 0) die("Error! Incorrect hours value.");
					$month = intval($_POST["month"]);
					if ($month > 12 || $month < 1) die("Error! Incorrect month value.");
					$day = intval($_POST["day"]);
					if ($day > 31 || $day < 1) die("Error! Incorrect day value.");
					$year = intval($_POST["year"]);
					if ($year > gmdate("y")+5 || $year < gmdate("y")) die("Error! Year out of range.");
					
					if ($_POST["ampm"]=="pm") $hours += 12;
					$time = gmmktime($hours, $minutes, 0, $month, $day, $year);
					
					$name = str_replace("'", "''", $_POST["title"]);
					if(!empty($name)) $DB->qry("UPDATE events SET title='{$name}' WHERE id={$id};");
					$description = str_replace("'", "''", $_POST["description"]);
					$address = str_replace("'", "''", $_POST["address"]);
					$price = round ((float)$_POST["price"], 2);
					$zip = intval($_POST["zip"]);
					if(!empty($zip) && $zip>9999) $DB->qry("UPDATE events SET zip={$zip} WHERE id={$id};");
					
					$DB->qry("UPDATE events SET address='{$address}',price={$price},time={$time},description='{$description}' WHERE id={$id};");
			
				}
			
			
				$row = $DB->qry("SELECT e.*, u.name, u.permissions, a.event_id AS attending, z.city, z.state_prefix AS state, z.lat, z.lon FROM events e LEFT JOIN zip_code z ON e.zip=z.zip_code JOIN users u ON e.username=u.username LEFT JOIN (SELECT * FROM attending WHERE username='{$user["username"]}') a ON e.id=a.event_id WHERE id={$id};",4);
		
				
				if(file_exists("images/icons/" . $row["type"] . ".png")){
					$icon_str = "images/icons/" . $row["type"] . ".png";
				} else {
					$icon_str = "images/icons/Default.png";
				}
				
				$edit = (isset($_GET["edit"]) && ($_GET["edit"]==1) && ($user["username"]==$row["username"]));
				
				echo "<table width=\"540px\"><tr><td width=\"100px\"><img src=\"{$icon_str}\"></td>";
				
				if($edit){
					echo "<form method=\"post\" action=\"index.php?view=event&id={$id}\">\n";
					echo "<td style=\"padding:5px;\" colspan=\"3\"><input type=\"text\" name=\"title\" size=\"50\" value=\"{$row["title"]}\"></big><br>";
				} else {
					echo "<td style=\"padding:5px;\" colspan=\"3\"><big>{$row["title"]}</big><br>";
				}
				
				
				
				echo "<small>&nbsp;&nbsp;&nbsp;by: <a href=\"index.php?view=user&name={$row["username"]}\"><img src=\"images/icons/user.png\"> {$row["name"]}" . ($row["username"]!=$user["username"] ? " ( <img src=\"images/icons/email.gif\"> <u>" . (is_subscribed($row["username"])?"unsubscribe":"subscribe") . "</u> )" : "") . "</a></small>";
				
				echo "<div class=\"event_edit\">";
				if($user["username"]==$row["username"]){
					 echo ($edit ? "<input type=\"submit\" name=\"submit\" value=\"Save Changes\"> " : "") . "<a href=\"index.php?view=event&id={$id}" . ($edit ? "" : "&edit=1") . "\"> [ " . ($edit ? "back" : "edit") . " ]</a><br>";
				}
				if($user["username"]==$row["username"] || ($user["permissions"]>=1 && $user["permissions"] > $row["permissions"])) echo "<a style=\"color:#FFCCCC;\" href=\"index.php?view=event&id={$id}&delete=1\">[ delete ]</a>";
				echo "</div>";
				
				echo "</td></tr>\n";
		
				if($row["attending"] === NULL) $attending = false;
				else $attending = true;

				echo "<tr><td style=\"max-width:100px\">";
				
				echo "<iframe src=\"//www.facebook.com/plugins/like.php?href=http%3A%2F%2F146.245.250.215%2F%7Eachikin%2Fproject%2Findex.php%3Fview%3Devent%26id%3D{$id}&amp;send=false&amp;layout=button_count&amp;width=100&amp;show_faces=false&amp;action=like&amp;colorscheme=dark&amp;font&amp;height=21\" scrolling=\"no\" frameborder=\"0\" style=\"border:none; overflow:hidden; width:100px; height:21px;\" allowTransparency=\"true\"></iframe>";
				
				echo "</td><td width=\"110px\">\n";
				echo "<a onclick=\"attend({$row["id"]})\"><img id=\"attend_ico\" src=\"images/icons/" . ($attending ? "leave" : "attend") . ".png\"></a></td><td><div id=\"attend_txt\"><small>" . ($attending ? "You are attending" : "You are not attending");
				echo "</small></div>";
				$att_cnt = attend_count($row["id"]);
				echo "<div style=\"float:left;\"><img src=\"images/icons/people.png\" title=\"People attending\"></div><div id=\"att_cnt\" style=\"float:left;\">" . (!empty($att_cnt) ? $att_cnt : "0") . "</div>";
				
				echo "</td>";
				
				if($edit){
					echo "<td style=\"color:#FFFF00;\">";
					echo "Date mm-dd-yy<br>";
					
					$mon = gmdate("m",$row["time"]);
					$day = gmdate("d",$row["time"]);
					$year = gmdate("y",$row["time"]);
					$min = gmdate("i",$row["time"]);
					$hour = gmdate("g",$row["time"]);
					
					echo "<input type=\"text\" name=\"month\" size=\"2\" maxlength=\"2\" value=\"{$mon}\" onKeyPress=\"return numbersonly(this, event)\">-";
					echo "<input type=\"text\" name=\"day\" size=\"2\" maxlength=\"2\" value=\"{$day}\" onKeyPress=\"return numbersonly(this, event)\">-";
					echo "<input type=\"text\" name=\"year\" size=\"2\" maxlength=\"2\" value=\"{$year}\" onKeyPress=\"return numbersonly(this, event)\">";
					echo "<br>";
					echo "<select name=\"hours\">";
						echo"<option value=\"-1\">-</option>";
						for ($hr=1;$hr<=12;$hr++){
							echo"<option value=\"{$hr}\"";
							if($hour == $hr) echo "selected=\"selected\"";
							echo ">{$hr}</option>";
							}
						echo"</select>";
						echo"<select name=\"minutes\">";
							echo"<option value=\"-1\">-</option>";
							echo"<option value=\"0\">00</option>";
							echo"<option value=\"5\">05</option>";
							echo"<option value=\"10\">10</option>";
							echo"<option value=\"15\">15</option>";
							echo"<option value=\"20\">20</option>";
							echo"<option value=\"25\">25</option>";
							echo"<option value=\"30\">30</option>";
							echo"<option value=\"35\">35</option>";
							echo"<option value=\"40\">40</option>";
							echo"<option value=\"45\">45</option>";
							echo"<option value=\"50\">50</option>";
							echo"<option value=\"55\">55</option>";
							echo"<option value=\"{$min}\" selected=\"selected\">{$min}</option>";
						echo "</select>";
						echo "<select name=\"ampm\">";
							echo "<option value=\"am\">am</option>";
							echo "<option value=\"pm\" selected=\"selected\">pm</option>";
						echo "</select>";							
				}
				else
				{
					echo "<td style=\"text-align:right; color:#FFFF00;\">";
					echo gmdate("F d, Y", $row["time"]) . "<br>" . gmdate("g:i a", $row["time"]); 
				}
				
				echo "</td></tr>\n<tr><td style=\"text-align: right;\">people: </td><td colspan=\"3\"><div id=\"people\">" . get_people($row["id"]) . "</div></td></tr>\n";
				
				echo "</td></tr>\n<tr><td style=\"text-align: right;\">{$row["state"]},</td><td colspan=\"3\">{$row["city"]}</td></tr></table>\n";
				
				if(!empty($row["address"])){
					$search_str = str_replace(" ","+",$row["address"]);
					$search_str = "{$search_str},{$row["zip"]}";
				} else {
					$search_str = "{$row["lat"]},{$row["lon"]}";
				}
				
				
				if(!empty($search_str)){
					echo "<div class=\"mapimg\">";
					echo "<a href=\"http://maps.google.com/maps?q={$search_str}&hl=en&sll={$search_str}&sspn={$search_str}&vpsrc=6&hq={$search_str}&t=m&z=15\" target=\"_blank\">Get directions<br><img src=\"http://maps.googleapis.com/maps/api/staticmap?center={$search_str}&zoom=12&size=150x150&markers=color:blue%7Clabel:A%7C{$search_str}&sensor=false\" style=\"border: 2px solid #FFFFFF;\"></a>";
					echo "</div>";
				} else {
					echo "<img src=\"images/map_not_found.jpg\">";
				}
			
				if($edit){
					echo "<br>Address: <input type=\"text\" name=\"address\" size=\"30\" value=\"{$row["address"]}\">";
					echo "ZipCode: <input type=\"text\" name=\"zip\" size=\"5\" maxlength=\"5\" value=\"{$row["zip"]}\" onKeyPress=\"return numbersonly(this, event)\"><br>";
					echo "Price: <input type=\"text\" name=\"price\" size=\"5\" value=\"{$row["price"]}\"onKeyPress=\"return numbersonly(this, event, true)\"><br>";
					echo "<br>Description: <br><textarea rows=\"5\" cols=\"50\" name=\"description\">{$row["description"]}</textarea>";
				
				}
				else {
					echo "<br>{$row["address"]}, {$row["zip"]}<br>";
					echo "Price: " . (intval($row["price"])==0?"<font color=\"#FFAAAA\">free</font>":"$ {$row["price"]}") . "<br>";
					echo "<br>" . (!empty($row["description"])?$row["description"]:"-- -- --");
				}
			
				if ($edit) echo "</form>";
				
				
				echo "<div id=\"comments\">";
				display_comments($row["id"]);
				echo "</div>";
			}
		}
	}
?>