<?php
//include top of the page
include_once('top.php');

//include functions
include_once('events.inc');

//main
if($_GET['event_id']) {
	$current = getCurrent();
	// display top controls
	displayControls($current, 'events.php', 'topctrl');

// gather all data needed
	// get event information
	$sql = 'SELECT events.club_id, date, start, end, cover, description, summary, address, map, ages.text AS age, clubs.name AS club FROM events, clubs, ages WHERE event_id='.$_GET['event_id'].' AND events.club_id = clubs.club_id AND events.age_id = ages.age_id';
	$result = $db->query($sql);
	$event = $result->fetch_assoc();
	// changing format of date and time
	$event['date'] = date('d F Y',strtotime($event['date']));
	$event['start'] = timeTo12($event['start'], 1);
	$event['end'] = timeTo12($event['end'], 1);
	// get lineup for given event
	$sql = 'SELECT lineups.room_id, lineups.dj_id, djs.name FROM lineups, djs WHERE event_id='.$_GET['event_id'].' AND lineups.dj_id = djs.dj_id ORDER BY lineups.priority';
	$lineup = $db->query($sql);
	// get flyer for given event
	$sql = 'SELECT picture FROM fliers WHERE side="front" AND event_id='.$_GET['event_id'];
	$result = $db->query($sql);
	$flier = $result->fetch_assoc();
	$pic = $flier['picture'] ? substr($flier['picture'], 0, -5).'s.jpg' : 'images/noflier.gif';
	$fl_size = getimagesize($pic);
	// get rooms for given club
	$sql = 'SELECT room_id, name FROM rooms WHERE club_id='.$event['club_id'].' ORDER BY priority';
	$rooms = $db->query($sql);

// display data
	echo '<div id="event">'."\n";
	echo '<div id="flier">';
	echo '<img src="'.$pic.'"';
	echo ' '.$fl_size[3];
	echo ' alt="flier" />';
	echo '</div>';
	echo '<div id="details">';
	echo '<h2>'.$event['date'].'</h2>'."\n";
	echo '<p><span id="club-name">'.$event['club'].'</span> <span  id="address">('.$event['address'].')</span></p>'."\n";
	echo '<p id="time">'.$event['start'];
	echo $event['end'] ? ' - '.$event['end'] : '';
	echo '</p>'."\n";
	echo '<p><span id="cover">'.$event['cover'].'</span>, <span id="age">'.$event['age'].'</span></p>'."\n";
	echo '<ul id="lineup">'."\n";
	while($room = $rooms->fetch_assoc()) {
		$use = false;
		$lineup->data_seek(0);
		while($row = $lineup->fetch_assoc()) {
			if($room['room_id'] == $row['room_id']) {
				$use = true;
				break;
			}
		}
		if($use) {
			echo '<li><h3>'.$room['name'].'</h3>'."\n";
			echo '<ul>';
			$lineup->data_seek(0);
			while($row = $lineup->fetch_assoc()) {
				if($room['room_id'] == $row['room_id']) {
					echo '<li><a href="djs.php?dj_id='.$row['dj_id'].'">'.$row['name'].'</a></li>'."\n";
				}
			}
			echo '</ul></li>';
		}
	}
	echo '</ul>'."\n";
	echo '</div>';
	echo '<ul id="links"><li><a href="playlists.php?event_id='.$_GET['event_id'].'">Play Lists</a></li><li><a href="photos.php?event_id='.$_GET['event_id'].'">Photos</a></li><li><a href="fliers.php?event_id='.$_GET['event_id'].'">Flier</a></li></ul>';
	echo '<div id="desc-sum">';
	if($event['description']) {
		echo '<h3>Description:</h3>'."\n";
		echo '<p';
		echo $event['summary'] ? ' id="description"' : ''; //needed to fix CSS padding if summary exists
		echo '>'.$event['description'].'</p>'."\n";
	}
	if($event['summary']) {
		echo '<h3>Summary:</h3>'."\n";
		echo '<p>'.$event['summary'].'</p>'."\n";
	}
	echo '</div>';
	echo '</div>'."\n";
	// display bottom controls
	displayControls($current, 'events.php', 'bottomctrl');
} else {
	echo '<h1>&gt;&gt; Events &lt;&lt;</h1>';
	listEvents('events.php');
}

//include bottom
include_once('bottom.php');
?>