<?php $menu = array(
"Radio" => array(
	"" => "/gids",
	"Luister live" => "/luister/live",
	"Kijk in de studio" => "/kijk/studio",
	"Regionieuws" => "/luister/regionieuws",
	"Programmagids" => "/gids",
	"Programma gemist" => "/gemist/programma",
	"Fragment gemist" => "/gemist/fragment"),
//	"Zondagsdienst" => "/kerkdienst"),
"TV" => array(
	"Kijk live" => "/kijk/live"),
//"Evenementen" => array(
//        "24 uur Scherp de Zeis (4 december)" => "/programma/1030/scherp-zeis",
//        "Vroeger of Later Luisterlijst (21 december)" => "/vol-luisterlijst"),
"Regioagenda" => "/agenda",
"Over ons" => array(
	"" => "/contact",
	"Contact" => "/contact",
	"Vacatures" => "/vacatures",
	"Frequenties" => "/frequenties",
	"Adverteren?" => "/adverteren",
),
	"NH Nieuws" => "https://nhnieuws.nl");
?>
<?php
function isActive($link, $checksubmenus) {
	if(is_array($link)) {
		if(!$checksubmenus) { return false; }
		foreach($link as $item => $link) {
			if(isActive($link, $checksubmenus)) { return true; }
		}

		return false;
	}

	return (substr($_SERVER["REQUEST_URI"], 0, strlen($link)) == $link);	
}

function buildMenu($menu, $ismobile) {
	$result = "";
	foreach($menu as $title => $link) {
		if($title == "") { continue; }
		$submenu = is_array($link);
 		$isactive = isActive($link, !$ismobile);
 		$icon = "";
                $isplayer = false;
                $liClass = "";
		if(!$submenu && substr($link, 0, 8) == '/luister') { $icon = "headphones"; $isplayer = true; }
		if(!$submenu && $link == '/kijk/live') { $icon = "tv"; }
		if(!$submenu && $link == '/kijk/studio') { $liClass = "watch-studio"; $icon = "video"; }

		$submenulink = $submenu && isset($link[""]) ? $link[""] : "#";
		$target = substr($submenu ? $submenulink : $link, 0, 4) == "http" ? "target='_blank'" : "";
		if($icon) { $icon = "<span class='fas fa-$icon'></span>&nbsp;"; }
		else { $icon = ""; }
		$result .=
			"<li class=\"" . ($submenu ? "submenu" : "") . ($isactive ? " selected" : "") . ($liClass ? " $liClass" : "") . "\">
			<a href=\"" . ($submenu ? $submenulink : $link) . "\" class=\"" . ($isplayer ? "player" : "") . "\" title=\"{$title}\" {$target}>{$icon}{$title}</a>";
		if($submenu) {
			$result .= "\t\t\t<ul>\n"
				. buildMenu($link, $ismobile)
				. "\t\t\t</ul>\n";
		}
		$result .= "\t\t</li>\n";
	}

	return $result;
}
?>
<nav>
<ul class="sf-menu">
@php($newsUrl = '/nieuws')
@if($news)
	<li class="submenu mega_menu_parent {{isActive($newsUrl, false) ? "selected" : ""}}">
		<a href="{{$newsUrl}}" title="Nieuws">
			Nieuws
		</a>
		<ul>
			@if($activeBlog != null)
			<li class="{{isActive($activeBlog->url, false) ? "selected" : ""}}">
				<a href="{{url($activeBlog->url)}}" title="Liveblog: {{$activeBlog->title}}">
					<b>Live-blog</b> | {{$activeBlog->title}}
				</a>
			</li>
			@endif
			<li class="submenu">
				<a href="{{$newsUrl}}" title="Laatste nieuws">
					Laatste nieuws
				</a>
				<ul class="mega_menu blog">
					@foreach($news as $item)
					<li class="post">
						@if($item->images && count($item->images))
						<a href="{{url($item->url)}}" title="{{$item->title}}">
							<img src='{{$imgBase . $item->images[0]->url}}' alt='{{$item->title}}'>
						</a>
						@endif
						<h5><a href="{{url($item->url)}}" title="{{$item->title}}">{{$item->title}}</a></h5>
						<ul class="post_details simple">
							<li class="category">{{$item->region->title}}</li>
							<li class="date">
								{{Formatter::relativeDate($item->published)}} om {{$item->published->format('H:i')}} uur
							</li>
						</ul>
					</li>
					@endforeach
				</ul>
			</li>
	@if($popular)
			<li class="submenu">
				<a href="{{$newsUrl}}" title="Meest gelezen">
					Meest gelezen
				</a>
				<ul class="mega_menu blog">
					@foreach($popular as $item)
					<li class="post">
						@if($item->images && count($item->images))
						<a href="{{url($item->url)}}" title="{{$item->title}}">
							<img src='{{$imgBase . $item->images[0]->url}}' alt='{{$item->title}}'>
						</a>
						@endif
						<h5><a href="{{url($item->url)}}" title="{{$item->title}}">{{$item->title}}</a></h5>
						<ul class="post_details simple">
							<li class="category">{{$item->region->title}}</li>
							<li class="date">
								{{Formatter::relativeDate($item->published)}}
							</li>
						</ul>
					</li>
					@endforeach
				</ul>
			</li>
	@endif
	@if($podcasts)
			<li class="submenu">
				<a href="{{route('gemist')}}" title="Gesprek gemist">
					Fragment gemist
				</a>
				<ul class="mega_menu blog">
					@foreach($podcasts as $item)
					@if($loop->index >= 3) @break @endif
					<li class="post">
						@if($item->image)
						<a href="{{route('gemist.fragment') . $item->url}}" title="{{$item->title}}" class="fixed-height">
							<img src='{{$imgBase . $item->image->url}}' alt='{{$item->title}}'>
						</a>
						@endif
						<h5><a href="{{route('gemist.fragment') . $item->url}}" title="{{$item->title}}">{{$item->title}}</a></h5>
						<ul class="post_details simple">
							@if($item->program)
							<li class="category">{{$item->program->name}}</li>
							@endif
							<li class="date">
								{{Formatter::relativeDate($item->created)}}
							</li>
						</ul>
					</li>
					@endforeach
				</ul>
			</li>
	@endif
		</ul>
	</li>
@else
	<li class="{{isActive($newsUrl, false) ? "selected" : ""}}">
		<a href="{{$newsUrl}}" title="Nieuws">Nieuws</a>
	</li>
@endif
{!! buildMenu($menu, false) !!}
</ul>
</nav>
<div class="mobile_menu_container" >
	<a href="#" class="mobile-menu-switch">
		<span class="line"></span>
		<span class="line"></span>
		<span class="line"></span>
	</a>
	<div class="mobile-menu-divider"></div>
	<nav>
	<ul class="mobile-menu">
		@if($activeBlog != null)
		<li class="{{isActive($activeBlog->url, false) ? "selected" : ""}}">
			<a href="{{url($activeBlog->url)}}" title="Liveblog: {{$activeBlog->title}}">
				<b>Live-blog</b> | {{$activeBlog->title}}
			</a>
		</li>
		@endif
		<li class="submenu {{isActive($newsUrl, false) ? "selected" : ""}}">
			<a href="{{$newsUrl}}" title="Nieuws">
				Nieuws
			</a>
		</li>
	{!! buildMenu($menu, true) !!}
	</ul>
	</nav>
</div>

