<?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) {
	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>

<!--		DEFINITION	START		-->


<!-- roundrect START-->
<xsl:template name="roundrect">
			<v:roundrect id="chartItem" style="width: 98%; height: 20%" 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 style="color: white; font-size: 80%; font-family: Verdana; width:100%; height:100%;" valign="middle" align="center">
				<b><xsl:value-of select="NAME" />NAME</b>
			</td>	
			</tr>
			</table>
			</v:textbox> 
			</v:fill>
			</v:roundrect>
</xsl:template>
<!-- roundrect END-->



<xsl:template name="NameRectShape">
<!-- zoom -->
	<xsl:value-of select="NAME" />
	<v:roundrect style="width: 90%; align:center; height: 5" fillcolor="#00ffff" arcsize="0.4">
	</v:roundrect>
</xsl:template>




<xsl:template name="StartEndShape">
<!-- zoom -->
	<v:roundrect onclick="ChartNodeClick(this)" id="chartItem" style="cursor:pointer; width: 31; height: 12" 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: 6; 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>
</xsl:template>





<!-- oval START -->
<xsl:template name="oval">
<!-- zoom -->
			<v:oval id="chartItem" onclick="ChartNodeClick(this)" style="z-index: 100; cursor:pointer; align: center; width: 5; height: 5; visibility: hidden; display: none;" tid="2" position="7 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="true" />
				<v:fill color="red" color2="#ffff00" type="gradient" opacity="1" />
			</v:oval>
</xsl:template>
<!-- oval END -->







<xsl:template name="elem">
<!-- zoom -->
	<table valign="top" border="1" borderColor="black" style="cursor:pointer;" onclick="SetSelected(this);" cellpadding="0" cellspacing="1" id="chartItem" width="37">
		<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="7">
			<td align="center" valign="center" style="background-color: blue; color: yellow; font-size:7; " nowrap="1">
				&#160;<b><xsl:value-of select="WHO" /></b>&#160;
			</td> 
		</tr>
		<tr height="14">
			<td align="center" valign="center" style="background-color: #ffffcc; font-size:7">
				<xsl:call-template name="NameRectShape"/>
			</td>	
		</tr>
	</table>
</xsl:template>










<!--		DEFINITION	END		-->




	<xsl:template match="STRUCTURE"></xsl:template>
	
	<!-- Chart element START-->
	<xsl:template match="ELEMENTS">
		<center>
<!-- zoom -->		
<span style="font-size: 5px">
		<table valign="top" border="0" cellpadding="0" cellspacing="0">
			<tr>
				<td align="center" valign="top">
					<!-- start element -->
					<xsl:apply-templates select="ELEMENT[PREV_ID = '0']" /> 
				</td>
			</tr>
			<tr>
				<xsl:apply-templates select="ELEMENT[PREV_ID = //ELEMENT[PREV_ID = '0']/@id]" />
			</tr>
			<tr>
				<td align="center" valign="top">
					<!-- end element -->
					<xsl:apply-templates select="ELEMENT[NAME = 'END']" /> 
				</td>
			</tr>
		</table>
</span>
		</center>
	</xsl:template>
	<!-- Chart element END-->

	<!-- Standard element START-->
	<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:choose>
		<xsl:when test="//ELEMENT[PREV_ID = $prevId] and aka:AnyNewElement(//ELEMENT[PREV_ID = $prevId])">
		<td align="center" valign="top"  style="font-size: 7">
			<table valign="top" border="0" cellpadding="0" cellspacing="0" borderColor="green">
				<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 START -->
							<table border="0"  cellpadding="0" cellspacing="0" valign="top">
<!-- zoom -->							
								<tr height="5"><td style="font-size: 12">&#160;</td></tr>
								<tr>
<!-- zoom -->								
									<td width="3">&#160;</td>
									<td align="center" valign="top">
										<xsl:choose>
										<xsl:when test="TYPE_ID = '4'">

											<xsl:call-template name="roundrect"/>

										</xsl:when>
										<xsl:otherwise>

											<xsl:call-template name="elem"/>

										</xsl:otherwise>
										</xsl:choose>
									</td>
<!-- zoom -->									
									<td width="3">&#160;</td>
								</tr>
							</table>
							<!-- the element END -->
							<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:  RECURSIVE  -->
									<xsl:apply-templates select="//ELEMENT[@id = $thisNextId] " />
								</table>	
								</xsl:if>
							</xsl:if>
						</td>
						</xsl:if>
					</xsl:for-each>
				</tr>
			</table>	
			<xsl:if test="//ELEMENT[@id = $nextId and not(NAME = 'END')] 
				and 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" style="font-size:7">
							<xsl:attribute name="colspan"><xsl:value-of select="$colSpan" /></xsl:attribute> 
							<table border="0" cellpadding="0" cellspacing="0">
								<!-- next node:  RECURSIVE  -->
								<xsl:apply-templates select="//ELEMENT[@id = $nextId and not(NAME = 'END')] " />
							</table>	
						</td>	
					</tr>
				</table>
			</xsl:if>
		</td>
		</xsl:when>
		<xsl:otherwise></xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	<!-- Standard element END-->
	
	
	
	
	<!-- Internal node START-->
	<xsl:template match="ELEMENT[TYPE_ID = '2']">
		<xsl:variable name="uid"><xsl:value-of select="@id" /></xsl:variable>
		<tr height="1">
			<td align="center" valign="top" style="font-size:1">
			<xsl:attribute name="colspan"><xsl:value-of select="count(//ELEMENT[NEXT_ID = $uid])" /></xsl:attribute> 
				<!-- Id=<xsl:value-of select="@id" /> -->
				<!-- oval START-->
				<xsl:call-template name="oval"/>
				<!-- oval END-->
			</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>
	<!-- Internal node END-->
	
	
	


	<xsl:template match="ELEMENT[TYPE_ID = '3']">
		<table valign="top" border="0" cellpadding="0" cellspacing="0" width="100%">
			<tr height="8"><td>&#160;</td></tr>
			<tr>
				<td align="center" valign="top">
					<xsl:call-template name="StartEndShape"/>
				</td>
			</tr>
		</table>
	</xsl:template>


</xsl:stylesheet>