<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/2006/10/ttaf1" >

<!-- Output method XML -->
<xsl:output method="xml" 
  indent="yes"
  omit-xml-declaration="no" 
  encoding="utf-8" />

<xsl:param name="compat"/>

<xsl:template match="/Trans">
  <tt>
    <xsl:apply-templates select="@xml:lang"/>
    <head>
      <metadata xmlns:ttm="http://www.w3.org/2006/10/ttaf1#metadata">
        <xsl:if test="Episode[@program]">
          <ttm:title><xsl:value-of select="Episode/@program"/></ttm:title>
        </xsl:if>
        <xsl:for-each select="Speakers/Speaker">
          <ttm:agent type="person" xml:id="{@id}">
            <ttm:name><xsl:value-of select="@name"/></ttm:name>
          </ttm:agent>
        </xsl:for-each>
        </metadata>
    </head>
    <body>
      <xsl:apply-templates select="Episode"/>
    </body>
  </tt>
</xsl:template>

<xsl:template match="Episode|Section|Turn">
  <div>
    <xsl:choose>
      <xsl:when test="$compat='subtitleeditor'">
        <xsl:apply-templates select="@*|descendant::Sync"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates select="@*|*"/>
      </xsl:otherwise>
    </xsl:choose>
  </div>
</xsl:template>

<xsl:template name="formatOffsetAsClocktime">
  <xsl:param name="offset"/>
  <xsl:variable name="h" select="floor($offset div 3600)"/>
  <xsl:variable name="m" select="floor(($offset - $h*3600) div 60)"/>
  <xsl:variable name="s" select="floor($offset - $h*3600 - $m*60)"/>
  <xsl:variable name="ms"  select="floor(1000*($offset - $h*3600 - $m*60 - $s))"/>
  <xsl:value-of select="concat(format-number($h,'00'),':',format-number($m,'00'),':',format-number($s,'00'),'.',$ms)"/>
</xsl:template>

<xsl:template match="Sync">
  <p>
    <xsl:attribute name="begin">
      <xsl:call-template name="formatOffsetAsClocktime">
      <xsl:with-param name="offset"  select="@time"/></xsl:call-template>
    </xsl:attribute>
    <xsl:variable name="end">
      <xsl:choose>
        <xsl:when test="following::Sync">
          <xsl:value-of select="following::Sync[1]/@time"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="ancestor::*[@endTime][1]/@endTime"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:attribute name="end">
      <xsl:call-template name="formatOffsetAsClocktime">
        <xsl:with-param name="offset" select="$end"/>
      </xsl:call-template>
    </xsl:attribute>
    <xsl:attribute name="dur">
      <xsl:text>00:00:</xsl:text><xsl:value-of select="format-number($end - @time,'##.###')"/>
    </xsl:attribute>
    <xsl:apply-templates select="following-sibling::text()[preceding-sibling::Sync[1][@time=current()/@time]]"/>
  </p>
</xsl:template>

<xsl:template match="@speaker" xmlns:ttm="http://www.w3.org/2006/10/ttaf1#metadata">
  <xsl:attribute name="ttm:agent"><xsl:value-of select="."/></xsl:attribute>
</xsl:template>
<!--
<xsl:template match="@startTime">
  <xsl:attribute name="begin">
    <xsl:call-template name="formatOffsetAsClocktime">
            <xsl:with-param name="offset"  select="."/></xsl:call-template>
  </xsl:attribute>
</xsl:template>

<xsl:template match="@endTime">
  <xsl:attribute name="end">
    <xsl:call-template name="formatOffsetAsClocktime">
      <xsl:with-param name="offset"  select="."/></xsl:call-template>
  </xsl:attribute>
</xsl:template>
-->
<xsl:template match="@xml:lang">
  <xsl:copy><xsl:value-of select="."/></xsl:copy>
</xsl:template>


<xsl:template match="@topic"  xmlns:ttm="http://www.w3.org/2006/10/ttaf1#metadata">
  <xsl:attribute name="ttm:title"><xsl:value-of select="/Trans/Topics/Topic[@id=current()]/@desc"/></xsl:attribute>
</xsl:template>

<xsl:template match="@*"/>
  <!-- default: Identity Transformation -->
  <xsl:template match="comment()|text()">
    <xsl:copy>
      <xsl:apply-templates select="comment()|text()"/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>