@extends('layouts/full')

@section('title', 'Home')

@section('content')
@parent
    <div class="row">
      <div class="col-lg-8 col-md-8 col-sm-12 col-xs-12 wow bounceInUp">

    @php($newsItem = current($news))
    @php($podcast = current($podcasts))
    @php($index = 0)
    @while($newsItem !== false || $podcast !== false)
      @if(++$index % 2 == 1)
        @if($index > 2)
        </div>
        @endif
        <div class="row">
      @endif
          <div class="col-lg-6 col-md-6 col-sm-6">
            <div class="left_content">
              <?php // Show podcast(s) if we have any and either (a) we are out of news items but have an odd number of items, so we complete the row
                    //     or (b) we have a news item but the podcast is the first podcast on the same date, in which case it has to come first for extra attention. ?>
              @if($newsItem === false && ($index % 2) == 1) 
                  </div><!--./left_content-->
                </div><!--./col--> 
                @break 
              @endif
              @if(($podcast !== false) && (($newsItem === false) || ($podcast->created >= $newsItem->published->setTime(0, 0, 0))))
                @php($podcastsInBlock = [])
                @php($firstPodcast = $podcast)
                @while(($podcast !== false) && ($podcast->program == $firstPodcast->program) && ($podcast->created->format('d-m-Y') == $firstPodcast->created->format('d-m-Y')))
                  @php($podcastsInBlock[] = $podcast)
                  @php($podcast = next($podcasts))
                @endwhile

                <h2><span class="fa fa-headphones"></span> Audiofragment</h2>
                <div class="details">
                @if(count($podcastsInBlock) == 1)
                  <h3 class="news-title"><a href="{{url('radio/gemist/fragment/' . $firstPodcast->url)}}" title="{{$firstPodcast->title}}">{{$firstPodcast->title}}</a></h3>
                  <div class="post-date">{{Formatter::relativeDate($firstPodcast->created)}}</div>
                  {!! $firstPodcast->excerpt() !!}
                  <p>
                    <a href="{{url('radio/gemist/fragment/' . $firstPodcast->url)}}" title="{{$firstPodcast->title}}" class="btn btn-info">Luister fragment</a>
                    <a href="{{url('radio/gemist')}}" title="Luister eerder uitgezonden fragmenten terug" class="btn btn-default">Meer fragmenten</a>
                  </p>
                @else
                  <h3 class="news-title">@if($firstPodcast->program) {{$firstPodcast->program->name}} @else Fragment gemist? @endif</h3>
                  <div class="post-date">{{Formatter::relativeDate($firstPodcast->created)}} uitgezonden</div>
                  @if(count($podcastsInBlock) < 3)
                  <div class="news-item news-item-small wow fadeInDown">
                    <div class="thumb">
                      <img class="entry-thumb" src="{{url('images/podcast.png')}}" alt="Fragment gemist" title="Luister fragmenten terug van {{$firstPodcast->program ? $firstPodcast->program->name : "onze eerdere uitzendingen" }}"/> 
                    </div><!--/.thumb-->
      	          </div><!--/.news-item-->
                  @endif
                  <ul>
                  @foreach($podcastsInBlock as $currentPodcast)
                    @if($loop->index > 10 && $loop->remaining > 2) 
                      <li><p>... en <a href="{{url('radio/gemist/programma' . $firstPodcast->program->url)}}" title="Bekijk meer fragmenten uit {{$firstPodcast->program->name}}">nog {{$loop->remaining}} fragmenten</a>.</p></li>
                      @break 
                    @endif
                    <li><span class="fa fa-angle-right"></span> <a href="{{url('radio/gemist/fragment/' . $currentPodcast->url)}}" title="{{$currentPodcast->title}}">{{$currentPodcast->titleWithoutProgram()}}</a></li>
                  @endforeach
                  </ul>
                  <div class="clearfix" style="height: 15px;"></div>
                  <p>
                    @if($firstPodcast->program) <a href="{{url('radio/programma' . $firstPodcast->program->url)}}" title="Meer informatie over {{$firstPodcast->program->name}}" class="btn btn-info">Over dit programma</a> @endif
                    <a href="{{url('radio/gemist' . ($firstPodcast->program ? '/programma' . $firstPodcast->program->url : ''))}}" title="Luister eerder uitgezonden fragmenten terug" class="btn btn-default">Meer fragmenten</a>
                  </p>
                @endif
                </div><!--./details-->
                @if($newsItem === false) <?php // If we got here, we are out of news items: we only included (a) podcast(s) to complete the column. Close the open tags and bail out of the while loop ?>
            </div><!--./left_content-->
          </div><!--./col-->
                  @break 
                @endif
              @elseif($newsItem !== false)
                <h2>@if($newsItem->video) <span class="fa fa-video-camera"></span> Video @else <span class="fa fa-newspaper-o"></span> Nieuws @endif </h2>
                @php($image = count($newsItem->images) ? $newsItem->images[0] : null)
                @if($image || $newsItem->video)
                <div class="news-item news-item-small wow fadeInDown">
                  <div class="thumb">
                    <a href="{{$newsItem->url}}" rel="bookmark" title="{{$newsItem->title}}">
                      <img class="entry-thumb" src="{{ url( $image ? $image->url : 'images/video.png' )}}" alt="{{$image ? $image->title : 'Video'}}" title="{{$image ? $image->title : 'Klik om de video te bekijken'}}"/> 
                    </a>
      	        </div><!--/.thumb-->
      	      </div><!--/.news-item-->
                @endif
                <div class="details">
                  <h3 class="news-title"><a href="{{$newsItem->url}}" rel="bookmark" title="{{$newsItem->title}}">{{$newsItem->title}}</a></h3>
                  <div class="post-date">
                    @if($newsItem->edited)
                    <time class="entry-date edited" datetime="{{$newsItem->edited->format('c')}}"><strong>Laatste update: {{Formatter::relativeDate($newsItem->edited, /*capitalize:*/ false)}} om {{$newsItem->edited->format("H:i")}} uur</strong></time>.
                    @else
                    <time class="entry-date created" datetime="{{$newsItem->published->format('c')}}">{{Formatter::relativeDate($newsItem->published)}} om {{$newsItem->published->format("H:i")}} uur</time>.
                    @endif
                  </div><!--./post-date-->
                  <p class="news-excerpt {{$image ? "short" : "long" }}"><b>{{$newsItem->region->title}}</b> | {!!Formatter::excerpt($newsItem->content, $image ? 200 : 500)!!}</p>
                  <p><a href="{{$newsItem->url}}" class="btn btn-info">{!! $newsItem->video ? "<span class='fa fa-video-camera'></span> Bekijk video" : "Lees verder <span class='fa fa-arrow-right'></span>" !!}</a>
                  <a href="/nieuws" class="btn btn-default">Meer nieuws</a></p>
                </div><!--/.details-->
                @php($newsItem = next($news))
              @endif
            </div><!--./left_content-->
          </div><!--./col-->
    @endwhile
        </div><!--./row-->
      </div><!--./col-->

      <div class="col-lg-4 col-md-4 col-sm-6 col-xs-12">
        <div class="left_content">
          @include('banners')
          <div class="wow bounceInRight">
            @if(isset($comingUp) && count($comingUp) > 0)
            <h2>Nu en straks op 6FM</h2>
              <p>
                 <b>Nu tot {{$comingUp[0]['end']->format('H:i')}} uur:</b><br/>
                 <a href="{{ url('radio/programma' . $comingUp[0]['program']->url) }}">{{$comingUp[0]['program']->name}}</a>
              </p>
              <p><b>Straks op 6FM:</b></p>
              <ul style="margin-top: -1em; margin-bottom: 1em; ">
              @php($date = $comingUp[1]['start']->format('d-m-Y'))
              @for($i = 1, $printed = 0; $i < count($comingUp) && $printed < 3; ++$i)
                @if($comingUp[$i]['program']->priority > 2) @continue @endif
                @if($date != ($newDate = $comingUp[$i]['start']->format('d-m-Y')))
                  @php($date = $newDate)
              </ul>
              <p><b>{{ Formatter::fullDate($comingUp[$i]['start'], 'W') }}:</b></p>
              <ul style="margin-top: -1em; margin-bottom: 1em; ">
                @endif
                <li>{{ $comingUp[$i]['start']->format('H:i') }} uur: <a href="{{ url('radio/programma' . $comingUp[$i]['program']->url) }}">{{$comingUp[$i]['program']->name}}</a></li>
                @php(++$printed)
              @endfor 
              </ul>
              <p><b>Meer programma's?</b><br/><a href="{{url( '/radio/programmering' )}}" class="btn btn-primary">Programmaschema</a></p>
            @endif
            <div id="popularnews"><span class="fa fa-spinner fa-2x"></span></div>
          </div><!--./ringle_post_content-->
        </div><!--./left_content-->
      </div><!--./col-->
    </div><!--./row-->

    <script type="text/javascript">
      $.ajax({ 
          'url': '{{ url('nieuws/populair') }}',
          'success': function(data, status, xhr) {
              $("#popularnews").html(data);
           }
      });
    </script>

@endsection
