<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:markup="http://ws.staywell.com/markup"
	xmlns:sw="http://ws.staywell.com" 
	exclude-result-prefixes="markup sw">
	<xsl:param name="GlossaryPage" />
	<xsl:param name="ImagePage" />
	<xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="no"/>

	<xsl:template match="/">
		<html>
			<head>
				<title><xsl:apply-templates select="//markup:RegularTitle"/></title>
				<meta name="Unique" content="{/sw:Document/@ContentTypeId}-{/sw:Document/@ContentId}" />
				<xsl:apply-templates select="/sw:Document/sw:Indexing/sw:Keywords[sw:Keyword]"/>
				<xsl:apply-templates select="/sw:Document/sw:Indexing/sw:MeSHList[sw:MeSH]"/>
				<meta name="DocumentType" content="Patient Education" />
				<xsl:call-template name="ICD9"/>
				<xsl:call-template name="CPT"/>
				<xsl:apply-templates select="/sw:Document/sw:AgeGroups[sw:AgeGroup]"/>
				<xsl:apply-templates select="/sw:Document/sw:Gender"/>
				<meta name="Language" content="{/sw:Document/sw:Language}" />
				<meta name="Source" content="Staywell" />
			</head>
			<body>
				<xsl:apply-templates select="//markup:body"/>
				<xsl:apply-templates select="//sw:NonValidBody"/>
				<div id="CopyrightStatement"><xsl:value-of select="CopyrightStatement"/></div>
			</body>
		</html>
	</xsl:template>

	<xsl:template match="markup:RegularTitle">
		<xsl:apply-templates />
	</xsl:template>

	<xsl:template match="sw:NonValidBody">
		<xsl:for-each select="*">
			<xsl:apply-templates />
		</xsl:for-each>
	</xsl:template>
	
	<xsl:template match="sw:Keywords">
		<xsl:for-each select="sw:Keyword">
			<meta name="Index entry" content="{.}" />
		</xsl:for-each>
	</xsl:template>
	<xsl:template match="sw:MeSHList">
		<xsl:for-each select="sw:MeSH">
			<meta name="ICD-9 codes" content="{@code}"/>
			<xsl:variable name="theCode" select="@code" />
			<xsl:for-each select="document('GetMeSHList.xml')/sw:MeSHList/sw:MeSH[@code=$theCode]/sw:Term">
				<xsl:element name="meta">
					<xsl:attribute name="name">Index entry</xsl:attribute>
					<xsl:attribute name="content">
						<xsl:value-of select="." />
					</xsl:attribute>
				</xsl:element>
			</xsl:for-each>
		</xsl:for-each>
	</xsl:template>
	
	<xsl:template name="ICD9">
		<xsl:for-each select="/sw:Document/sw:Indexing/sw:ICD9List/sw:ICD9">
			<meta name="ICD-9 codes" content="{@code}"/>
		</xsl:for-each>
	</xsl:template>

	<xsl:template name="CPT">
		<xsl:for-each select="/sw:Document/sw:Indexing/sw:CPTList/sw:CPT">
			<meta name="CPT codes" content="{@code}"/>
		</xsl:for-each>
	</xsl:template>
	
	<xsl:template match="sw:AgeGroups">
		<xsl:element name="meta">
			<xsl:attribute name="name">Age</xsl:attribute>
			<xsl:attribute name="content">
				<xsl:choose>
					<xsl:when test="sw:AgeGroup[1] = 'Infant (0 - 11 mo)'">0</xsl:when>
					<xsl:when test="sw:AgeGroup[1] = 'Childhood (11 mo - 12 yrs)'">1</xsl:when>
					<xsl:when test="sw:AgeGroup[1] = 'Teen (12 - 18 yrs)'">12</xsl:when>
					<xsl:when test="sw:AgeGroup[1] = 'Adult (18+)'">18</xsl:when>
					<xsl:when test="sw:AgeGroup[1] = 'Senior'">60</xsl:when>
				</xsl:choose>
				<xsl:text>-</xsl:text>
				<xsl:choose>
					<xsl:when test="sw:AgeGroup[last()] = 'Infant (0 - 11 mo)'">1</xsl:when>
					<xsl:when test="sw:AgeGroup[last()] = 'Childhood (11 mo - 12 yrs)'">12</xsl:when>
					<xsl:when test="sw:AgeGroup[last()] = 'Teen (12 - 18 yrs)'">18</xsl:when>
					<xsl:when test="sw:AgeGroup[last()] = 'Adult (18+)' or sw:AgeGroup[last()] = 'Senior'"></xsl:when>
				</xsl:choose>
			</xsl:attribute>
		</xsl:element>
	</xsl:template>
	
	<xsl:template match="sw:Gender">
		<xsl:element name="meta">
			<xsl:attribute name="name">Gender</xsl:attribute>
			<xsl:attribute name="content">
				<xsl:choose>
					<xsl:when test=". = 'Female'">female</xsl:when>
					<xsl:when test=". = 'Male'">male</xsl:when>
					<xsl:when test=". = 'All'">both</xsl:when>
				</xsl:choose>
			</xsl:attribute>
		</xsl:element>
	</xsl:template>

	<xsl:template match="*|@*">
			<xsl:copy>
				<xsl:apply-templates select="node()|@*|text()"/>
			</xsl:copy>
	</xsl:template>
	
	<xsl:template match="comment()"><!-- doing this so that < > and & embedded in the comment don't get "escaped" as &lt; &gt; &amp; respectively -->
		<xsl:text disable-output-escaping="yes">&lt;!--</xsl:text>
			<xsl:value-of select="." disable-output-escaping="yes"/>
		<xsl:text disable-output-escaping="yes">--&gt;</xsl:text>
	</xsl:template>
	
	<xsl:template match="markup:*">
		<xsl:element name="{name()}">
			<xsl:apply-templates select="node()|@*|text()"/>
		</xsl:element>
	
	</xsl:template>

	<xsl:template match="markup:body">
		<div class="buct{/sw:Document/@ContentTypeId}">
			<xsl:apply-templates/>
		</div>
	</xsl:template>

	<xsl:template match="markup:Page">
		<xsl:apply-templates/>
		<hr/>
	</xsl:template>
	
	<xsl:template match="markup:Hotfield">
		<xsl:apply-templates />
	</xsl:template>

	<xsl:template match="markup:p" mode="#all">
		<p>
			<xsl:apply-templates/>
			<xsl:if test="ancestor::markup:td and not(descendant::text()) and not(descendant::markup:img)"><!-- if a table cell is empty, fill it with a space so browsers display borders on the cells -->
				&#160;
			</xsl:if>
		</p>
	</xsl:template>

	<xsl:template match="markup:a">
		<a href="{@href}" title="{@title}" target="_blank"><xsl:apply-templates/></a>
	</xsl:template>

	<xsl:template match="markup:ContentLink [@ContentTypeId=18 or @ContentTypeId=36 or @ContentTypeId=44]">
		<a class="GlossaryLink" href="{$GlossaryPage}?TypeId={@ContentTypeId}&amp;ContentId={@ContentId}" onclick="JavaScript:return InitPopup('{$GlossaryPage}?TypeId={@ContentTypeId}&amp;ContentId={@ContentId}','toolbar=no,location=no,status=no,menubar=no,resizable=no,scrollbars=yes,left=80,top=80,width=700,height=200');"><xsl:apply-templates/></a>
	</xsl:template>

	<xsl:template match="markup:ContentLink">
		<a href="{@ContentTypeId},{@ContentId}"><xsl:if test="@class and @class != ''"><xsl:attribute name="class"><xsl:value-of select="@class"/></xsl:attribute></xsl:if><xsl:apply-templates/></a>
	</xsl:template>

	<xsl:template match="markup:Box">
		<div class="Box">
			<xsl:if test="@width">
				<xsl:attribute name="style">width: <xsl:value-of select="@width" />;</xsl:attribute>
			</xsl:if>
			<xsl:apply-templates />
		</div>
	</xsl:template>
	
	<xsl:template match="markup:Section">
		<xsl:if test="@id">
		<a name="{@id}"/>
		</xsl:if>
		<xsl:apply-templates />
	</xsl:template>
	
	<xsl:template match="markup:img" mode="#all">
		<xsl:choose>
			<xsl:when test="@title and @title != ''">
				<div class="img {@class}" style="width: {@width}px;">
					<xsl:choose>
						<xsl:when test="@expandedimageid != '' and @expandedimageid">
							<!--NewWindow function sets exp img display window to size of Exp Image height & width without displayng toolbars etc. Additional pixels added to windows height & width to offset bordering imposed by browser -->
							<a href="{$ImagePage}?ImageId={@expandedimageid}.img" onclick="javascript:return InitPopup('{$ImagePage}?ImageId={@expandedimageid}.img','toolbar=no,location=no,status=no,menubar=no,resizable=no,scrollbars=yes,left=80,top=80,width={@expimgwidth+40},height={@expimgheight+30}');"><img src="../Images/{@imageid}.img" alt="{@alt}" height="{@height}" width="{@width}" class="{@class}" border="0" /></a>
						</xsl:when>
						<xsl:otherwise>
							<img src="../Images/{@imageid}.img" alt="{@alt}" class="{@class}" border="0">
							<xsl:if test="@width and @width != ''">
								<xsl:attribute name="width"><xsl:value-of select="@width"/></xsl:attribute>
								<xsl:attribute name="height"><xsl:value-of select="@height"/></xsl:attribute>
							</xsl:if>
							</img>
						</xsl:otherwise>
					</xsl:choose>
					<div class="caption">
						<xsl:value-of select="@title" />
					</div>
				</div>	
			</xsl:when>
			<xsl:otherwise>
				<xsl:choose>
					<xsl:when test="@expandedimageid != '' and @expandedimageid">
						<!--NewWindow function sets exp img display window to size of Exp Image height & width without displayng toolbars etc. Additional pixels added to windows height & width to offset bordering imposed by browser -->
						<a href="{$ImagePage}?ImageId={@expandedimageid}.img" onclick="javascript:return InitPopup('{$ImagePage}?ImageId={@expandedimageid}.img','toolbar=no,location=no,status=no,menubar=no,resizable=no,scrollbars=yes,left=80,top=80,width={@expimgwidth+40},height={@expimgheight+30}');"><img src="../Images/{@imageid}.img" alt="{@alt}" height="{@height}" width="{@width}" class="{@class}" border="0" /></a>
					</xsl:when>
					<xsl:otherwise>
						<img src="../Images/{@imageid}.img" alt="{@alt}" class="{@class}" border="0">
						<xsl:if test="@width and @width != ''">
							<xsl:attribute name="width"><xsl:value-of select="@width"/></xsl:attribute>
              				<xsl:attribute name="height"><xsl:value-of select="@height"/></xsl:attribute>
						</xsl:if>
						</img>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

  <xsl:template match="markup:media">
    <xsl:choose>
      <xsl:when test="@mimetype='video/x-flv'">
        <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="{@width}" height="{@height}">
          <param name="movie" value="player.swf"/>
          <param name="allowScriptAccess" value="always"/>
          <param name="quality" value="high"/>
          <param name="allowFullScreen" value="true"/>
          <param name="FlashVars" value="filename={@mediaurl}"/>
          <param name="wmode" value="transparent" />
          <embed src="player.swf" width="{@width}" height="{@height}" quality="high" allowFullScreen="true" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" FlashVars="filename={@mediaurl}" wmode="transparent"></embed>
        </object>
      </xsl:when>
      <xsl:when test="@mimetype='application/pdf'">
        <xsl:choose>
          <xsl:when test="@title != '' and @title">
            <a href="/displayimage.aspx?{@mediaid}.pdf"><xsl:value-of select="@title"/></a>
          </xsl:when>
          <xsl:otherwise>
            <a href="/displayimage.aspx?{@mediaid}.pdf">PDF Link</a>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:when>
      <xsl:when test="@mimetype='application/x-shockwave-flash'">
        <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="{@width}" height="{@height}">
          <param name="movie" value="/displayimage.aspx?{@mediaid}.swf"/>
          <param name="allowScriptAccess" value="always"/>
          <param name="quality" value="high"/>
          <xsl:if test="@flashvars and @flashvars != ''">
          	<param name="FlashVars" value="{@flashvars}"/>
          </xsl:if>
          <embed src="/displayimage.aspx?{@mediaid}.swf" width="{@width}" height="{@height}" quality="high" allowFullScreen="true" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash">
					  <xsl:if test="@flashvars and @flashvars != ''">
						  <xsl:attribute name="FlashVars">
							  <xsl:value-of select="@flashvars"/>
						  </xsl:attribute>
					  </xsl:if>
          </embed>			
				</object>
      </xsl:when>
    </xsl:choose>
  </xsl:template>

	<xsl:template match="markup:ul">
		<ul>
			<xsl:apply-templates/>
		</ul>
	</xsl:template>
	
	<xsl:template match="markup:li">
		<li>
			<xsl:apply-templates/>
		</li>
	</xsl:template>
	
	<xsl:template match="markup:ol">
		<ol class="text">
			<xsl:apply-templates />
		</ol>
	</xsl:template>

	<!-- ***************************************************** Table mode row ***************************************************** -->
	<!-- All Decision Guides are of this type (and most other tables in HI) -->
	<xsl:template match="markup:table [@type='row']">
   		<xsl:if test="contains(translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'decision guide for')">
			<a name="#decisionguide"/>
		</xsl:if>
   		 <table cellpadding="3" cellspacing="1" border="0" width="100%">
   			<xsl:if test="./@title">
				<tr>
					<!-- Set the colspan to 80 (odds are the table won't have that many columns) -->
					<th colspan="80" class="charthead">
						<xsl:value-of select="./@title" />
					</th>
				</tr>
			</xsl:if>
			<xsl:apply-templates mode="row"/>
		</table>
	</xsl:template>
	
	<xsl:template match="markup:caption" mode="row">
		<tr>
			<!-- Set the colspan to 80 (odds are the table won't have that many columns) -->
			<td colspan="80" class="charthead">
				<xsl:value-of select="." />
			</td>
		</tr>
	</xsl:template>
	
	<xsl:template match="markup:tr" mode="row">
		<tr>
			<xsl:apply-templates mode="row"/>
		</tr>
	</xsl:template>
	
	<xsl:template match="markup:th" mode="row">
		<!-- set the colspan if it exists, otherwise, treat it as a 'sub-caption' that spans all columns -->
		<xsl:choose>
			<xsl:when test="@colspan !='' and @colspan">
				<th colspan="{@colspan}" class="chartsub">
					<xsl:if test="@width and @width != ''">
						<xsl:attribute name="width"><xsl:value-of select="@width"/></xsl:attribute>
					</xsl:if>
					<xsl:apply-templates mode="row"/>
					<xsl:if test="not(descendant::text()) and not(descendant::markup:img)"><!-- if a table cell is empty, fill it with a space so browsers display borders on the cells -->
						&#160;
					</xsl:if>
				</th>
			</xsl:when>
			<xsl:otherwise>
				<th class="chartsub">
					<xsl:if test="@width and @width != ''">
						<xsl:attribute name="width"><xsl:value-of select="@width"/></xsl:attribute>
					</xsl:if>
					<xsl:apply-templates />
					<xsl:if test="not(descendant::text()) and not(descendant::markup:img)"><!-- if a table cell is empty, fill it with a space so browsers display borders on the cells -->
						&#160;
					</xsl:if>
				</th>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
	<xsl:template match="markup:td" mode="row">
		<xsl:if test="count(parent::*/preceding-sibling::*) mod 2 = 1">
			<td colspan="{@colspan}" class="chartcolor1" valign="top">
				<xsl:if test="@width and @width != ''">
					<xsl:attribute name="width"><xsl:value-of select="@width"/></xsl:attribute>
				</xsl:if>
				<xsl:apply-templates mode="#all"/>
				<xsl:if test="not(descendant::text()) and not(descendant::markup:img)"><!-- if a table cell is empty, fill it with a space so browsers display borders on the cells -->
					&#160;
				</xsl:if>
			</td>
		</xsl:if>
		<xsl:if test="count(parent::*/preceding-sibling::*) mod 2 = 0">
			<td colspan="{@colspan}" class="chartcolor2" valign="top">
				<xsl:if test="@width and @width != ''">
					<xsl:attribute name="width"><xsl:value-of select="@width"/></xsl:attribute>
				</xsl:if>
				<xsl:apply-templates mode="#all"/>
				<xsl:if test="not(descendant::text()) and not(descendant::markup:img)"><!-- if a table cell is empty, fill it with a space so browsers display borders on the cells -->
					&#160;
				</xsl:if>
			</td>
		</xsl:if>
	</xsl:template>
	<!-- ***************************************************** Table mode row ***************************************************** -->

	<!-- *************************************************** Table mode column *************************************************** -->
	<!-- Probably not used in Wellness / Welladvised -->
	<!-- This will need to be debugged when more content is created with this table type -->
	<xsl:template match="markup:table [@type='column']">
		<table border="1" cellpadding="2" cellspacing="0" bordercolor="#FFFFFF">
			<xsl:apply-templates mode="column"/>
		</table>
	</xsl:template>
	<xsl:template match="markup:caption" mode="column">
			<tr>
				<td colspan="80" class="charthead">
					<xsl:apply-templates />
				</td>
			</tr>
	</xsl:template>

	<xsl:template match="markup:tr" mode="column">
		<tr>
			<xsl:apply-templates mode="column"/>
		</tr>
	</xsl:template>
	
	<xsl:template match="markup:th" mode="column">
		<th colspan="{@colspan}" class="head{position() mod 2}">
			<xsl:if test="@width and @width != ''">
				<xsl:attribute name="width"><xsl:value-of select="@width"/></xsl:attribute>
			</xsl:if>
			<xsl:apply-templates/>
			<xsl:if test="not(descendant::text()) and not(descendant::markup:img)"><!-- if a table cell is empty, fill it with a space so browsers display borders on the cells -->
				&#160;
			</xsl:if>
		</th>
	</xsl:template>
	
	<xsl:template match="markup:td" mode="column">
		<td colspan="{@colspan}" class="cell{position() mod 2}">
			<xsl:if test="@width and @width != ''">
				<xsl:attribute name="width"><xsl:value-of select="@width"/></xsl:attribute>
			</xsl:if>
			<xsl:apply-templates/>
			<xsl:if test="not(descendant::text()) and not(descendant::markup:img)"><!-- if a table cell is empty, fill it with a space so browsers display borders on the cells -->
				&#160;
			</xsl:if>
		</td>
	</xsl:template>
	<!-- ***************************************************** Table mode column ***************************************************** -->
	
	<!-- ******************************************************* Table mode grid ******************************************************* -->
	<xsl:template match="markup:table [@type='grid']">
		<table border="1" cellpadding="2" cellspacing="0" bordercolor="#606060">
			<xsl:apply-templates mode="grid"/>
		</table>
	</xsl:template>
	<xsl:template match="markup:caption" mode="grid">
		<tr>
			<td colspan="80" class="charthead">
				<xsl:value-of select="."/>
			</td>
		</tr>
	</xsl:template>
	<xsl:template match="markup:tr" mode="grid">
		<tr>
			<xsl:apply-templates mode="grid"/>
		</tr>
	</xsl:template>
	<xsl:template match="markup:th" mode="grid">
		<th colspan="{@colspan}" class="head0">
			<xsl:if test="@width and @width != ''">
				<xsl:attribute name="width"><xsl:value-of select="@width"/></xsl:attribute>
			</xsl:if>
			<xsl:apply-templates/>
			<xsl:if test="not(descendant::text()) and not(descendant::markup:img)"><!-- if a table cell is empty, fill it with a space so browsers display borders on the cells -->
				&#160;
			</xsl:if>
		</th>
	</xsl:template>
	<xsl:template match="markup:td" mode="grid">
		<xsl:if test="count(parent::*/preceding-sibling::*) mod 2 = 1">
			<td valign="top" colspan="{@colspan}" class="cell0">
				<xsl:if test="@width and @width != ''">
					<xsl:attribute name="width"><xsl:value-of select="@width"/></xsl:attribute>
				</xsl:if>
				<xsl:apply-templates/>
				<xsl:if test="not(descendant::text()) and not(descendant::markup:img)"><!-- if a table cell is empty, fill it with a space so browsers display borders on the cells -->
					&#160;
				</xsl:if>
			</td>
		</xsl:if>
		<xsl:if test="count(parent::*/preceding-sibling::*) mod 2 = 0">
			<td valign="top" colspan="{@colspan}" class="cell{position() mod 2}">
				<xsl:if test="@width and @width != ''">
					<xsl:attribute name="width"><xsl:value-of select="@width"/></xsl:attribute>
				</xsl:if>
				<xsl:apply-templates/>
				<xsl:if test="not(descendant::text()) and not(descendant::markup:img)"><!-- if a table cell is empty, fill it with a space so browsers display borders on the cells -->
					&#160;
				</xsl:if>
			</td>
		</xsl:if>
	</xsl:template>
	<!-- ******************************************************* Table mode grid ******************************************************* -->

	<!-- ******************************************************* Table mode standard ******************************************************* -->
	<xsl:template match="markup:table [@type='standard']">
		<table border="1" cellpadding="2" cellspacing="0" bordercolor="#333333">
			<xsl:apply-templates mode="standard"/>
		</table>
	</xsl:template>
	<xsl:template match="markup:caption" mode="standard">
		<tr>
			<td colspan="80" class="charthead">
				<xsl:value-of select="."/>
			</td>
		</tr>
	</xsl:template>
	<xsl:template match="markup:tr" mode="standard">
		<tr>
			<xsl:apply-templates mode="standard"/>
		</tr>
	</xsl:template>
	<xsl:template match="markup:th" mode="standard">
		<th colspan="{@colspan}" class="ChartStandardHead">
			<xsl:if test="@width and @width != ''">
				<xsl:attribute name="width"><xsl:value-of select="@width"/></xsl:attribute>
			</xsl:if>
			<xsl:apply-templates/>
			<xsl:if test="not(descendant::text()) and not(descendant::markup:img)"><!-- if a table cell is empty, fill it with a space so browsers display borders on the cells -->
				&#160;
			</xsl:if>
		</th>
	</xsl:template>
	<xsl:template match="markup:td" mode="standard">
		<td valign="top" colspan="{@colspan}" class="cell0">
			<xsl:if test="@width and @width != ''">
				<xsl:attribute name="width"><xsl:value-of select="@width"/></xsl:attribute>
			</xsl:if>
			<xsl:apply-templates/>
			<xsl:if test="not(descendant::text()) and not(descendant::markup:img)"><!-- if a table cell is empty, fill it with a space so browsers display borders on the cells -->
				&#160;
			</xsl:if>
		</td>
	</xsl:template>
	<!-- ******************************************************* Table mode grid ******************************************************* -->
</xsl:stylesheet>

