{{ define "title" }}
  {{ .Page.Title | plainify }} | {{ .Site.Title }}
{{ end }}

{{ define "main-class" }}
  {{- if .IsDescendant ( .GetPage "/cv/organisations" ) -}}--single{{- else -}}--section{{- end -}}
{{ end }}

{{ define "header-scripts" }} 
  {{ if ( or ( eq .Type "cv" ) ( .IsDescendant ( .GetPage "/cv/organisations" ) ) ) }}
      <script type="text/javascript" src="/js/google-charts.js"></script>
  {{ end }}
{{ end }}

{{ define "footer-scripts" }}
  {{ if eq .Type "cv" }}
      {{ $js := resources.Get "js/roles-chart.js" | resources.ExecuteAsTemplate "/js/render-roles-timeline.js" . }}
      <script type="text/javascript" src="{{ $js.RelPermalink }}"></script>
   {{ else if .IsDescendant ( .GetPage "/cv/organisations" ) }}
      {{ $titles := .Scratch.Get "titles" }}
      <script>
        google.charts.load('current', {'packages':['timeline']});
        google.charts.setOnLoadCallback(drawChart);

        function drawChart() {
         var container = document.getElementById('timeline');
         var chart = new google.visualization.Timeline(container);
         var dataTable = new google.visualization.DataTable();
         
         dataTable.addColumn({ type: 'string', id: 'Type' });
         dataTable.addColumn({ type: 'string', id: 'Job Title' });
         dataTable.addColumn({ type: 'string', id: 'style', role: 'style' });
         dataTable.addColumn({ type: 'date', id: 'Start' });
         dataTable.addColumn({ type: 'date', id: 'End' });
           dataTable.addRows([    
           {{ range where ((.GetPage "cv").Sections) "Title" "Roles" -}}
             {{- range where (where ( where .Site.Pages "Section" "cv" ) "Parent.Title" .Title ) ".Params.organisations" "intersect" $titles -}}
               [ 
                  "{{- delimit (intersect .Params.organisations $titles ) "" | safeHTML -}}", 
                  "{{- .Title | safeHTML -}}", 
                 "{{ if .Params.end_date }}#FFFFF0{{ else }}#BBBBB0{{ end }}",
                 new Date("{{ .Date.Format "2006-01-02" }}"), 
                 new Date("{{ if .Params.end_date }}{{ delimit ( first 1 .Params.end_date ) "" }}{{ else }}{{ now.Format "2006-01-02" }}{{ end }}") 
               ],
             {{- end -}}
           {{ end -}}
         ]);
         
         var options = {
              title: '{{ .Title | plainify }} Roles Timeline',
         timeline: { showBarLabels: true }
         };
         
         chart.draw(dataTable, options);
         
         {{ if $titles }}
            container.style.height = ( ( ( {{ len $titles }} - 1 ) * 41 ) + 15 )+ "px";
         {{ end }}
        }
      </script>
   {{ end }}
{{ end }}

{{ define "main-header" }}
  {{ if .IsDescendant ( .GetPage "/cv/organisations" ) }}
    <header class="site-content__header">
      <div class="article-header__featured-image"
        {{ if .Params.featured_image }}
          {{ with .Resources.GetMatch .Params.featured_image }}
          style="background-image: url({{ .RelPermalink }})"
          {{ end }}
        {{ end }}>
        <div class="article-header__title-wrapper{{ if .Params.title_in_logo }} article-header__title-wrapper--no-title{{ end }}">
            <h1 class="article-header__title">{{ .Title | safeHTML }}LIST</h1>
        </div>
      </div>
    </header>
  {{ else }}
    <header class="site-content__header">
      <h1 class="page-title">{{ .Title | safeHTML }}.</h1>
      {{ .Content }}
      {{ if eq .Type "cv" }}
       <nav class="page-header__minor-links">
         <ul>
            {{ range ( where .Sections "Type" "not in" ( slice "cv/roles" ) ).ByTitle }}
               <li><a href="{{ .RelPermalink }}"><h2>{{ .Title }}</h2></a> <p>({{ len (where ( where .Site.Pages "Section" "cv" ) "Parent.Title" .Title ) }})</p></li>
           {{ end }}
         </ul>
       </nav>
      {{ end }}
      {{ if eq .Type "cv" }}
         <aside class="timeline__legend">
            <span><div class="colour-square colour-square--current"></div> <p>Current</p></span>
            <span><div class="colour-square colour-square--past"></div> <p>Past</p></span>
         </aside>
      {{ end }}
    </header>
  {{ end }}
{{ end }}

{{ define "main-body" }}
  {{ if .IsDescendant ( .GetPage "/cv/organisations" ) }}
      {{ $titles := slice (.Title | plainify) }}
      {{ range .Sections.ByTitle }}
         {{ $titles = $titles | append (slice (.Title | plainify) )}}
         {{ $titles = $titles | append ( partial "cv/organisations/recursive-children-titles.html" . ) }}
      {{ end }}
      {{ .Scratch.Set "titles" $titles }}

      <main id="organisation-body" class="site-content__body">
	      <section id="description">
          {{ .Content }}
        </section>
        
        <section id="roles-timelimne">
         <aside class="timeline__legend">
            <span><div class="colour-square colour-square--current"></div> <p>Current</p></span>
            <span><div class="colour-square colour-square--past"></div> <p>Past</p></span>
         </aside>
          <div id="timeline" aria-label="Role Time" role="img">Javascript must be enabled to view this chart.</div>
        </section>
        
        <section id="related" class="row">
          {{ if ne .Parent.Title "Organisations" }}
			      <section id="parents" class="col-6 col-m-12">
				      <h2 class="subheading">Parent</h2>
			        {{ with .Parent }}
			        <ul>
			          <li><a href="{{ .RelPermalink }}">{{ .Title | safeHTML }}</a></li>
			        </ul>
			        {{ end }}
			      </section>
		      {{ end }}
		      
          {{ if gt (len .Sections) 0 }}
			      <section id="children" class="col-6 col-m-12">
				      <h2 class="subheading">Children</h2>
              <ul>
			       {{ range .Sections.ByTitle }}
                  <li>            
                    <a href="{{ .RelPermalink }}">{{ .Title | safeHTML }}</a>
                    {{ partial "cv/organisations/recursive-children.html" . }}
                  </li>
                {{ end }}
              </ul>
	          </section>
          {{ end }}

          {{- $titles := .Scratch.Get "titles" -}}
          
          {{ range (where ((.GetPage "blog").Sections) "Title" "Blog Posts") }}
            {{ $section_title := .Title }}
            {{ with where (where ( where .Site.Pages "Section" "blog" ) "Parent.Title" .Title ) ".Params.organisations" "intersect" $titles }}
              {{ partial "cv/organisations/organisation-items.html" ( dict "section_title" $section_title "content" . ) }}
            {{ end }}
          {{ end }}
          
          {{ range (where ((.GetPage "portfolio").Sections) "Title" "!=" "Appearances").ByTitle }}
            {{ $section_title := .Title }}
            {{ with where (where ( where .Site.Pages "Section" "portfolio" ) "Parent.Title" .Title ) ".Params.organisations" "intersect" $titles }}
              {{ partial "cv/organisations/organisation-items.html" ( dict "section_title" $section_title "content" . ) }}
            {{ end }}
          {{ end }}
           
          {{ range (where ((.GetPage "cv").Sections) "Title" "Qualifications") }}
            {{ $section_title := .Title }}
            {{ with where (where ( where .Site.Pages "Section" "portfolio" ) "Parent.Title" .Title ) ".Params.organisations" "intersect" $titles }}
              {{ partial "cv/organisations/organisation-items.html" ( dict "section_title" $section_title "content" . ) }}
            {{ end }}
          {{ end }}
          
          {{ range (where ((.GetPage "cv").Sections) "Title" "Awards") }}
            {{ $section_title := .Title }}
            {{ with where (where ( where .Site.Pages "Section" "portfolio" ) "Parent.Title" .Title ) ".Params.organisations" "intersect" $titles }}
              {{ partial "cv/organisations/organisation-items.html" ( dict "section_title" $section_title "content" . ) }}
            {{ end }}
          {{ end }}
          
          {{ range (where ((.GetPage "portfolio").Sections) "Title" "Appearances").ByTitle }}
            {{ $section_title := .Title }}
            {{ with where (where ( where .Site.Pages "Section" "portfolio" ) "Parent.Title" .Title ) ".Params.organisations" "intersect" $titles }}
              {{ partial "cv/organisations/organisation-items.html" ( dict "section_title" $section_title "content" . ) }}
            {{ end }}
          {{ end }}
	      </section>
      </main>
  {{ else }}
     {{ if eq .Type "cv" }}
        <article class="site-content__body site-content__body--list">
            <div id="timeline" width="1000" height="400" aria-label="Roles Timeline" role="img">Javascript must be enabled to view this chart.</div>
        </article>
     {{ else if eq .Type "cv/organisations" . }}
        {{ with .GetPage "cv/organisations" }}
           {{ partial "items-table.html" . }}
        {{ end }}
     {{ else }}
         {{ partial "items-grid.html" . }}
     {{ end }}
  {{ end }}
{{ end }}