<table class="schedule" style="width:100%;">
	<thead class="head" >
		<tr class="">
			<th style="font-size: 10pt; border-bottom: 2px solid #1c43a6; width: 85px;">Status</th>
			<th style="font-size: 10pt; border-bottom: 2px solid #1c43a6; width: 110px;" colspan="2">Details</th>			
			<th style="font-size: 10pt; border-bottom: 2px solid #1c43a6; width: 120px;" colspan="2">Date</th>			
			<th style="font-size: 10pt; border-bottom: 2px solid #1c43a6; width: 100px;">Start Time</th>
			<th style="font-size: 10pt; border-bottom: 2px solid #1c43a6; width: 345px;">Opponent</th>
			<th style="font-size: 10pt; border-bottom: 2px solid #1c43a6; width: 60px;">Dismiss</th>
			<th style="font-size: 10pt; border-bottom: 2px solid #1c43a6; width: 60px;">Depart</th>
			<th style="font-size: 10pt; border-bottom: 2px solid #1c43a6; width: 60px;">Return</th>
		</tr>
	</thead>

	<tbody>
		<?php
			$show_swap_legend = false;

			// Get a list of displayable statuses
			$statuses = array(
				'PND'  => array('Pending', 'Contest is waiting to be approved.', 'images/icons/status_pnd_22px.png'),
				'SCHD' => array('Scheduled', 'Contest is scheduled for the future.', 'images/icons/status_schd_2_22px.png'), 
				'CXL'  => array('Canceled', 'Contest has been canceled.', 'images/icons/status_cxl_22px.png'),
				'PPD'  => array('Postponed', 'Contest has been delayed and postponed to a later date/time.', 'images/icons/status_ppd_22px.png'),
				'LIVE' => array('Live', 'Contest is live.', 'images/icons/status_live_22px.png'), 
				'DONE' => array('Done', 'Contest is finished and done.', null), 
			);

			// Allow OSAA users to include deleted contests
			if (Input::has('show-delete') and Auth::check() and Auth::user()->isOsaaUser())
			{
				$statuses['DEL'] = array('Deleted', 'Contest has been deleted from this schedule.', 'images/icons/status_del_22px.png');
			}

			// Get a list of contest types
			$types = array(
				'J'   => array('Jamboree'),
				'EX'  => array('Exhibition'),
				'A'   => array('Alumni'),
				'NL'  => array('Non-League'),
				'L'   => array('League'),
				'LPO' => array('League Playoff'),
				'PI'  => array('Play-In'),
				'PO'  => array('Playoff'),
				'PP'  => array('Pool Play')
			);
		?>

		<?php if (isset ($team->schedule)): ?>
			<?php foreach ($team->schedule as $item): ?>

				<?php
					// Skip this contest if its status is not in the list of displayable statuses
					if (!Helpers::strEqual($item['status'], array_keys($statuses)))
					{
						continue;
					}
				?>				

				<tr style="height: 2em;" data-status="<?php echo $item['status']; ?>">
					<td>
						<?php if (Helpers::strEqual($item['status'], 'DONE')): ?>

				        	<?php
					        	$results = 'Done';

					        	if (!is_null($item['contest']->home_team_score) /*and $item['contest']->is_scoreable*/)
			                    {
			                        // Get my team's designation
			                        $is_home_team = ($team->id == $item['contest']->home_team);                        
			                        
			                        // I am the home team
			                        if ($is_home_team)
			                        {   
			                            // Home team wins!
			                            if ($item['contest']->home_team_score > $item['contest']->away_team_score)
			                            {
			                                $win = 'W';      
			                                $mine = $item['contest']->home_team_score;
			                                $opp = $item['contest']->away_team_score;
			                            }                     
			                            // Home team lost :(
			                            else if ($item['contest']->home_team_score < $item['contest']->away_team_score)
			                            {                                
			                                $win = 'L';   
			                                $opp = $item['contest']->away_team_score;
			                                $mine = $item['contest']->home_team_score;                               
			                            }
			                            // Otherwise, it's a tie
			                            else
			                            {                               
			                                $win = 'T';
			                                $mine = $item['contest']->home_team_score;
			                                $opp = $item['contest']->away_team_score;
			                            }
			                        }
			                        // I am the away team
			                        else
			                        {
			                            // Home team wins :(
			                            if ($item['contest']->home_team_score > $item['contest']->away_team_score)
			                            {
			                                $win = 'L';      
			                                $opp = $item['contest']->home_team_score;
			                                $mine = $item['contest']->away_team_score;
			                            }                     
			                            // Home team lost!
			                            else if ($item['contest']->home_team_score < $item['contest']->away_team_score)
			                            {                                
			                                $win = 'W';   
			                                $mine = $item['contest']->away_team_score;
			                                $opp = $item['contest']->home_team_score;                               
			                            }
			                            // Otherwise, it's a tie
			                            else
			                            {                               
			                                $win = 'T';
			                                $opp = $item['contest']->home_team_score;
			                                $mine = $item['contest']->away_team_score;
			                            }
			                        }			                        

			                        if (is_null($item['contest']->is_forfeit))
			                        {
			                        	$results = '<b>' . $win . '</b> ' . $mine . '-' . $opp;
			                        }
			                        elseif (Helpers::isExcludableForfeit($item['contest']->is_forfeit))
			                        {
										$results = '<b>' . $win . '</b> <small>Forfeit</small>';
			                        }
                                    else
                                    {
                                        $results = '<b>' . $win . '</b> <small>' . $mine . '-' . $opp . ' Forfeit</small>';
                                    }

			                        if ($item['contest']->is_swap_designation)
			                        {
			                        	$results = '*' . $results;
			                        	$show_swap_legend = true;
			                        }
			                    }
			                ?>

			                <?php echo $results; ?>

						<?php else: ?>

							<?php if (!is_null($statuses[$item['status']][2])): ?>
								<img src="<?php echo asset($statuses[$item['status']][2]); ?>" alt="" title="<?php echo $statuses[$item['status']][1]; ?>" class="tooltip" style="vertical-align: middle; width: 18px; height: 18px;" />
							<?php endif; ?>

							<?php if ($item['status'] == 'LIVE' and $team->level == 'V' and $item['contest']->is_scoreable): ?>
						        <a href="<?php echo url('/contests/' . $item['contest']->id . '/submit-scores'); ?>" class="tooltip" style="font-weight: bold;" title="Submit scores for this contest.">
						    <?php endif; ?>							

							<div class="status_name" style="font-size: 8pt; display: inline-block;"><?php echo $statuses[$item['status']][0]; ?></div>

							<?php if ($item['status'] == 'LIVE' and $team->level == 'V' and $item['contest']->is_scoreable): ?>
								</a>
							<?php endif; ?>

						<?php endif; ?>
					</td>
					<td>
						<?php
							$link_title = 'Click to view details about this contest.';

							if (Auth::check() and Auth::user()->isOsaaUser())
							{
								$link_title .= ' Contest ID = ' . $item['contest']->id;
							}
						?>

						<img src="<?php echo asset('/images/icons/information_24px.png'); ?>" alt="" title="<?php echo $link_title; ?>" class="tooltip contest_details" data-contest="<?php echo $item['contest']->id; ?>" style="vertical-align: middle; width: 18px; height: 18px; float: left; margin: 7px 4px 7px 0;" />
					</td>
					<td style="font-size: 8pt; line-height: 1.15em;">
						<?php echo $types[$item['contest']->contest_type][0]; ?>
						<?php
							// Get playoff round slugs
							if (Helpers::strEqual($item['contest']->contest_type, 'PO'))
							{
								$matchup = matchup::where('contest', '=', $item['contest']->id)->firstOrFail();
								$round = round::find($matchup->round);
								$round_type = roundtype::where('slug', '=', $round->round_type)->firstOrFail();

								echo ' <span class="tooltip" title="' . $round_type->name . ' Round of OSAA Playoffs">(' . $round->round_type . ')</span>';
							}
						?>
						<?php if ($item['contest']->is_tournament): ?>
							<br /><span style="color: #0012ff;">Tournament</span>
						<?php endif; ?>
						<?php if ($item['contest']->is_endowment): ?>
							<br /><span style="color: #ad8309;">Endowment</span>
						<?php endif; ?>
						<?php if ($item['contest']->is_neutral_location): ?>
							<br /><span style="color: #37a913;">Neutral Site</span>
						<?php endif; ?>						
					</td>
					<td style="padding: 0 4px 0 4px; font-size: 11pt;">
						<?php
							$day = null;

							switch(date('N', strtotime($item['event']->start_at)))
							{
								case 1: $day = 'M'; break;
								case 2: $day = 'T'; break;
								case 3: $day = 'W'; break;
								case 4: $day = 'R'; break;
								case 5: $day = 'F'; break;
								case 6: $day = 'S'; break;
								case 7: $day = 'Su'; break;
							}
						?>

						<?php /*  $item['day']  */ ?>
						<?php echo $day; ?>
					</td>
					<td style="font-size: 11pt;">
						<a href="<?php echo url('/activities/' . strtolower($item['contest']->activity) . '/schedules?date=' . date('Y-m-d', strtotime($item['date'])) . '&amp;div=' . $item['contest']->division); ?>"><?php echo date('n/j/y', strtotime($item['date'])); ?></a>
					</td>					
					<td style="font-size: 11pt;">
						<?php echo $item['time']; ?>
					</td>
					<td style="font-size: 11pt;">
						<?php echo $item['opponent']; ?>
					</td>
					<td style="font-size: 10pt;">
						<?php if (is_null($item['contest']->{ $item['designation'] . '_dismiss_time' })): ?>
							
						<?php else: ?>
							<?php if (Helpers::strEqual(date('g:i a', strtotime($item['contest']->{ $item['designation'] . '_dismiss_time' })), '12:00 AM')): ?>
								TBD
							<?php else: ?>
								<?php echo Helpers::formatDateTime('%TEAM_SCHEDULE_TIME%', strtotime($item['contest']->{ $item['designation'] . '_dismiss_time' }), $item['event']->time_zone); ?>
                			<?php endif; ?>
						<?php endif; ?>
					</td>
					<td style="font-size: 10pt;">
						<?php if (is_null($item['contest']->{ $item['designation'] . '_depart_time' })): ?>
							
						<?php else: ?>
							<?php if (Helpers::strEqual(date('g:i a', strtotime($item['contest']->{ $item['designation'] . '_depart_time' })), '12:00 AM')): ?>
								TBD
							<?php else: ?>
								<?php echo Helpers::formatDateTime('%TEAM_SCHEDULE_TIME%', strtotime($item['contest']->{ $item['designation'] . '_depart_time' }), $item['event']->time_zone); ?>
                			<?php endif; ?>
						<?php endif; ?>
					</td>
					<td style="font-size: 10pt;">
						<?php if (is_null($item['contest']->{ $item['designation'] . '_return_time' })): ?>
							
						<?php else: ?>
							<?php if (Helpers::strEqual(date('g:i a', strtotime($item['contest']->{ $item['designation'] . '_return_time' })), '12:00 AM')): ?>
								TBD
							<?php else: ?>
								<?php echo Helpers::formatDateTime('%TEAM_SCHEDULE_TIME%', strtotime($item['contest']->{ $item['designation'] . '_return_time' }), $item['event']->time_zone); ?>
                			<?php endif; ?>
						<?php endif; ?>
					</td>
				</tr>

			<?php endforeach; ?>
		<?php endif; ?>

		<?php if (count($team->schedule) == 0): ?>
			<tr>
				<td colspan="10">No contests have been added to this schedule, yet.</td>
			</tr>
		<?php endif; ?>

	</tbody>

</table>




<?php if ($show_swap_legend): ?>
	<br />
	<small>* Indicates the teams swapped designations; home team batted first.</small>
<?php endif; ?>