<?php 

	header('Content-type: text/html; charset=utf-8'); 
	require_once('../admin/lib/viewer_functions.php');
	
	// global items
	list($global, $globalMetaData) = getRecords(array(
		'tableName'   => 'global_items',
		'where'       => '', // load first record
		'loadUploads' => true,
		'allowSearch' => false,
		'limit'       => '1',
	));
	$global = @$global[0]; // get first record

	// load records from 'trainings_events_pages'
	list($subnav, $subnavMetaData) = getRecords(array(
		'tableName'   => 'trainings_events_pages',
		'loadUploads' => true,
		'allowSearch' => false,
		));

	// load record from 'trainings_events_pages'
	list($page, $pageMetaData) = getRecords(array(
		'tableName'   => 'trainings_events_pages',
		'where'       => whereRecordNumberInUrl(1),
		'loadUploads' => true,
		'allowSearch' => false,
		'limit'       => '1',
		));
		$page = @$page[0]; // get first record
		if (!$page) { header("Location: ./"); exit; }


	// load records from 'categories'
	list($categoriesRecords, $categoriesMetaData) = getRecords(array(
		'tableName'   => 'categories',
		'loadUploads' => true,
		'allowSearch' => false,
		));
		
		
	// load records from 'courses'
	list($coursesRecords, $coursesMetaData) = getRecords(array(
		'tableName'   => 'courses',
		'loadUploads' => false,
		'allowSearch' => false,
		));
	
	// load records from 'events'
	list($eventsRecords, $eventsMetaData) = getRecords(array(
		'tableName'   => 'events',
		'loadUploads' => true,
		'allowSearch' => false,
		));
		
	
	// random banner photos
	list($photos, $photosMetaData) = getRecords(array(
		'tableName'   => 'banner_photos',
		'where'       => '', // load first record
		'orderBy'	  => 'RAND()',
		'loadUploads' => true,
		'allowSearch' => false,
		'limit'       => '1',
	));
	$photos = @$photos[0]; // get first record


	$pageTitle = $page['title'];
	$bodyClass = "training";
	$pageKeywords = $page['keywords'];
	$pageDescription = $page['description'];

	include('../includes/header.inc.php');
?>
		
		<div class="column twoBlock content">


				<!-- Upcoming Trainings -->
		
				<?php if ($page['content_module'] === "upcoming"): ?>
	
					<div class="leftColumn">
					
						<?php echo $page['content']; ?>
						
						
						<?php
							
							//INSERT ALL YOUR getRecord() CALLS HERE, REPLACING MINE
		
							list($courses, $coursesMetaData) = getRecords(array(
								'tableName' => 'events',
								'orderBy' => 'start_date',
							));
							
							list($events, $eventsMetaData) = getRecords(array(
								'tableName' => 'course_occurrences',
								'where' => "removeDate >= NOW() OR removeDate='0000-00-00 00:00:00'",
								'orderBy' => 'start_date',
							));
							
							
							// put all the records in one list
							
							$allRecords = array_merge(
								$courses,
								$events
							);
							
							
							// discard everything after the 3rd record
							
							$newestRecords = array_slice($allRecords, 0, 1000);
							
						?>
						
						<?php if ($events): ?>
							<h2>Schedule</h2>
						<?php endif; ?>						
						
						
						<?php foreach ($newestRecords as $record): ?>
							
							<?php  if (@$record['course']): ?>
								<?php $relatedTraining = mysql_select("courses", "num = {$record['course']}"); ?> 
							<?php endif ?>
													
							<div class="listing boxed">	
								<h4>
									<?php echo @$record['title'] ?>
									<?php echo @$record['course:label'] ?>
								</h4>
								<h5><?php echo htmlencode($record['dates_time_info']) ?></h5>
								
								<p>Venue: <a href="./venue.php?<?php echo $record['location'] ?>"><?php echo htmlencode($record['location:label']) ?></a></p>
								
								<p>
									<?php if (@$record['course']): ?>
										<?php foreach ($relatedTraining as $training): ?>
											<strong>Fee: <?php echo $training['price']; ?></strong><br />
										<?php endforeach ; ?>
									<?php elseif ($record['title'] && $record['price']): ?>
										<strong>Fee: <?php echo $record['price'] ?></strong>
									<?php else: ?>
									<?php endif; ?>
									<em><?php echo htmlencode(@$record['notes']) ?></em>
								</p>
								
								<ul class="action_buttons">
									<?php if(@$record['registration_status'] === "open" and $record['cvent_url']): ?>
										<li class="btn_register"><a href="<?php echo htmlencode($record['cvent_url']) ?>">Register</a></li>
									<?php elseif(@$record['registration_status'] === "waitlist" and $record['cvent_url']): ?>
										<li class="btn_waitlist"><a href="<?php echo htmlencode($record['cvent_url']) ?>">Join Wait List</a></li>
									<?php elseif(@$record['registration_status'] === "closed"): ?>
										<li class="btn_closed">Closed</li>
									<?php elseif(@$record['cvent_url']): ?>
										<li class="btn_register"><a href="<?php echo htmlencode($record['cvent_url']) ?>">Register</a></li>
									<?php else: ?>
									<?php endif; ?>
									<li class="btn_info">
										<a href="
											<?php
												if(@$record['course:label']) {
													echo "/trainings/detail.php?{$record['course']}";
												} else {
													echo "/trainings/event.php?{$record['num']}";
												}
											?>">
												More Info
											</a>
									</li><!-- /btn_info -->
								</ul>		

									
							</div><!-- /listing boxed -->
							
						<?php endforeach; ?>
						
						

						<?php if (!$events): ?>
							<p>There are no trainings schedule at this time. Please check back soon, or <a href="/contact/?topic=training">contact us</a> to added to our mailing list.</p>
						<?php endif; ?>

	
					</div><!-- /leftColumn -->


					<div class="rightColumn">
						<?php echo $page['sidebar']; ?>
					</div>

	
				<?php endif; ?>



				<!-- Trainings by Category -->
	
				<?php if ($page['content_module'] === "by_category"): ?>
	
					<div class="leftColumnFull">
					
						<?php echo $page['content']; ?>

						<?php foreach ($categoriesRecords as $category): ?>
			
							<?php $counter = mysql_count('courses', "category = ".$category['num']); ?>
							<?php if($counter === 0) continue; ?>
		
								<div class="listing boxed">
									<h4><?php echo htmlencode($category['title']) ?></h4>
									<ul>
										<?php foreach ($coursesRecords as $course): ?>
											<?php if($category['num'] == $course['category']): ?>
												<li>
													<a href="<?php echo $course['_link']; ?>">
														<?php echo $course['title']; ?>
													</a>
												</li>
											<?php endif; ?>
										<?php endforeach ?>
									</ul>
								</div>
						<?php endforeach ?>

						<?php echo $page['sidebar']; ?>

					</div>
	
				<?php endif; ?>
	
	

				<!-- Trainings by Title -->
	
				<?php if ($page['content_module'] === "by_title"): ?>
	
					<div class="leftColumnFull">
					
						<?php echo $page['content']; ?>

						<?php foreach ($coursesRecords as $course): ?>
							<h3>
								<a href="<?php echo $course['_link']; ?>">
									<?php echo $course['title']; ?>
								</a>
							</h3>
						<?php endforeach ?>

						<?php echo $page['sidebar']; ?>

					</div>
	
				<?php endif; ?>
	
	

				<!-- Events/workshops -->
	
				<?php if ($page['content_module'] === "events_workshops"): ?>

					<div class="leftColumn">
					
						<?php echo $page['content']; ?>
						
						<?php foreach ($eventsRecords as $record): ?>

							<div class="listing boxed">
								<h4><?php echo $record['title']; ?></h4>
								<h5><?php echo htmlencode($record['dates_time_info']) ?></h5>
								<p>Venue: <a href="./venue.php?<?php echo htmlencode($record['location']) ?>"><?php echo htmlencode($record['location:label']) ?></a></p>
								<ul class="action_buttons">
									<?php if($record['cvent_url']): ?>
										<li class="btn_register"><a href="<?php echo htmlencode($record['cvent_url']) ?>">Register</a></li>
									<?php endif; ?>
									<li class="btn_info"><a href="<?php echo $record['_link'] ?>">More Info</a></li>
								</ul>
								
							</div>
						<?php endforeach ?>
	
					</div>


					<div class="rightColumn">
						<?php echo $page['sidebar']; ?>
					</div>

	
				<?php endif; ?>



				<!-- other/none -->
	
				<?php if ($page['content_module'] === "none" OR !$page['content_module']): ?>

					<div class="leftColumn">
						<?php echo $page['content']; ?>
					</div>

					<div class="rightColumn">
						<?php echo $page['sidebar']; ?>
					</div>
	
				<?php endif; ?>


		</div><!-- /twoBlock content -->
			
		<div class="column block sidebar">
			
			<div class="subNav">
				
				<?php $subnav_counter = 0; ?>
				<?php foreach ($subnav as $record): ?>
					<?php $subnav_counter++; ?>
					<?php if($subnav_counter === 1): ?>
						<p class="title group"><a href="/trainings/">Trainings &amp; Events</a><span></span></p>
						<ul>
					<?php else: ?>
							<li>
								<a href="<?php echo htmlencode($record['_link']) ?>"<?php if($record['num'] === $page['num']) echo " class='current'"?>>
									<?php echo htmlencode($record['navigation_text']) ?>
								</a>
							</li>
					<?php endif; ?>
				<?php endforeach; ?>
				</ul>
			
			</div><!-- /subNav -->
			
		</div><!-- /sidebar -->
		

<?php include('../includes/footer.inc.php'); ?>