<!DOCTYPE html>
<html lang="en">

    <!-- HTML Head -->
    <head>

        <!-- Page Title -->
        <title>
            Preselection Report
        </title>
        
        <!-- Meta Tage -->          
        <meta charset="utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <meta name="description" content="Oregon School Activities Association">
        <meta name="viewport" content="width=device-width">
        
        <!-- Stylesheets -->
        <link rel="stylesheet" type="text/css" href="http://www.osaa.org/css/default.css">
        <link rel="stylesheet" type="text/css" href="http://www.osaa.org/css/fix_nav.css">                 

        <link rel="stylesheet" type="text/css" href="http://code.jquery.com/ui/1.10.3/themes/redmond/jquery-ui.css">       

        <!-- Inline Styles -->
        <style type="text/css">
    	    * {
                margin: 0;
                padding: 0;
            }

            body {
                font-family: Arial, Helvetica, sans-serif;
                font-size: 9pt;
                line-height: 115%;
                color: #000000;
                background: #ffffff;                
            }  

            .page {
                page-break-after: always;
                padding: 1em;
            }              

            table {
                border-collapse: collapse;
                width: 100%;
                
            }

            table .odd {
                background-color: #cccccc;
            }

            table tr {
                line-height: 2em;
            }

            table td {
                padding-left: 0.25em;
            }

            img.icon {
                vertical-align: middle;
            }

            .title {
                font-size: 11pt;
                font-weight: bold;
            }

            .counts {
                margin-bottom: 1em;
            }

            .league_title {
                font-weight: bold;

                border-bottom: 1px solid black;
            }
    
        </style>

        <!-- JQuery -->
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
        <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>                
 
        <!-- JQuery Page Initialization -->
        <script type="text/javascript">         
            $(document).ready(function()
            {
                if(!window.console)
                {
                    window.console = { log: $.noop, group: $.noop, groupEnd: $.noop };            
                }
                
                // Table row highlighter
                function colorRows ()
                {       
                    $('table').each(function ()
                    {
                        var i = 0;

                        $('tbody tr:visible', $(this)).each(function ()
                        {
                            if (i % 2 == 1)
                            {
                                $(this).addClass('odd');
                            }
                            else
                            {
                                $(this).removeClass('odd');
                            }
                            i = i + 1;
                        });     
                    });
                }   
                colorRows();    
        
            });    
        </script>

    </head>

    <body>

        <?php
            switch($seasonal_school_form->season)
            {
                case 'F':
                    $season = 'Fall';
                    break;
                case 'W':
                    $season = 'Winter';
                    break;
                case 'S':
                    $season = 'Spring';
                    break;                
            }

            $year = intval($seasonal_school_form->school_year);

            if ($next)
            {
                $year++;
            }

            $next_year = $year . '-' . substr($year + 1, 2, 2);
        ?>
        
        <?php /*  Process each activity  */ ?>
        <?php foreach ($placements as $activity_slug => $divisions): ?>

            <?php /*  Process each division in this activity  */ ?>
            <?php foreach ($divisions as $division => $division_object): ?>

                <!-- Create a new page -->
                <div class="page">

                    <!-- Page Title: Year/Season/Division/Activity -->
                    

                    <div class="title">
                        <?php echo $next_year; ?> <?php echo $season; ?> <?php echo $division; ?> <?php echo Helpers::getActivityName($activity_slug); ?>
                        <?php if ($next): ?>
                            
                        <?php else: ?>
                            Preselections
                        <?php endif; ?>
                    </div>

                    <?php
                        // Count the number of teams
                        $total = array('current' => 0, 'future' => 0, 'difference' => 0, 'independent' => 0);                        

                        foreach ($division_object['leagues'] as $league_slug => $league_object)
                        {
                            foreach ($league_object['schools'] as $school_object)
                            {
                                foreach (array('current', 'future') as $count_type)
                                {
                                    if ($school_object[$count_type])
                                    {
                                        $total[$count_type]++;
                                    }
                                }
                            }
                        }
                        foreach ($division_object['independent']['schools'] as $school_object)
                        {
                            foreach (array('current', 'future') as $count_type)
                            {
                                if ($school_object[$count_type])
                                {
                                    $total[$count_type]++;

                                    if (Helpers::strEqual($count_type, 'current'))
                                    {
                                        $total['independent']++; 
                                    }
                                }
                            }
                        }                        

                        $total['difference'] = $total['future'] - $total['current'];
                    ?>                    

                    <div class="counts">
                        <?php if ($next): ?>
                            
                            <?php echo $total['future']; ?> teams in <?php echo count($division_object['leagues']); ?> 

                            <?php if (count($division_object['leagues']) == 1): ?>
                                <?php echo (Helpers::strContains('-SD', key($division_object['leagues']))) ? 'special district' : 'league'; ?>
                            <?php else: ?>
                                <?php echo (Helpers::strContains('-SD', key($division_object['leagues']))) ? 'special districts' : 'leagues'; ?>
                            <?php endif; ?>                            

                            <?php if ($total['independent'] > 0): ?>
                                with <?php echo $total['independent']; ?> independent <?php echo ($total['independent'] == 1) ? 'team' : 'teams'; ?>
                            <?php endif; ?>

                        <?php else: ?>

                            <?php echo $total['current']; ?> current teams in <?php if (count($division_object['leagues']) == 1): ?>
                                <?php echo (Helpers::strContains('-SD', key($division_object['leagues']))) ? 'special district' : 'league'; ?>,
                            <?php else: ?>
                                <?php echo (Helpers::strContains('-SD', key($division_object['leagues']))) ? 'special districts' : 'leagues'; ?>,
                            <?php endif; ?>
                            <?php if ($total['independent'] > 0): ?>
                                with <?php echo $total['independent']; ?> independent <?php echo ($total['independent'] == 1) ? 'team' : 'teams'; ?>,
                            <?php endif; ?>
                            next year will have <?php echo $total['future']; ?> teams
                            <?php if ($total['difference'] == 0): ?>
                                (same # of teams)
                            <?php else: ?>
                                (<?php echo ($total['difference'] < 0) ? '-' : '+'; ?><?php echo abs($total['difference']); ?> <?php echo ($total['difference'] == 1) ? 'team' : 'teams'; ?>)
                            <?php endif; ?>
                            
                        <?php endif; ?>
                    </div>

                    <div class="leagues columns">
                        <?php
                            $index = 0;                            
                        ?>
                        <?php /*  Process each league  */ ?>
                        <?php foreach ($division_object['leagues'] as $league_slug => $league_object): ?>
                            <?php 
                                $index++;

                                // Count the number of teams
                                $total = array('current' => 0, 'future' => 0, 'difference' => 0);                        

                                foreach ($league_object['schools'] as $school_object)
                                {
                                    foreach (array('current', 'future') as $count_type)
                                    {
                                        if ($school_object[$count_type])
                                        {
                                            $total[$count_type]++;
                                        }
                                    }
                                }                            

                                $total['difference'] = $total['future'] - $total['current'];

                                if ($total['difference'] == 0)
                                {
                                    $league_count = $total['current'];
                                }
                                else
                                {
                                    $league_count = $total['current'] . '/' . $total['future'];
                                }

                                $league_title = preg_replace('/( \(.*\))$/', '', $league_slug) . ' ' . $league_object['name'] . ' (' . $league_count . ')';

                                if (strlen($league_title) >= 33)
                                {
                                    $league_title = '<small>' . $league_title . '</small>';
                                }
                            ?>

                            <div class="third">                                                           

                                <div class="league_title"><?php echo $league_title; ?></div>

                                <table>
                                    <tbody>
                                        <?php foreach ($league_object['schools'] as $school_object): ?>                                    
                                            <tr>
                                                <td>
                                                    <?php echo $school_object['name']; ?>

                                                    <?php if ($school_object['current'] and !$school_object['future']): ?>
                                                        <img src="<?php echo asset('/images/icons/not_selected_16px.png'); ?>" alt="" title="" class="icon" />
                                                    <?php elseif (!$school_object['current'] and $school_object['future']): ?>
                                                        <img src="<?php echo asset('/images/icons/add_icon_16px.png'); ?>" alt="" title="" class="icon" />
                                                    <?php endif; ?>

                                                    <?php if ($school_object['guessed']): ?>
                                                        <img src="<?php echo asset('/images/icons/help-small.png'); ?>" alt="" title="" class="icon" />
                                                    <?php endif; ?>

                                                </td>
                                            </tr>
                                        <?php endforeach; ?>
                                    </tbody>

                                </table>                                

                            </div>

                            <?php if ($index % 3 == 0 and $index < count($division_object['leagues'])): ?>
                                </div>
                                <br class="clear" /><br />
                                <div class="columns">
                            <?php endif; ?>
                            

                        <?php endforeach; ?>
                        <?php /*  End processing leagues  */ ?>
                    </div>

                    <br class="clear" />

                    <?php if (count($division_object['independent']['schools']) > 0): ?>

                        <br />
                        <div class="columns">
                            <div class="third">                                                           

                                <div class="league_title">Independent</div>

                                <table>
                                    <tbody>
                                        <?php foreach ($division_object['independent']['schools'] as $school_object): ?>                                    
                                            <tr>
                                                <td style="line-height: 1.15em;">
                                                    <?php echo $school_object['name']; ?>

                                                    <?php if ($school_object['current'] and !$school_object['future']): ?>
                                                        <img src="<?php echo asset('/images/icons/not_selected_16px.png'); ?>" alt="" title="" class="icon" />
                                                    <?php elseif (!$school_object['current'] and $school_object['future']): ?>
                                                        <img src="<?php echo asset('/images/icons/add_icon_16px.png'); ?>" alt="" title="" class="icon" />
                                                    <?php endif; ?>

                                                    <?php if ($school_object['guessed']): ?>
                                                        <img src="<?php echo asset('/images/icons/help-small.png'); ?>" alt="" title="" class="icon" />
                                                    <?php endif; ?>

                                                </td>
                                            </tr>
                                        <?php endforeach; ?>
                                    </tbody>

                                </table>                                

                            </div>
                        </div>
                        <br class="clear" />
                    <?php endif; ?>



                </div>

            <?php endforeach; ?>
            <?php /*  End processing divisions  */ ?>


        <?php endforeach; ?>
        <?php /*  End processing activities  */ ?>         

    </body>
</html>         