<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:i18n="http://apache.org/cocoon/i18n/2.1"
                xmlns:kiln="http://www.kcl.ac.uk/artshums/depts/ddh/kiln/ns/1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                exclude-result-prefixes="#all"
                version="2.0">

  <!-- Transforms a context-free menu into one annotated based on the
       supplied context (ie, place in the menu structure). -->

  <xsl:import href="../../../stylesheets/defaults.xsl">
      <xsl:stylesheet xmlns:xi="http://www.w3.org/2001/XInclude" exclude-result-prefixes="#all"
                      version="2.0">
  <!--
      Defaults stylesheet. Defines default globals and reads
      parameters from the sitemap.
  -->

  <xsl:param name="language"/>

         <!-- Specify a mount path if you are mounting the webapp in a
       subdirectory rather than at the root of the domain. This path
       must either be empty or begin with a "/" and not include a
       trailing slash.

       The value is the URL root for the webapp. -->
  <xsl:variable name="kiln:mount-path" select="''"/>

         <!-- $kiln:context-path defines the URL root for the webapp. -->
  <xsl:variable name="kiln:context-path">
            <xsl:value-of select="$kiln:mount-path"/>
         </xsl:variable>

         <!-- Base URL for non-textual content (images, video, etc). If these
       are being served by Cocoon, this should be specified as
       relative to $context-path. Otherwise, a full URL including
       protocol and domain is required.

       This URL must not include a trailing slash. -->
  <xsl:variable name="kiln:content-url" select="''"/>
         <xsl:variable name="kiln:content-path">
            <xsl:if test="not(starts-with($kiln:content-url, 'http'))">
               <xsl:value-of select="$kiln:mount-path"/>
            </xsl:if>
            <xsl:value-of select="$kiln:content-url"/>
         </xsl:variable>

         <!-- Base URL for assets (non-content images, CSS, JavaScript,
       etc). If these are being served by Cocoon, this should be
       specified as relative to $context-path. Otherwise, a full URL
       including protocol and domain is required.

       This URL must not include a trailing slash. -->
  <xsl:variable name="kiln:assets-url" select="'/assets'"/>
         <xsl:variable name="kiln:assets-path">
            <xsl:if test="not(starts-with($kiln:assets-url, 'http'))">
               <xsl:value-of select="$kiln:mount-path"/>
            </xsl:if>
            <xsl:value-of select="$kiln:assets-url"/>
         </xsl:variable>

         <!-- Base URL for content images. -->
  <xsl:variable name="kiln:images-url" select="concat($kiln:content-path, '/images')"/>
         <xsl:variable name="kiln:images-path">
            <xsl:if test="not(starts-with($kiln:images-url, 'http'))">
               <xsl:value-of select="$kiln:mount-path"/>
            </xsl:if>
            <xsl:value-of select="$kiln:images-url"/>
         </xsl:variable>

      </xsl:stylesheet>
   </xsl:import>

  <xsl:param name="url" select="''"/>

  <xsl:variable name="kiln:nav-url" select="concat($kiln:context-path, '/', $url)"/>

  <xsl:template match="kiln:root">
      <kiln:nav>
         <ul type="menu">
            <xsl:apply-templates select="kiln:menu" mode="menu"/>
         </ul>
         <ul type="breadcrumbs">
            <xsl:choose>
               <xsl:when test=".//*[@href=$kiln:nav-url]">
                  <xsl:apply-templates mode="breadcrumbs" select=".//kiln:menu[descendant::*[@href=$kiln:nav-url]]"/>
               </xsl:when>
               <xsl:otherwise>
                  <xsl:apply-templates mode="breadcrumbs" select=".//kiln:menu[starts-with($kiln:nav-url, @path)]"/>
               </xsl:otherwise>
            </xsl:choose>
         </ul>
      </kiln:nav>
  </xsl:template>

  <xsl:template match="kiln:menu | kiln:item" mode="menu">
      <li>
      <!-- Active item. -->
      <xsl:if test="@href = $kiln:nav-url">
            <xsl:attribute name="class" select="'active'"/>
         </xsl:if>
         <a>
            <xsl:if test="@href">
               <xsl:attribute name="href" select="@href"/>
            </xsl:if>
            <xsl:apply-templates select="@label"/>
         </a>
         <!-- Sub-items. -->
      <xsl:if test="child::*">
            <ul>
               <xsl:apply-templates mode="menu"/>
            </ul>
         </xsl:if>
      </li>
  </xsl:template>

  <xsl:template match="kiln:item[@delete='delete'] |                        kiln:menu[@delete='delete']"
                 mode="#all"/>

  <xsl:template match="kiln:menu" mode="breadcrumbs">
      <li>
         <a href="{@href}">
            <xsl:apply-templates select="@label"/>
         </a>
      </li>
  </xsl:template>

  <xsl:template match="kiln:menu[not(@root)][not(@delete)]" mode="breadcrumbs"/>

  <xsl:template match="@label">
      <i18n:text>
         <xsl:copy-of select="../@i18n:key"/>
         <xsl:value-of select="."/>
      </i18n:text>
  </xsl:template>

  <xsl:template match="@*|node()">
      <xsl:copy>
         <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>
  </xsl:template>

</xsl:stylesheet>