{{ $home := .Site.GetPage "home" }}
{{ $section := .Section }}
{{ $current := . }}

<header>
    <nav >
        <a id="brand" href="{{ $home.Permalink }}">
            <span class="logo">{{ with resources.Get "inline_images/istio-whitelogo-bluebackground-rounded.svg" }}{{ ( . | minify).Content | safeHTML }}{{ end }}</span>

            {{ if .Site.Data.args.archive_landing }}
                <span class="name">Istioldies</span>
            {{ else if .Site.Data.args.archive }}
                <span class="name">Istioldie {{ .Site.Data.args.version }}</span>
            {{ else if .Site.Data.args.preliminary }}
                <span class="name">Istio Prelim {{ .Site.Data.args.version }}</span>
            {{ else }}
                <span class="name">Istio {{ .Site.Data.args.version }}</span>
            {{ end }}
        </a>

        <div id="hamburger">
            {{ partial "icon.html" "hamburger" }}
        </div>

        <div id="header-links">
            {{ if not .Site.Data.args.archive_landing }}
                {{ with (.Site.GetPage "/docs") }}
                    {{ if eq .Permalink $current.Permalink }}
                        <span {{ if eq $section "docs" }}class="current" {{ end }}title="{{ .Description }}">{{ .LinkTitle }}</span>
                    {{ else }}
                        <a {{ if eq $section "docs" }}class="current" {{ end }}title="{{ .Description }}" href="{{ .Permalink  }}">{{ .LinkTitle }}</a>
                    {{ end }}
                {{ end }}

                {{ with (.Site.GetPage "/blog") }}
                    {{ $posts := (where .Site.Pages "Section" "blog").ByPublishDate.Reverse }}
                    {{ $latest_post := index $posts 0 }}
                    {{ $link := printf "%v%v/" .Permalink $latest_post.PublishDate.Year }}

                    {{ if eq $link $current.Permalink }}
                        <span {{ if eq $section "blog" }}class="current" {{ end }}title="{{ .Description }}">{{ .LinkTitle }}<i class="dot" data-prefix="/blog"></i></span>
                    {{ else }}
                        <a {{ if eq $section "blog" }}class="current" {{ end }}title="{{ .Description }}" href="{{ $link }}">{{ .LinkTitle }}<i class="dot" data-prefix="/blog"></i></a>
                    {{ end }}
                {{ end }}

                {{ with (.Site.GetPage "/news")  }}
                    {{ if eq .Permalink $current.Permalink }}
                        <span {{ if eq $section "news" }}class="current" {{ end }}title="{{ .Description }}">{{ .LinkTitle }}<i class="dot" data-prefix="/news"></i></span>
                    {{ else }}
                        <a {{ if eq $section "news" }}class="current" {{ end }}title="{{ .Description }}" href="{{ .Permalink }}">{{ .LinkTitle }}<i class="dot" data-prefix="/news"></i></a>
                    {{ end }}
                {{ end }}

                {{ with (.Site.GetPage "/faq") }}
                    {{ if eq .Permalink $current.Permalink }}
                        <span {{ if eq $section "faq" }}class="current" {{ end }}title="{{ .Description }}">{{ .LinkTitle }}</span>
                    {{ else }}
                        <a {{ if eq $section "faq" }}class="current" {{ end }}title="{{ .Description }}" href="{{ .Permalink }}">{{ .LinkTitle }}</a>
                    {{ end }}
                {{ end }}

                {{ with (.Site.GetPage "/about") }}
                    {{ if eq .Permalink $current.Permalink }}
                        <span {{ if eq $section "about" }}class="current" {{ end }}title="{{ .Description }}">{{ .LinkTitle }}</span>
                    {{ else }}
                        <a {{ if eq $section "about" }}class="current" {{ end }}title="{{ .Description }}" href="{{ .Permalink  }}">{{ .LinkTitle }}</a>
                    {{ end }}
                {{ end }}
            {{ end }}

            <div class="menu">
                <button id="gearDropdownButton" class="menu-trigger" title='{{ i18n "options_menu" }}'
                        aria-label="Options and Settings" aria-controls="gearDropdownContent">
                    {{ partial "icon.html" "gear" }}
                </button>

                <div id="gearDropdownContent" class="menu-content" aria-labelledby="gearDropdownButton" role="menu">
                    <!--
                    <a tabindex="-1" role="menuitem" lang="en" id="switch-lang-en" {{ if eq $home.Lang "en" }}class="active"{{ end }}>English</a>
                    <a tabindex="-1" role="menuitem" lang="zh" id="switch-lang-zh" {{ if eq $home.Lang "zh" }}class="active"{{ end }}>中文</a>

                    <div role="separator"></div>
                    -->

                    <a tabindex="-1" role="menuitem" class="active" id="light-theme-item">{{ i18n "light_theme" }}</a>
                    <a tabindex="-1" role="menuitem" id="dark-theme-item">{{ i18n "dark_theme" }}</a>

                    <div role="separator"></div>

                    <a tabindex="-1" role="menuitem" id="syntax-coloring-item">{{ i18n "syntax_coloring" }}</a>

                    {{ if not .Site.Data.args.archive }}
                        <div role="separator"></div>

                        {{ if .Params.source_repo }}
                            {{ $msg := i18n "generated_file" }}
                            {{ $title := printf $msg .Page.Params.source_repo }}
                            <a tabindex="-1" role="menuitem" class="disabled" title="{{ $title }}">{{ i18n "edit_on_github" }}</a>
                        {{ else }}
                            {{ if eq $home.Lang "zh" }}
                                <a tabindex="-1" role="menuitem" href="https://github.com/istio/istio.io/edit/{{ .Site.Data.args.doc_branch_name }}/content/zh/{{ .Path }}">{{ i18n "edit_on_github" }}</a>
                            {{ else }}
                                <a tabindex="-1" role="menuitem" href="https://github.com/istio/istio.io/edit/{{ .Site.Data.args.doc_branch_name }}/content/en/{{ .Path }}">{{ i18n "edit_on_github" }}</a>
                            {{ end }}
                        {{ end }}

                        <a tabindex="-1" role="menuitem" href="https://github.com/istio/istio.io/issues/new?title=Issue%20with%20{{ .Path}}">{{ i18n "report_site_bugs" }}</a>
                    {{ end }}

                    <div role="separator"></div>

                    <h6>{{ i18n "other_versions_of_site" }}</h6>

                    {{ $next := .Site.Data.versions.preliminary }}
                    {{ $current := .Site.Data.versions.main }}

                    {{ $fileDir := "" }}
                    {{ with .File }}
                        {{ $fileDir = .Dir }}
                    {{ end }}

                    {{ if or .Site.Data.args.archive .Site.Data.args.archive_landing }}
                        <a tabindex="-1" role="menuitem" onclick="navigateToUrlOrRoot('https://istio.io/{{ $fileDir }}');return false;">{{ i18n "current_release" }}</a>
                        <a tabindex="-1" role="menuitem" onclick="navigateToUrlOrRoot('https://preliminary.istio.io/{{ $fileDir }}');return false;">{{ i18n "next_release" }}</a>
                        <a tabindex="-1" role="menuitem" href="https://archive.istio.io">{{ i18n "archived_releases" }}</a>
                    {{ else if .Site.Data.args.preliminary }}
                        <a tabindex="-1" role="menuitem" onclick="navigateToUrlOrRoot('https://istio.io/{{ $fileDir }}');return false;">{{ i18n "current_release" }}</a>
                        <a tabindex="-1" role="menuitem" href="https://archive.istio.io">{{ i18n "archived_releases" }}</a>
                    {{ else }}
                        <a tabindex="-1" role="menuitem" onclick="navigateToUrlOrRoot('https://preliminary.istio.io/{{ $fileDir }}');return false;">{{ i18n "next_release" }}</a>
                        <a tabindex="-1" role="menuitem" href="https://archive.istio.io">{{ i18n "archived_releases" }}</a>
                    {{ end }}
                </div>
            </div>

            <button id="search-show" title='{{ i18n "search" }}' aria-label='{{ i18n "search_label" }}'>{{ partial "icon.html" "magnifier" }}</button>
        </div>

        <form id="search-form" name="cse" role="search">
            {{ if .Site.Data.args.preliminary }}
                <input type="hidden" name="cx" value="{{ .Site.Data.args.preliminary_search_engine_id }}" />
            {{ else if or .Site.Data.args.archive .Site.Data.args.archive_landing }}
                <input type="hidden" name="cx" value="{{ .Site.Data.args.archive_search_engine_id }}" />
            {{ else }}
                <input type="hidden" name="cx" value="{{ .Site.Data.args.main_search_engine_id }}" />
            {{ end }}
            <input type="hidden" name="ie" value="utf-8" />
            <input type="hidden" name="hl" value="en" />
            <input type="hidden" id="search-page-url" value="{{ .Site.BaseURL }}/search" />
            <input id="search-textbox" class="form-control" name="q" type="search" aria-label='{{ i18n "search" }}'/>
            <button id="search-close" title='{{ i18n "search_cancel" }}' type="reset" aria-label='{{ i18n "search_cancel" }}'>{{ partial "icon.html" "cancel-x" }}</button>
        </form>
    </nav>
</header>
