<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:ixsl="http://saxonica.com/ns/interactiveXSLT"
  xmlns:js="http://saxonica.com/ns/globalJS"
  xmlns:prop="http://saxonica.com/ns/html-property"
  xmlns:style="http://saxonica.com/ns/html-style-property"
  xmlns:o="http://piez.org/TEIoverlap/xslt/util"
  xpath-default-namespace="http://www.w3.org/1999/xhtml"
  exclude-result-prefixes="#all"
  extension-element-prefixes="ixsl">

<!-- SaxonCE browser event-handling code for Interedition demonstration
     by Wendell Piez, October 2011.

     Released under a Creative Commons Attribution 3.0 Unported License
     http://creativecommons.org/licenses/by/3.0/
-->
  <xsl:import href="p5o-browser-html.xsl"/>
  
  <xsl:template name="start">
    <xsl:result-document href="#page-head" method="ixsl:append-content">
      <xsl:call-template name="css"/>
    </xsl:result-document>
    <xsl:result-document href="#page-title" method="ixsl:replace-content">
      <xsl:call-template name="title-content"/>
    </xsl:result-document>
    <xsl:result-document href="#page-content" method="ixsl:replace-content">
      <xsl:call-template name="page-content"/>
    </xsl:result-document>
  </xsl:template>
  
  <xsl:template name="css">
    <xsl:call-template name="base-css"/>
    <style type="text/css">
      div.main { margin-left: 280px  }
      div.note { width: 280px;
        color:  ;
        background-color: <xsl:value-of select="$note-background"/>;
        padding: 5px; border: <xsl:value-of select="$note-border"/>;
        font-size: 80%; line-height: 140%;
        margin-left: -310px; margin-top: -3ex; float: left; clear: both;
        position: absolute; visibility: hidden; text-indent: 0em }
      div.page { margin-top: 5px; margin-bottom: 5px;
        padding: 10px 30px; border: thin solid black }
      div.page-no { text-align: center; margin-top: 15px }
      div.text p.continued { text-indent: 0px }
      span.note-ref { background-color: <xsl:value-of select="$ref-background"/> }
      div.notes { visibility: hidden }
    </style>
  </xsl:template>
  
  <!-- color scheme: http://colorschemedesigner.com/#1i4009VE8FZHl -->
  <xsl:variable name="ref-background" as="xs:string">#B4E59E</xsl:variable>
  <xsl:variable name="said-flashed" as="xs:string">#FFF4CB</xsl:variable>
  <xsl:variable name="annotated-flashed" as="xs:string">#D9D6EE</xsl:variable>
  <xsl:variable name="annotated-shown" as="xs:string">#C9C4EE</xsl:variable>
  <xsl:variable name="note-background" as="xs:string">#FCDEE3</xsl:variable>
  <xsl:variable name="note-border" as="xs:string">medium solid #A281A7</xsl:variable>
  
  <xsl:function name="o:class" as="xs:string*">
    <!-- returns the values of @class, tokenized -->
    <xsl:param name="e" as="element()"/>
    <xsl:sequence select="tokenize($e/@class,'\s+')"/>
  </xsl:function>
  
  <xsl:function name="o:is-showing" as="xs:boolean">
    <xsl:param name="n" as="element(div)"/>
    <xsl:sequence select="$n/@style:visibility = 'visible'"/>  
  </xsl:function>
  
  <xsl:function name="o:annotates" as="element(span)*">
    <xsl:param name="r" as="element()"/>
    <!-- $r is a note reference (span[@class='note-ref']), 
         or a note (div[@class='note']); we return
         the spans to which the note attaches -->
    <xsl:sequence
      select="root($r)//span[o:class(.) = replace($r/@id,'^(ref-|note-)','n-')]"/>
  </xsl:function>
 
  <xsl:function name="o:annotated-ref" as="element(span)*">
    <xsl:param name="n" as="element(span)"/>
    <!-- $n is an annotated span; we return the reference(s)
      to the note(s) annotating it -->
    <xsl:sequence
      select="id(for $c in (o:class($n)[starts-with(.,'n-')])
      return replace($c,'^n-','ref-'),root($n))"/>
  </xsl:function>
  
  <xsl:function name="o:annotated-by" as="element(div)*">
    <xsl:param name="n" as="element(span)"/>
    <!-- $n is an annotated span, or a note reference; we return the note(s)
      annotating it -->
    <xsl:sequence
      select="id(
        for $c in ( o:class($n)[starts-with(.,'n-')] )
      return replace($c,'^(ref-|n-)','note-'),root($n))"/>
  </xsl:function>
  
  <xsl:template match="span[o:class(.)='said']" mode="ixsl:onmouseover">
    <!-- when moused, a span marked as a direct quote is flashed, along
      with other spans belonging to the same quote -->
    <!-- among the assigned class values, the one starting "said-" marks
      this particular quote (along with the other spans belonging to it) -->
    <xsl:variable name="said-id" select="o:class(.)[starts-with(.,'said-')]"/>
    <xsl:apply-templates select="//span[o:class(.) = $said-id]" mode="flash">
      <xsl:with-param name="color" select="$said-flashed"/>
    </xsl:apply-templates>
  </xsl:template>
  
  <xsl:template match="span[o:class(.)='said']" mode="ixsl:onmouseout">
    <!-- when moused off, a span marking a quote is flashed off, with 
         other spans belonging to the same quote -->
    <xsl:variable name="said-id" select="o:class(.)[starts-with(.,'said-')]"/>
    <xsl:apply-templates select="//span[o:class(.) = $said-id]" mode="flash"/>
  </xsl:template>
  
  <xsl:template match="span[o:class(.) = 'note-ref']" mode="ixsl:onmouseover">
    <!-- when a note reference is moused, it and the spans it annotates
         flashes on -->
    <xsl:variable name="target-id" select="replace(@id,'^ref-','note-')"/>
    <xsl:variable name="target" select="id($target-id)"/>
    <xsl:apply-templates select=".|o:annotates(.)" mode="flash">
      <!-- if the target note is already showing, the color of the reference
           and annotated spans reflects that-->
      <xsl:with-param name="color" select="if (o:is-showing($target))
        then $annotated-shown else $annotated-flashed"/>
    </xsl:apply-templates>
  </xsl:template>
  
  <xsl:template match="span[o:class(.) = 'note-ref']" mode="ixsl:onmouseout">
    <!-- on a note reference's mouse out, if its note is not showing, it
         and the spans it annotates are restored to their default background 
         colors -->
    <xsl:variable name="target-id" select="replace(@id,'^ref-','note-')"/>
    <xsl:variable name="target" select="id($target-id)"/>
    <xsl:if test="not(o:is-showing($target))">
      <xsl:apply-templates select="o:annotates(.)" mode="flash-off"/>
      <xsl:apply-templates select="." mode="flash">
        <xsl:with-param name="color" select="$ref-background"/>
      </xsl:apply-templates>
    </xsl:if>
  </xsl:template>
  
  <xsl:template match="span[o:class(.) = 'note-ref']" mode="ixsl:onclick">
    <!-- if a note reference is clicked, it toggles its note on or off -->
    <xsl:variable name="target-id" select="replace(@id,'^ref','note')"/>
    <xsl:variable name="target" select="id($target-id)"/>
    <!-- first all other notes are turned off -->
    <xsl:apply-templates select="//div[o:class(.)='note'] except $target" mode="off"/>
    <xsl:choose>
      <xsl:when test="o:is-showing($target)">
        <!-- a note that is already showing is hidden and its reference
             and annotated spans are restored to their defaults -->
        <xsl:apply-templates select="$target" mode="off"/>
        <xsl:apply-templates select="." mode="flash">
          <xsl:with-param name="color" select="$annotated-flashed"/>
        </xsl:apply-templates>
      </xsl:when>
      <xsl:otherwise>
        <!-- a note that isn't already showing is shown, and its reference
             and annotated spans are flashed to reflect that -->
        <xsl:apply-templates select="$target" mode="on"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
  <xsl:template match="span[o:class(.) = 'annotated']" mode="ixsl:onmouseover">
    <xsl:apply-templates select="o:annotated-ref(.)" mode="flash">
      <xsl:with-param name="color" select="$annotated-shown"/>
    </xsl:apply-templates>
  </xsl:template>
  
  <xsl:template match="span[o:class(.) = 'annotated']" mode="ixsl:onmouseout">
    <!-- an annotated span flashes off on mouse out -->
    <xsl:apply-templates select="o:annotated-ref(.)" mode="flash">
      <xsl:with-param name="color" select="$ref-background"/>
    </xsl:apply-templates>
    <xsl:apply-templates select="." mode="flash-off"/>
  </xsl:template>

  <xsl:template match="span[o:class(.) = 'annotated']" mode="flash-off">
    <xsl:apply-templates select="." mode="flash"/>
  </xsl:template>

  <xsl:template match="span[o:class(.) = 'annotated']
    [some $n in o:annotated-by(.) satisfies o:is-showing($n)]"
    priority="2" mode="flash-off">
    <!-- overriding flash off for annotated spans with notes
         showing: their color is set to be 'shown' not inherited -->
    <xsl:apply-templates select="." mode="flash">
      <xsl:with-param name="color" select="$annotated-shown"/>
    </xsl:apply-templates>
  </xsl:template>
  

  <xsl:template match="span[o:class(.) = 'annotated']" mode="ixsl:onclick">
    <!-- an annotated span should turn off any notes -->
    <xsl:apply-templates select="o:annotated-by(.)" mode="off"/>
  </xsl:template>
  
  <xsl:template match="div[o:class(.) = 'note']" mode="ixsl:onclick">
    <!-- a note is turned off when clicked -->
    <xsl:apply-templates select="." mode="off"/>
  </xsl:template>

  <xsl:template match="div[o:class(.) = 'note']" mode="on">
    <xsl:variable name="ref" select="id(replace(@id,'^note-','ref-'))"/>
    <xsl:apply-templates select="$ref|o:annotates(.)" mode="flash">
      <xsl:with-param name="color" select="$annotated-shown"/>
    </xsl:apply-templates>
    <ixsl:set-attribute name="style:visibility" select="'visible'"/>
  </xsl:template>
  
  <xsl:template match="div[o:class(.) = 'note']" mode="off">
    <xsl:apply-templates select="o:annotates(.)" mode="flash"/>
    <xsl:variable name="ref" select="id(replace(@id,'^note-','ref-'))"/>
    <xsl:apply-templates select="$ref" mode="flash">
      <xsl:with-param name="color" select="$ref-background"/>
    </xsl:apply-templates>
    <xsl:apply-templates select="." mode="hide"/>
  </xsl:template>

  <xsl:template match="*" mode="hide">
    <ixsl:set-attribute name="style:visibility" select="'hidden'"/>
  </xsl:template>
  
  <xsl:template match="*" mode="flash">
    <!-- when called without a parameter this template will "unflash"
         an element (set its background color to inherit) -->
    <xsl:param name="color">inherit</xsl:param>
    <ixsl:set-attribute name="style:backgroundColor" select="$color"/>
  </xsl:template>
    
</xsl:stylesheet>