<?php
/**
 * @var $this SportController
 * @var $events SportEvent[]
 * @var $links_block string
 * @var $content string
 * @var $country string
 * @var $category string
 * @var $year integer
 * @var $years integer[]
 */
?>
<?php $this->renderPartial('//snippets/headerGallery'); ?>
<!-- header end-->
<div class="wrapper content-wrapper">
    <div class="col-wrapper">
        <div class="col2 aside-no-title">
            <?php $this->renderPartial('//snippets/bajaBanner');?>
            <?php $this->renderPartial('//sport/_sidebar');?>
            <div class="usefull-links-box">
                <div class="usefull-links">
                    <?php echo $links_block?>
                </div>
            </div>
        </div>
        <div class="col10">
            <h1><?php echo CHtml::encode($this->pageName)?></h1>

            <div class="simple-filters"
                 style="float: left; clear:none; margin-right: 20px;"><?php echo Yii::t('site', 'Дисциплина')?>:
                <ul class="simple-link-menu">
                    <?php
                    /** @var $categories SportEventCategory[] */

                    if (count($categories) > 1) {
                        if ($category == 'all') echo '<li class="active">';
                        else echo '<li>';
                        echo CHtml::link(Yii::t('site', 'Все события'), array('calendar', 'category' => 'all', 'country' => $country, 'year' => $year)),
                        "</li>";
                    }
                    foreach ($categories as $item) {
                        if ($category == $item->id) echo '<li class="active">';
                        else echo '<li>';
                        echo CHtml::link($item->i18n->name, array('calendar', 'category' => $item->id, 'country' => $country, 'year' => $year)), "</li>";
                    }
                    ?>
                </ul>
            </div>

            <div class="simple-filters"
                 style="float: left; clear: none; margin-left: 20px;"><?php echo Yii::t('site', 'Год')?>:
                <ul class="simple-link-menu">
                    <?php
                    foreach ($years as $item) {
                        if ($year == $item) echo '<li class="active">';
                        else echo '<li>';
                        echo CHtml::link($item, array('calendar', 'category' => $category, 'country' => $country, 'year' => $item)), "</li>";
                    }
                    ?>
                </ul>
            </div>

            <div class="simple-filters right"><?php echo Yii::t('site', 'Показывать:');?>
                <ul class="simple-link-menu">
                    <?php

                    if ($country == 'all') echo '<li class="active">';
                    else echo '<li>';
                    echo CHtml::link(Yii::t('site', 'Все'), array('calendar', 'category' => $category, 'country' => 'all', 'year' => $year)), "</li>";
                    foreach (SportEvent::getCountries() as $id => $label) {
                        if ($country == $id) echo '<li class="active">';
                        else echo '<li>';
                        echo CHtml::link($label, array('calendar', 'category' => $category, 'country' => $id, 'year' => $year)), "</li>";
                    }
                    ?>
                </ul>
            </div>

            <table class="calendar" border="0" cellpadding="0" cellspacing="0">
                <?php
                //$year = date('Y');
                $weekDayNames = Yii::app()->locale->getWeekDayNames('abbreviated', true);

                $data = array();
                for ($m = 1; $m < 13; $m++) {
                    $s = (int)date('d', mktime(0, 0, 1, $m, 1, $year));
                    $e = (int)date('d', mktime(0, 0, -1, $m + 1, 1, $year));
                    $days = array();
                    for ($i = $s; $i <= $e; $i++) $days[$i] = array();
                    $data[$m] = $days;
                }

                foreach ($events as $event) {
                    $start = $event->start_date;
                    if (substr($start, 0, 4) + 1 == $year) $start = date('Y-m-d', mktime(0, 0, 0, 1, 1, $year));
                    $end = $event->end_date;
                    if (substr($end, 0, 4) - 1 == $year) $end = date('Y-m-d', mktime(0, 0, 1, 1, 0, $year + 1));
                    $startM = (int)substr($start, 5, 2);
                    $endM = (int)substr($end, 5, 2);
                    $startD = (int)substr($start, -2);
                    $endD = (int)substr($end, -2);
//                    CVarDumper::dump(array($start, $end, $startM, $endM, $startD, $endD), 10, true);
                    if ($endM != $startM) {
                        for ($i = $startD; isset($data[$startM][$i]); $i++)
                            $data[$startM][$i][] = $event->id;

                        for ($m = $startM + 1; $m < $endM; $m++) {
                            foreach ($data[$m] as &$v) {
                                $v[] = $event->id;
                            }
                            unset($v);
                        }
                        for ($i = 1; $i <= $endD; $i++)
                            $data[$endM][$i][] = $event->id;
                    } else {
                        for ($i = $startD; $i <= $endD; $i++)
                            $data[$endM][$i][] = $event->id;
                    }
                }

                for ($m = 1; $m < 13; $m++) {
                    foreach ($data[$m] as &$v) {
                        if (!empty($v)) {
                            $cls = 'event-back';
                            foreach ($v as $id) {
                                if ($events[$id]->country_id == 1) $cls .= ' pink';
                                if ($events[$id]->country_id == 2) $cls .= ' green';
                                if ($events[$id]->country_id == 3) $cls .= ' blue';
                            }
                            $t = array(
                                'events' => implode(',', $v),
                                'class' => $cls,
                            );
                            $v = $t;
                        }
                    }
                    unset($v);
                }
                ?>
                <?php for ($row = 0; $row < 4; $row++): ?>
                <tr>
                    <?php for ($col = 0; $col < 3; $col++): ?>
                    <?php
                    $month = $row * 3 + $col + 1;
                    $monthName = Yii::app()->locale->getMonthName($month, 'wide', true);
                    ?>
                    <td class="<?php echo ($col == 0) ? 'left' : ($col == 1 ? 'center' : 'rigth')?>">
                        <div class="month-wrapper">
                            <p class="month-title"><?php echo $monthName ?></p>
                            <table class="month" border="0" cellpadding="0" cellspacing="0">
                                <tr>
                                    <?php
                                    foreach (array(1, 2, 3, 4, 5, 6, 0) as $i)
                                        echo "<th>{$weekDayNames[$i]}</th>";
                                    ?>
                                </tr>
                                <?php
                                $s = (int)date('d', mktime(0, 0, 1, $month, 1, $year));
                                $e = (int)date('d', mktime(0, 0, -1, $month + 1, 1, $year));
                                $i = $s;
                                $w = date('w', mktime(0, 0, 1, $month, 1, $year));
                                if ($w == 0) $w = 7;
                                if ($w != 1) {
                                    echo '<tr>';
                                    for ($d = (int)date('d', mktime(0, 0, 0, $month, -$w + 2, $year)); $d <= (int)date('d', mktime(0, 0, 0, $month, 0, $year)); $d++)
                                        echo "<td><div class=\"day-wrapper\"><div class=\"day another-month\">$d</div></div></td>";
                                    for ($i = 1; $i <= 7 - $w + 1; $i++)
                                        echo '<td><div class="day-wrapper" data-event-ids="'
                                            . (empty($data[$month][$i]) ? '' : $data[$month][$i]['events']) .
                                            '"><div class="day">' . $i . '</div>'
                                            . (empty($data[$month][$i]) ? '' : '<div class="' . $data[$month][$i]['class'] . '"></div>') .
                                            '</div></td>';
                                    echo '</tr>';
                                }
                                while ($i <= $e) {
                                    echo '<tr>';
                                    if ($e - $i >= 7) {
                                        for ($t = $i + 7; $i < $t; $i++)
                                            echo '<td><div class="day-wrapper" data-event-ids="'
                                                . (empty($data[$month][$i]) ? '' : $data[$month][$i]['events']) .
                                                '"><div class="day">' . $i . '</div>'
                                                . (empty($data[$month][$i]) ? '' : '<div class="' . $data[$month][$i]['class'] . '"></div>') .
                                                '</div></td>';
                                        //if($i==$e)
                                    } else {
                                        for (; $i <= $e; $i++)
                                            echo '<td><div class="day-wrapper" data-event-ids="'
                                                . (empty($data[$month][$i]) ? '' : $data[$month][$i]['events']) .
                                                '"><div class="day">' . $i . '</div>'
                                                . (empty($data[$month][$i]) ? '' : '<div class="' . $data[$month][$i]['class'] . '"></div>') .
                                                '</div></td>';
                                        $t = 7 - (int)date('w', mktime(0, 0, -1, $month + 1, 1, $year));
                                        if ($t != 7)
                                            for ($i = 1; $i <= $t; $i++)
                                                echo "<td><div class=\"day-wrapper\"><div class=\"day another-month\">$i</div></div></td>";
                                        $i = $e + 1;
                                    }
                                    echo '</tr>';
                                }
                                ?>
                            </table>
                        </div>
                    </td>
                    <?php endfor;?>
                </tr>
                <?php endfor;?>
            </table>
            <div class="text-content"><?php echo $content ?></div>
        </div>
    </div>
</div>


<?php
$cs = Yii::app()->clientScript;
$cs->registerCoreScript('jquery');
//$cs->registerScriptFile(Yii::app()->baseUrl . '/js/fancybox/jquery.mousewheel-3.0.4.pack.js');
$eventsData = array();
foreach ($events as $id => $event) {

    $eventsData[$id] = array(
        'dates' => $event->getCountry() . ' (' . Yii::app()->getDateFormatter()->format('d MMMM yyyy', CDateTimeParser::parse($event->start_date, 'yyyy-MM-dd')) . ' - '
            . Yii::app()->getDateFormatter()->format('d MMMM yyyy', CDateTimeParser::parse($event->end_date, 'yyyy-MM-dd')) . ')',
        'desc' => $event->i18n->description,
    );
}
$eventsData = CJavaScript::encode($eventsData);
$src = <<<JS
var eventsData= {$eventsData};

var tip = $('<div class="calendar-tip-wrapper" style="left: 300px; top: 600px;">'
    +'<div class="tail"></div>'
    +'<p></p>'
    +'</div>').hide();
$('body').append(tip);
$('.calendar').on('mouseleave','.day-wrapper',{},function(){
    tip.hide();
}).on('mouseenter','.day-wrapper',{},function(){
    var ids = $(this).data('eventIds');

    if(ids){
       if(typeof ids == 'string')
           ids = ids.split(',');
       else
           ids = [''+ids];
       if(ids.length){
           var html = '';
           for(var k in ids){
              var data = eventsData[[ids[k]]];
              html += '<b>'+data['desc']+'</b><br/>'+data['dates']+'<br/>';
           }

           var o = $(this).offset();
           $('p',tip).html(html);
           tip.css({
               'left':o.left-80,
               'top':o.top-49-tip.height()
           });
           tip.show();
       }
    }
})
JS;

$cs->registerScript('calendar', $src);
?>
