<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:v="urn:schemas-microsoft-com:vml"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:aka="http:/akanda.com"
>

<msxsl:script language="JScript" implements-prefix="aka">
var aShown = new Array();
function IsNewElement(uid) {
	//if (uid == null) return false;
	//if (!uid) return false;
	var val = uid[0].text; //it's nodelist
	var isNew = (!IsInShown(val));
	if (isNew) aShown[aShown.length] = val; 
	return isNew;	
} 

function IsInShown(val) {
	var e = new Enumerator(aShown);
	for (; !e.atEnd(); e.moveNext()) {
		if (e.item() == val) return true;
	}
	return false;
} 

function AnyNewElement(nodes) {
	var node = nodes.nextNode();
	while(node) {
		var id = node.selectSingleNode("@id").text;
		if (!IsInShown(id)) return true; //at least one new
		node = nodes.nextNode();
	}
	return false;
} 
</msxsl:script>


<!--
<xsl:output method="text" version="1.0" encoding="UTF-8" indent="no" /> 
-->

<xsl:template match="ELEMENTS">
	<center>
	<table valign="top" border="0" cellpadding="0" cellspacing="0">
		<tr>
			<td align="center" valign="top">
				<xsl:apply-templates select="ELEMENT[PREV_ID = '0']" /> <!-- start element -->
			</td>
		</tr>
		<tr>
			<xsl:apply-templates select="ELEMENT[PREV_ID = //ELEMENT[PREV_ID = '0']/@id]" />
		</tr>
	</table>
	</center>
</xsl:template>

<!-- Chart element -->
<xsl:template match="ELEMENT[TYPE_ID = '1' or TYPE_ID = '4']">
	<xsl:variable name="uid"><xsl:value-of select="@id" /></xsl:variable>
	<xsl:variable name="prevId"><xsl:value-of select="PREV_ID" /></xsl:variable>
	<xsl:variable name="nextId"><xsl:value-of select="NEXT_ID" /></xsl:variable>	
	<xsl:variable name="colSpan"><xsl:value-of select="count(//ELEMENT[NEXT_ID = $nextId])" /></xsl:variable>
	<xsl:variable name="colSpan2"><xsl:value-of select="count(//ELEMENT[PREV_ID = $prevId])" /></xsl:variable>
	
	<xsl:if test="//ELEMENT[PREV_ID = $prevId] and aka:AnyNewElement(//ELEMENT[PREV_ID = $prevId])">
	<td align="center" valign="top">
	<table valign="top" border="0" cellpadding="0" cellspacing="0" borderColor="green">
<!--	
			<tr height="20">
				<td>
				<xsl:attribute name="colspan"><xsl:value-of select="$colSpan2" /></xsl:attribute> 
				&#160;
				</td>
			</tr>
-->			
			<tr>
				<!-- show element with all siblings -->
				<xsl:for-each select="//ELEMENT[PREV_ID = $prevId]">
				<xsl:variable name="thisNextId"><xsl:value-of select="NEXT_ID" /></xsl:variable>
				<xsl:if test="aka:IsNewElement(@id)">
				<td align="center" valign="top" nowrap="1">

					<!-- the element -->
					<table cellpadding="0" cellspacing="0" valign="top">
						<tr height="20">
							<td>
								&#160;
							</td>	
						</tr>

						<tr>
							<td width="10">&#160;</td>
							<td align="center" valign="top">
							
								<xsl:choose>
								
								<xsl:when test="TYPE_ID = '4'">
									<v:roundrect id="chartItem" style="width: 98%; height: 16" fillcolor="green" arcsize="0.4">
										<xsl:attribute name="uid"><xsl:value-of select="@id" /></xsl:attribute>
										<xsl:attribute name="pid"><xsl:value-of select="PREV_ID" /></xsl:attribute>
										<xsl:attribute name="nid"><xsl:value-of select="NEXT_ID" /></xsl:attribute>
										<v:fill color2="#33cc00" type="gradient"> 
										<v:textbox style="padding-left:2; v-text-anchor: middle-center;">
											<table valign="center" width="100%" height="100%" cellpadding="0" cellspacing="0">
												<tr>
													<td nowrap="1" style="color: white; font-size: 11px; font-family: Verdana; width:100%; height:100%;" valign="middle" align="center">
														&#160;<xsl:value-of select="NAME" />&#160;
													</td>	
												</tr>
											</table>
										</v:textbox> 
										</v:fill>
									</v:roundrect>
								</xsl:when>
								
								<xsl:otherwise>
								
								<table valign="top" border="1" borderColor="black" style="cursor:pointer;" onclick="ElementClick(this)" cellpadding="0" cellspacing="1" id="chartItem" width="110">
									<xsl:attribute name="title"><xsl:value-of select="DESC" /></xsl:attribute>
									<xsl:attribute name="uid"><xsl:value-of select="@id" /></xsl:attribute>
									<xsl:attribute name="pid"><xsl:value-of select="PREV_ID" /></xsl:attribute>
									<xsl:attribute name="nid"><xsl:value-of select="NEXT_ID" /></xsl:attribute>

									<tr height="20">
										<td align="center" valign="center" style="background-color: blue; color: yellow; font-size:11px; " nowrap="1">
											&#160;<b><xsl:value-of select="NAME" /></b>&#160;
										</td> 
									</tr>
									<tr height="40">
										<td align="center" valign="center" style="background-color: #ffffcc;">
											<v:roundrect style="width: 90; height: 15" fillcolor="#00ffff" arcsize="0.4">
												<v:fill>
												<v:textbox style="padding-left:2; v-text-anchor: middle-center;">
													<table valign="top" width="100%" height="100%" cellpadding="0" cellspacing="0">
														<tr>
															<td style="color: black; font-size: 11px; font-family: Verdana; width:100%; height:100%;" valign="middle" align="center">
																<xsl:value-of select="WHO" />
															</td>	
														</tr>
													</table>
												</v:textbox> 
												</v:fill>
											</v:roundrect>
										</td>	
									</tr>
								</table>
								
								</xsl:otherwise>
								</xsl:choose>
								
							</td>
							<td width="10">&#160;</td>
						</tr>
					</table>
					
					<!-- element's children -->
<!--					
					<xsl:if test="//ELEMENT[PREV_ID = $uid]" />
					<table border="0">
						<tr>
							<xsl:apply-templates select="//ELEMENT[PREV_ID = $uid] " />
						</tr>
					</table>
					</xsl:if>
					
					<xsl:if test="//ELEMENT[@id = $nextId]" />
					<table border="0">
						<tr>
							<xsl:apply-templates select="//ELEMENT[@id = $nextId] " />
						</tr>
					</table>
					</xsl:if>
-->					
					<xsl:if test="(count(//ELEMENT[NEXT_ID = $thisNextId]) != count(//ELEMENT[PREV_ID = $prevId])) and (count(//ELEMENT[NEXT_ID = $thisNextId]) = 1) "> <!-- not all current nodes have the same NEXT_ID -->
					<xsl:if test="//ELEMENT[@id = $thisNextId] and aka:IsNewElement($thisNextId)">
					<table valign="top">
						<!-- next node -->
						<!-- next table=<xsl:value-of select="$thisNextId" /> -->
						<xsl:apply-templates select="//ELEMENT[@id = $thisNextId] " />
					</table>	
					</xsl:if>
					</xsl:if>
					
				</td>
				</xsl:if>
				</xsl:for-each>
			</tr>
			
		</table>	
		
			<xsl:if test="aka:IsNewElement($nextId) and ( count(//ELEMENT[NEXT_ID = $nextId]) = count(//ELEMENT[PREV_ID = $prevId]) or count(//ELEMENT[NEXT_ID = $nextId]) != 1 )">
			<table border="0" cellpadding="0" cellspacing="0">
			<tr>
				<td align="center" valign="top">
					<xsl:attribute name="colspan"><xsl:value-of select="$colSpan" /></xsl:attribute> 
					<!-- next row=<xsl:value-of select="$nextId" /> -->
					<table>
						<xsl:apply-templates select="//ELEMENT[@id = $nextId] " />
					</table>	
				</td>	
			</tr>
			</table>
			</xsl:if>
		
	</td>
	
	</xsl:if>
	
</xsl:template>


<!-- Node -->
<xsl:template match="ELEMENT[TYPE_ID = '2']">
	<xsl:variable name="uid"><xsl:value-of select="@id" /></xsl:variable>
	<tr>
		<td align="center" valign="top">
		<xsl:attribute name="colspan"><xsl:value-of select="count(//ELEMENT[NEXT_ID = $uid])" /></xsl:attribute> 
		<!--
			colspan=<xsl:value-of select="count(//ELEMENT[NEXT_ID = $uid])" />
		-->	
			<!-- Id=<xsl:value-of select="@id" /> -->
			<v:oval id="chartItem" style="align: center; width: 1; height: 1" tid="2" position="20 0">
				<xsl:attribute name="uid"><xsl:value-of select="@id" /></xsl:attribute>
				<xsl:attribute name="pid"><xsl:value-of select="PREV_ID" /></xsl:attribute>
				<xsl:attribute name="nid"><xsl:value-of select="NEXT_ID" /></xsl:attribute>
				<v:stroke on="false" />
				<v:fill color="red" opacity="1" />
			</v:oval>
		</td>	
	</tr>
	<xsl:if test="//ELEMENT[PREV_ID = $uid]">
	<tr>
		<td align="center" valign="top">
			<xsl:attribute name="colspan"><xsl:value-of select="count(//ELEMENT[NEXT_ID = $uid])" /></xsl:attribute> 
			<table valign="top" border="0" borderColor="lightblue">
				<tr>
					<xsl:apply-templates select="//ELEMENT[PREV_ID = $uid]" />
				</tr>
			</table>
		</td>
	</tr>
	</xsl:if>
</xsl:template>



<!-- START and END nodes -->
<xsl:template match="ELEMENT[TYPE_ID = '3']">
	<table valign="top" border="0" cellpadding="0" cellspacing="0" width="100%">
		<tr height="25"><td>&#160;</td></tr>
		<tr>
			<td align="center" valign="top">
				<v:roundrect id="chartItem" style="width: 60; height: 20" fillcolor="#000000" arcsize="0.4">
					<xsl:attribute name="uid"><xsl:value-of select="@id" /></xsl:attribute>
					<v:fill color2="#bbbbbb" type="gradient">
						<v:textbox style="padding-left:2; v-text-anchor: middle-center;">
							<table valign="top" width="100%" height="100%" cellpadding="0" cellspacing="0">
								<tr>
									<td style="color: white; font-size: 11px; font-family: Verdana; width:100%; height:100%;" valign="middle" align="center">
										<b><xsl:value-of select="NAME" /></b>
									</td>	
								</tr>
							</table>
						</v:textbox> 
					</v:fill>
				</v:roundrect>
			</td>
		</tr>
	</table>
</xsl:template>


</xsl:stylesheet>