<?php
$date         = mktime( 12, 0, 0, $this -> month, 1, $this -> year );
$daysInMonth  = date( "t", $date );
$offset       = date( "w", $date ) - 1; if ( $offset < 0 ) $offset = 6;
$rows         = 1;
$prev_month   = $this -> month - 1;
$prev_year    = $this -> year;

if ( $this -> month == 1 ) 
{
  $prev_month = 12;
  $prev_year  = $this -> year - 1;
}

$next_month = $this -> month + 1;
$next_year  = $this -> year;

if ( $this -> month == 12 ) 
{
  $next_month = 1;
  $next_year  = $this -> year + 1;
}

?>
<? if ( !$this -> ajax ):?>
  <div id="calendar-container">
<? endif;?>
    <div class='panel-heading text-center'>
      <div class='row'>
        <div class='col-md-3 col-xs-4'>
          <a class='month-prev btn btn-light btn-sm active' title="Poprzedni miesiąc" month="<?= $prev_month;?>" year="<?= $prev_year;?>">
            <span class='glyphicon glyphicon-arrow-left'></span>
          </a>
        </div>
        <div class='col-md-6 col-xs-4'>
          <strong><?= $this -> months[ $this -> month ] . ' ' . $this -> year;?></strong>
        </div>
        <div class='col-md-3 col-xs-4 '>
          <a class='month-next btn btn-light btn-sm active' title="Następny miesiąc" month="<?= $next_month;?>" year="<?= $next_year;?>">
            <span class='glyphicon glyphicon-arrow-right'></span>
          </a>
        </div>
      </div>
    </div>
    <table class='table table-bordered'>
      <tr>
        <th>Pn</th>
        <th>Wt</th>
        <th>Śr</th>
        <th>Cz</th>
        <th>Pt</th>
        <th>Sb</th>
        <th>Nd</th>
      </tr>
      <tr>
        <? for ( $i = 1; $i <= $offset; $i++ ):?>
          <td></td>
        <? endfor;?>
        <? 
          for ( $day = 1; $day <= $daysInMonth; $day++ ) 
          {
            if ( ( $day + $offset - 1 ) % 7 == 0 && $day != 1 ) 
            {
              echo "</tr><tr>";
              $rows++;
            }
            echo "<td class='";
            
              if ( date( 'w', strtotime( $day . '-' . $this -> month . '-' . $this -> year ) ) == 6 )
                echo 'sb ';
              
              if ( date( 'w', strtotime( $day . '-' . $this -> month . '-' . $this -> year ) ) == 0 )
                echo 'nd ';
              
              $class    = '';
              $out_list = '';
             
              if ( is_array( $this -> articles ) ) foreach ( $this -> articles as $article )
              {
                $date_tmp = date( 'Y-m-d', strtotime( $day . '-' . $this -> month . '-' . $this -> year ) );
                
                if ( $date_tmp >= $article['date_start'] and $date_tmp <= $article['date_end'] )
                {
                  $class = 'event ';
                  
                  if ( !$out_list )
                    $out_list = '<ul id="events-' . $date_tmp . '">';
                  
                  $article['language']['seo_link'] ? $url = $article['language']['seo_link'] : $url = 'a-' . $article['id'] . '-' . \S::seo( $article['language']['title'] );
  
                  $out_list .= '<li>';
                    $out_list .= '<a href="/' . $url . '" title="' . $article['language']['title'] . '"';
                      if ( $this -> article['language']['noindex'] ) 
                        $out_list .= 'rel="nofollow"';
                    $out_list .= '>' . $article['language']['title'] . '</a>';
                  $out .= '</li>';
                }
              }
              
              if ( $out_list )
                $out_list .= '</ul>';
              
              $out .= $out_list;
              
              echo $class;
            echo "' date='" . $date_tmp . "'>" . $day . "</td>";
          }
          while ( ($day + $offset) <= $rows * 7 )
          {
            echo "<td></td>";
            $day++;
          }
        ?>
      </tr>
    </table>
    <?= $out;?>
    <a href="#" id="event-back">Wstecz</a>
<? if ( !$this -> ajax ):?>
  </div>
  <script type="text/javascript">
    $( document ).ready( function()
    {
      $( 'body' ).on( 'click', '.event', function()
      {
        var date = $( this ).attr( 'date' );
        $( '#calendar-container .panel-heading, #calendar-container table, #calendar-container ul' ).hide();
        $( '#events-' + date + ', #event-back' ).show();
      });
      
      $( 'body' ).on( 'click', '#event-back', function()
      {
        var date = $( this ).attr( 'date' );
        $( '#calendar-container .panel-heading, #calendar-container table' ).show();
        $( '#calendar-container ul, #event-back' ).hide();
      });
      
      $( 'body' ).on( 'click', '.month-next.active', function()
      {
        $( '.month-next' ).removeClass( 'active' );
        $( '#calendar-container' ).addClass( 'disable' );

        $.ajax(
        {
          type: 'POST',
          cache: false,
          url: '/ajax.php',
          data: 
          {
            a: 'calendar',
            month: $( '.month-next' ).attr( 'month' ),
            year: $( '.month-next' ).attr( 'year' )
          },
          success: function( data ) 
          {
            $( '#calendar-container' ).html( data );
            $( '.month-next' ).addClass( 'active' );
            $( '#calendar-container' ).removeClass( 'disable' );
          }
        });
      });

      $( 'body' ).on( 'click', '.month-prev.active', function()
      {
        $( '.month-prev' ).removeClass( 'active' );
        $( '#calendar-container' ).addClass( 'disable' );

        $.ajax(
        {
          type: 'POST',
          cache: false,
          url: '/ajax.php',
          data: 
          {
            a: 'calendar',
            month: $( '.month-prev' ).attr( 'month' ),
            year: $( '.month-prev' ).attr( 'year' )
          },
          success: function( data ) 
          {
            $( '#calendar-container' ).html( data );
            $( '.month-prev' ).addClass( 'active' );
            $( '#calendar-container' ).removeClass( 'disable' );
          }
        });
      });
    });
  </script>
<? endif;?>