<?xml version="1.0"?>

<!DOCTYPE xsl:stylesheet [
<!ENTITY copy
  '<xsl:text disable-output-escaping="yes">&amp;copy;</xsl:text>'>
]>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.1" xmlns="http://www.w3.org/1999/xhtml">

    <xsl:param name="output-dir" select="'website'" />

    <xsl:output
        name="output-style"
        method="xhtml"
        doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
        doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
        indent="no"
        omit-xml-declaration="no" />

    <xsl:output
        method="xml"
        indent="yes" />

    <xsl:template match="/">
        <website xmlns="">
            <pages>
                <xsl:apply-templates select="website[@id]|section[@id]" mode="create-files" />
            </pages>
            <slides>
                <xsl:apply-templates select="//slide" mode="create-files" />
            </slides>
        </website>
    </xsl:template>

    <xsl:template match="website[@id]|section[@id]" mode="create-files">
        <xsl:variable name="directory"><xsl:for-each select="ancestor-or-self::section[@id]"><xsl:value-of select="@id" />/</xsl:for-each></xsl:variable>
        <xsl:variable name="filename">index.html</xsl:variable>
        <xsl:variable name="root"><xsl:for-each select="ancestor-or-self::section[@id]">../</xsl:for-each></xsl:variable>

        <page xmlns="">
            <xsl:value-of select="$output-dir" />/<xsl:value-of select="$directory" /><xsl:value-of select="$filename" />
        </page>

        <xsl:result-document href="{$output-dir}/{$directory}{$filename}" format="output-style">

            <html xml:lang="en" lang="en">
                <head>
                    <title><xsl:value-of select="/website/@title" /><xsl:if test="not(self::website)"> - <xsl:value-of select="@name" /></xsl:if></title>
                    <link rel="stylesheet" title="User Defined" type="text/css" href="{$root}userstyle.css" />
                    <link rel="alternate stylesheet" title="Blue Background" type="text/css" href="{$root}blue.css" />
                    <link rel="alternate stylesheet" title="Green Background" type="text/css" href="{$root}green.css" />
                    <link rel="icon" type="image/x-icon" href="{$root}favicon.ico" />
                    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                    <xsl:apply-templates select="meta" mode="page" />
                </head>
                <body>
                    <table class="main">
                        <tr>
                            <td class="title" colspan="3"><a href="{$root}"><img src="{$root}images/logo.png" alt="" width="90" height="92" /><img src="{$root}images/netbeetle.png" alt="netbeetle.com" width="348" height="49" /></a></td>
                        </tr>
                        <tr>
                            <td class="menu dark" rowspan="3">

                                <div class="text">Menu:</div>

                                <xsl:apply-templates select="/" mode="menu">
                                    <xsl:with-param name="current" select="." />
                                    <xsl:with-param name="root" select="$root" />
                                    <xsl:with-param name="match" select="/website/section[@id]" />
                                </xsl:apply-templates>
                                <a href="mailto:josh@netbeetle.com" class="link level1">Email Me</a>
                                <p class="center"><a href="http://www.cafeshops.com/netbeetle/"><img src="{$root}images/store.png" width="100" height="55" alt="Visit the netbeetle.com Online Store" /></a></p>

                            </td>
                            <td class="linkbar medium">

                                <xsl:for-each select="ancestor::website[@id]|ancestor::section[@id]">
                                    <xsl:variable name="directory2"><xsl:for-each select="ancestor-or-self::section[@id]"><xsl:value-of select="@id" />/</xsl:for-each></xsl:variable>
                                    <a href="{$root}{$directory2}"><xsl:value-of select="@name" /></a><xsl:text> / </xsl:text>
                                </xsl:for-each>
                                <xsl:value-of select="@name" />

                            </td>
                            <td class="sidebar dark" rowspan="3"></td>
                        </tr>
                        <tr>
                            <td class="body light">
                                <h2><xsl:value-of select="@name" /></h2>

                                <xsl:apply-templates select="." mode="page" />
                            </td>
                        </tr>
                        <tr>
                            <td class="linkbar medium right">
                                <a href="http://validator.w3.org/check/referer"><img src="{$root}images/valid-xhtml10.png" alt="Valid XHTML 1.0!" style="margin:3px;border:0;width:88px;height:31px" /></a>
                                <a href="http://jigsaw.w3.org/css-validator/check/referer"><img src="{$root}images/valid-css.png" alt="Valid CSS!" style="margin:3px;border:0;width:88px;height:31px" /></a>
                            </td>
                        </tr>
                        <tr>
                            <td class="copyright" colspan="3">Copyright &copy; 2001-2004 Joshua Beitelspacher.</td>
                        </tr>
                    </table>

                    <p>
                        <script type="text/javascript">
                            google_ad_client = "pub-3520555367624904";
                            google_ad_width = 728;
                            google_ad_height = 90;
                            google_ad_format = "728x90_as";
                            google_ad_channel ="";
                            google_color_border = "336699";
                            google_color_bg = "FFFFFF";
                            google_color_link = "0000FF";
                            google_color_url = "008000";
                            google_color_text = "000000";
                        </script>
                        <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
                        </script>
                    </p>
                </body>
            </html>

        </xsl:result-document>
        <xsl:apply-templates select="section[@id]" mode="create-files" />
    </xsl:template>

    <xsl:template match="website[@id]|section[@id]" mode="menu">
        <xsl:param name="current" />
        <xsl:param name="root" />
        <xsl:param name="match" />
        <xsl:for-each select="$match">

            <xsl:variable name="directory"><xsl:for-each select="ancestor-or-self::section[@id]"><xsl:value-of select="@id" />/</xsl:for-each></xsl:variable>

            <xsl:choose>
                <xsl:when test="$current=.">
                    <div>
                        <xsl:attribute name="class">level<xsl:value-of select="count(ancestor-or-self::section[@id])" /> current</xsl:attribute>
                        <xsl:value-of select="@name" />
                    </div>
                </xsl:when>
                <xsl:otherwise>
                    <a href="{$root}{$directory}">
                        <xsl:choose>
                            <xsl:when test="$current=./*">
                                <xsl:attribute name="class">link level<xsl:value-of select="count(ancestor::website[@id]|ancestor::section[@id])" /> parent</xsl:attribute>
                            </xsl:when>
                            <xsl:when test="$current=.//*">
                                <xsl:attribute name="class">link level<xsl:value-of select="count(ancestor::website[@id]|ancestor::section[@id])" /> ancestor</xsl:attribute>
                            </xsl:when>
                            <xsl:otherwise>
                                <xsl:attribute name="class">link level<xsl:value-of select="count(ancestor::website[@id]|ancestor::section[@id])" /></xsl:attribute>
                            </xsl:otherwise>
                        </xsl:choose>
                        <xsl:value-of select="@name" />
                    </a>
                </xsl:otherwise>
            </xsl:choose>

            <xsl:if test="$current=. or $current=.//*">
                <xsl:apply-templates select="." mode="menu">
                    <xsl:with-param name="current" select="$current" />
                    <xsl:with-param name="root" select="$root" />
                    <xsl:with-param name="match" select="section[@id]" />
                </xsl:apply-templates>
            </xsl:if>

        </xsl:for-each>
    </xsl:template>

    <xsl:template match="meta" mode="page">
        <meta name="{@name}" content="{@content}" />
    </xsl:template>

    <xsl:template match="website[@id]|section[@id]" mode="page">
        <xsl:apply-templates select="*" mode="inline" />
    </xsl:template>

    <xsl:template match="*" mode="inline">
        <xsl:apply-templates mode="inline" />
    </xsl:template>

    <xsl:template match="description" mode="inline"></xsl:template>

    <xsl:template match="website[@id]|section[@id]" mode="inline">
        <xsl:variable name="root"><xsl:for-each select="ancestor::section[@id]">../</xsl:for-each></xsl:variable>
        <xsl:variable name="directory"><xsl:if test="self::section[@id]"><xsl:value-of select="@id" />/</xsl:if></xsl:variable>
        <p class="indent">
            <a href="{$directory}">
                <xsl:if test="description[@image-src]">
                    <img src="{$directory}{description/@image-src}" class="description" alt="{@name}" />
                </xsl:if>
                <b><xsl:value-of select="@name" /></b>
            </a><xsl:if test="@flag"> - <xsl:value-of select="@flag" /></xsl:if><br />
            <xsl:apply-templates select="description/*|description/text()" mode="inline">
                <xsl:with-param name="root" select="$root" />
            </xsl:apply-templates>
        </p>
    </xsl:template>

    <xsl:template match="intro" mode="inline">
        <p><xsl:apply-templates mode="inline" /></p>
    </xsl:template>

    <xsl:template match="para" mode="inline">
        <p class="indent"><xsl:apply-templates mode="inline" /></p>
    </xsl:template>

    <xsl:template match="emph" mode="inline">
        <i><xsl:apply-templates mode="inline" /></i>
    </xsl:template>

    <xsl:template match="head" mode="inline">
        <xsl:choose>
            <xsl:when test="parent::para">
                <b><xsl:apply-templates mode="inline" /></b><br />
            </xsl:when>
            <xsl:otherwise>
                <p class="indent"><b><xsl:apply-templates mode="inline" /></b></p>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <xsl:template match="image" mode="inline">
        <xsl:param name="root"><xsl:for-each select="ancestor::section[@id]|ancestor::slide-show">../</xsl:for-each></xsl:param>
        <img src="{@src}" alt="" class="plain">
            <xsl:if test="starts-with(@src,'$')">
                <xsl:variable name="src" select="substring-after(@src,'$')" />
                <xsl:attribute name="src"><xsl:value-of select="$root" /><xsl:value-of select="$src" /></xsl:attribute>
            </xsl:if>
            <xsl:if test="@width">
                <xsl:attribute name="width"><xsl:value-of select="@width" /></xsl:attribute>
            </xsl:if>
            <xsl:if test="@height">
                <xsl:attribute name="height"><xsl:value-of select="@height" /></xsl:attribute>
            </xsl:if>
            <xsl:if test="@alt">
                <xsl:attribute name="alt"><xsl:value-of select="@alt" /></xsl:attribute>
            </xsl:if>
        </img>
    </xsl:template>

    <xsl:template match="link" mode="inline">
        <xsl:param name="root"><xsl:for-each select="ancestor::section[@id]|ancestor::slide-show">../</xsl:for-each></xsl:param>
        <xsl:choose>
            <xsl:when test="starts-with(@href,'#')">
                <xsl:variable name="href" select="substring-after(@href,'#')" />
                <xsl:variable name="target" select="//website[@id=$href]|//section[@id=$href]" />
                <xsl:variable name="directory"><xsl:for-each select="$target/ancestor-or-self::section[@id]"><xsl:value-of select="@id" />/</xsl:for-each></xsl:variable>
                <a href="{$root}{$directory}">
                    <xsl:apply-templates mode="inline" />
                </a>
            </xsl:when>
            <xsl:when test="starts-with(@href,'$')">
                <xsl:variable name="href" select="substring-after(@href,'$')" />
                <a href="{$root}{$href}">
                    <xsl:apply-templates mode="inline" />
                </a>
            </xsl:when>
            <xsl:otherwise>
                <a href="{@href}">
                    <xsl:apply-templates mode="inline" />
                </a>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <xsl:template match="program" mode="inline">
        <p class="indent">
            <xsl:if test="description[@image-src]">
                <img src="{description/@image-src}" style="float: left;" alt="" />
            </xsl:if>
            <b><xsl:value-of select="@name" /></b><br />
            <xsl:apply-templates select="description/*|description/text()" mode="inline" /><br />
            <xsl:for-each select="files">
                <xsl:if test="@type"><xsl:value-of select="@type" />: </xsl:if>
                <xsl:for-each select="file">
                    <a href="{@href}"><xsl:apply-templates select="text()" mode="inline" /></a>
                    <xsl:if test="position()!=last()"> | </xsl:if>
                </xsl:for-each>
                <xsl:if test="position()!=last()"><br /></xsl:if>
            </xsl:for-each>
        </p>
    </xsl:template>

    <xsl:template match="publication" mode="inline">
        <p class="indent">
            <xsl:if test="description[@image-src]">
                <img src="{description/@image-src}" style="float: left;" alt="" />
            </xsl:if>
            <a href="{@href}"><b><xsl:value-of select="title" /></b></a><br />
            <xsl:if test="date"><xsl:value-of select="date" /> - </xsl:if>
            <xsl:apply-templates select="description/*|description/text()" mode="inline" />
        </p>
    </xsl:template>

    <xsl:template match="code" mode="inline">
        <code>
            <xsl:copy-of select="*|text()" />
        </code>
    </xsl:template>

    <xsl:template match="slide-show" mode="inline">
        <table class="indent medium slidetable" cellspacing="0">
            <xsl:if test="@name">
                <tr>
                    <td class="title dark" colspan="3"><xsl:value-of select="@name" /></td>
                </tr>
            </xsl:if>
            <xsl:for-each select="slide[position() mod 3=1]">
                <tr>
                    <xsl:variable name="href">slides/<xsl:call-template name="strip-filename"><xsl:with-param name="string" select="@href" /></xsl:call-template>.html</xsl:variable>
                    <td><a href="{$href}"><img src="{@thumbnail}" alt="{title/text()}" /></a></td>
                    <xsl:for-each select="following-sibling::slide[position()&lt;3]">
                        <xsl:variable name="href2">slides/<xsl:call-template name="strip-filename"><xsl:with-param name="string" select="@href" /></xsl:call-template>.html</xsl:variable>
                        <td><a href="{$href2}"><img src="{@thumbnail}" alt="{title/text()}" /></a></td>
                    </xsl:for-each>
                </tr>
            </xsl:for-each>
        </table>
    </xsl:template>

    <xsl:template match="slide" mode="create-files">
        <xsl:variable name="directory"><xsl:for-each select="ancestor-or-self::section[@id]"><xsl:value-of select="@id" />/</xsl:for-each>slides/</xsl:variable>
        <xsl:variable name="filename"><xsl:call-template name="strip-filename"><xsl:with-param name="string" select="@href" /></xsl:call-template>.html</xsl:variable>
        <xsl:variable name="root"><xsl:for-each select="ancestor::section[@id]">../</xsl:for-each>../</xsl:variable>

        <slide xmlns="">
            <xsl:value-of select="$output-dir" />/<xsl:value-of select="$directory" /><xsl:value-of select="$filename" />
        </slide>

        <xsl:variable name="imageroot">../</xsl:variable>

        <xsl:variable name="previous"><xsl:if test="position()!=1"><xsl:call-template name="strip-filename"><xsl:with-param name="string" select="preceding-sibling::slide[position()=1]/@href" /></xsl:call-template></xsl:if><xsl:if test="not(preceding-sibling::slide)"><xsl:call-template name="strip-filename"><xsl:with-param name="string" select="following-sibling::slide[position()=last()]/@href" /></xsl:call-template></xsl:if>.html</xsl:variable>

        <xsl:variable name="next"><xsl:if test="position()!=last()"><xsl:call-template name="strip-filename"><xsl:with-param name="string" select="following-sibling::slide[position()=1]/@href" /></xsl:call-template></xsl:if><xsl:if test="not(following-sibling::slide)"><xsl:call-template name="strip-filename"><xsl:with-param name="string" select="preceding-sibling::slide[position()=last()]/@href"/></xsl:call-template></xsl:if>.html</xsl:variable>
        <xsl:variable name="back"><xsl:value-of select="$imageroot" /></xsl:variable>
        <xsl:result-document href="{$output-dir}/{$directory}{$filename}" format="output-style">

            <html xml:lang="en" lang="en">
                <head>
                    <title><xsl:value-of select="/website/@title" /> - <xsl:value-of select="parent::slide-show/parent::*/@name" /> - <xsl:value-of select="title/text()" /></title>
                    <link rel="stylesheet" title="User Defined" type="text/css" href="{$root}userstyle.css" />
                    <link rel="alternate stylesheet" title="Blue Background" type="text/css" href="{$root}blue.css" />
                    <link rel="alternate stylesheet" title="Green Background" type="text/css" href="{$root}green.css" />
                    <link rel="icon" type="image/x-icon" href="{$root}favicon.ico" />
                    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                </head>

                <body>
                    <table class="slide">
                        <tr>
                            <td class="photo light" colspan="2">
                                <img src="{$imageroot}{@href}" alt="{title/text()}" />
                            </td>
                        </tr>
                        <tr>
                            <td class="caption light" colspan="2">
                                <b><xsl:apply-templates select="title/text()|title/*" mode="inline" /></b><br />
                                <xsl:apply-templates select="caption/text()|caption/*" mode="inline" />
                            </td>
                        </tr>
                        <tr>
                            <td class="menu left dark">
                                <a href="{$previous}">Previous</a>
                            </td>
                            <td class="menu right dark">
                                <a href="{$next}">Next</a>
                            </td>
                        </tr>
                        <tr>
                            <td class="menu center dark" colspan="2">
                                <a href="{$back}">Back to <xsl:value-of select="parent::slide-show/parent::*/@name" /></a>
                            </td>
                        </tr>
                    </table>
                </body>
            </html>
        </xsl:result-document>
    </xsl:template>

    <xsl:template name="strip-filename">
        <xsl:param name="string" />
        <xsl:choose>
            <xsl:when test="contains($string,'/')">
                <xsl:call-template name="strip-filename">
                    <xsl:with-param name="string" select="substring-after($string,'/')" />
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="substring-before($string,'.')" />
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <xsl:template match="xhtml:html-content" xmlns:xhtml="http://www.w3.org/1999/xhtml" mode="inline">
        <xsl:copy-of select="*|text()" />
    </xsl:template>

</xsl:stylesheet>