<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:msxsl="urn:schemas-microsoft-com:xslt">

	<xsl:output method="html"/>

	<xsl:param name="albumForDetail"/>
	<xsl:param name="tuneForDetail"/>

    <!-- extract artist name -->
	<xsl:template match="/">
		<xsl:apply-templates select="catalog/artist"/>
	</xsl:template>
	
	<xsl:template match="/" mode="AlbumList">
	
		<xsl:variable name="albums">
			<xsl:apply-templates select="catalog/artist/album/title" mode="Copy">
				<xsl:sort select="."/>
			</xsl:apply-templates>
		</xsl:variable>
	
		<xsl:variable name="albumCount" select="count(msxsl:node-set($albums)/title)"/>
		<xsl:variable name="columnCount" select="2"/>
		<xsl:variable name="albumsPerColumn" select="ceiling($albumCount div $columnCount)"/>
		
		<table cellpadding="4" class="albums">
			<tr>
				<xsl:call-template name="albumColumns">
					<xsl:with-param name="albums" select="msxsl:node-set($albums)"/>
					<xsl:with-param name="columnCount" select="$columnCount"/>
					<xsl:with-param name="albumsPerColumn" select="$albumsPerColumn"/>
				</xsl:call-template>
			</tr>
		</table>	
	</xsl:template>
	
	<xsl:template match="/" mode="TuneList">

		<!-- all tracks, sorted by name -->
		<xsl:variable name="tracks">
			<xsl:apply-templates select="catalog/artist/album/track" mode="Copy">
				<xsl:sort select="."/>
			</xsl:apply-templates>
		</xsl:variable>

		<!-- tunes are uniquely named tracks -->
		<xsl:variable name="tunes">
			<xsl:apply-templates select="msxsl:node-set($tracks)/track" mode="CreateNonrepeating"/>
		</xsl:variable>
		
		<xsl:variable name="tuneCount" select="count(msxsl:node-set($tunes)/track)"/>
		<xsl:variable name="columnCount" select="3"/>
		<xsl:variable name="tunesPerColumn" select="ceiling($tuneCount div $columnCount)"/>
			
		<table cellpadding="4" class="tunes">
			<tr>
				<xsl:call-template name="tuneColumns">
					<xsl:with-param name="tunes" select="msxsl:node-set($tunes)"/>
					<xsl:with-param name="columnCount" select="$columnCount"/>
					<xsl:with-param name="tunesPerColumn" select="$tunesPerColumn"/>
				</xsl:call-template>
			</tr>
		</table>
	</xsl:template>
		
	<xsl:template match="/" mode="AlbumDetail">
		Album detail for <b><xsl:value-of select="$albumForDetail"/></b>
		<table cellpadding="2" cellspacing="0" class="details">
			<tr>
				<td><b>Track</b></td>
				<td width="300"><b>Tune</b></td>
				<td><b>Time</b></td>
			</tr>
			<xsl:apply-templates select="catalog/artist/album[title = $albumForDetail]" mode="AlbumDetail"/>
		</table>
	</xsl:template>
	
	<xsl:template match="/" mode="TuneDetail">
		Tune detail for <b><xsl:value-of select="$tuneForDetail"/></b>
		<table cellpadding="2" cellspacing="0" class="details">
			<tr>
				<td width="300"><b>Album</b></td>
				<td><b>Track</b></td>
				<td><b>Time</b></td>
			</tr>
			<xsl:apply-templates select="catalog/artist/album/track[node() = $tuneForDetail]" mode="TrackDetailForTune">
				<xsl:sort select="../title"/>
			</xsl:apply-templates>
		</table>
	</xsl:template>
	
	<xsl:template match="artist">
		<h1><xsl:value-of select="name"/></h1>
	</xsl:template>

	<xsl:template match="album" mode="AlbumDetail">
		<xsl:apply-templates select="track" mode="TrackDetailForAlbum"/>
	</xsl:template>
	
	<xsl:template match="title" mode="Copy">
		<xsl:copy-of select="."/>
	</xsl:template>
	
	<xsl:template match="title" mode="AlbumDetailLink">
		<table class="albums">
			<tr>
				<td valign="top">
					<a>
						<xsl:attribute name="href">javascript:albumDetail("<xsl:value-of select="."/>")</xsl:attribute>
						<img border="0" alt="album artwork" align="top"><xsl:attribute name="src">http://www.perfectxml.com\articles\XML\InteractiveCatalog\images\<xsl:value-of select="@image"/></xsl:attribute></img>
					</a>
				</td>
				<td valign="top">
					<a>
						<xsl:attribute name="href">javascript:albumDetail("<xsl:value-of select="."/>")</xsl:attribute>
						<xsl:value-of select="."/>
					</a>
				</td>
			</tr>
		</table>
	</xsl:template>
	
	<!-- Copy a track element -->
	<xsl:template match="track" mode="Copy">
		<xsl:copy-of select="."/>
	</xsl:template>
	
	<!-- Copy a track, if different from the previous one -->
	<xsl:template match="track" mode="CreateNonrepeating">
		<xsl:if test="position() = 1 or preceding-sibling::track[1]/node() != node()">
			<xsl:copy-of select="."/>
		</xsl:if>
	</xsl:template>
	
	<!-- Create the value of the track within a trackDetail link -->
	<xsl:template match="track" mode="TuneDetailLink">
		<a>
			<xsl:attribute name="href">javascript:tuneDetail("<xsl:value-of select="."/>")</xsl:attribute>
			<xsl:value-of select="."/>
		</a>
		<br/>
	</xsl:template>

	<xsl:template match="track" mode="TrackDetailForTune">
		<tr>
			<xsl:if test="position() mod 2">
				<xsl:attribute name="bgcolor">yellow</xsl:attribute>
			</xsl:if>
			<td>
				<xsl:value-of select="../title"/>
				<xsl:if test="@alternateTake = 'true'"> (alternate take)</xsl:if>
			</td>
			<td><xsl:number/></td>
			<td align="right"><xsl:value-of select="@time"/></td>
		</tr>
	</xsl:template>
	
	<xsl:template match="track" mode="TrackDetailForAlbum">
		<tr>
			<xsl:if test="position() mod 2">
				<xsl:attribute name="bgcolor">cyan</xsl:attribute>
			</xsl:if>
			<td><xsl:number/></td>
			<td>
				<xsl:value-of select="."/>
				<xsl:if test="@alternateTake = 'true'"> (alternate take)</xsl:if>
			</td>
			<td align="right"><xsl:value-of select="@time"/></td>
		</tr>
	</xsl:template>

	<!-- Named templates -->
	
	<xsl:template name="albumColumns">
		<xsl:param name="column" select="1"/>
		<xsl:param name="startPosition" select="1"/>
		<xsl:param name="albums"/>
		<xsl:param name="columnCount"/>
		<xsl:param name="albumsPerColumn"/>
		
		<td valign="top" width="{100 div $columnCount}%">
			<xsl:apply-templates 
				select="$albums/title[position() &gt;= $startPosition][position() &lt;= $albumsPerColumn]"
				mode="AlbumDetailLink"/>
		</td>

		<!-- recurse -->
		<xsl:if test="$column &lt; $columnCount">
			<xsl:call-template name="albumColumns">
				<xsl:with-param name="column" select="$column + 1"/>
				<xsl:with-param name="startPosition" select="$startPosition + $albumsPerColumn"/>
				<xsl:with-param name="albums" select="$albums"/>
				<xsl:with-param name="columnCount" select="$columnCount"/>
				<xsl:with-param name="albumsPerColumn" select="$albumsPerColumn"/> 
			</xsl:call-template>
		</xsl:if>
	</xsl:template>

	<xsl:template name="tuneColumns">
		<xsl:param name="column" select="1"/>
		<xsl:param name="startPosition" select="1"/>
		<xsl:param name="tunes"/>
		<xsl:param name="columnCount"/>
		<xsl:param name="tunesPerColumn"/>
		
		<td valign="top" width="{100 div $columnCount}%">
			<xsl:apply-templates 
				select="$tunes/track[position() &gt;= $startPosition][position() &lt;= $tunesPerColumn]"
				mode="TuneDetailLink"/>
		</td>

		<!-- recurse -->
		<xsl:if test="$column &lt; $columnCount">
			<xsl:call-template name="tuneColumns">
				<xsl:with-param name="column" select="$column + 1"/>
				<xsl:with-param name="startPosition" select="$startPosition + $tunesPerColumn"/>
				<xsl:with-param name="tunes" select="$tunes"/>
				<xsl:with-param name="columnCount" select="$columnCount"/>
				<xsl:with-param name="tunesPerColumn" select="$tunesPerColumn"/> 
			</xsl:call-template>
		</xsl:if>
	</xsl:template>

</xsl:stylesheet>

