<xsl:stylesheet 
 	xmlns:xlink="http://www.w3.org/1999/xlink"
 	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   	xmlns:lxslt="http://xml.apache.org/xslt"
   	xmlns:redirect="org.apache.xalan.xslt.extensions.Redirect"
   	extension-element-prefixes="redirect"
   	version="1.0">
  <xsl:output method="html"  indent="yes" />

<xsl:key
  name = "topic" 
  match = "topic" 
  use = "concat('#',@id)" /> 

<xsl:key
  name = "instance" 
  match = "topic" 
  use = "instanceOf/topicRef/@xlink:href" /> 

<xsl:key
  name = "subjectIndicator" 
  match = "topic" 
  use = "subjectIdentity/subjectIndicatorRef/@xlink:href" /> 

<xsl:key
  name = "classAssoc" 
  match = "association[instanceOf/topicRef/@xlink:href='#class-subclass']" 
  use = "member[roleSpec/topicRef/@xlink:href='#class']/topicRef/@xlink:href" /> 

<xsl:param name="out"/>
<xsl:variable name="out-dir" select="concat($out,'/')"/>
<xsl:variable name="root" select="key('subjectIndicator',concat('#',/topicMap/@id))"/>


<xsl:template match="/">
===Welcome to sample Cogitative Topicmap Website Generator===
	<xsl:for-each select="/topicMap/topic">
	<redirect:write select="concat($out-dir,@id,'.html')">
		<HTML>
			<HEADER>
				<TITLE></TITLE>
				<STYLE>
				A  {
					    COLOR: #cc6600;
					    FONT-FAMILY: sans serif;
					    FONT-SIZE: 11pt;
					    FONT-WEIGHT: bold;
					text-decoration : none;
				}
				.A {
					    COLOR: #0066cc;
					    FONT-FAMILY: sans serif;
					    FONT-SIZE: 11pt;
					    FONT-WEIGHT: bold;
				}
				.h1 {
					FONT-SIZE: 26pt;
					COLOR: #0066cc;
				}
				.bibitem {
					    FONT-FAMILY: sans serif;
					    FONT-SIZE: 8pt;
				}
				</STYLE>
			</HEADER>
			<BODY>
			<a href="default.html" class="h1">
				<xsl:call-template name="name">
					<xsl:with-param name="topic" select="$root"/>
				</xsl:call-template>
			</a>
			<hr/>
			<table width="800"><tr>
				<td valign="top" width="200">
					<xsl:call-template name="sitemap"> 
						<xsl:with-param name="classRef" select="'#animal-kingdom'"/>
						<xsl:with-param name="current" select="."/>
					</xsl:call-template>
				</td>
				<td valign="top"><xsl:call-template name="page-layout"/></td>
			</tr></table>
			</BODY>
		</HTML>			
	</redirect:write>
	</xsl:for-each>
</xsl:template>  

<xsl:template name="sitemap">
	<xsl:param name="classRef"/>
	<xsl:param name="current"/>
	<xsl:variable name="topic" select="key('topic',$classRef)"/>
	<xsl:choose>
		<xsl:when test="$topic=$current">
			<xsl:apply-templates select="$topic" mode="label"/>
		</xsl:when>
		<xsl:otherwise> 
			<xsl:apply-templates select="$topic" mode="link"/>
		</xsl:otherwise>
	</xsl:choose>
	<xsl:variable name="aref" select="key('classAssoc',$classRef)"/>
	<xsl:copy-of select="$aref"/>
	<xsl:if test="$aref">
		<ul>
			<xsl:for-each select="$aref/member[roleSpec/topicRef/@xlink:href='#sub-class']/topicRef">
				<li>
				<xsl:call-template name="sitemap">
					<xsl:with-param name="classRef" select="@xlink:href"/>
					<xsl:with-param name="current" select="$current"/>
				</xsl:call-template>
				</li>
			</xsl:for-each>
		</ul>
	</xsl:if>
</xsl:template>  



	
<xsl:template name="page-layout">
<!--determine type of context topic-->
	<xsl:variable name="taxon">
		<xsl:call-template name="getTopicRef">	
			<xsl:with-param name="topic" select="."/>
			<xsl:with-param name="ref">#taxon</xsl:with-param>		
	 	</xsl:call-template>
	</xsl:variable>
	<xsl:choose>
	<xsl:when test="normalize-space($taxon)='#phylum'">
		<xsl:call-template name="phylum-page-layout"/>
	</xsl:when>
	<xsl:when test="normalize-space($taxon)='#animal-class'">
		<xsl:call-template name="class-page-layout"/>
	</xsl:when>
	<xsl:otherwise>
		<xsl:call-template name="generic-page-layout"/>
	</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template name="class-page-layout">
	<h1><xsl:call-template name="name"/></h1>
	<h3>
		Class 
		<xsl:call-template name="name">
			<xsl:with-param name="scope">taxon</xsl:with-param>
		</xsl:call-template>
	</h3>
	[
		<xsl:call-template name="name">
			<xsl:with-param name="scope">also-known-as</xsl:with-param>
		</xsl:call-template>
	]<hr/>
	<table align="right"><tr><td align="right">
		<xsl:apply-templates select="occurrence[instanceOf/topicRef/@xlink:href = '#landsc-img']"/>
		<xsl:apply-templates select="occurrence[instanceOf/topicRef/@xlink:href = '#portr-img']"/>
	</td></tr></table>
	<xsl:apply-templates select="occurrence[instanceOf/topicRef/@xlink:href = '#definition']"/>
</xsl:template>
<xsl:template name="phylum-page-layout">
	<h1><xsl:call-template name="name"/></h1>
	<h2>Phylum 
		<xsl:call-template name="name">
			<xsl:with-param name="scope">taxon</xsl:with-param>
		</xsl:call-template>
	</h2>
	<hr/>
	<xsl:apply-templates select="occurrence"/>
	<hr/>
	Classes of Phylum <xsl:call-template name="sitemap"> 
		<xsl:with-param name="classRef" select="concat('#',@id)"/>
		<xsl:with-param name="current" select="."/>
	</xsl:call-template>
</xsl:template>
<xsl:template name="generic-page-layout">
	<table align="right"><tr><td align="right">
		<xsl:apply-templates select="occurrence[instanceOf/topicRef/@xlink:href = '#landsc-img']"/>
		<xsl:apply-templates select="occurrence[instanceOf/topicRef/@xlink:href = '#portr-img']"/>
	</td></tr></table>
	<xsl:apply-templates select="occurrence[instanceOf/topicRef/@xlink:href = '#description']"/>
</xsl:template>

<xsl:template name="name">
	<xsl:param name="topic" select="."/>
	<xsl:param name="scope"/>
		<xsl:choose>
		<xsl:when test="$topic/baseName/scope/topicRef/@xlink:href=concat('#',$scope)">
			<xsl:value-of select="$topic/baseName[scope/topicRef/@xlink:href=concat('#',$scope)]/baseNameString"/>
		</xsl:when>
		<xsl:otherwise>
			<xsl:value-of select="$topic/baseName[not(scope)]/baseNameString"/>
		</xsl:otherwise>
		</xsl:choose>
</xsl:template>
<xsl:template name="link">
	<xsl:param name="topic" select="."/>
	<xsl:param name="scope"/>
	<a href="{@id}.html">
		<xsl:call-template name="name">
			<xsl:with-param name="topic" select="$topic"/>
			<xsl:with-param name="scope" select="$scope"/>
		</xsl:call-template>
	</a>
</xsl:template>

<xsl:template match="topic" mode="link">
	<a href="{@id}.html"><xsl:call-template name="name"/></a>
</xsl:template>
<xsl:template match="topic" mode="label">
	<span class="A"><xsl:call-template name="name"/></span>
</xsl:template>

<xsl:template match="occurrence">
	<xsl:variable name="scope-name">
		<xsl:call-template name="name">
			<xsl:with-param name="topic" select="key('topic',scope/topicRef/@xlink:href)"/>	
		</xsl:call-template>
	</xsl:variable>	
	<xsl:choose>
		<xsl:when test="instanceOf/topicRef/@xlink:href = '#landsc-img'">
			<table border="1" bgcolor="#ffffde"><tr><td>
				<img src="../images/{resourceRef/@xlink:href }" width="130"/>
			</td></tr><tr><td>
				<font size="-1">Photo by : <i>
				<b><xsl:value-of select="$scope-name"/></b></i></font>
			</td></tr></table>
		</xsl:when>
		<xsl:when test="instanceOf/topicRef/@xlink:href = '#portr-img'">
			<table border="1" bgcolor="#ffffde"><tr><td>
				<img src="../images/{resourceRef/@xlink:href }" width="100"/>
			</td><td>
				<font size="-1">Photo by :<br/>
				<i><b><xsl:value-of select="$scope-name"/></b></i></font>
			</td></tr></table>
		</xsl:when>
		<xsl:when test="instanceOf/topicRef/@xlink:href = '#definition'">
			<i><xsl:value-of select="resourceData"/></i>
			<p class="bibitem">[<xsl:value-of select="$scope-name"/>]</p>
		</xsl:when>
		<xsl:when test="instanceOf/topicRef/@xlink:href = '#description'">
			<font size="+1"><b><xsl:value-of select="resourceData"/></b></font>
		</xsl:when>
	</xsl:choose>
	<br/>
</xsl:template>

 <xsl:template name="getTopicRef">
 <xsl:param name="topic"/>
 <xsl:param name="ref"/>
	<xsl:choose>
	<xsl:when test="$topic/instanceOf/topicRef/@xlink:href=$ref">
		#<xsl:value-of select="$topic/@id"/>
	</xsl:when>
	<xsl:otherwise>
		<xsl:for-each select="key('topic',$topic/instanceOf/topicRef/@xlink:href)"> 
			<xsl:call-template name="getTopicRef">
			<xsl:with-param name="topic" select="."/>
			<xsl:with-param name="ref" select="$ref"/>
			</xsl:call-template>
		</xsl:for-each>
	</xsl:otherwise>
	</xsl:choose>
</xsl:template>

</xsl:stylesheet>
