<?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.name, 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};");
		if ($DB->numrows()===0){
			echo "Invalid event id";	
		} else {					
			$row = mysql_fetch_assoc($event);
			
			if(file_exists("images/icons/" . $row["type"] . ".png")){
				$icon_str = "images/icons/" . $row["type"] . ".png";
			} else {
				$icon_str = "images/icons/Default.png";
			}
			
			
			echo "<table width=\"540px\"><tr><td width=\"100px\"><img src=\"{$icon_str}\"></td><td style=\"padding:5px;\" colspan=\"3\"><big>{$row["title"]}</big><br><small>&nbsp;&nbsp;&nbsp;by: <a href=\"index.php?view=user&name={$row["username"]}\"><img src=\"images/icons/user.png\"> {$row["name"]} ( <img src=\"images/icons/email.gif\"> <u>" . (is_subscribed($row["username"])?"unsubscribe":"subscribe") . "</u> )</a></small></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><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\">";
			}
		
			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"]:"-- -- --");
			echo "<div id=\"comments\">";
			display_comments($row["id"]);
			echo "</div>";
		}
	}
?>