<table class="schedule" style="width:100%;">
	<thead class="head ui-tabs-nav ui-widget-header ui-corner-all" >
		<tr>
			<th style="width:70px;">Status</th>
			<th style="width:75px;">Type</th>
			<th style="width:105px;">Date</th>
			<th style="width:50px;">Day</th>
			<th style="width:110px;">Time</th>
			<th>Opponent</th>
			<th style="width:120px;">Results</th>										
		</tr>
	</thead>

	<tbody>
		<?php
			$show_swap_legend = false;

		?>

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

				<?php if (Helpers::strEqual ($item['status'], array('PND', 'SCHD', 'CXL', 'PPD', 'LIVE', 'DONE'))): ?>						

					<tr style="height:2em;"
						<?php if ($item['status'] == 'PND'): ?> data-pending="true" <?php endif; ?>
						<?php if ($item['status'] == 'CXL'): ?> data-canceled="true" <?php endif; ?>
						<?php if ($item['status'] == 'PPD'): ?> data-postponed="true" <?php endif; ?>
						<?php if ($item['status'] == 'LIVE'): ?> data-live="true" <?php endif; ?>
					>
						<td><?php echo $item['status']; ?></td>
						<td><?php echo $item['type']; ?></td>
						<td><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 $item['date']; ?></a></td>
						<td><?php echo $item['day']; ?></td>
						<td><?php echo $item['time']; ?></td>
						<td><?php echo $item['opponent']; ?></td>													
						<td>
							<?php if ($item['status'] == 'DONE' /* and $team->level == 'V'*/): ?>
					        	<?php
						        	if (is_null($item['contest']->home_team_score))
				                    {
				                        $results = '<div class="button float_right contest_details" style="font-size:7pt;" data-contest="' . $item['contest']->id . '">Details</a>';
				                    }
				                    else
				                    {
				                        // 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 (!$item['contest']->is_forfeit)
				                        {
				                        	$results = '<b>' . $win . '</b> ' . $mine . '-' . $opp;
				                        }
				                        else
				                        {
											$results = '<b>' . $win . '</b> <small>Forfeit</small>';
				                        }

				                        $results .= ' <div class="button float_right contest_details" style="font-size:7pt;" data-contest="' . $item['contest']->id . '">Details</a>';

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

				                    echo $results;
					        	?>
					        <?php elseif ($item['status'] == 'LIVE' and $team->level == 'V' and $item['contest']->is_scoreable): ?>
						        <a href="<?php echo url('/contests/' . $item['contest']->id . '/submit-scores'); ?>"
		               class="button tooltip submit_scores"
		               style="font-size:8pt;"
		               title="Submit scoring information for this contest.">Submit Scores</a> 
		               	<?php else: ?>
		               		<div class="button float_right contest_details" style="font-size:7pt;" data-contest="<?php echo $item['contest']->id; ?>">Details</a>

					        <?php endif; ?>
						</td>
					</tr>									

				<?php endif; ?>

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

		<?php if (count($team->schedule) == 0): ?>
			<tr>
				<td colspan="6">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; ?>