<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<!-- 14/07/2006 created by karl -->
	<!-- To generate multiple html pages using Saxon -->
	<!-- format OU XML to HTML for OCI Preview -->

	<xsl:param name="samedir">no</xsl:param>
	<!-- imscp can be no for non ims formats, 1 for ims_cp, or 2 for ims_cc slw2-->
	<xsl:param name="imscp">no</xsl:param>
	<xsl:param name="csspath">..</xsl:param>
	<xsl:param name="itemspath"></xsl:param>
	<xsl:param name="basepath"></xsl:param>

	<xsl:variable name="commoncart">2</xsl:variable>
	<xsl:variable name="ims_cp">1</xsl:variable>
	<xsl:variable name="stylefile">styles.css</xsl:variable>
	<xsl:variable name="lcletters">abcdefghijklmnopqrstuvwxyz</xsl:variable>
	<xsl:variable name="ucletters">ABCDEFGHIJKLMNOPQRSTUVWXYZ</xsl:variable>
	<xsl:variable name="colon">:</xsl:variable>
	<xsl:variable name="esccolon">-</xsl:variable>
	<!-- variables no longer used
	<xsl:variable name="fileName" select="//ContentGroup/ContentFile"/>
	<xsl:variable name="CourseId" select="substring-before($fileName, '.xml')"/> -->

	<!-- include the common cartridge templates -->
    <!-- note because we cannot have an if here like this
        <xsl:if test="($imscp = $commoncart) or ($imscp = $ims_cp)">
        <xsl:include href="common_cart.xsl"/>
        and because we do not want to confuse unit re-working users
        what was included is now appended to this file -->
    <xsl:variable name="forumdirname">Forum</xsl:variable>
    <xsl:variable name="forumfilename">unit</xsl:variable>
    <xsl:variable name="weblinkdirname">WebLink</xsl:variable>
    <xsl:variable name="weblinkfilename">link</xsl:variable>
    <xsl:variable name="forumxmlfilename" select="'forumfile.xml'"/>
    <xsl:variable name="content_items" select="'/items/'"/>

	<xsl:key name="idkey" match="*[@id]" use="@id"/>
	<xsl:key name="figkey" match="Figure" use="@id"/>
	<xsl:key name="tablekey" match="Table" use="@id"/>

	<xsl:output method="html" indent="yes" name="html" encoding="UTF-8"/>
    <xsl:output name="cc-format" method="xml"/>

    <!-- ************************************************************ -->
    <!-- main template matching xmlFileList.xml -->
	<xsl:template match="/">

    	<!-- set up common variables -->
    	<xsl:variable name="wherecreate">
      <xsl:choose>
            <xsl:when test="($imscp = $commoncart) or ($imscp = $ims_cp)">
                <xsl:value-of select="concat('/',$itemspath,'/')"/>
            </xsl:when>
            <xsl:otherwise></xsl:otherwise>
          </xsl:choose>
        </xsl:variable>

        <!-- for each course(s) -->
		<xsl:for-each select="//ContentGroup/ContentFile">
            <!-- set up course variables -->
			<xsl:variable name="fileName" select="node()"/>
			<xsl:variable name="CourseId" select="substring-before($fileName, '.xml')"/>
            <!--course id <xsl:value-of select="$CourseId"/>-->
			<!-- set up location of 'Items' for files but only for ims_cp or ims_cc -->
			<xsl:variable name="items_path">
    			<xsl:choose>
	               <xsl:when test="($imscp = $commoncart) or ($imscp = $ims_cp)">
	                   <xsl:value-of select="concat($basepath,$CourseId,$wherecreate)"/>
	               </xsl:when>
	               <xsl:otherwise>
	                   <xsl:value-of select="concat($CourseId,'/')"/>
	               </xsl:otherwise>
    			</xsl:choose>
			</xsl:variable>

            <!-- set up where we find the content, because it depends on the param samedir
            this 'if' fails with message 'cast does not allow an empty sequence'
            <xsl:variable name="wherecontent">
                <xsl:if test="$samedir='no'">
                    <xsl:value-of select="concat($CourseId,'/')"/>
			</xsl:if>
            </xsl:variable> -->
	        <xsl:variable name="wherecontent">
	           <xsl:choose>
	               <xsl:when test="$samedir='no'">
	                   <xsl:value-of select="concat($CourseId,'/')"/>
	               </xsl:when>
	               <xsl:otherwise></xsl:otherwise>
	           </xsl:choose>
	        </xsl:variable>

	        <!-- find the input files -->
	        <xsl:variable name="currentFile" select="document(concat($wherecontent,$fileName))"/>
	        <xsl:variable name="metadataFile" select="document(concat($wherecontent,'metadata.xml'))"/>
	        <xsl:variable name="ForumFile">
	           <xsl:choose>
	               <xsl:when test="($imscp = $commoncart) or ($imscp = $ims_cp)">
	                   <xsl:copy-of select="document(concat($wherecontent,$forumxmlfilename))"/>
   </xsl:when>
	           </xsl:choose>
	        </xsl:variable>

			<xsl:variable name="TotalPages" select="count($currentFile//Item/FrontMatter/Introduction | $currentFile//Session/Section[not(SubSection)] | $currentFile//Session/Section/SubSection | $currentFile//References | $currentFile//Acknowledgements | $currentFile//Appendices)"/>
			<xsl:variable name="indexpage" select="concat($items_path,$CourseId,'_index.html')"/>

			<xsl:if test="$imscp = 'no'"><!-- was removed in sarah's -->
    			<xsl:result-document href="{$indexpage}" format="html">
    				<xsl:call-template name="Main">
					    <xsl:with-param name="CourseId" select="$CourseId"/>
    					<xsl:with-param name="currentFile" select="$currentFile"/>
    					<xsl:with-param name="metadataFile" select="$metadataFile"/>
    					<xsl:with-param name="pageType">index</xsl:with-param>
    				</xsl:call-template>
    			</xsl:result-document>
			</xsl:if>

			<xsl:for-each select="$currentFile//Item/FrontMatter/Introduction | $currentFile//Session/Section[not(SubSection)] | $currentFile//Session/Section/SubSection | $currentFile//References | $currentFile//Acknowledgements | $currentFile//Appendices">
				<xsl:variable name="currentPage" select="position() - 1"/>

				<xsl:variable name="filename" select="concat($items_path,$CourseId,'_section',$currentPage,'.html')"/>

				<!--filename <xsl:value-of select="$filename"/>-->

				<xsl:result-document href="{$filename}" format="html">
					<xsl:call-template name="Main">
						<xsl:with-param name="CourseId" select="$CourseId"/>
						<xsl:with-param name="currentFile" select="$currentFile"/>
						<xsl:with-param name="pageType">section<xsl:if test="not(name() = 'Section')">extras</xsl:if>
						</xsl:with-param>
						<xsl:with-param name="TotalPages" select="$TotalPages"/>
						<xsl:with-param name="currentPage" select="$currentPage"/>
						<xsl:with-param name="currentPageType" select="name()"/>
					</xsl:call-template>
				</xsl:result-document>
			</xsl:for-each>
			<!-- if this is a common cartridge then we need to create the weblinks &forums slw2 -->
			<xsl:if test="$imscp = $commoncart">
				<!-- create weblinks -->
				<xsl:call-template name="create_weblinks">
					<xsl:with-param name="metadataFile" select="$metadataFile"/>
					<xsl:with-param name="CourseId" select="$CourseId"/>
				</xsl:call-template>
				<!-- create forums -->
				<xsl:call-template name="create_forums">
					<xsl:with-param name="CourseId" select="$CourseId"/>
					<xsl:with-param name="ForumFile" select="$ForumFile"/>
				</xsl:call-template>
			</xsl:if>
		</xsl:for-each>
	</xsl:template>
    <!-- *********************************************** -->

	<xsl:template name="Main">
		<xsl:param name="CourseId"/>
		<xsl:param name="currentFile"/>
		<xsl:param name="metadataFile"/>
		<xsl:param name="pageType"/>
		<xsl:param name="TotalPages"/>
		<xsl:param name="currentPage"/>
		<xsl:param name="currentPageType"/>
		<html>
			<head>
				<title><xsl:value-of select="$currentFile//Item/Unit/UnitTitle"/></title>
				<!-- set up the CSS path using the param passed <link rel="stylesheet" href="../styles.css"/> -->
			<xsl:element name="link">
				<xsl:attribute name="rel">stylesheet</xsl:attribute>
				<xsl:attribute name="href">
				<xsl:choose>
				<xsl:when test="$csspath">
				<xsl:value-of select="concat($csspath,'/',$stylefile)"/>
				</xsl:when>
				<xsl:otherwise><xsl:value-of select="$stylefile"/></xsl:otherwise>
				</xsl:choose>
				</xsl:attribute>
			</xsl:element>

            <xsl:if test="$imscp = 'no'">

				<script type="text/javascript"><![CDATA[
					function showcontent(content)
					{
					   document.getElementById(content).style.display="block";
					}

					var newwindow;
					function popupinfo(url, windowName, windowFeatures)
					{
						popupclose();
						newwindow= window.open (url, windowName, windowFeatures);
					}
					function popupclose() {
						if (newwindow != null) {
							if (!newwindow.closed) {
								newwindow.close();
							}
						}
					}
				]]></script>
         </xsl:if>
			</head>
			<!-- change to accomadate microsoft reader -->
			<body>
			<xsl:if test="$imscp != $commoncart">
			<xsl:attribute name="onUnload">popupclose()</xsl:attribute>
			</xsl:if>


				<div id="content">
					<table id="layout-table">
						<tbody>
							<tr>
								<td id="left-column"/>
								<td id="middle-column">
									<table class="categoryboxcontent">
										<tbody>
											<tr>
												<td>
													<div class="pagenums">
														<xsl:call-template name="PagesNums">
															<xsl:with-param name="CourseId" select="$CourseId"/>
															<xsl:with-param name="pageType" select="$pageType"/>
															<xsl:with-param name="TotalPages" select="$TotalPages"/>
															<xsl:with-param name="currentPage" select="$currentPage"/>
															<xsl:with-param name="countPage">
																<xsl:choose>
																	<xsl:when test="number($currentPage) &lt;= 5 or number($TotalPages) &lt;= 10">1</xsl:when>
																	<xsl:when test="(number($TotalPages) - number($currentPage)) &lt; 6 and (number($TotalPages) > 10)">
																		<xsl:value-of select="number($TotalPages) - 10"/>
																	</xsl:when>
																	<xsl:otherwise>
																		<xsl:value-of select="number($currentPage) - 4"/>
																	</xsl:otherwise>
																</xsl:choose>
															</xsl:with-param>
														</xsl:call-template>
													</div>
												</td>
											</tr>
											<tr>
												<td>
													<h1>
														<xsl:value-of select="./../Title"/>
													</h1>
												</td>
											</tr>
											<tr>
												<td>
													<xsl:choose>
														<xsl:when test="$pageType = 'index'">
															<xsl:call-template name="indexPage">
																<xsl:with-param name="CourseId" select="$CourseId"/>
																<xsl:with-param name="currentFile" select="$currentFile"/>
																<xsl:with-param name="metadataFile" select="$metadataFile"/>
															</xsl:call-template>
														</xsl:when>
														<xsl:when test="$pageType = 'section' or contains($currentPageType, 'Reference') or contains($currentPageType, 'Appendice')">
															<xsl:call-template name="MainBody">
																<xsl:with-param name="currentPageType" select="$currentPageType"/>
															</xsl:call-template>
														</xsl:when>
														<xsl:otherwise>
															<xsl:apply-templates/>
															<xsl:if test="$currentPageType = 'Introduction'">
                                                                <xsl:call-template name="LearningOutcomes">
																    <xsl:with-param name="currentFile" select="$currentFile"/>
															</xsl:call-template>
                                                            </xsl:if>
														</xsl:otherwise>
													</xsl:choose>
												</td>
											</tr>
											<tr>
												<td>
													<div class="pagenums">
														<xsl:call-template name="PagesNums">
															<xsl:with-param name="CourseId" select="$CourseId"/>
															<xsl:with-param name="pageType" select="$pageType"/>
															<xsl:with-param name="TotalPages" select="$TotalPages"/>
															<xsl:with-param name="currentPage" select="$currentPage"/>
															<xsl:with-param name="countPage">
																<xsl:choose>
																	<xsl:when test="number($currentPage) &lt;= 5 or number($TotalPages) &lt;= 10">1</xsl:when>
																	<xsl:when test="(number($TotalPages) - number($currentPage)) &lt; 6 and (number($TotalPages) > 10)">
																		<xsl:value-of select="number($TotalPages) - 10"/>
																	</xsl:when>
																	<xsl:otherwise>
																		<xsl:value-of select="number($currentPage) - 4"/>
																	</xsl:otherwise>
																</xsl:choose>
															</xsl:with-param>
														</xsl:call-template>
													</div>
												</td>
											</tr>
										</tbody>
									</table>
								</td>
								<td id="right-column"/>
							</tr>
						</tbody>
					</table>
				</div>
			</body>
		</html>
	</xsl:template>
	<xsl:template name="PagesNums">
		<!-- this canbe moved to seperate page -->
		<xsl:param name="CourseId"/>
		<xsl:param name="pageType"/>
		<xsl:param name="TotalPages"/>
		<xsl:param name="currentPage"/>
		<xsl:param name="countPage"/>
		<xsl:if test="$imscp = 'no'">
   		<xsl:variable name="startpagenumber">
   			<xsl:choose>
   				<xsl:when test="(number($TotalPages) - number($currentPage)) &lt; 6 and (number($TotalPages) > 10)">
   					<xsl:value-of select="number($TotalPages) - 10"/>
   				</xsl:when>
   				<xsl:when test="number($currentPage) - 4 > 0 and number($TotalPages) > 10">
   					<xsl:value-of select="number($currentPage) - 4"/>
   				</xsl:when>
   				<xsl:otherwise>1</xsl:otherwise>
   			</xsl:choose>
   		</xsl:variable>
   		<xsl:variable name="endpagenumber">
   			<xsl:choose>
   				<xsl:when test="(number($TotalPages) - number($currentPage)) > 5 and number($currentPage) > 5">
   					<xsl:value-of select="number($currentPage) + 6"/>
   				</xsl:when>
   				<xsl:when test="number($currentPage) &lt; 6 and (number($TotalPages) > 10)">11</xsl:when>
   				<xsl:otherwise>
   					<xsl:value-of select="$TotalPages"/>
   				</xsl:otherwise>
   			</xsl:choose>
   		</xsl:variable>
   			<xsl:choose>
   				<xsl:when test="number($countPage) = number($startpagenumber) and number($currentPage) = 0">Pages: &lt;Previous</xsl:when>
   				<xsl:when test="number($currentPage) > 0 and number($countPage) = number($startpagenumber)">
   					Pages: <a href="{concat($CourseId,'_section',number($currentPage)-1,'.html')}">&lt;Previous</a>
   				</xsl:when>
   			</xsl:choose>&#160;
   			<xsl:if test="number($startpagenumber) &lt;= number($endpagenumber)">
   				<xsl:choose>
   					<xsl:when test="$currentPage = ($countPage - 1)">
   						<b>
   							<xsl:value-of select="$countPage"/>
   						</b>
   					</xsl:when>
   					<xsl:otherwise>
   						<a href="{concat($CourseId,'_section',number($countPage)-1,'.html')}">
   							<xsl:value-of select="$countPage"/>
   						</a>
   					</xsl:otherwise>
   				</xsl:choose>&#160;
   				<xsl:choose>
   					<xsl:when test="number($countPage) = number($endpagenumber) and number($currentPage)+1 >= number($TotalPages)">Next ></xsl:when>
   					<xsl:when test="number($countPage) = number($endpagenumber)  and number($currentPage)+1 &lt; number($TotalPages)">
   						<a href="{concat($CourseId,'_section',number($currentPage)+1,'.html')}">Next ></a>
   					</xsl:when>
   				</xsl:choose>
   			</xsl:if>
   			<xsl:if test="number($countPage) &lt; number($endpagenumber)">
   				<xsl:call-template name="PagesNums">
   					<xsl:with-param name="CourseId" select="$CourseId"/>
   					<xsl:with-param name="pageType" select="$pageType"/>
   					<xsl:with-param name="TotalPages" select="$TotalPages"/>
   					<xsl:with-param name="currentPage" select="$currentPage"/>
   					<xsl:with-param name="countPage" select="number($countPage) + 1"/>
   				</xsl:call-template>
   			</xsl:if>
         </xsl:if>
	</xsl:template>
	<xsl:template name="indexPage">
		<xsl:param name="currentFile"/>
		<xsl:param name="CourseId"/>
		<xsl:param name="metadataFile"/>
		<h1 class="indexpage">
		   <xsl:choose>
            <xsl:when test="$imscp='no'">
   			   Preview of: <xsl:value-of select="$currentFile//Unit/UnitID"/><xsl:text> </xsl:text><xsl:value-of select="$currentFile//Item//Session/Title"/>
            </xsl:when>
            <xsl:otherwise>
               <xsl:value-of select="$currentFile//Unit/UnitID"/><xsl:text> </xsl:text><xsl:value-of select="$currentFile//Item//Session/Title"/>
            </xsl:otherwise>
         </xsl:choose>
		</h1>
		<br/>
		<div align="center">
		<xsl:if test="$currentFile//Preface/Figure">
			<img src="{$currentFile//Preface/Figure/Image/@src}" alt="{$currentFile//Preface/Figure/Description}"/>
		</xsl:if>
		<xsl:if test="$currentFile//Preface/MediaContent">
			<xsl:apply-templates select="$currentFile//Preface/MediaContent"/>
		</xsl:if>
		</div>
        <xsl:if test="$imscp = 'no'">
		<xsl:call-template name="metaData">
			<xsl:with-param name="metadataFile" select="$metadataFile"/>
		</xsl:call-template>
        </xsl:if>
		<xsl:for-each select="$currentFile//Item/FrontMatter/Introduction | $currentFile//Session/Section[not(SubSection)] | $currentFile//Session/Section/SubSection | $currentFile//References | $currentFile//Acknowledgements | $currentFile//Appendices">
			<xsl:variable name="titleContent">
				<xsl:choose>
					<xsl:when test="name() = 'Section'">
						<xsl:value-of select="Title"/>
					</xsl:when>
					<xsl:when test="name() = 'SubSection'">
						<xsl:value-of select="Title"/>
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="name()"/>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:variable>
			<xsl:variable name="paraContent">
				<xsl:if test=".//Paragraph[1] and (name() = 'Section' or name() = 'SubSection' or name() = 'Introduction')">
					<xsl:apply-templates select=".//Paragraph[1]"/>
				</xsl:if>
			</xsl:variable>
			<xsl:if test="name() = 'SubSection' and substring(@id, string-length(@id) - 2, string-length()) = '001'">
				<h1>
					<xsl:value-of select="../Title"/>
				</h1>
			</xsl:if>
			<a>
				<xsl:attribute name="href"><xsl:value-of select="$CourseId"/>_section<xsl:value-of select="position() - 1"/>.html</xsl:attribute>
				<h2>
					<xsl:value-of select="$titleContent"/>
				</h2>
			</a>
			<p class="paradefault indexpage">
				<xsl:call-template name="trimPara">
					<xsl:with-param name="stringLenRequired">200</xsl:with-param>
					<xsl:with-param name="stringInput" select="$paraContent"/>
				</xsl:call-template>
			</p>
			<br /><!-- It would be nice to have a div here with evenrow/oddrow colours, but the section groupings are different from live publishing -->
		</xsl:for-each>
	</xsl:template>
	<xsl:template name="metaData">
		<xsl:param name="metadataFile"/>
		<xsl:if test="exists($metadataFile//time)">
			<div class="metadata">
				<p class="metadata"><b>Time</b>: <xsl:value-of select="$metadataFile//time"/></p>
				<p class="metadata"><b>Level</b>: <xsl:value-of select="$metadataFile//level"/></p>
			</div>
		</xsl:if>
		<xsl:if test="exists($metadataFile//relations)">
			<div class="ru_block">
				<h2 class="ru_head">Related educational resources</h2>
				<div class="ru_links">
					<p class="metadata">If you are interested in this unit you may also be interested in:</p><br/>
					<xsl:for-each select="$metadataFile//relations/relation[lower-case(urlgroup) = 'ol links']">
						<xsl:variable name="title" select="urltitle"/>
						<xsl:variable name="href" select="url"/>
						<xsl:if test="position() = 1">
							<h1>OpenLearn Links</h1>
						</xsl:if>
						<p class="metadata ru">
						<a title="{$title}" href="{$href}"><xsl:value-of select='urltitle'/></a>
						</p>
					</xsl:for-each>
					<xsl:for-each select="$metadataFile//relations/relation[lower-case(urlgroup) = 'ou links']">
						<xsl:variable name="title" select="urltitle"/>
						<xsl:variable name="href" select="url"/>
						<xsl:if test="position() = 1">
							<h1>Open University Links</h1>
						</xsl:if>
						<p class="metadata ru">
						<a title="{$title}" href="{$href}"><xsl:value-of select='urltitle'/></a>
						</p>
					</xsl:for-each>
					<xsl:for-each select="$metadataFile//relations/relation[lower-case(urlgroup) = 'others']">
						<xsl:variable name="title" select="urltitle"/>
						<xsl:variable name="href" select="url"/>
						<xsl:if test="position() = 1">
							<h1>Other Links</h1>
						</xsl:if>
						<p class="metadata ru">
						<a title="{$title}" href="{$href}"><xsl:value-of select='urltitle'/></a>
						</p>
					</xsl:for-each>
				</div>
			</div>
		</xsl:if>
	</xsl:template>
	<xsl:template name="MainBody">
		<xsl:param name="currentPageType"/>
		<xsl:if test="not(contains($currentPageType, 'Section'))">
			<h2>
				<xsl:value-of select="name()"/>
			</h2>
		</xsl:if>
		<xsl:apply-templates/>
	</xsl:template>
	<xsl:template name="trimPara">
		<!-- pass the string to be cropped with required string length -->
		<xsl:param name="stringLenRequired"/>
		<xsl:param name="stringInput"/>
		<xsl:value-of select="substring($stringInput,1,number($stringLenRequired))"/>
		<xsl:value-of select="substring-before(concat(substring($stringInput,number($stringLenRequired)+1,number($stringLenRequired) +20),' ' ),' ')"/>
		<xsl:if test="string-length($stringInput) > number($stringLenRequired)">...</xsl:if>
	</xsl:template>
	<xsl:template match="Heading | SubHeading | Caption">
		<h3>
			<xsl:apply-templates/>
		</h3>
	</xsl:template>
	<xsl:template match="Title">
		<h2>
			<xsl:apply-templates/>
		</h2>
	</xsl:template>
	<xsl:template match="ListItem/Paragraph | SubListItem/Paragraph">
		<xsl:apply-templates/>
		<div class="gap2"><img src="/pix/spacer.gif" width="1" height="1" /></div>
	</xsl:template>
	<xsl:template match="Paragraph | Reference | Description">
		<p class="paradefault">
			<xsl:apply-templates/>
		</p>
	</xsl:template>
	<xsl:template match="NumberedList | NumberedSubsidiaryList">
		<ol>
			<xsl:for-each select="ListItem | SubListItem">
				<li class="listitem">
					<xsl:apply-templates/>
				</li>
			</xsl:for-each>
		</ol>
	</xsl:template>
	<xsl:template match="BulletedList | BulletedSubsidiaryList">
		<ul>
			<xsl:for-each select="ListItem | SubListItem">
				<li class="listitem">
					<xsl:apply-templates/>
				</li>
			</xsl:for-each>
		</ul>
	</xsl:template>
	<xsl:template match="UnNumberedList | UnNumberedSubsidiaryList">
		<dl>
			<xsl:for-each select="ListItem | SubListItem">
				<dd class="listitem">
					<xsl:apply-templates/>
				</dd>
			</xsl:for-each>
		</dl>
	</xsl:template>
	<xsl:template match="Box | Reading | StudyNote | Summary">
		<xsl:variable name="id">
			<xsl:value-of select="@id"/>
		</xsl:variable>
		<div align="center">
			<a name="{$id}" />
			<div class="boxcontent" align="left">
				<xsl:apply-templates/>
			</div>
		</div>
	</xsl:template>
	<xsl:template match="a">
    	<a>
    		<xsl:for-each select="@*">
    			<xsl:attribute name="{name()}">
    				<xsl:value-of select="."/>
    			</xsl:attribute>
    		</xsl:for-each>
    		<xsl:apply-templates />
    	</a>
	<!--
		<xsl:choose>
			<xsl:when test="@target = '_blank' or contains(@target, 'new')">
				<a target="_blank">
					<xsl:attribute name="href"><xsl:value-of select="@href"/></xsl:attribute>
					<xsl:value-of select="."/>
				</a>
			</xsl:when>
			<xsl:otherwise>
        		<a>
        			<xsl:for-each select="@*">
        				<xsl:attribute name="{name()}">
        					<xsl:value-of select="."/>
        				</xsl:attribute>
        			</xsl:for-each>
        			<xsl:apply-templates />
        		</a>
			</xsl:otherwise>
		</xsl:choose>
    -->
	</xsl:template>
	<xsl:template match="ComputerDisplay | ProgramListing | ComputerCode">
		<div class="oci-computerdisplay">
			<xsl:apply-templates/>
		</div>
	</xsl:template>
	<xsl:template match="Dialogue">
		<div>
			<div class="oci-dialogue">
				<xsl:call-template name="include-crossref-id"/>
				<xsl:apply-templates/>
			</div>&#160;</div>
	</xsl:template>
	<xsl:template match="Dialogue/Heading | Transcript/Heading">
		<h3>
			<xsl:call-template name="include-crossref-id"/>
			<xsl:apply-templates/>
		</h3>
	</xsl:template>
	<xsl:template match="Dialogue/Speaker | Transcript/Speaker"/>
	<xsl:template match="Dialogue/Remark | Transcript/Remark">
		<div class="oci-dialogue-line">
			<xsl:call-template name="include-crossref-id"/>
			<xsl:for-each select="preceding-sibling::*[position()=1 and local-name()='Speaker']">
				<div class="oci-dialogue-speaker">
					<xsl:call-template name="include-crossref-id"/>
					<xsl:apply-templates/>
				</div>
			</xsl:for-each>
			<div class="oci-dialogue-remark">
				<xsl:apply-templates/>
			</div>
			<div class="clearer"/>
		</div>
	</xsl:template>
	<xsl:template name="include-crossref-id">
		<xsl:if test="@id and string(@id)!=''">
			<xsl:attribute name="crossrefid"><xsl:value-of select="@id"/></xsl:attribute>
		</xsl:if>
	</xsl:template>
	<xsl:template match="CrossRef">
		<xsl:variable name="thissec">
			<xsl:choose>
				<xsl:when test="ancestor::SubSection[1]">
					<xsl:value-of select="ancestor::SubSection[1]/@id"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="ancestor::Section[1]/@id"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:variable name="targetsec">
			<xsl:choose>
				<xsl:when test="key('idkey',@idref)/ancestor::SubSection[1]">
					<xsl:value-of select="key('idkey',@idref)/ancestor::SubSection[1]/@id"/>
				</xsl:when>
				<xsl:when test="key('idkey',@idref)/self::SubSection or key('idkey',@idref)/self::Section">
					<xsl:value-of select="key('idkey',@idref)/@id"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="key('idkey',@idref)/ancestor::Section[1]/@id"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:choose>
			<xsl:when test="$thissec=$targetsec">
				<a href="#{@idref}">
					<xsl:apply-templates/>
				</a>
			</xsl:when>
			<xsl:otherwise>
				<xsl:variable name="targetPage">
					<xsl:for-each select="ancestor::Item/Unit/Session/Section[not(SubSection)] | ancestor::Item/Unit/Session/Section/SubSection">
						<xsl:if test="@id=$targetsec"><xsl:value-of select="position()"/></xsl:if>
					</xsl:for-each>
				</xsl:variable>
				<a href="{concat(ancestor::Item/Unit/UnitID,'_section',$targetPage)}.html#{@idref}">
					<xsl:apply-templates/>
				</a>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	<xsl:template match="a[@type = 'figure']">
		<xsl:variable name="thissec">
			<xsl:choose>
				<xsl:when test="ancestor::SubSection[1]">
					<xsl:value-of select="ancestor::SubSection[1]/@id"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="ancestor::Section[1]/@id"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:variable name="figsec">
			<xsl:choose>
				<xsl:when test="key('figkey',@href)/ancestor::SubSection[1]">
					<xsl:value-of select="key('figkey',@href)/ancestor::SubSection[1]/@id"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="key('figkey',@href)/ancestor::Section[1]/@id"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:choose>
			<xsl:when test="$thissec=$figsec">
				<a href="#{@href}">
					<xsl:apply-templates/>
				</a>
			</xsl:when>
			<xsl:otherwise>
				<xsl:variable name="imagePage">
					<xsl:for-each select="ancestor::Item/Unit/Session/Section[not(SubSection)] | ancestor::Item/Unit/Session/Section/SubSection">
						<xsl:if test="@id=$figsec"><xsl:value-of select="position()"/></xsl:if>
					</xsl:for-each>
				</xsl:variable>
				<a href="{concat(ancestor::Item/Unit/UnitID,'_section',$imagePage)}.html#{@href}">
					<xsl:apply-templates/>
				</a>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	<xsl:template match="a[@type = 'table']">
		<xsl:variable name="thissec">
			<xsl:choose>
				<xsl:when test="ancestor::SubSection[1]">
					<xsl:value-of select="ancestor::SubSection[1]/@id"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="ancestor::Section[1]/@id"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:variable name="tablesec">
			<xsl:choose>
				<xsl:when test="key('tablekey',@href)/ancestor::SubSection[1]">
					<xsl:value-of select="key('tablekey',@href)/ancestor::SubSection[1]/@id"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="key('tablekey',@href)/ancestor::Section[1]/@id"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:choose>
			<xsl:when test="$thissec=$tablesec">
				<a href="#{@href}">
					<xsl:apply-templates/>
				</a>
			</xsl:when>
			<xsl:otherwise>
				<xsl:variable name="tablePage">
					<xsl:for-each select="ancestor::Item//Session/Section[not(SubSection)] | ancestor::Item//Session/Section/SubSection">							<xsl:if test="@id=$tablesec"><xsl:value-of select="position()"/></xsl:if>
					</xsl:for-each>
				</xsl:variable>
				<a href="{concat(ancestor::Item/Unit/UnitID,'_section',$tablePage)}.html#{@href}">
					<xsl:apply-templates/>
				</a>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	<xsl:template match="Session">
		<div class="session">
			<xsl:apply-templates/>
		</div>
	</xsl:template>
	<xsl:template match="SubSubSection">
        <xsl:variable name="id">
        	<xsl:value-of select="@id"/>
        </xsl:variable>
        <a name="{$id}"></a>
		<xsl:apply-templates/>
	</xsl:template>
	<xsl:template match="CaseStudy">
		<xsl:variable name="id">
			<xsl:value-of select="@id"/>
		</xsl:variable>
		<a name="{$id}"></a>
		<xsl:apply-templates/>
	</xsl:template>
	<xsl:template match="SAQ | ITQ | Exercise">
		<xsl:variable name="id">
			<xsl:value-of select="@id"/>
		</xsl:variable>
		<a name="{$id}"></a>
    	<xsl:apply-templates/>
	</xsl:template>
	<xsl:template match="b">
		<b>
			<xsl:apply-templates/>
		</b>
	</xsl:template>
	<xsl:template match="i">
		<i>
			<xsl:apply-templates/>
		</i>
	</xsl:template>
	<xsl:template match="u">
		<u>
			<xsl:apply-templates/>
		</u>
	</xsl:template>
	<xsl:template match="sup">
		<sup>
			<xsl:apply-templates/>
		</sup>
	</xsl:template>
	<xsl:template match="sub">
		<sub>
			<xsl:apply-templates/>
		</sub>
	</xsl:template>
	<xsl:template match="Table">
		<div align="center">
    		<xsl:variable name="id">
    			<xsl:value-of select="@id"/>
    		</xsl:variable>
    		<a name="{$id}"></a>
			<h3>
				<xsl:apply-templates select="TableHead"/>
			</h3>
			<table class="tableprop" cellpadding="2">
				<xsl:for-each select=".//table/tr">
					<tr>
						<xsl:apply-templates/>
					</tr>
				</xsl:for-each>
			</table>
			<p style="font-family: verdana-normal, sans-serif; font-size: 0.75em; font-weight: normal; color: #333333;">
				<xsl:if test="normalize-space(SourceReference) != ''">
    				<xsl:apply-templates select="SourceReference"/>
				</xsl:if>
				<xsl:if test="normalize-space(TableFootnote) != ''">
					<br/>Note: <xsl:apply-templates select="TableFootnote"/>
				</xsl:if>
			</p>
		</div>
	</xsl:template>
	<xsl:template match="th">
		<td class="tablerowtitle">
			<xsl:attribute name="align"><xsl:choose><xsl:when test="contains(@class,'Cent')">center</xsl:when><xsl:when test="contains(@class,'Right')">right</xsl:when><xsl:otherwise>left</xsl:otherwise></xsl:choose></xsl:attribute>
			<xsl:if test="string-length(@colspan) &gt; 0">
				<xsl:attribute name="colspan"><xsl:value-of select="@colspan"/></xsl:attribute>
			</xsl:if>
			<xsl:if test="string-length(.) = 0">&#160;</xsl:if>
			<xsl:apply-templates/>
		</td>
	</xsl:template>
	<xsl:template match="td">
		<td class="tablerowvalues">
			<xsl:attribute name="align"><xsl:choose><xsl:when test="contains(@class,'Cent')">center</xsl:when><xsl:when test="contains(@class,'Right')">right</xsl:when><xsl:otherwise>left</xsl:otherwise></xsl:choose></xsl:attribute>
			<xsl:if test="string-length(@colspan) &gt; 0">
				<xsl:attribute name="colspan"><xsl:value-of select="@colspan"/></xsl:attribute>
			</xsl:if>
			<xsl:if test="string-length(.) = 0">&#160;</xsl:if>
			<xsl:apply-templates/>
		</td>
	</xsl:template>
	<xsl:template match="Activity/Timing">
		<p class="paradefault">
			<xsl:variable name="childtest">
				<xsl:value-of select="*[not(*)]"/>
			</xsl:variable>
    		<xsl:choose>
    			<xsl:when test="$childtest != ''">
                    You should allow <xsl:value-of select="Hours"/> hour(s), <xsl:value-of select="Minutes"/> minute(s).
    			</xsl:when>
    			<xsl:otherwise>
    				<xsl:value-of select="."/>
    			</xsl:otherwise>
    		</xsl:choose>
		</p>
	</xsl:template>
	<xsl:template match="Verse">
		<div class="activity">
			<xsl:apply-templates/>
		</div>
	</xsl:template>
	<xsl:template match="Activity | Example | Extract | Quote">
		<div class="activity">
    		<xsl:variable name="id">
    			<xsl:value-of select="@id"/>
    		</xsl:variable>
    		<a name="{$id}"></a>
    		<xsl:apply-templates/>
		</div>
	</xsl:template>
	<xsl:template match="Answer | Discussion">
      <xsl:choose>
         <xsl:when test="$imscp = 'no'">
      		<p class="paradefault">
      			<a href="javascript: showcontent('{concat(name(),../@id)}')">
      				Now read the <xsl:value-of select="translate(name(),$ucletters,$lcletters)"/>
      			</a>
      		</p>
      		<div class="activity" id="{concat(name(),../@id)}" style="display:none">
      			<xsl:apply-templates/>
      		</div>
      </xsl:when>

      <xsl:otherwise>
      		<div class="activity" id="{concat(name(),../@id)}">
      			<xsl:apply-templates/>
      		</div>
      </xsl:otherwise>
   </xsl:choose>
	</xsl:template>
	<xsl:template match="InlineFigure | InlineEquation">
		<xsl:apply-templates select="Image"/>
	</xsl:template>
	<xsl:template match="Figure">
		<div align="center">
    		<xsl:variable name="id">
    			<xsl:value-of select="@id"/>
    		</xsl:variable>
    		<a name="{$id}"></a>
				<xsl:apply-templates select="Image"/>
			<xsl:apply-templates select="Caption"/>
			<xsl:if test="normalize-space(Description) != '' ">
            <xsl:choose>
               <xsl:when test="$imscp = 'no'">
				      <a href="javascript: popupinfo('{@id}.html', 'ImageDescription', 'location=0,status=0,scrollbars=1,width=300,height=200')" title="Resource">View description</a>
               </xsl:when>
               <xsl:otherwise>
				      <a href="{@id}.html" title="Resource">View description</a>
               </xsl:otherwise>
            </xsl:choose>
		   </xsl:if>
		</div>
		<!-- add in the same location as the content files to the figure files, but may need to pull this out on testing slw2 , make sure if a package they go in an items folder
		-->
		<xsl:variable name="wherecreate">
		<xsl:choose>
			<xsl:when test="($imscp = $commoncart) or ($imscp = $ims_cp)"><xsl:value-of select="concat('/',$itemspath,'/')"/></xsl:when>
				<xsl:otherwise></xsl:otherwise>
		</xsl:choose>
		</xsl:variable>

		<xsl:variable name="figurepage">
		<xsl:choose>
		<xsl:when test="ancestor::Unit/UnitID = ''">
			 <xsl:value-of select="concat($basepath,ancestor::Unit/UnitTitle,'/',$wherecreate,@id,'.html')"/>
		</xsl:when>
		<xsl:otherwise>
			<xsl:value-of select="concat($basepath,ancestor::Unit/UnitID,'/',$wherecreate,@id,'.html')"/>
		</xsl:otherwise>
		</xsl:choose>
		</xsl:variable>

		<xsl:result-document href="{$figurepage}" format="html">
			<html>
				<head>
					<title>Image Description</title>
				</head>
				<body>
					<p>
						<xsl:apply-templates select="Description"/>
					</p>
					<p>
						<input type="button" value="Close" onclick="window.close()"/>
					</p>
				</body>
			</html>
		</xsl:result-document>
	</xsl:template>
	<xsl:template match="Image">
		<!--  Check if we are in a Figure or InlineFirgue -->
		<xsl:variable name="alt">
			<xsl:choose>
				<xsl:when test="name(parent::node()) = 'Figure'">
					<xsl:choose>
						<!--  If we are in the figure check for the exsistence of the Alternative Tag, if it exsists and isn't empty use it -->
						<xsl:when test="string(../Alternative)">
							<!-- Trim off any extra white space and change all :'s to -'s because I think the publisher splits around : or does something with it because anything before and :'s are lost and we want to mirror what they will see online -->
							<xsl:value-of select="translate(normalize-space(../Alternative), $colon, $esccolon)"/>
						</xsl:when>
						<xsl:otherwise>
							<!-- Trim off any extra white space and change all :'s to -'s because I think the publisher splits around : or does something with it because anything before and :'s are lost and we want to mirror what they will see online -->
							<xsl:value-of select="translate(normalize-space(../Description), $colon, $esccolon)"/>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:when>
				<xsl:otherwise>
					<!-- Trim off any extra white space and change all :'s to -'s because I think the publisher splits around : or does something with it because anything before and :'s are lost and we want to mirror what they will see online -->
					<xsl:value-of select="translate(normalize-space(../Description), $colon, $esccolon)"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<!--  Check if we are in a InlineFigure or InlineEquation -->
		<xsl:variable name="ImageSrc">
			<xsl:value-of select="@src"/>
		</xsl:variable>
		<span>
    		<xsl:choose>
        		<xsl:when test="(name(parent::node()) = 'InlineFigure' or name(parent::node()) = 'InlineEquation') and ../@shiftby">
            		<xsl:variable name="shiftby">
        				<xsl:value-of select="../@shiftby"/>
            		</xsl:variable>
                    <xsl:variable name="shiftbya"><xsl:value-of select="normalize-space($shiftby)" /></xsl:variable>
            		<xsl:variable name="shiftbynew">
        				<xsl:choose>
        					<xsl:when test="$shiftbya &gt; 0">
        						<xsl:value-of select="0 - $shiftbya"/>
        					</xsl:when>
        					<xsl:otherwise>
        						<xsl:value-of select="$shiftbya * -1"/>
        					</xsl:otherwise>
        				</xsl:choose>
            		</xsl:variable>
    				<xsl:attribute name="style">position:relative;top:<xsl:value-of select="$shiftbynew"/>px</xsl:attribute>
        		</xsl:when>
    		</xsl:choose>
			<img src="{$ImageSrc}" alt="{$alt}"/>
		</span>
		<xsl:if test="name(parent::node()) != 'InlineEquation' and name(parent::node()) != 'InlineFigure'">
			<div class="gap2"><img src="/pix/spacer.gif" width="1" height="1" /></div>
		</xsl:if>
		<!-- need to break to next line to avoid more then 1 image going side by side enlarging the middle column - karl-->
	</xsl:template>
	<xsl:template match="MediaContent">
		<xsl:variable name="id">
			<xsl:value-of select="@id"/>
		</xsl:variable>
		<a name="{$id}"></a>
		<xsl:choose>
			<xsl:when test="contains(@type,'activity')">
				<p class="paradefault">
					<a href="{@src}" target="_new">Open <xsl:value-of select="substring-after(@type, 'activity')"/> now...</a>
				</p>
			</xsl:when>
			<xsl:otherwise>
				<xsl:apply-templates select="Description"/><!-- Caption | -removed now caption used below -->
				<xsl:choose>
					<xsl:when test="@type = 'audio'">
						<p class="paradefault">
							<a href="{@src}.mp3">
                                <xsl:if test="Caption != ''">
                                    <xsl:value-of select="Caption"/> -
                                </xsl:if>
                                Listen in separate player
                            </a>
							<br/>
							<font size="0.7em;">Click play to start.</font>
						</p>
					</xsl:when>
					<xsl:when test="@type = 'videoboth'">
						<p class="paradefault">
							<a href="{@src}.mp4" target="_blank">
                                Launch high resolution video
                                <xsl:if test="Caption != ''">
                                	- <xsl:value-of select="Caption"/>
                                </xsl:if>
                            </a>
						</p>
						<p class="paradefault">
							<a href="{@src}.flv">
                                Download low resolution video
                                <xsl:if test="Caption != ''">
                                	- <xsl:value-of select="Caption"/>
                                </xsl:if>
                            </a>
							<br/>
							<font size="0.7em;">Click play to start.</font>
						</p>
					</xsl:when>
					<xsl:when test="@type = 'videolow'">
						<p class="paradefault">
							<a href="{@src}.flv">
                                Download low resolution video
                                <xsl:if test="Caption != ''">
                                	- <xsl:value-of select="Caption"/>
                                </xsl:if>
                            </a>
							<br/>
							<font size="0.7em;">Click play to start.</font>
						</p>
					</xsl:when>
					<xsl:when test="@type = 'videohigh'">
						<p class="paradefault">
							<a href="{@src}.mp4" target="_blank">
                                Launch high resolution video
                                <xsl:if test="Caption != ''">
                                	- <xsl:value-of select="Caption"/>
                                </xsl:if>
                            </a>
						</p>
					</xsl:when>
					<xsl:when test="@type = 'flash'">
						<xsl:variable name="w">
							<xsl:choose>
								<xsl:when test="@width"><xsl:value-of select="@width" /></xsl:when>
								<xsl:otherwise><xsl:text>590</xsl:text></xsl:otherwise>
							</xsl:choose>
						</xsl:variable>
						<xsl:variable name="h">
							<xsl:choose>
								<xsl:when test="@height"><xsl:value-of select="@height" /></xsl:when>
								<xsl:otherwise><xsl:text>490</xsl:text></xsl:otherwise>
							</xsl:choose>
						</xsl:variable>
						<xsl:if test="name(..) != 'Preface'">
							<p class="paradefault">
								<a target="new" href="{@src}.swf">Launch in separate player</a>
							</p>
						</xsl:if>
						<p class="mediaplugin swf">
							<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="{$w}" height="{$h}" id="mp3player">
							 <param name="movie" value="{@src}.swf" />
							 <param name="quality" value="high" />
							 <param name="AllowScriptAccess" value="never" />
							 <embed src="{@src}.swf" quality="high" width="{$w}" height="{$h}" name="flashfilter" AllowScriptAccess="never" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">
							 </embed>
							</object>
						</p>
					</xsl:when>
					<xsl:when test="@type = 'pdf'">
						<p class="paradefault">
							<a target="_blank" href="{@src}.pdf">
                                View document
                                <xsl:if test="Caption != ''">
                                    about <xsl:value-of select="Caption"/>
                                </xsl:if>
                            </a>
						</p>
					</xsl:when>
					<xsl:when test="@type = 'xls'">
						<p class="paradefault">
							<a target="_blank" href="{@src}.xls">
                                View spreadsheet
                                <xsl:if test="Caption != ''">
                                    - <xsl:value-of select="Caption"/>
                                </xsl:if>
                            </a>
						</p>
					</xsl:when>
					<xsl:when test="@type = 'mov'">
						<p class="paradefault">
							<a target="_blank" href="{@src}.mov">
                                <xsl:if test="Caption != ''">
                                    <xsl:value-of select="Caption"/> -
                                </xsl:if>
                                Launch in separate player
                            </a>
						</p>
					</xsl:when>
				</xsl:choose>
				<xsl:apply-templates select="SourceReference"/>
                <xsl:if test="@supportDoc != ''">
                    <p class="paradefault">
                    <xsl:choose>
                        <xsl:when test="@type = 'flash'">
                            <a target="_blank" href="{@supportDoc}.pdf">View description</a>
                        </xsl:when>
                        <xsl:otherwise>
                            <a target="_blank" href="{@supportDoc}.pdf">
                                View transcript
                                <xsl:if test="Caption != ''">
                                    of <xsl:value-of select="Caption"/>
                                </xsl:if>
                            </a>
                        </xsl:otherwise>
                    </xsl:choose>
                    </p>
                </xsl:if>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<!--
	  Ignore the ItemRights tags as we don't want them outputting any data, they
	  are used by the publisher to create a html file that is linked to from the
	  SourceReference tag. See below where it mentions ItemRights/RefURL inside
	  <xsl:template match="SourceReference">
	-->
	<xsl:template match="ItemRights" />
	<xsl:template match="SourceReference">
		<xsl:variable name="sourceref">
    		<xsl:apply-templates/>
		</xsl:variable>
		<xsl:if test="$sourceref != ''">
    		<p class="paradefault">
    		    <xsl:value-of select="$sourceref"/>
				<xsl:if test="ItemRights/ItemAcknowledgement">
				  <br/><xsl:apply-templates select="ItemRights/ItemAcknowledgement" />
				</xsl:if>
    		</p>
		</xsl:if>
	</xsl:template>

	<xsl:template name="LearningOutcomes">
		<xsl:param name="currentFile"/>
		<div align="center">
		<div class="boxcontent" align="left">
		<h2>Learning Outcomes</h2>
		<xsl:apply-templates select="$currentFile//Item/FrontMatter/LearningOutcomes/Paragraph"/>
		    <ul>
			    <xsl:for-each select="$currentFile//Item/FrontMatter/LearningOutcomes/LearningOutcome">
				    <li class="ListItem">
				        <xsl:apply-templates/>
				    </li>
			    </xsl:for-each>
		    </ul>
		 </div>
		 </div>
	</xsl:template>
	<!--xsl:template match="text()">
		<xsl:value-of select="." disable-output-escaping="no"/>
	</xsl:template-->

    <!-- *********************************** common cartridge includes-->
    <xsl:template name="create_weblinks">
        <xsl:param name="metadataFile"/>
        <xsl:param name="CourseId"/>
        <xsl:variable name="cc_path" select="concat($basepath,$CourseId,'/')"/>
        <xsl:for-each select="$metadataFile//unit[identifier=$CourseId]/relations/relation">
            <xsl:variable name="pos" select="position()"/>
            <xsl:call-template name="doweblink">
                <xsl:with-param name="where" select="$cc_path"/>
                <xsl:with-param name="weblink" select="."/>
                <xsl:with-param name="pos" select="$pos"/>
            </xsl:call-template>
        </xsl:for-each>
    </xsl:template>

    <xsl:template name="doweblink">
        <xsl:param name="where"/>
        <xsl:param name="weblink"/>
        <xsl:param name="pos"/>
         <xsl:variable name="linkfile" select="concat($where,$weblinkdirname,$pos,'/',$weblinkfilename,$pos,'.xml')"/>
        <!-- <xsl:value-of select="$linkfile"/> -->
         <xsl:result-document href="{$linkfile}" format="cc-format">
        <wl:webLink xmlns:wl="http://www.imsglobal.org/xsd/imswl_v1p0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            <title><xsl:value-of select="$weblink/urltitle"/></title>
            <xsl:element name="url">
                <xsl:attribute name="href"><xsl:value-of select="$weblink/url"/></xsl:attribute>
                <xsl:attribute name="target">_blank</xsl:attribute>
            </xsl:element>
        </wl:webLink>
        </xsl:result-document>
    </xsl:template>

    <xsl:template name="create_forums">
        <xsl:param name="CourseId"/>
        <xsl:param name="ForumFile"/>
        <xsl:variable name="cc_path" select="concat($basepath,$CourseId,'/')"/>
        <!-- the forums are standard , so we'll just read the forums xml file and create them -->
        <!-- and we have to do a foreach just so we can pass the where variable down, sigh -->
        <!-- we look for all forums for this courseid, if there are none, we look fo rdefaults. then we look for the ones for unit = all, cos they are for all courses, suprise, suprise -->
        <xsl:variable name="what">
            <xsl:choose>
                <xsl:when test="count($ForumFile//unit[identifier=$CourseId]/forum) = 0">default</xsl:when>
                <xsl:otherwise><xsl:value-of select="$CourseId"/></xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <xsl:for-each select="$ForumFile//unit[identifier=$what]/forum|$ForumFile//unit[identifier='all']/forum">
            <xsl:variable name="pos" select="position()"/>
            <xsl:call-template name="doforum">
                <xsl:with-param name="where" select="$cc_path"/>
                <xsl:with-param name="forum" select="."/>
                <xsl:with-param name="pos" select="$pos"/>
            </xsl:call-template>
        </xsl:for-each>
    </xsl:template>

    <xsl:template name="doforum">
        <xsl:param name="where"/>
        <xsl:param name="forum"/>
        <xsl:param name="pos"/>
        <xsl:variable name="linkfile" select="concat($where,$forumdirname,$pos,'/',$forumfilename,$pos,'.xml')"/>
        <!-- <xsl:value-of select="$linkfile"/> -->
        <xsl:result-document href="{$linkfile}" format="cc-format">
            <xsl:element name="dt:topic" namespace="dt:http://www.imsglobal.org/xsd/imsdt_v1p0">
                <xsl:attribute namespace="xsi:http://www.w3.org/2001/XMLSchema-instance" name="xsi:schemaLocation">http://www.imsglobal.org/xsd/imsdt_v1p0/imscc_cam0p04\imsdt_fromdoc.xsd</xsl:attribute>
                <xsl:apply-templates select="$forum/title|$forum/text" mode="forum"/>
            </xsl:element>
        </xsl:result-document>
    </xsl:template>

    <xsl:template match="title|text" mode="forum">
	   <xsl:copy-of select="."/>
    </xsl:template>
    <!-- *********************************** -->

	<xsl:template match="TeX">
        $$<xsl:apply-templates/>$$
	</xsl:template>

</xsl:stylesheet>

