<?xml version="1.0" encoding="UTF-8"?>
<!-- Common utilities to generate HTML content and slides -->
<!-- I. Herman $Date: 2004/01/11 16:01:46 $ -->
<!--
     Copyright (c) 2003 W3C
     See http://www.w3.org/Consortium/Legal/copyright-software-19980720
     for W3C software licensing (open source compatible)
     Author: Ivan Herman <ivan@w3.org>     
-->
<!-- *********************************************************************** -->
<!-- FOR XSLT 2.0: the extension elements related to SAXON should be deleted -->
<!-- *********************************************************************** -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" extension-element-prefixes="saxon" xmlns:saxon="http://icl.com/saxon" xmlns:x="http://www.w3.org/1999/xhtml" xmlns:slide="http://www.w3.org/Consortium/Offices/Presentation/xsltSlidemaker" xmlns:xlink="http://www.w3.org/1999/xlink">
    <xsl:include href="common.xsl"/>
    <!-- ================================================================== -->
    <!--                               SVG Copy                             -->
    <!-- ================================================================== -->
    <!-- 
        Function to copy the content of an SVG file to another directory.
        The css reference is changed on the fly to ensure proper display.
        The real copy is done in a separate recursive function, called
        from this template.
    -->
    <xsl:template name="copySVG">
        <xsl:param name="obj"/>
        <xsl:variable name="fileName" select="$obj/@data"/>
        <!-- 
            This is a somewhat longish setting of a variable.
            There four possible cases: either the css URL should be relative or absolute and,
            in both cases, the possible setting in the object element itself should take precedence
            over the global setting through meta elements.
        -->
        <xsl:variable name="css">
            <xsl:call-template name="getCss">
                <xsl:with-param name="obj" select="$obj"/>
            </xsl:call-template>
        </xsl:variable>
        <!-- 
            This is a somewhat longish setting of a variable.
            There two possible cases:  the possible setting in the object element itself should take precedence
            over the global setting through meta elements.
        -->
        <xsl:variable name="fromURL">
            <xsl:call-template name="getFromURL">
                <xsl:with-param name="obj" select="$obj"/>
            </xsl:call-template>
        </xsl:variable>
        <!-- 
            This is a somewhat longish setting of a variable.
            There four possible cases: either the css URL should be relative or absolute and,
            in both cases, the possible setting in the object element itself should take precedence
            over the global setting through meta elements.
        -->
        <xsl:variable name="toURL">
            <xsl:call-template name="getToURL">
                <xsl:with-param name="obj" select="$obj"/>
									<xsl:with-param name="orig" select="'html'"/>									
            </xsl:call-template>
        </xsl:variable>
        <!-- 
            The basename of the svg file name is necessary; this is generated through a 
            separate recursive function
        -->
        <xsl:variable name="name">
            <xsl:call-template name="basename">
                <xsl:with-param name="in" select="$fileName"/>
            </xsl:call-template>
        </xsl:variable>
        <!-- 
            The target file name for the copied svg file
        -->
        <xsl:variable name="target" select="concat($svgs,$name)"/>
        <xsl:message>Copying svg file <xsl:value-of select="$name"/> to <xsl:value-of select="$target"/>
        </xsl:message>
        <!-- The real processing starts here -->
        <!-- ******************************************************************************** -->
        <!-- FOR XSLT 2.0: the element below should be <xsl:result-document href="{$target}>" -->   
        <!-- ******************************************************************************** -->
        <saxon:output href="{$target}">
            <xsl:text>&#xA;</xsl:text>
            <!-- If the CSS URL is meaningful, it is added to the svg output -->
            <xsl:if test="string-length($css)!=0">
                <xsl:processing-instruction name="xml-stylesheet">href="<xsl:value-of select="$css"/>" type="text/css"</xsl:processing-instruction>
                <xsl:text>&#xA;</xsl:text>
            </xsl:if>
            <xsl:for-each select="document($fileName)/svg">
                <!-- The for-each clause is actually unnecessary, we know that there is only one svg element... -->
                <svg xmlns="http://www.w3.org/2000/svg">
                    <!-- get all the attributes in place -->
                    <xsl:for-each select="@*">
                        <xsl:copy/>
                    </xsl:for-each>
                    <!-- Get all the child elements managed by a separate (recursive) function -->
                    <xsl:for-each select="child::*">
                        <xsl:call-template name="svg">
                            <xsl:with-param name="element" select="."/>
                            <xsl:with-param name="fromURL" select="$fromURL"/>
                            <xsl:with-param name="toURL" select="$toURL"/>
                        </xsl:call-template>
                    </xsl:for-each>
                </svg>
            </xsl:for-each>
        <!-- ******************************************************************************* -->
        <!-- FOR XSLT 2.0: the element below should be </xsl:result-document>"               -->
        <!-- ******************************************************************************* -->
        </saxon:output>
    </xsl:template>
    <!-- ================================================================== -->
    <!--                          Include an image statement                -->
    <!-- ================================================================== -->
    <xsl:template name="inclImg">
        <xsl:param name="object"/>
        <xsl:param name="name"/>
        <xsl:variable name="title" select="$object/@title"/>
        <xsl:variable name="float">
            <xsl:call-template name="returnAttr">
                <xsl:with-param name="key" select="'float'"/>
                <xsl:with-param name="element" select="$object"/>
            </xsl:call-template>
        </xsl:variable>
        <xsl:variable name="sizes">
            <xsl:call-template name="getImgSizes">
                <xsl:with-param name="object" select="$object"/>
            </xsl:call-template>
        </xsl:variable>
        <xsl:variable name="w" select="substring-before($sizes,';')"/>
        <xsl:variable name="h" select="substring-after($sizes,';')"/>
        <xsl:variable name="finalW" select="substring-after($w,':')"/>
        <xsl:variable name="finalH" select="substring-after($h,':')"/>

        <xsl:choose>
            <xsl:when test="string-length($float) = 0">
                <div class="center" xmlns="http://www.w3.org/1999/xhtml">
                    <img src="{$name}">
                        <xsl:attribute name="alt">
                            <xsl:choose>
                                <xsl:when test="string-length($title) != 0">
                                    <xsl:value-of select="$title"/>                                
                                </xsl:when>
                                <xsl:otherwise>image replacement for an SVG file...</xsl:otherwise>
                            </xsl:choose>
                        </xsl:attribute>
                        <!-- <xsl:attribute name="style"><xsl:value-of select="$sizes"/></xsl:attribute> -->
                    </img>
                </div>
            </xsl:when>
            <xsl:otherwise>
                <img src="{$name}">
                    <!-- <xsl:attribute name="style"><xsl:value-of select="concat('float:',$float,';',$sizes)"/></xsl:attribute> -->
                    <xsl:attribute name="style"><xsl:value-of select="concat('float:',$float)"/></xsl:attribute>
                    <xsl:attribute name="alt">
                        <xsl:choose>
                            <xsl:when test="string-length($title) != 0">
                                <xsl:value-of select="$title"/>                                
                            </xsl:when>
                            <xsl:otherwise>image replacement for an SVG file...</xsl:otherwise>
                        </xsl:choose>
                    </xsl:attribute>
                </img>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    <!-- ================================================================== -->
    <!--                  Creation of the title page content                -->
    <!-- ================================================================== -->
    <xsl:template name="titlePageContent">
        <div class="slideContent" xmlns="http://www.w3.org/1999/xhtml">
           <p class="TitleMajor"><xsl:value-of select="$OverallTitle"/></p>
           <p class="TitleMinor"><xsl:value-of select="$Author"/></p>
            <xsl:if test="$Event">
                <p class="TitleMinor2"><xsl:value-of select="$Event"/></p>                    
            </xsl:if>
           <p class="TitleMinor3"><xsl:value-of select="$Date"/></p>
            <xsl:if test="$SlidesURL">
                <p class="TitleMinor4"><xsl:value-of select="$SlidesURL"/></p>                    
            </xsl:if>           
        </div>
    </xsl:template>
    <!-- ================================================================== -->
    <!--                               SVG Object                           -->
    <!-- ================================================================== -->
    <!-- 
        This template essentially copies the object and its content, except
        that it changes the file name on the fly.
        The latter step happens if the SVGMove governing parameter
        demands it.
    -->
    <xsl:template match="x:object[@type='image/svg+xml']">
        <xsl:variable name="fileName" select="@data"/>
        <xsl:variable name="name">
            <xsl:call-template name="basename">
                <xsl:with-param name="in" select="$fileName"/>
            </xsl:call-template>
        </xsl:variable>
        <xsl:variable name="bname" select="substring-before($name,'.')"/>
        <xsl:variable name="imgtarget">
            <xsl:choose>
                <xsl:when test="@slide:svgs">
                    <xsl:value-of select="concat(@slide:svgs,$bname,'.png')"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="concat($svgs,$bname,'.png')"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>   
        <xsl:choose>
            <xsl:when test="$SVGtoImage = 'replace'">
                <xsl:call-template name="inclImg">
                    <xsl:with-param name="object" select="."/>
                    <xsl:with-param name="name" select="$imgtarget"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:variable name="target" select="concat($svgs,$name)"/>
                <object xmlns="http://www.w3.org/1999/xhtml">
                    <xsl:attribute name="standBy">Loading SVG...</xsl:attribute>
                    <xsl:for-each select="@*">
                        <xsl:copy/>
                    </xsl:for-each>
                    <xsl:if test="$SVGMove='true'">
                        <xsl:attribute name="data">
                            <xsl:value-of select="$target"/>
                        </xsl:attribute>
                    </xsl:if>
                        <xsl:choose>
                            <xsl:when test="$SVGtoImage = 'extend'">
                                <xsl:call-template name="inclImg">
                                    <xsl:with-param name="object" select="."/>
                                    <xsl:with-param name="name" select="$imgtarget"/>
                                </xsl:call-template>
                            </xsl:when>
                            <xsl:otherwise>
                                <xsl:apply-templates/>
                            </xsl:otherwise>
                        </xsl:choose>
                </object>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    
    <!-- ================================================================== -->
    <!--                      Generation of the logo                        -->
    <!-- ================================================================== -->
    <xsl:template name="UserLogo">
        <xsl:if test="string-length($Logo) != 0">
          <xsl:choose>
              <xsl:when test="string-length($LogoURI) = 0">
                   <img src="{$Logo}" alt="Logo" xmlns="http://www.w3.org/1999/xhtml"/>
              </xsl:when>
              <xsl:otherwise>
                  <a href="{$LogoURI}" xmlns="http://www.w3.org/1999/xhtml">
                       <img src="{$Logo}" alt="Logo"/>
                  </a>
              </xsl:otherwise>
          </xsl:choose>
        </xsl:if>
    </xsl:template>

    <!-- ================================================================== -->
    <!--                      Generation of the signature                   -->
    <!-- ================================================================== -->
    <xsl:template name="signature">
        <xsl:param name="slideNumber"/>
        <div class="signature" xmlns="http://www.w3.org/1999/xhtml">
            <table summary="One line table with author on the left, date in the middle, and # of slide on the right">
                <tr>
                    <td class="authorSig">
                        <xsl:call-template name="sigList">
                            <xsl:with-param name="aFirst" select="substring-before(concat($Author,','),',')"/>
                            <xsl:with-param name="uFirst" select="normalize-space(substring-before(concat($AuthorURL,','),','))"/>
                            <xsl:with-param name="aRest" select="substring-after(concat($Author,','),',')"/>
                            <xsl:with-param name="uRest" select="normalize-space(substring-after(concat($AuthorURL,','),','))"/>
                            <xsl:with-param name="target" select="'html'"/>
                        </xsl:call-template>                        
						<xsl:if test="string-length($Copyright) != 0">
							<xsl:choose>
								<xsl:when test="string-length($CopyrightLink) != 0">
									<a href="{$CopyrightLink}"><br/><xsl:value-of select="$Copyright"/></a>
								</xsl:when>
								<xsl:otherwise><br/><xsl:value-of select="$Copyright"/></xsl:otherwise>
							</xsl:choose>
						</xsl:if>
                    </td>
                    <td class="dateSig">
                        <xsl:value-of select="$Date"/>
                    </td>
                    <td class="slideNumberSig"><xsl:value-of select="$slideNumber"/> (<xsl:value-of select="$NumberOfSlides"/>)
                    </td>
                </tr>
            </table>
        </div>
    </xsl:template>
    <!-- ================================================================== -->
    <!--                          Boilerplates                              -->
    <!-- ================================================================== -->
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="x:div[@class = 'svgOnly']"/>
</xsl:stylesheet>
