<xsl:stylesheet version="1.0" 
                xmlns:dflt="http://www.imsproject.org/xsd/imscp_rootv1p1p2"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
                xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
                xmlns:adlcp="http://www.adlnet.org/xsd/adlcp_rootv1p2"
                >
	<xsl:param name="query_param" />
	<xsl:param name="query_mode" />
	<xsl:param name="bIsOLP" />

	<xsl:template match="/">
		<xsl:element name="Root">
			<xsl:choose>
				<xsl:when test="$query_mode='1'" >
					<xsl:apply-templates select="//dflt:item[@identifier=$query_param and not(@isvisible[. = 'false'] or @isvisible[. = '0'])]" />
				</xsl:when>
				<xsl:when test="$query_mode='2'" >
					<xsl:apply-templates select="//dflt:manifest[1]/dflt:organizations/dflt:organization[@identifier=$query_param]/dflt:item[not(@isvisible[. = 'false'] or @isvisible[. = '0'])]" />
				</xsl:when>
				<xsl:when test="$query_mode='3'" >
					<xsl:apply-templates select="//dflt:item[@identifier=$query_param]/dflt:item[not(@isvisible[. = 'false'] or @isvisible[. = '0'])]" />
				</xsl:when>
				<xsl:when test="$query_mode='4'" >
					<xsl:apply-templates select="//dflt:item[@identifier=$query_param]//dflt:item[not(@isvisible[. = 'false'] or @isvisible[. = '0'])]" />
				</xsl:when>
				<xsl:when test="$query_mode='5'" >
					<xsl:apply-templates select="/dflt:manifest[1]/dflt:organizations//dflt:organization" />
				</xsl:when>
				<xsl:when test="$query_mode='6'" >
					<xsl:apply-templates select="/dflt:manifest[1]/dflt:organizations/dflt:organization[@identifier=$query_param]" />
				</xsl:when>
			</xsl:choose>
		</xsl:element>
	</xsl:template>
	
	<xsl:template match="dflt:item">
		<xsl:choose>
			<xsl:when test="//dflt:manifest/dflt:organizations/dflt:organization[@identifier = current()/@identifierref]">
				<xsl:apply-templates select="//dflt:manifest/dflt:organizations/dflt:organization[@identifier = current()/@identifierref]/dflt:item[not(@isvisible[. = 'false'] or @isvisible[. = '0'])]" />
			</xsl:when>	 
			<xsl:otherwise>
				<xsl:element name="ContentItem">
					<xsl:attribute name="Name"><xsl:value-of select="dflt:title" /></xsl:attribute>
					<xsl:attribute name="ID"><xsl:value-of select="@identifier" /></xsl:attribute>
					<xsl:attribute name="ResourceRef"><xsl:value-of select="@identifierref" /></xsl:attribute>
          <xsl:variable name="ResourceName" select="@identifierref" />
					<xsl:attribute name="Href"><xsl:value-of select="//dflt:manifest//dflt:resource[@identifier = $ResourceName]/@href" /></xsl:attribute>         
					<xsl:attribute name="ContentWheelIcon"></xsl:attribute>

					<xsl:choose>
						<xsl:when test="$bIsOLP='true'">
							<xsl:attribute name="Available"><xsl:value-of select="@available" /></xsl:attribute>
						</xsl:when>
						<xsl:otherwise>
							<xsl:attribute name="Available">true</xsl:attribute>
						</xsl:otherwise>
					</xsl:choose>

					<xsl:choose>
						<xsl:when test="name(parent::*)='item'" >
							<xsl:attribute name="ParentID"><xsl:value-of select="../@identifier" /></xsl:attribute>
						</xsl:when>
						<xsl:otherwise>
							<xsl:attribute name="ParentID"></xsl:attribute>
						</xsl:otherwise>
					</xsl:choose>
	
					<xsl:attribute name="LessonStatus">not attempted</xsl:attribute>
					<xsl:attribute name="Prerequisites"><xsl:value-of select="adlcp:prerequisites" /></xsl:attribute>
					<xsl:attribute name="PrerequisitesMet">true</xsl:attribute>
					<xsl:attribute name="DataFromLMS"><xsl:value-of select="adlcp:datafromlms" /></xsl:attribute>

					<xsl:choose>
						<xsl:when test="dflt:item[not(@isvisible[. = 'false'] or @isvisible[. = '0'])]">
							<xsl:attribute name="HasChildren">1</xsl:attribute>

							<xsl:variable name="custiconcollapse1" select="dflt:customsyllabusicons/dflt:parentnodecollapse/@href"/>
							<xsl:variable name="custiconcollapse2" select="/dflt:manifest/dflt:metadata/dflt:userinterface/dflt:customsyllabusicons/dflt:parentnodecollapse/@href"/>
							<xsl:choose>
								<xsl:when test="$custiconcollapse1">
									<xsl:attribute name="CloseIcon"><xsl:value-of select="$custiconcollapse1"/></xsl:attribute>
								</xsl:when>
								<xsl:when test="$custiconcollapse2">
									<xsl:attribute name="CloseIcon"><xsl:value-of select="$custiconcollapse2"/></xsl:attribute>		
								</xsl:when>
								<xsl:otherwise>
									<!--<xsl:choose>-->
										<!--<xsl:when test="scrpt:GetContentWheelIcon(child::*[name()='item' and not(@isvisible[. = 'false'] or @isvisible[. = '0'])])=''">-->
                      <xsl:attribute name="CloseIcon">appart/toc_plus_normal.png</xsl:attribute>
										<!--</xsl:when>-->
										<!--<xsl:otherwise>
											<xsl:attribute name="LeafIcon">appart/toc_bullet.gif</xsl:attribute>
										</xsl:otherwise>
									</xsl:choose>-->
								</xsl:otherwise>
							</xsl:choose>

							<xsl:variable name="custiconexpand1" select="dflt:customsyllabusicons/dflt:parentnodeexpand/@href"/>
							<xsl:variable name="custiconexpand2" select="/dflt:manifest/dflt:metadata/dflt:userinterface/dflt:customsyllabusicons/dflt:parentnodeexpand/@href"/>
							<xsl:choose>
								<xsl:when test="$custiconexpand1">
									<xsl:attribute name="OpenIcon"><xsl:value-of select="$custiconexpand1"/></xsl:attribute>
								</xsl:when>
								<xsl:when test="$custiconexpand2">
									<xsl:attribute name="OpenIcon"><xsl:value-of select="$custiconexpand2"/></xsl:attribute>
								</xsl:when>
								<xsl:otherwise>
									<!--<xsl:choose>
										<xsl:when test="scrpt:GetContentWheelIcon(child::*[name()='item' and not(@isvisible[. = 'false'] or @isvisible[. = '0'])])=''">-->
                      <xsl:attribute name="OpenIcon">appart/toc_minus_normal.png</xsl:attribute>
										<!--</xsl:when>-->
										<!--<xsl:otherwise>
											<xsl:attribute name="LeafIcon">appart/toc_bullet.gif</xsl:attribute>
										</xsl:otherwise>
									</xsl:choose>-->
								</xsl:otherwise>
							</xsl:choose>
						</xsl:when>
						<xsl:otherwise>
							<xsl:attribute name="HasChildren">0</xsl:attribute>

							<xsl:variable name="custiconleaf1" select="dflt:customsyllabusicons/dflt:leafnode/@href"/>
							<xsl:variable name="custiconleaf2" select="/dflt:manifest/dflt:metadata/dflt:userinterface/dflt:customsyllabusicons/dflt:leafnode/@href"/>
							<xsl:choose>
								<xsl:when test="$custiconleaf1">
									<xsl:attribute name="LeafIcon"><xsl:value-of select="$custiconleaf1"/></xsl:attribute>
								</xsl:when>
								<xsl:when test="$custiconleaf2">
									<xsl:attribute name="LeafIcon"><xsl:value-of select="$custiconleaf2"/></xsl:attribute>
								</xsl:when>
								<xsl:otherwise>
									<xsl:attribute name="LeafIcon">appart/toc_bullet.gif</xsl:attribute>
								</xsl:otherwise>
							</xsl:choose>
						</xsl:otherwise>
		    			</xsl:choose>
				</xsl:element>
			</xsl:otherwise>
		</xsl:choose>		
	</xsl:template>

	<xsl:template match="dflt:organization">
		<xsl:element name="TocItem">
			<xsl:choose>
				<xsl:when test="//dflt:organizations[@default = current()/@identifier]" >
					<xsl:attribute name="IsDefault">1</xsl:attribute>
				</xsl:when>
				<xsl:otherwise>
					<xsl:attribute name="IsDefault">0</xsl:attribute>
				</xsl:otherwise>
			</xsl:choose>

			<xsl:attribute name="Title"><xsl:value-of select="dflt:title" /></xsl:attribute>
			<xsl:attribute name="ID"><xsl:value-of select="@identifier" /></xsl:attribute>
		</xsl:element>
	</xsl:template>

	<!--<msxsl:script language="javascript" implements-prefix="scrpt">
	<![CDATA[

		function GetFullPath(_contextNodeList)
		{
			var sPath = "";

			try {    
				// Constructs path to accommodate xml:base attribute on manifest, resources and resource elements
				var node = _contextNodeList.item(0);
      				
				var sManID = GetParentID(node,"manifest");

				var oResourceNodeHref = node.selectSingleNode("//dflt:manifest[@identifier='" + sManID + "']//dflt:resource[@identifier='" + node.getAttribute("identifierref") + "']/@href");
				if (oResourceNodeHref)
				{
					var sHref = oResourceNodeHref.text;
					if (sHref)
					{
						var oManXMLBase = node.selectSingleNode("//dflt:manifest[@identifier='" + sManID + "']/@xml:base");
						if (oManXMLBase)
							sPath += AppendSlash(oManXMLBase.text);
				
						var oResourcesNode = node.selectSingleNode("//dflt:manifest[@identifier='" + sManID + "']/dflt:resources/@xml:base");
						if (oResourcesNode)
							sPath += AppendSlash(oResourcesNode.text);
				
						var oResourceNode = node.selectSingleNode("//dflt:manifest[@identifier='" + sManID + "']//dflt:resource[@identifier='" + node.getAttribute("identifierref") + "']/@xml:base");
						if (oResourceNode)
							sPath += AppendSlash(oResourceNode.text);
					
						if (sHref.indexOf("http:") != -1 || sHref.indexOf("https:") != -1 || 
		    		   	   	 sHref.indexOf("file:") != -1 || sHref.indexOf("\\\\") != -1)
							sPath = sHref;
						else
							sPath += sHref;
					}
				}
			}
			catch(e)
			{
			}

			return sPath;
		}

		function AppendSlash(s)
		{
			var sSlashes = "\\/";
			return (sSlashes.indexOf(s.charAt(s.length-1)) == -1) ? s + "/" : s;
		}
		
		function GetParentID(node,sNodeName)
		{
			var sID = "";

			try
			{
				if (node.nodeName != sNodeName)
					 sID = GetParentID(node.parentNode,sNodeName);
				else
					 sID = node.getAttribute("identifier");
			}
			catch(e)
			{
			}
			
			return sID;
		}

		function GetExternalManifestParentID(_contextNodeList)
		{
			var sID = "";

			try
			{
				var node = _contextNodeList.item(0);
								
				var sOrgID = GetParentID(node,"organization")
				
				var oNode = node.selectSingleNode("//dflt:item[@identifierref='" + sOrgID + "']");
			
				if (oNode.parentNode.nodeName != "item")
					sID = oNode.getAttribute("identifier");
				else
					sID = oNode.parentNode.getAttribute("identifier");
			}
			catch(e)
			{
			}				
				
			return sID;
		}

		function GetContentWheelIcon(_contextNodeList)
		{
			var sIcon = "";

			try
			{
				var node = _contextNodeList.item(0);

				// Get all metadata contained within the item's resource; a topic may contain metadata for both v-lab and content wheel icon.
				var oLangStrings = node.selectNodes("//dflt:resource[@identifier='" + node.getAttribute("identifierref") + "']/dflt:metadata/imsmd:lom/imsmd:general/imsmd:description/imsmd:langstring");
				var oLangString = null;

				// Loop through the nodelist
				while (oLangString = oLangStrings.nextNode())
				{
					// If the value is numeric, it's content wheel icon metadata.
					if (!isNaN(oLangString.text)) 
					{
						sIcon = oLangString.text;
						break;
					}
				}
			}
			catch(e)
			{
			}

			return sIcon;
		}
	]]>
	</msxsl:script>-->
</xsl:stylesheet>