<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
		xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                exclude-result-prefixes="#default">

  <!-- ========================================================================== -->
  <!-- emne.xsl (versjon 001)                                                     -->
  <!-- ========================================================================== -->

  <!-- Felles for studiedokumenter -->
  <xsl:include href="../../ressurser/profil-v001.xsl"/>
  <xsl:include href="../../ressurser/translations-v001.xsl"/>
  <xsl:include href="../../ressurser/mapping-v001.xsl"/>
  <xsl:include href="../../ressurser/hent-fellestekst-v001.xsl"/>

  <!-- Felles for alle dokumenter -->
  <xsl:include href="../../../system/api-v001.xsl"/>
  <xsl:include href="../../../felles/ressurser/output-method-html-v001.xsl"/>

  <!-- XSL til komponenter -->
  <xsl:include href="../../../felles/komponenter/fritekst/v002/fritekst.xsl"/>
  <xsl:include href="../../komponenter/adresse/v001/adresse.xsl"/>
  <xsl:include href="../../komponenter/hent-ekstern-adresse/v001/hent-ekstern-adresse.xsl"/>

  <xsl:template match="/">
    <html>      
      <xsl:variable name="emne-kode">
        <xsl:call-template name="emnekode-med-mellomrom"/>
      </xsl:variable>
      <head>
        <title><xsl:value-of select="emne/tittel/emnekode"/> - <xsl:value-of select="emne/tittel/emnenavn"/></title>
        <meta name="description" content="{emne/kort-om-emnet/avsnitt[1]}" />
        <meta name="keywords" content="{emne/tittel/emnekode}, {emne/tittel/emnenavn}, {$emne-kode}" />

        <!-- Henter css-stiler fra profil-v001.xsl -->
        <xsl:call-template name="inkluder-metadata-til-profil"/>

        <style type="text/css">
          h2,h3 {margin-bottom: -5px;}
          .master {margin-left: 10px; margin-bottom: 5px;} <!-- utdyper studietilbud.css -->
          table table td p {display:inline;}
          h1 {margin-bottom: 0.5em;}
          
          .engelsk {
            background: transparent url(/vrtx/vortex-webdocuments/studier/emne/v001/union-jack.png) no-repeat 0px 2px;
          }
          .norsk {
            background: transparent url(/vrtx/vortex-webdocuments/studier/emne/v001/norway.png) no-repeat 0px 2px;
          }
          .engelsk a, .norsk a {
          padding-left: 20px;
          }
        </style>
      </head>

      <body>

        <table cellspacing="0" cellpadding="1" align="right">
          <tr> 
            <td class="navigasjon">
              <!-- Peker og flagg til versjon på motsatt spraak -->
              <xsl:apply-templates select="//emnekode" mode="sjekk-spraakversjon" /> 
            </td>
          </tr>
        </table>

        <xsl:call-template name="studiesti"/>
        
        <xsl:apply-templates select="//status" mode="statusmessage"/>
                
        <h1 id="tekststart">
          <xsl:value-of select="emne/tittel/emnekode"/> - <xsl:value-of select="emne/tittel/emnenavn"/>
        </h1>

        <table cellspacing="0" cellpadding="0" width="50%" align="right" style="margin-left:10px;margin-bottom:10px;">
          <tr>
            <td>
              
            <table class="emner" cellpadding="5" cellspacing="5" width="100%">
              <tr>
                <td colspan="2">
                  <b>
                    <xsl:call-template name="oversett-diverse">
                      <xsl:with-param name="diversetekst" select="'Fakta om emnet:'"/>
                    </xsl:call-template>
                  </b>
                </td>
              </tr>
              <xsl:apply-templates select="emne/studiepoeng" mode="faktaboks" />
              <xsl:apply-templates select="emne/emnetype" mode="faktaboks"/>
              <!-- xsl:apply-templates select="emne/primaerkategori" mode="faktaboks" / -->
              <xsl:apply-templates select="emne/undervisningssemester" mode="faktaboks" />
              <xsl:apply-templates select="emne/eksamenssemester" mode="faktaboks"/>
              <xsl:apply-templates select="emne/undervisningsspraak" mode="faktaboks" />
              <xsl:apply-templates select="emne/tilbys-av" mode="faktaboks" />
            </table>

            </td>
          </tr>
          <tr><td>&#160;</td></tr>
          <tr>
            <td>
              <xsl:call-template name="undervisning-i-emnet" /> <!-- ligger i mapping-v001.xsl -->
            </td>
          </tr>
       </table>
       <xsl:call-template name="innholdsfortegnelse"/>
       <xsl:apply-templates /> 
     </body>
    </html>
  </xsl:template>

  <xsl:template match="status" mode="statusmessage">
    <xsl:if test="not(@status='gjeldende-versjon')">
      <h2 class="statusmessage"> 
        <xsl:call-template name="oversett-attributtverdi" />
      </h2>
    </xsl:if>
  </xsl:template>

  <xsl:template match="status"/>

  <xsl:template name="innholdsfortegnelse">
    <div style="padding-top:5px;">
      <xsl:for-each select="//nettbasert-undervisning/following-sibling::*">
        <!-- Forkunnskaper skal ut uansett? -->
        <xsl:if test="not(normalize-space(.)='') or (not(@fellestekst='ingen-fellestekst') and @fellestekst) or name()='forkunnskaper'">
          <a href="#{name()}"><xsl:call-template name="oversett-element"/></a>
          <xsl:if test="following-sibling::*"><xsl:text> - </xsl:text></xsl:if>
        </xsl:if>
      </xsl:for-each>
    </div>
  </xsl:template>

  <xsl:template match="studiepoeng" mode="faktaboks">
    <xsl:if test="not(normalize-space(.)='')">
      <tr valign="top">
        <td><xsl:call-template name="oversett-element" />:</td>
        <td><xsl:value-of select="." /></td>
      </tr>     
    </xsl:if>
  </xsl:template>

  <xsl:template match="emnetype" mode="faktaboks">
    <xsl:if test="not(@*='ikke-satt')">
    <tr valign="top">
      <td><xsl:call-template name="oversett-element" /></td>
      <td><xsl:call-template name="oversett-attributtverdi" /></td>
    </tr>     
    </xsl:if>
  </xsl:template>

  <xsl:template match="tilbys-av" mode="faktaboks">
    <tr valign="top">
      <td><xsl:call-template name="oversett-element" /></td>
      <td>
        <xsl:choose>
          <xsl:when test="@hent-fra-adresse='ja' and //hent-ekstern-adresse='Ja'">
            <xsl:apply-templates select="//hent-ekstern-adresse" mode="faktaboks" />
          </xsl:when>
          <xsl:when test="@hent-fra-adresse='ja'">
            <xsl:apply-templates select="//adresse" mode="faktaboks" />
          </xsl:when>
          <xsl:otherwise>
            <xsl:apply-templates select="weblenke"/>
          </xsl:otherwise>
        </xsl:choose>
      </td>
    </tr>     
  </xsl:template>

  <!-- xsl:template match="primaerkategori" mode="faktaboks">
    <tr valign="top">
      <td>Tema:</td>
      <td><a href="">Samfunn</a><br/><div style="font-size:80%"><a href="">Kultur</a>, <a href="">Teknologi</a></div></td>
    </tr>     
  </xsl:template -->

  <xsl:template match="undervisningssemester|eksamenssemester|undervisningsspraak" mode="faktaboks">
    <xsl:if test="not(@*='annet' and normalize-space(.)='')">
    <tr valign="top">
      <td><xsl:call-template name="oversett-element" /></td>
      <td>
        <xsl:choose>
          <xsl:when test="@*='bestemte-semestre'">
            <xsl:apply-templates select="bestemte-semestre" mode="faktaboks"/>
          </xsl:when>
          <xsl:when test="not(@*='annet')">
            <xsl:call-template name="oversett-attributtverdi" /> <!-- skriver ut attributtverdien -->
          </xsl:when>
        </xsl:choose>
        <xsl:if test="not(normalize-space(fritekst)='')"> <!-- skriver ut teksten -->
          <xsl:if test="not(@*='annet')"><br/><br/></xsl:if>
          <xsl:apply-templates select="fritekst"/>
        </xsl:if>
      </td>
    </tr>                    
    </xsl:if>
  </xsl:template>

  <xsl:template match="bestemte-semestre" mode="faktaboks">
    <xsl:for-each select="./*">
      <xsl:call-template name="oversett-diverse">
        <xsl:with-param name="diversetekst" select="semester"/>
      </xsl:call-template>&#160;<xsl:value-of select="aar" />
      <xsl:choose>
        <xsl:when test="not(following-sibling::*)">
          <xsl:text></xsl:text>
        </xsl:when>
        <xsl:when test="count(following-sibling::*) > 1">
          <xsl:text>, </xsl:text>
        </xsl:when>
        <xsl:otherwise>
          <!-- og -->
          <xsl:call-template name="oversett-diverse"><xsl:with-param name="diversetekst" select="'og'"/></xsl:call-template>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:for-each>
  </xsl:template>

  <!-- Elementer som eksplisitt matches må ut av den normale matchinga -->
  <xsl:template match="emnenavn|spraak"/>
  <xsl:template match="emnekode"/>
  <xsl:template match="studiepoeng"/>
  <xsl:template match="undervisningssemester"/>
  <xsl:template match="eksamenssemester"/>
  <xsl:template match="undervisningsspraak"/>
  <xsl:template match="tilbys-av"/>
  <xsl:template match="kursavgift"/>
  <xsl:template match="undervisning-kveldstid-eller-helg"/>
  <xsl:template match="nettbasert-undervisning"/>

  <xsl:template match="forkunnskaper">
    <h2 id="{name()}"><xsl:call-template name="oversett-element" /></h2>
    <xsl:choose>
      <xsl:when test="normalize-space(obligatoriske-forkunnskaper)='' and obligatoriske-forkunnskaper/@fellestekst='ingen-fellestekst' and normalize-space(anbefalte-forkunnskaper)=''">
        <p>
          <xsl:call-template name="oversett-diverse">
            <xsl:with-param name="diversetekst" select="'Ingen forutsetninger'" />
          </xsl:call-template>
        </p>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!-- overskrift skal ut kun hvis det er innhold eller satt fellestekst-->
  <xsl:template match="obligatoriske-forkunnskaper">
    <xsl:if test="not(normalize-space(.)='') or not(@fellestekst='ingen-fellestekst')">
      <h3><xsl:call-template name="oversett-element" /></h3>
      <xsl:apply-templates />
      <xsl:apply-templates select="@fellestekst"/>
    </xsl:if>
  </xsl:template>

  <!-- skal bare ut hvis det finnes tekstinnhold i elementet -->
  <xsl:template match="anbefalte-forkunnskaper">
    <xsl:if test="not(normalize-space(.)='')">
      <h3 id="{name()}"><xsl:call-template name="oversett-element" /></h3>
      <xsl:apply-templates />
    </xsl:if>
  </xsl:template>

  <xsl:template match="opptak-og-adgang">
    <xsl:if test="not(normalize-space(.)='') or not(@fellestekst='ingen-fellestekst')">
      <h2 id="{name()}"><xsl:call-template name="oversett-element"/></h2>
      <xsl:apply-templates select="fritekst"/>
      <xsl:apply-templates select="@fellestekst"/>
      <xsl:apply-templates select="privatist/fritekst"/>
      <xsl:apply-templates select="privatist/@fellestekst"/>
    </xsl:if>
  </xsl:template>

  <xsl:template match="kort-om-emnet | hva-laerer-du | annet">
    <xsl:if test="not(normalize-space(.)='')">
      <h2 id="{name()}"><xsl:call-template name="oversett-element" /></h2>
      <xsl:apply-templates/>
    </xsl:if>
  </xsl:template>

  <xsl:template match="overlapp">
    <xsl:if test="not(normalize-space(.)='')">
      <h2 id="{name()}"><xsl:call-template name="oversett-element" /></h2>
      <xsl:apply-templates/>
    </xsl:if>
  </xsl:template>

  <xsl:template match="emneoverlapp">
    <p><xsl:apply-templates/></p>
  </xsl:template>

  <xsl:template match="emnekode-med-overlapp">
    <xsl:value-of select="studiepoeng-reduksjon"/>&#160;<xsl:call-template name="oversett-diverse"><xsl:with-param name="diversetekst" select="'studiepoeng-mot'"/></xsl:call-template>&#160;<xsl:apply-templates select="emnekode-referanse"/>.
  </xsl:template>

  <xsl:template match="undervisning">
    <h2 id="{name()}"><xsl:call-template name="oversett-element" /></h2>
    <xsl:apply-templates />
  </xsl:template>

  <xsl:template match="vurdering-og-eksamen">
    <h2 id="{name()}"><xsl:call-template name="oversett-element" /></h2>
    <xsl:apply-templates select="fritekst"/>
    <xsl:apply-templates select="@fellestekst"/>
    <xsl:apply-templates select="hjelpemidler"/>
    <xsl:apply-templates select="eksamensspraak"/>
    <xsl:apply-templates select="sensur"/>
    <xsl:apply-templates select="klage"/>
    <xsl:apply-templates select="ny-utsatt-vurdering"/>
    <xsl:apply-templates select="antall-forsok-trekk"/>
    <xsl:apply-templates select="tilrettelagt-eksamen"/>
  </xsl:template>

  <xsl:template match="sensur">
    <xsl:if test="not(karakterfastsetting/@fellestekst='ingen-fellestekst') or not(ekstern-sensor/@fellestekst='ingen-fellestekst') or not(normalize-space(.)='')">
      <h3 id="{name()}"><xsl:call-template name="oversett-element" /></h3>
      <xsl:apply-templates />
    </xsl:if>
  </xsl:template>

  <xsl:template match="adgang|hjelpemidler|eksamensspraak|klage|ny-utsatt-vurdering|antall-forsok-trekk|tilrettelagt-eksamen">
    <xsl:if test="not(@fellestekst='ingen-fellestekst') or not(normalize-space(.)='')">
      <h3><xsl:call-template name="oversett-element"/></h3>
      <xsl:apply-templates select="fritekst"/>
      <xsl:apply-templates select="@fellestekst"/>
    </xsl:if>
  </xsl:template>

  <xsl:template match="tidsrom-for-undervisning">
    <xsl:if test="not(@fellestekst='ingen-fellestekst') or not(normalize-space(.)='')">
      <xsl:apply-templates select="fritekst"/>
      <xsl:apply-templates select="@fellestekst"/>
    </xsl:if>
  </xsl:template>

  <xsl:template match="karakterfastsetting|ekstern-sensor">
    <xsl:apply-templates select="fritekst"/>
    <xsl:apply-templates select="@fellestekst"/>
  </xsl:template>

  <xsl:template match="evaluering-av-emnet">
    <xsl:if test="not(@fellestekst='ingen-fellestekst')  or not(normalize-space(.)='')">
      <h2 id="{name()}"><xsl:call-template name="oversett-element"/></h2>
      <xsl:apply-templates select="@fellestekst"/>
      <xsl:apply-templates select="fritekst"/>
    </xsl:if>
  </xsl:template>

  <!-- Henter alle fellestekstene på samme måte -->
  <xsl:template match="obligatoriske-forkunnskaper/@fellestekst|privatist/@fellestekst|opptak-og-adgang/@fellestekst|tidsrom-for-undervisning/@fellestekst|adgang/@fellestekst|hjelpemidler/@fellestekst|eksamensspraak/@fellestekst|karakterfastsetting/@fellestekst|ekstern-sensor/@fellestekst|klage/@fellestekst|ny-utsatt-vurdering/@fellestekst|antall-forsok-trekk/@fellestekst|tilrettelagt-eksamen/@fellestekst|evaluering-av-emnet/@fellestekst|vurdering-og-eksamen/@fellestekst">
    <xsl:call-template name="hent-fellestekst"/>
  </xsl:template>

  <xsl:template match="kontaktinformasjon">
    <h2 id="{name()}"><xsl:call-template name="oversett-element"/></h2>
    <xsl:apply-templates/>
  </xsl:template>

  <!-- brukes i emnets faktaboks - lager lenke til adressatnavn -->
  <xsl:template match="hent-ekstern-adresse" mode="faktaboks">
    <xsl:if test=".='Ja'">
      <xsl:apply-templates select="document('../adresse.xml',self::node())/adresser/*[name()=$malspraak]/adresse" mode="faktaboks"/><br />
    </xsl:if>
  </xsl:template>

  <!-- brukes i emnets faktaboks - lager lenke til adressatnavn -->
  <xsl:template match="adresse" mode="faktaboks">
   <xsl:variable name="webadressen"> 
     <xsl:choose>
       <!-- sjekker om underelementet webadresse inneholder http:// -->
       <xsl:when test="not(contains(webadresse,'http://'))">
         <xsl:value-of select="concat('http://', webadresse)" />
       </xsl:when>
       <xsl:otherwise>
         <xsl:value-of select="webadresse" />
       </xsl:otherwise>
     </xsl:choose>
   </xsl:variable>
    <a href="{$webadressen}"><xsl:value-of select="adressatnavn" /></a>
  </xsl:template>

  <!-- ========================================================================== -->
  <!-- Template som brukes i metataggen description for å generere                -->
  <!-- emnekoder med mellomrom mellom prefiks og første tall i emnekoden          -->
  <!-- ========================================================================== -->

  <xsl:template name="emnekode-med-mellomrom">
    <xsl:param name="emnekode" select="current()//emnekode" />
    
    <xsl:variable name="pos">
      <xsl:call-template name="finn-forste-tall">
        <xsl:with-param name="streng" select="$emnekode" />
        <xsl:with-param name="pos" select="1" />
      </xsl:call-template>
    </xsl:variable>

    <xsl:variable name="postfiks" select="substring($emnekode, $pos)"/>
    <xsl:variable name="prefiks" select="substring-before($emnekode, $postfiks)"/>

    <xsl:value-of select="concat($prefiks,' ',$postfiks)" />
  </xsl:template>

  <!-- første tegn i strengen er pos=1, returnerer pos. -->
  <xsl:template name="finn-forste-tall">
    <xsl:param name="streng" />
    <xsl:param name="pos" />
    <xsl:choose>
      <xsl:when test="substring($streng, $pos, 1) = '0' or
                      substring($streng, $pos, 1) = '1' or
                      substring($streng, $pos, 1) = '2' or
                      substring($streng, $pos, 1) = '3' or
                      substring($streng, $pos, 1) = '4' or
                      substring($streng, $pos, 1) = '5' or
                      substring($streng, $pos, 1) = '6' or
                      substring($streng, $pos, 1) = '7' or
                      substring($streng, $pos, 1) = '8' or
                      substring($streng, $pos, 1) = '9'">    
      <xsl:value-of select="$pos" />
      </xsl:when>
      <xsl:otherwise>
        <xsl:choose>
          <xsl:when test="substring($streng, $pos)=''">
            <xsl:value-of select="0" /><!-- emnekoden inneholder ikke tall -->
          </xsl:when>
          <xsl:otherwise>
            <xsl:call-template name="finn-forste-tall">
              <xsl:with-param name="streng" select="$streng" />
              <xsl:with-param name="pos" select="$pos+1" />
            </xsl:call-template>  
          </xsl:otherwise>
        </xsl:choose>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>
