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

<!-- ============== Navigation =========================================== -->

<!-- 
MAIN_NAVI is the list of navigation buttons. Each element is represented as
a list element, containing a link, containing a button. The button text is
ripped from the title attribute. So the general structure is:
<li>
	<a href="...">
		<button type="button">
			Text
		</button>
	</a>
</li>
For compatibility with Internet Explorer the href is repeated in the button
element as javascript event handler. 
-->
<xsl:template match="h:link" mode="MAIN_NAVI">
	<xsl:element name="li">
		<xsl:element name="a">
			<xsl:apply-templates select="@*"/>
			<xsl:element name="button">
				<xsl:attribute name="type">
					<xsl:text>button</xsl:text>
				</xsl:attribute>
				<xsl:if test="$DSTTYPE='html'">
					<xsl:attribute name="onclick">
						<xsl:text>self.location.href='</xsl:text>
						<xsl:value-of select="@href"/>
						<xsl:if test="@type='automatic'">
							<xsl:text>.</xsl:text>
							<xsl:value-of select="$DSTTYPE"/>
							<xsl:text>.</xsl:text>
							<xsl:value-of select="$LANG"/>
						</xsl:if>
						<xsl:text>'</xsl:text>
					</xsl:attribute>
					<xsl:attribute name="onkeypress">
						<xsl:text>self.location.href='</xsl:text>
						<xsl:value-of select="@href"/>
						<xsl:if test="@type='automatic'">
							<xsl:text>.</xsl:text>
							<xsl:value-of select="$DSTTYPE"/>
							<xsl:text>.</xsl:text>
							<xsl:value-of select="$LANG"/>
						</xsl:if>
						<xsl:text>'</xsl:text>
					</xsl:attribute>
				</xsl:if>
				<xsl:value-of select="@title"/>
			</xsl:element>
		</xsl:element>
	</xsl:element>
</xsl:template>

<xsl:template match="h:link" mode="PREV_NEXT">
	<xsl:element name="a">
		<xsl:apply-templates select="@*"/>
		<xsl:element name="button">
			<xsl:attribute name="type">
				<xsl:text>button</xsl:text>
			</xsl:attribute>
			<xsl:if test="$DSTTYPE='html'">
				<xsl:attribute name="onclick">
					<xsl:text>self.location.href='</xsl:text>
					<xsl:value-of select="@href"/>
					<xsl:if test="@type='automatic'">
						<xsl:text>.</xsl:text>
						<xsl:value-of select="$DSTTYPE"/>
						<xsl:text>.</xsl:text>
						<xsl:value-of select="$LANG"/>
					</xsl:if>
					<xsl:text>'</xsl:text>
				</xsl:attribute>
				<xsl:attribute name="onkeypress">
					<xsl:text>self.location.href='</xsl:text>
					<xsl:value-of select="@href"/>
					<xsl:if test="@type='automatic'">
						<xsl:text>.</xsl:text>
						<xsl:value-of select="$DSTTYPE"/>
						<xsl:text>.</xsl:text>
						<xsl:value-of select="$LANG"/>
					</xsl:if>
					<xsl:text>'</xsl:text>
				</xsl:attribute>
			</xsl:if>
			<xsl:attribute name="class">
				<xsl:value-of select="@rel"/>
			</xsl:attribute>
			<xsl:element name="img">
				<xsl:attribute name="src">
					<xsl:text>/myicons/</xsl:text>
					<xsl:value-of select="@rel"/>
					<xsl:text>.png</xsl:text>
				</xsl:attribute>
				<xsl:attribute name="alt">
					<xsl:value-of select="@rel"/>
				</xsl:attribute>
			</xsl:element>
		</xsl:element>
	</xsl:element>
</xsl:template>

<!--
Additional navigation in the header. These navigational elements are each a
list element containing the link, with the link text from the title attribute.
As opposed to the MAIN_NAVI, these elements are not put into a button.
The link to "this" does not get a link, but only a plain text element within
the list element. Usage is mainly for the skiplink and the breadcrumb navi.
-->
<xsl:template match="h:link" mode="HEADNAVI">
	<xsl:element name="li">
		<xsl:choose>
			<xsl:when test="@rel='this'">
				<xsl:value-of select="@title"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:element name="a">
					<xsl:apply-templates select="@*"/>
					<xsl:choose>
						<xsl:when test="@type='application/rss+xml'">
							<xsl:element name="img">
								<xsl:attribute name="src">
									<xsl:value-of select="$DIR"/>
									<xsl:text>myicons/feed</xsl:text>
									<xsl:value-of select="$IMGTYPE"/>
								</xsl:attribute>
								<xsl:attribute name="alt">
									<xsl:value-of select="@title"/>
								</xsl:attribute>
							</xsl:element>
						</xsl:when>
						<xsl:otherwise>
							<xsl:value-of select="@title"/>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:element>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:element>
</xsl:template>

<!--
The license for the footer navi. 
-->
<xsl:template match="h:link[@rel='license']|h:link[@rel='DC.rights']" mode="FOOTER_NAVI">
	<xsl:element name="li">
		<xsl:element name="a">
			<xsl:apply-templates select="@*"/>
			<xsl:element name="img">
				<xsl:attribute name="class">
					<xsl:text>badge</xsl:text>
				</xsl:attribute>
				<xsl:attribute name="src">
					<xsl:choose>
						<xsl:when test="contains(@href,'creativecommons')">
							<xsl:text>http://i.creativecommons.org/l</xsl:text>
							<xsl:value-of select="substring-after(@href,'creativecommons.org/licenses')"/>
							<xsl:text>80x15.png</xsl:text>
						</xsl:when>
						<xsl:otherwise>
							<xsl:value-of select="$DIR"/>
							<xsl:text>badges/</xsl:text>
							<xsl:value-of select="@title"/>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:attribute>
			</xsl:element>
		</xsl:element>
	</xsl:element>
</xsl:template>
<!--
The feed url
-->
<xsl:template match="h:link[@rel='alternate'][@type]" mode="FOOTER_NAVI">
	<xsl:element name="li">
		<xsl:element name="a">
			<xsl:apply-templates select="@*"/>
			<xsl:element name="img">
				<xsl:attribute name="class">
					<xsl:text>badge</xsl:text>
				</xsl:attribute>
				<xsl:attribute name="src">
					<xsl:value-of select="$DIR"/>
					<xsl:text>badges/</xsl:text>
					<xsl:value-of select="@title"/>
					<xsl:value-of select="$IMGTYPE"/>
				</xsl:attribute>
				<xsl:attribute name="alt">
					<xsl:value-of select="@title"/>
				</xsl:attribute>
			</xsl:element>
		</xsl:element>
	</xsl:element>
</xsl:template>

<xsl:template match="h:link[@rel='vote-for']" mode="FOOTER_NAVI">
	<xsl:element name="li">
		<xsl:element name="a">
			<xsl:apply-templates select="@*"/>
			<xsl:element name="img">
				<xsl:attribute name="class">
					<xsl:text>badge</xsl:text>
				</xsl:attribute>
				<xsl:attribute name="src">
					<xsl:value-of select="$DIR"/>
					<xsl:text>badges/</xsl:text>
					<xsl:value-of select="@rel"/>
					<xsl:text>.</xsl:text>
					<xsl:value-of select="@title"/>
					<xsl:value-of select="$IMGTYPE"/>
				</xsl:attribute>
				<xsl:attribute name="alt">
					<xsl:value-of select="@rel"/>
					<xsl:text> </xsl:text>
					<xsl:value-of select="@title"/>
				</xsl:attribute>
			</xsl:element>
		</xsl:element>
	</xsl:element>
</xsl:template>
<!-- 
FOOTER_NAVI is the list of navigational elements in the footer. It is visually
similar to the MAIN_NAVI, but instead of a button the element is a small picture,
a socalled badge. The name of the image element is the value of the title attribute,
and the alternate text is the title attribute.
-->
<xsl:template match="h:link[@rel='meta']" mode="FOOTER_NAVI">
	<xsl:element name="li">
		<xsl:element name="a">
			<xsl:apply-templates select="@*"/>
			<xsl:element name="img">
				<xsl:attribute name="class">
					<xsl:text>badge</xsl:text>
				</xsl:attribute>
				<xsl:attribute name="src">
					<xsl:value-of select="$DIR"/>
					<xsl:choose>
						<xsl:when test="@title and not(@title='')">
							<xsl:text>badges/</xsl:text>
							<xsl:value-of select="@title"/>
						</xsl:when>
						<xsl:otherwise>
							<xsl:text>badges/</xsl:text>
							<xsl:value-of select="@rel"/>
						</xsl:otherwise>
					</xsl:choose>
					<xsl:value-of select="$IMGTYPE"/>
				</xsl:attribute>
				<xsl:attribute name="alt">
					<xsl:value-of select="@title"/>
				</xsl:attribute>
			</xsl:element> <!-- img (badge) -->
		</xsl:element>
	</xsl:element>
</xsl:template>

<xsl:template match="h:meta[@name='description']" mode="FOOTER_NAVI"/>
<xsl:template match="h:meta[@name='geo.region']" mode="FOOTER_NAVI"/>
<xsl:template match="h:meta[@name='geo.placename']" mode="FOOTER_NAVI"/>
<xsl:template match="h:meta[@name='microid']" mode="FOOTER_NAVI"/>
<xsl:template match="h:link[@rel='this']" mode="FOOTER_NAVI"/>
<xsl:template match="h:link[@rel='top']" mode="FOOTER_NAVI"/>
<xsl:template match="h:link[@rel='up']" mode="FOOTER_NAVI"/>
<xsl:template match="h:link[@rel='section']" mode="FOOTER_NAVI"/>
<xsl:template match="h:link[@rel='subsection']" mode="FOOTER_NAVI"/>
<xsl:template match="h:link[@rel='chapter']" mode="FOOTER_NAVI"/>
<xsl:template match="h:link[@rel='contents']" mode="FOOTER_NAVI"/>
<xsl:template match="h:link[@rel='start']" mode="FOOTER_NAVI"/>
<xsl:template match="h:link[@rel='prev']" mode="FOOTER_NAVI"/>
<xsl:template match="h:link[@rel='next']" mode="FOOTER_NAVI"/>
<xsl:template match="h:link[@rel='last']" mode="FOOTER_NAVI"/>

<xsl:template match="*[@class='vcard']" mode="FOOTER_NAVI">
	<xsl:element name="li">
		<xsl:element name="a">
			<xsl:attribute name="rel"><xsl:text>tag</xsl:text></xsl:attribute>
			<xsl:attribute name="href">
				<xsl:text>http://microformats.org/wiki/hcard</xsl:text>
			</xsl:attribute>
			<xsl:element name="img">
				<xsl:attribute name="class"><xsl:text>badge</xsl:text></xsl:attribute>
				<xsl:attribute name="src">
					<xsl:value-of select="$DIR"/>
					<xsl:text>badges/hcard.png</xsl:text>
				</xsl:attribute>
				<xsl:attribute name="alt"><xsl:text>hCard ready</xsl:text></xsl:attribute>
			</xsl:element>
		</xsl:element>
	</xsl:element>
</xsl:template>


<xsl:template match="h:meta[@name='geo.position']" mode="FOOTER_NAVI">
	<xsl:element name="li">
		<xsl:attribute name="class"><xsl:text>geo</xsl:text></xsl:attribute>
		<xsl:element name="span">
			<xsl:attribute name="class"><xsl:text>latitude</xsl:text></xsl:attribute>
			<xsl:value-of select="normalize-space(substring-before(@content,';'))"/>
		</xsl:element>
		<xsl:text>,</xsl:text>
		<xsl:element name="span">
			<xsl:attribute name="class"><xsl:text>longitude</xsl:text></xsl:attribute>
			<xsl:value-of select="normalize-space(substring-after(@content,';'))"/>
		</xsl:element>
<!-- http://maps.google.com/maps?ll=49.6941,8.8609&q=49.6941,8.8609 -->
		<xsl:element name="a">
			<xsl:attribute name="href">
				<xsl:text>http://maps.google.com/maps?ll=</xsl:text>
				<xsl:value-of select="normalize-space(substring-before(@content,';'))"/>
				<xsl:text>,</xsl:text>
				<xsl:value-of select="normalize-space(substring-after(@content,';'))"/>
				<xsl:text>&amp;q=</xsl:text>
				<xsl:value-of select="normalize-space(substring-before(@content,';'))"/>
				<xsl:text>,</xsl:text>
				<xsl:value-of select="normalize-space(substring-after(@content,';'))"/>
			</xsl:attribute>
			<xsl:attribute name="title"><xsl:text>Google Maps</xsl:text></xsl:attribute>
			<xsl:element name="img">
				<xsl:attribute name="class"><xsl:text>badge</xsl:text></xsl:attribute>
				<xsl:attribute name="src">
					<xsl:value-of select="$DIR"/>
					<xsl:text>badges/geo.png</xsl:text>
				</xsl:attribute>
			</xsl:element>
		</xsl:element>
	</xsl:element>
</xsl:template>

<xsl:template match="h:meta[@name='ICBM']" mode="FOOTER_NAVI">
	<xsl:if test="not(../h:meta[@name='geo.position'])">
		<xsl:element name="li">
			<xsl:attribute name="class"><xsl:text>geo</xsl:text></xsl:attribute>
			<xsl:element name="span">
				<xsl:attribute name="class"><xsl:text>latitude</xsl:text></xsl:attribute>
				<xsl:value-of select="normalize-space(substring-before(@content,','))"/>
			</xsl:element>
			<xsl:text>,</xsl:text>
			<xsl:element name="span">
				<xsl:attribute name="class"><xsl:text>longitude</xsl:text></xsl:attribute>
				<xsl:value-of select="normalize-space(substring-after(@content,','))"/>
			</xsl:element>
<!-- http://maps.google.com/maps?ll=49.6941,8.8609&q=49.6941,8.8609 -->
			<xsl:element name="a">
				<xsl:attribute name="href">
					<xsl:text>http://maps.google.com/maps?ll=</xsl:text>
					<xsl:value-of select="normalize-space(substring-before(@content,','))"/>
					<xsl:text>,</xsl:text>
					<xsl:value-of select="normalize-space(substring-after(@content,','))"/>
					<xsl:text>&amp;q=</xsl:text>
					<xsl:value-of select="normalize-space(substring-before(@content,','))"/>
					<xsl:text>,</xsl:text>
					<xsl:value-of select="normalize-space(substring-after(@content,','))"/>
				</xsl:attribute>
				<xsl:attribute name="title"><xsl:text>Google Maps</xsl:text></xsl:attribute>
				<xsl:element name="img">
					<xsl:attribute name="class"><xsl:text>badge</xsl:text></xsl:attribute>
					<xsl:attribute name="src">
						<xsl:value-of select="$DIR"/>
						<xsl:text>badges/geo.png</xsl:text>
					</xsl:attribute>
				</xsl:element>
			</xsl:element>
		</xsl:element>
	</xsl:if>
</xsl:template>

<xsl:template match="h:*" mode="FOOTER_NAVI">
<!-- DEBUG -->
<!--
	<xsl:value-of select="name()"/>
	<xsl:text>: </xsl:text>
	<xsl:value-of select="@name"/>
-->
</xsl:template>


<xsl:template match="h:link[@rel='this']" mode="ALT_FOOTER">
	<xsl:element name="li">
		<xsl:element name="a">
			<xsl:attribute name="rel">
				<xsl:text>alternate</xsl:text>
			</xsl:attribute>
			<xsl:apply-templates select="@title|@lang"/>
			<xsl:choose>
				<xsl:when test="../@lang=$LANG">	<!-- alternate format -->
					<xsl:choose>
						<xsl:when test="$DSTTYPE='html'">
							<xsl:attribute name="type">
								<xsl:text>application/xhtml+xml</xsl:text>
							</xsl:attribute>
							<xsl:attribute name="href">
								<xsl:value-of select="@href"/>
								<xsl:text>.xhtml.</xsl:text>
								<xsl:value-of select="$LANG"/>
								<xsl:value-of select="$ENCODING"/>
							</xsl:attribute>
							<xsl:element name="img">
								<xsl:attribute name="src">
									<xsl:value-of select="$DIR"/>
									<xsl:text>badges/xhtml11</xsl:text>
									<xsl:value-of select="$IMGTYPE"/>
								</xsl:attribute>
								<xsl:attribute name="alt">
									<xsl:text>xhtml 1.1</xsl:text>
								</xsl:attribute>
								<xsl:attribute name="class">
									<xsl:text>badge</xsl:text>
								</xsl:attribute>
								<xsl:attribute name="title">
									<xsl:text>xhtml version</xsl:text>
								</xsl:attribute>
							</xsl:element>
						</xsl:when>
						<xsl:when test="$DSTTYPE='xhtml'">
							<xsl:attribute name="type">
								<xsl:text>text/html</xsl:text>
							</xsl:attribute>
							<xsl:attribute name="href">
								<xsl:value-of select="@href"/>
								<xsl:text>.html.</xsl:text>
								<xsl:value-of select="$LANG"/>
								<xsl:value-of select="$ENCODING"/>
							</xsl:attribute>
							<xsl:element name="img">
								<xsl:attribute name="src">
									<xsl:value-of select="$DIR"/>
									<xsl:text>badges/html401</xsl:text>
									<xsl:value-of select="$IMGTYPE"/>
								</xsl:attribute>
								<xsl:attribute name="alt">
									<xsl:text>html 4.01</xsl:text>
								</xsl:attribute>
								<xsl:attribute name="class">
									<xsl:text>badge</xsl:text>
								</xsl:attribute>
								<xsl:attribute name="title">
									<xsl:text>html version</xsl:text>
								</xsl:attribute>
							</xsl:element>
						</xsl:when>
					</xsl:choose>
				</xsl:when> <!-- alternate format -->
				<xsl:otherwise>	<!-- alternate language, same format -->
					<xsl:choose>
						<xsl:when test="$DSTTYPE='html'">
							<xsl:attribute name="type">
								<xsl:text>text/html</xsl:text>
							</xsl:attribute>
							<xsl:attribute name="href">
								<xsl:value-of select="@href"/>
								<xsl:text>.html.</xsl:text>
								<xsl:value-of select="../@lang"/>
								<xsl:value-of select="$ENCODING"/>
							</xsl:attribute>
							<xsl:element name="img">
								<xsl:attribute name="src">
									<xsl:value-of select="$DIR"/>
									<xsl:text>badges/</xsl:text>
									<xsl:value-of select="../@lang"/>
									<xsl:value-of select="$IMGTYPE"/>
								</xsl:attribute>
								<xsl:attribute name="alt">
									<xsl:value-of select="../@lang"/>
								</xsl:attribute>
								<xsl:attribute name="class">
									<xsl:text>badge</xsl:text>
								</xsl:attribute>
							</xsl:element>
						</xsl:when>
						<xsl:when test="$DSTTYPE='xhtml'">
							<xsl:attribute name="type">
								<xsl:text>application/xhtml+xml</xsl:text>
							</xsl:attribute>
							<xsl:attribute name="href">
								<xsl:value-of select="@href"/>
								<xsl:text>.xhtml.</xsl:text>
								<xsl:value-of select="../@lang"/>
								<xsl:value-of select="$ENCODING"/>
							</xsl:attribute>
							<xsl:attribute name="hreflang">
								<xsl:value-of select="../@lang"/>
							</xsl:attribute>
							<xsl:element name="img">
								<xsl:attribute name="src">
									<xsl:value-of select="$DIR"/>
									<xsl:text>badges/</xsl:text>
									<xsl:value-of select="../@lang"/>
									<xsl:value-of select="$IMGTYPE"/>
								</xsl:attribute>
								<xsl:attribute name="alt">
									<xsl:value-of select="../@lang"/>
								</xsl:attribute>
								<xsl:attribute name="class">
									<xsl:text>badge</xsl:text>
								</xsl:attribute>
							</xsl:element>
						</xsl:when>
					</xsl:choose>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:element>
	</xsl:element>
</xsl:template>


<!-- 
If the type attribute is "automatic" then it is converted to either
"application/xhtml+xml" for xhtml output, or "text/html" for text output. 
Other types are just copied.
-->
<xsl:template match="@type">
	<xsl:choose>
		<xsl:when test="name(..)='link' or name(..)='a'">
			<xsl:choose>
				<xsl:when test=".='automatic'">
					<xsl:choose>
						<xsl:when test="$DSTTYPE='html'">
							<xsl:attribute name="type">
								<xsl:text>text/html</xsl:text>
							</xsl:attribute>
						</xsl:when>
						<xsl:when test="$DSTTYPE='xhtml'">
							<xsl:attribute name="type">
								<xsl:text>application/xhtml+xml</xsl:text>
							</xsl:attribute>
						</xsl:when>
					</xsl:choose>
				</xsl:when>
				<xsl:otherwise>
					<xsl:copy/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:when>
		<xsl:otherwise>
			<xsl:copy/>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<!--
The href attribute may get an additional ".html" or ".xhtml", depending
on the type attribute of the element, and an additional language extension
-->
<xsl:template match="@href">
	<xsl:attribute name="href">
		<xsl:value-of select="."/>
		<xsl:if test="../@type='automatic'">
			<xsl:text>.</xsl:text>
			<xsl:value-of select="$DSTTYPE"/>
			<xsl:text>.</xsl:text>
			<xsl:value-of select="$LANG"/>
		</xsl:if>
	</xsl:attribute>
</xsl:template>

<!-- 
replace the rel="this" with rel="DC.identifier", prepend the base url
and append the file extensions to the href attribute and set the title
to the documents title
-->
<xsl:template match="h:link[@rel='this']">
	<xsl:element name="link">
		<xsl:attribute name="rel">
			<xsl:text>DC.identifier</xsl:text>
		</xsl:attribute>
		<xsl:attribute name="href">
			<xsl:value-of select="$BASE"/>
			<xsl:text>/</xsl:text>
			<xsl:value-of select="@href"/>
			<xsl:call-template name="DSTEXT"/>
		</xsl:attribute>
		<xsl:attribute name="title">
			<xsl:value-of select="normalize-space(/h:html/h:head[@lang=$LANG]/h:title)"/>
		</xsl:attribute>
	</xsl:element>
	<xsl:element name="link">
		<xsl:apply-templates select="@*" mode="CANONICAL"/>
	</xsl:element>
</xsl:template>

<xsl:template match="@href" mode="CANONICAL">
	<xsl:attribute name="href">
		<xsl:value-of select="."/>
		<xsl:text>.xhtml</xsl:text>
		<xsl:if test="$LANG != ''">
			<xsl:text>.</xsl:text>
			<xsl:value-of select="$LANG"/>
		</xsl:if>
	</xsl:attribute>
</xsl:template>

<xsl:template match="@rel" mode="CANONICAL">
	<xsl:attribute name="rel">
		<xsl:text>canonical</xsl:text>
	</xsl:attribute>
</xsl:template>

<xsl:template match="@type" mode="CANONICAL">
	<xsl:attribute name="type">
		<xsl:text>application/xhtml+xml</xsl:text>
	</xsl:attribute>
</xsl:template>

<xsl:template match="@*" mode="CANONICAL">
	<xsl:copy-of select="."/>
</xsl:template>



</xsl:stylesheet>
