<?xml version="1.0" encoding="ISO-8859-1" ?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  
	xmlns:rf="http://schemas.roadintranet.org/road-faq-1">
    <xsl:import href="banner.xsl"/>
    <xsl:include href="html.xsl"/>
    <xsl:include href="nav.xsl"/>
    <xsl:param name="http_root" select="'../..'"/>
    <xsl:variable name="topic-map" select="document('../xml/topic-map.xml')" />
    <xsl:variable name="topics" select="document('../xml/topics.xml')" />
    <xsl:variable name="root" select="/"/>
    <xsl:variable name="p4web-url">http://perforce</xsl:variable>
	<xsl:variable name="bug-url">http://bugs/show_bug.cgi?id=</xsl:variable>
    <xsl:key name="ItemKey"
        match="rf:section | rf:item" use="@id"/>
	
	<!-- html output -->
	<xsl:template match="/">
		<html>
		<head>
			<title><xsl:value-of select="/rf:topic/@title"/></title>
			<link rel="stylesheet" type="text/css" href="{$http_root}/road-faq/css/road.css" />
		    <script type="text/javascript" src="{$http_root}/road-faq/js/road.js"></script>
		</head>
		<body>
		<div id="page">		
		    <a  name="TOP"/> <!-- h2 id is not a reliable target  -->
		    <xsl:call-template name="banner"/>
			<xsl:call-template name="nav"/>
			
			<div id="content">
			    
				<h2><xsl:value-of select="/rf:topic/@title"/></h2>
				<xsl:apply-templates select="/rf:topic/text()" />
				
				<!-- table of contents -->
				<xsl:for-each select="/rf:topic/rf:section">
					<xsl:variable name="section-id" select="@id" />
					<div class="sectionheader">
					    <a  name="{@id}"/> <!-- h5 id is not a reliable target  -->
			    
						<h4><a href="#TOP"><img src="{$http_root}/road-faq/img/nav/up.gif" class="uparrow" width="11" height="9" border="0" alt="UP" /></a><xsl:value-of select="@title" /></h4>
					</div>
					<div class="sectionitems">
						<ul>
							<xsl:choose>
								<xsl:when test="@order='sorted'">
									<xsl:for-each select="./rf:item">
										<xsl:sort select="@title" order="ascending" />
									                <xsl:choose>
									                    <xsl:when test="contains(@title, ' -- ') ">
									                        <li><a href="#{@id}"><xsl:value-of
									                            select="substring-before(@title, ' -- ') " /></a> --
									                            <xsl:value-of select="substring-after(@title, ' -- ' )"/></li>
									                    </xsl:when>
									                    <xsl:otherwise>
									                        <li><a href="#{@id}"><xsl:value-of select="@title" /></a></li>
									                    </xsl:otherwise>
									                </xsl:choose>
									</xsl:for-each>
								</xsl:when>
								<xsl:otherwise>
									<xsl:for-each select="./rf:item">
									    <xsl:choose>
									        <xsl:when test="contains(@title, ' -- ') ">
									            <li><a href="#{@id}"><xsl:value-of
									                select="substring-before(@title, ' -- ') " /></a> --
									                <xsl:value-of select="substring-after(@title, ' -- ' )"/></li>
									        </xsl:when>
									        <xsl:otherwise>
									            <li><a href="#{@id}"><xsl:value-of select="@title" /></a></li>
									        </xsl:otherwise>
									    </xsl:choose>
									</xsl:for-each>
								</xsl:otherwise>
							</xsl:choose>
						</ul>
					</div>
					<div class="sectionbody">
						<xsl:apply-templates select="node()[name(.)!='rf:item']"/>
					</div>
				</xsl:for-each>

				<div class="update-notice">
					updated <xsl:value-of select="concat(substring(/rf:topic/@fileChange, 12,10), ' by ',
						substring(/rf:topic/@fileChange, 42))"/>
				</div>
				<!-- answers -->
			     	<div id="answers">
			     		<xsl:for-each select="/rf:topic/rf:section">
			     			<xsl:variable name="section-id" select="@id" />
			     			<xsl:if test="count(rf:item) != 0">
			     				<div class="sectionheader">
			     					<h4><xsl:value-of select="@title" /></h4>
			     				</div>			     			
			     			</xsl:if>
			     			<xsl:choose>
			     				<xsl:when test="@order='sorted'">
			     					<xsl:for-each select="./rf:item">
			     						<xsl:sort select="@title" order="ascending" />
			     						<xsl:call-template name="item-contents" />
			     					</xsl:for-each>
			     				</xsl:when>
			     				<xsl:otherwise>
			     					<xsl:for-each select="./rf:item">
			     						<xsl:call-template name="item-contents" />
			     					</xsl:for-each>
			     				</xsl:otherwise>	
			     			</xsl:choose>
			     		</xsl:for-each>
				</div>
			    <p><i>
          <xsl:value-of select="/rf:topic/rf:copyright"/>			        
			    </i></p>
			</div>
		</div>
		</body>
		</html>
	</xsl:template>

    <xsl:template name="item-contents">
        <div class="faq-title">
            <a  name="{@id}"/> <!-- h5 id is not a reliable target  -->
            <h5><a href="#TOP">
                <img src="{$http_root}/road-faq/img/nav/up.gif" class="uparrow" width="11" height="9" border="0" alt="UP" /></a>
                <xsl:value-of select="@title" />		
            </h5>
        </div>
        <div class="faq-body">
            <xsl:apply-templates />
            <xsl:if test="@date and @author">
                <div class="update-notice">
                    updated <xsl:value-of select="@date"/> by  <xsl:value-of select="@author"/>
                </div>
            </xsl:if>
        </div>
    </xsl:template>
    
    <xsl:template match="rf:iref">
        <xsl:choose>
            <xsl:when test="@item">
                <xsl:variable name="iref" select="."/>
                <xsl:for-each select="$topic-map/rf:content">
                    <xsl:variable name="item" select="key('ItemKey', $iref/@item)"/>
                    <xsl:variable name="item-id" select="$item/@id"/>
                    <xsl:variable name="item-file" select="$item/@file"/>
                    <xsl:choose>
                        <xsl:when test="not($item)">
                            <xsl:choose>
                                <xsl:when test="$root//a/@name[. = $iref/@item]">
                                    <xsl:text>[</xsl:text><a href="#{$iref/@item}">
                                        <xsl:value-of select="$iref/@item"/></a><xsl:text>]</xsl:text>
                                </xsl:when>
                                <xsl:otherwise>
                                    UNDEFINED <xsl:value-of select="$iref/@item"/>
                                </xsl:otherwise>
                            </xsl:choose>
                        </xsl:when>
                        <xsl:when test="$item-file = $root/rf:topic/@file">
                            <xsl:choose>
                                <xsl:when test="$iref/@page = '' or $iref/@part = '' ">
                                    [<a href="#{$item-id}">
                                        <xsl:value-of select="$item-id"/></a>
                                    <xsl:text>]</xsl:text>
                                </xsl:when>
                                <xsl:when test="$iref/@page">
                                    [<a href="#{$item-id}">
                                        <xsl:value-of select="$item-id"/></a>, page
                                    <xsl:value-of select="$iref/@page"/>
                                    <xsl:text>]</xsl:text>
                                </xsl:when>
                                <xsl:when test="$iref/@part">
                                    [<a href="#{$item-id}">
                                        <xsl:value-of select="$item-id"/></a>, <xsl:value-of
                                            select="$iref/@part"/>
                                    <xsl:text>]</xsl:text>
                                </xsl:when>
                                <xsl:when test="$iref/@title">
                                    <a href="#{$item-id}">
                                        <xsl:value-of select="$iref/@title"/></a>
                                </xsl:when>
                                <xsl:otherwise>
                                    <a href="#{$item-id}">
                                        <xsl:value-of select="$item/@title"/></a>
                                </xsl:otherwise>
                            </xsl:choose>
                        </xsl:when>
                        <xsl:otherwise>
                              <xsl:choose>
                                <xsl:when test="$iref/@page = ''">
                                    [<a href="{$item-file}#{$item-id}">
                                        <xsl:value-of select="$item-id"/></a>
                                    <xsl:text>]</xsl:text>
                                </xsl:when>
                                <xsl:when test="$iref/@page">
                                    [<a href="{$item-file}#{$item-id}">
                                        <xsl:value-of select="$item-id"/></a>, page
                                    <xsl:value-of select="$iref/@page"/>
                                    <xsl:text>]</xsl:text>
                                </xsl:when>
                                <xsl:when test="$iref/@title">
                                    <a href="{$item-file}#{$item-id}">
                                        <xsl:value-of select="$iref/@title"/></a>
                                </xsl:when>
                                <xsl:otherwise>
                                    <xsl:choose>
                                        <xsl:when test="contains($item/@title, ' -- ') ">
                                            <a href="{$item-file}#{$item-id}"><xsl:value-of
                                                select="substring-before($item/@title, ' -- ') " /></a> --
                                                <xsl:value-of select="substring-after($item/@title, ' -- ' )"/>
                                        </xsl:when>
                                        <xsl:otherwise>
                                            <a href="{$item-file}#{$item-id}"><xsl:value-of select="$item/@title" /></a>
                                        </xsl:otherwise>
                                    </xsl:choose>
                                </xsl:otherwise>
                            </xsl:choose>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:for-each>
            </xsl:when>
            <xsl:when test="@p4">
                <xsl:choose>
                    <xsl:when test="@title">
                        <a href="{$p4web-url}/{@p4}"><xsl:value-of select="@title"/></a>
                    </xsl:when>
                    <xsl:otherwise>
                        <a href="{$p4web-url}/{@p4}"><xsl:value-of select="@p4"/></a>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:when>
            <xsl:when test="@bug">
                <xsl:choose>
                    <xsl:when test="@title">
                        <a href="{$bug-url}{@bug}"><xsl:value-of select="@title"/></a>
                    </xsl:when>
                    <xsl:otherwise>
                        <a href="{$bug-url}{@bug}">Bug <xsl:value-of select="@bug"/></a>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:when>
            <xsl:otherwise>
                <xsl:message>An <b>iref</b> in '<xsl:value-of select="ancestor::item/@id"/>' requires an 'item' or 'p4' attribute.</xsl:message>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    
</xsl:stylesheet>



