<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- attention: this is a xslt version 2.0 sheet. needs an up-to-date engine -->

	<xsl:output method="html" indent="yes" doctype-system="http://www.w3.org/TR/REC-html40/strict.dtd" doctype-public="//W3C//DTD HTML 4.01//EN"/>

	<xsl:template match="document">

		<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
		<!-- PROCESSING OF MAIN PAGES: PAGE WITH NAVIGATION   -->
		<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->

        <xsl:for-each select="//page">
           processing: <xsl:value-of select="@pageID" />

			<!-- 	open new file                                        -->
			<!-- ======================================================= -->

			<!-- attention: the variable decl must be on one line due to line feeds. any other solution? -->
            <xsl:variable name="outputFile"><xsl:call-template name="calc_page_url" ><xsl:with-param name="callingPageID" select="@pageID" /></xsl:call-template></xsl:variable>
			<xsl:result-document href="{$outputFile}" >

				<!-- 	build header                                         -->
				<!-- ======================================================= -->
                <html>
				<xsl:comment>/ by Patrick Heusser / xsl template file from 26.3.2004 / generated with XSLT 2.0 /</xsl:comment>
				<head>
				<xsl:for-each select="/document/pages/meta" >
					<meta name="{(@name)}" content="{(@content)}" />
				</xsl:for-each>
				<title>
					<xsl:value-of select="/document/mainTitle" />	(<xsl:value-of select="(../../@label | ../@label  | @label )[1]  " />)
				</title>
				<link rel="stylesheet" type="text/css" href="../resources/main.css" />
                <link rel="icon" href="../resources/x8ing.ico" type="image/ico" />

				<!-- real browser test javascript -->
				<script src="../resources/js/utils.js" type="text/javascript" > </script>

				</head>
                <body onload="isRealBrowser();">


				<!-- 	build top area                                       -->
				<!-- ======================================================= -->
				<h2>
		            <xsl:variable name="aboutPage"><xsl:call-template name="calc_page_url" ><xsl:with-param name="callingPageID" select="(/document/pages//page[@isAboutPage='true']/@pageID)[1]" /></xsl:call-template></xsl:variable>
					<a href="{concat('../',$aboutPage)}">
						<xsl:value-of select="/document/mainTitle" />
					</a>
				</h2>
				<h1>
					<!-- for search engines a hidden h1 entry (h1 is overriden by css): google ranking -->
					<xsl:value-of select="@label " /> <xsl:text > - </xsl:text><xsl:value-of select="/document/pages/meta[@isHtmlH1='true']/@content" />
				</h1>


				<!-- 	build content                                        -->
				<!-- ======================================================= -->
				<div id="main">

					<!--  ***** FOR TEST ONLY *****
					<xsl:call-template name="calc_page_url" >
					<xsl:with-param name="callingPageID" select="@pageID" />
					</xsl:call-template> -->

                    <xsl:for-each select="content/paragraph">

						<xsl:if test="@title">
	                    	<h3><xsl:value-of select="@title" /></h3>
	                    </xsl:if>


      					<!-- ***** FOR TEST ONLY *****
      					<xsl:value-of select="$outputFile" /> -->


 	            		<xsl:apply-templates>
 	            			<xsl:with-param tunnel="yes" name="caller" select="$outputFile" />
 	            			<xsl:with-param name="callerElement" select="../.." />
 	            		</xsl:apply-templates>
 	            		<br/><br/>
                    </xsl:for-each>


                    <!--    build buttom navigation (previous, next, etc.        -->
                    <!-- ======================================================= -->
					<div id="buttomNavigation">
						<span style="position:absolute; left:2em; text-align:left;">
				            <xsl:variable name="aboutPage"><xsl:call-template name="calc_page_url" ><xsl:with-param name="callingPageID" select="(/document/pages//page[@isAboutPage='true']/@pageID)[1]" /></xsl:call-template></xsl:variable>
							<a href="{concat('../',$aboutPage)}"> info </a>
						</span>
							<xsl:variable name="navPrev" ><xsl:call-template name="calc_page_url" ><xsl:with-param name="callingPageID" select="((preceding::page[1]/@pageID|parent::page/@pageID)[position()=last()])" /></xsl:call-template>	</xsl:variable>

                            <xsl:choose>
                                <xsl:when test="$navPrev">
                                    <a href="{concat('../',($navPrev))}">
                                       previous
                                    </a>
                                </xsl:when>
                                <xsl:otherwise>
                                	<!-- display the content disabled without link -->
                                    <span class="buttomNavigationInactive">
                                        previous
                                    </span>
	                            </xsl:otherwise>
	                        </xsl:choose>

							<xsl:variable name="navNext"><xsl:call-template name="calc_page_url" ><xsl:with-param name="callingPageID" select="((following::page/@pageID)|child::page/@pageID)[1]" /></xsl:call-template></xsl:variable>

                            <xsl:choose>
                                <xsl:when test="$navNext">
                                    <a href="{concat('../',($navNext))}">
                                       next
                                    </a>
                                </xsl:when>
                                <xsl:otherwise>
                                	<!-- display the content disabled without link -->
                                    <span class="buttomNavigationInactive">
                                        next
                                    </span>
	                            </xsl:otherwise>
	                        </xsl:choose>

						<span style="position:absolute; right:2em; text-align:right;">
							<!-- search the copyright page -->
				            <xsl:variable name="copyrightPage"><xsl:call-template name="calc_page_url" ><xsl:with-param name="callingPageID" select="(/document/pages//page[@isCopyrightPage='true']/@pageID)[1]" /></xsl:call-template></xsl:variable>
							<a href="{concat('../',$copyrightPage)}"> copyright </a>
						</span>
					</div>

                    <div class="blindText">
                        all-page-keywords:
                        <xsl:value-of select="/document/pages/meta/@content" />
                    </div>
				</div> <!-- end main div -->



				<!-- 	build navigation                                     -->
				<!-- ======================================================= -->
                <!-- attention: this code is similar used in the summary page may be create functions? -->
				<div id="sidebar">
				<!-- xxx_h3y_xxx from root parent all children of current -->
  				<xsl:for-each select="/document/pages/page | ancestor-or-self::page | child::page | ../page | ../../page">
  					<!-- check if we have the current page selected -->
					<xsl:variable name="navCurrent" ><xsl:call-template name="calc_page_url" ><xsl:with-param name="callingPageID" select="@pageID" /></xsl:call-template>	</xsl:variable>
  					<xsl:choose>
	  					<xsl:when  test="$outputFile=$navCurrent">
	  						<!-- the page is the current selected -->
                            <div class="nav{(count(ancestor::node())-2)}">
                                <a href="{concat('../',$navCurrent)}" style="width=100%;color:#B9B7A3" >
                                    <xsl:value-of select="@label" />
                                    <span style="position: absolute; right:0;"> &lt;&lt; </span>
                                </a>
                            </div>
  						</xsl:when>
  						<xsl:otherwise>
  							<!-- we have an ordinary navigation entry -->
                            <div class="nav{(count(ancestor::node())-2)}">
                                <a href="{concat('../',$navCurrent)}">
                                	<!-- check if node has children
             		                <xsl:choose>
									   <xsl:when test="count(child::page)&gt;0">
											+
									   </xsl:when>
										<xsl:otherwise>
											&#160;
										</xsl:otherwise>
									</xsl:choose>
									-->
                                    <xsl:value-of select="@label" />
                                </a>
                            </div>
  						</xsl:otherwise>
					</xsl:choose>
				</xsl:for-each>
				
				<!-- ADVERT -->
				<div style="margin-top:60px; font-size:x-small">
	                    <a href="http://www.panoramaexplorer.com/?submit=x8ing" onclick="return openNewWindow('http://www.panoramaexplorer.com/?submit=x8ing');">
	                    	Click to visit a page, which shares photos organized on the world map
	                    </a>
                    </div>
               	</div>



				</body>
				</html>

			</xsl:result-document>

		</xsl:for-each>

  	</xsl:template>



	<!-- 	content: image  FULL-SCREEN MODE                     -->
	<!-- ======================================================= -->
  	<xsl:template match="image">
		<!-- the variable is tunneled to be able to link back to calling url-referer -->
	  	<xsl:param name="caller" tunnel="yes">default_if_not_tunneled</xsl:param>
	  	<xsl:param name="callerElement">default_if_not_tunneled</xsl:param>


		<!-- ***** FOR TEST ONLY *****
		<br />caller=
		<xsl:value-of select="$caller" />
		<br />callerElement=
		<xsl:value-of select="$callerElement/@pageID" />
		<br />current element=
		<xsl:value-of select="@fileNameNormal" /> -->

        <!--
        <xsl:variable name="imageDetail"><xsl:call-template name="calc_fullscreen_url" ><xsl:with-param name="callingPageID" select="@pageID" /></xsl:call-template></xsl:variable>
        -->
	   <xsl:variable name="imageDetail" select="concat(substring-before($caller,'/'),'/',substring-before(@fileNameNormal,'.jpg'),'_fullscreen.html')" />


  		<div class="image">
			<a name="{(concat('#',@fileNameNormal))}" id="{(@fileNameNormal)}" />  <!-- an internal anchor for linking back browser -->
			<a href="{(concat('../',$imageDetail))}">
    			<img src="../resources/{normalize-space((@fileNameNormal))}" alt="{(@title)}" />
			</a>
			<br/>
			<a href="{(concat('../',$imageDetail))}">
				<xsl:value-of select="@title" />
			</a>
		</div>

        <!--    create a new file for the detail   (fullScreen)      -->
        <!-- ======================================================= -->
		<xsl:result-document href="{($imageDetail)}" >

            <!--  build header of detail image file  -->
            <html>
            <xsl:comment>
                / by Patrick Heusser / xsl template file from 26.3.2004 / generated with XSLT 2.0 /
            </xsl:comment>
            <head>
                <xsl:for-each select="/document/pages/meta" >
                    <meta name="{(@name)}" content="{(@content)}" />
                </xsl:for-each>
                <title>
                    <xsl:value-of select="/document/mainTitle" />   (<xsl:value-of select="(../../@label | ../@label  | @label )[1]  " />)
                </title>
                <link rel="stylesheet" type="text/css" href="../resources/main.css" />
                <link rel="icon" href="../resources/x8ing.ico" type="image/ico" />
				<!-- real browser test javascript -->
				<script src="../resources/js/utils.js" type="text/javascript" > </script>
            </head>
            <body onload="isRealBrowser();">
				<h2>

		            <xsl:variable name="aboutPage"><xsl:call-template name="calc_page_url" ><xsl:with-param name="callingPageID" select="(/document/pages//page[@isAboutPage='true']/@pageID)[1]" /></xsl:call-template></xsl:variable>
					<a href="{(concat('../',$aboutPage))}">
						<xsl:value-of select="/document/mainTitle" />
					</a>
				</h2>
				<h1>
					<!-- for search engines a hidden h1 entry (h1 is overriden by css): google ranking -->
					<xsl:value-of select="@label " /> <xsl:text > - </xsl:text><xsl:value-of select="/document/pages/meta[@isHtmlH1='true']/@content" />
				</h1>

				<xsl:variable name="exitLink" select="(substring-after(concat($caller,'#',@fileNameNormal),'/'))" />

	            <div>
                    <div class="image">
                        <a href="{($exitLink)}"> <!-- create an internal anchor for backlinking in browser -->
                            <img src="../resources/{(normalize-space(@fileNameLarge))}" alt="{(normalize-space(@title))}"  />
                        </a>
                        <br/>
                        <xsl:value-of select="@title" />
                        <br/>
                    </div>


                    <!--    build buttom navigation                              -->
                    <!-- ======================================================= -->
                    <div id="buttomNavigation">

                        <span style="position:absolute; left:2em; text-align:left">
				            <xsl:variable name="aboutPage"><xsl:call-template name="calc_page_url" ><xsl:with-param name="callingPageID" select="(/document/pages//page[@isAboutPage='true']/@pageID)[1]" /></xsl:call-template></xsl:variable>
							<a href="{concat('../',$aboutPage)}"> info </a>
                        </span>
                        <!-- TODO: actually the tree should be returend in pre-order, what is not the case -->
                        <!-- so the order is not as expected by the user... check this once                -->
                        <xsl:variable name="navNext" select="normalize-space(((following::image)/@fileNameLarge)[1])" />
                        <xsl:variable name="navPrev" select="(normalize-space((./preceding::image/@fileNameLarge)[position()=last()]))" />

                        <a href="{($exitLink)}">
                            exit fullscreen
                        </a>

                        <span style="position:absolute; right:2em; text-align:right">
                            <!-- search the copyright page -->
				            <xsl:variable name="copyrightPage"><xsl:call-template name="calc_page_url" ><xsl:with-param name="callingPageID" select="(/document/pages//page[@isCopyrightPage='true']/@pageID)[1]" /></xsl:call-template></xsl:variable>
							<a href="{concat('../',$copyrightPage)}"> copyright </a>
                        </span>

                    </div>
				</div>
                <div class="blindText">
                    all-page-keywords:
                    <xsl:value-of select="/document/pages/meta/@content" />
                </div>

            </body>
            </html>

		</xsl:result-document>

  	</xsl:template>


	<!-- 	content: text                                        -->
	<!-- ======================================================= -->
  	<xsl:template match="text">
		<xsl:value-of select="." />
  	</xsl:template>


	<!-- 	content: TOC - table of content                      -->
	<!-- ======================================================= -->
  	<xsl:template match="tableOfContent">
  		<h3> table of contents </h3>
  		<div id="toc">
            <xsl:for-each select="//page" >
                <div class="toc{(count(ancestor::node())-2)}">
                    <a href="{(concat(@pageID,'.html'))}" >
                        <xsl:value-of select="@label" />
                    </a>
                </div>
            </xsl:for-each>
        </div>
  	</xsl:template>


	<!-- 	content: link                                        -->
	<!-- ======================================================= -->
  	<xsl:template match="link">
		<xsl:if test="@external">
			<a href="{(@external)}">
				<xsl:value-of select="." />
			</a>
		</xsl:if>
		<xsl:if test="@pageID">
            <xsl:variable name="theLink"><xsl:call-template name="calc_page_url" ><xsl:with-param name="callingPageID" select="@pageID" /></xsl:call-template></xsl:variable>
			<a href="{(concat('../',$theLink))}" >
				<xsl:value-of select="." />
			</a>
		</xsl:if>
  	</xsl:template>



	<!-- 	content: preview                                       -->
	<!-- ========================================================  -->

  	<xsl:template match="preview">

	    <xsl:if test="@displayInfoText='true'">
            <div class="systemText">
            You see a preview of the pictures of the pictures of all sub-sections of this chapter.
            Please use the navigation on the left side to explore the pictures in the sub-sections of this chapter or click on the pictures.
            </div>
		</xsl:if>

  		<xsl:variable name="gallery" select="((../../../. | ../../. | ../../../.)[1])" />
  		<xsl:for-each select="$gallery">
	        <xsl:call-template name="preview_template" />
  		</xsl:for-each>

	</xsl:template>


	<!-- 	helper function: preview_template                    -->
	<!-- ======================================================= -->
	<xsl:template name="preview_template">
        <div>
            <xsl:for-each select="descendant-or-self::image">
                <div class="image" style="margin:20px 20px 20px 20px;float:left;">
                    <a href="{concat((../../../@pageID),'.html','#',@fileNameNormal)}"> <!-- a href link with internal anchor -->
                        <img src="../resources/{normalize-space((@fileNameSmall))}" alt="{normalize-space((@title))}" />
                    </a>
                    <br/>
                    <xsl:value-of select="./title" />
                </div>
            </xsl:for-each>
        </div>
	</xsl:template>


	<!-- 	helper function: calculate url for page              -->
	<!-- ======================================================= -->
	<xsl:template  name="calc_page_url">
	  	<xsl:param name="callingPageID">none</xsl:param>

        <xsl:choose>
            <xsl:when  test="$callingPageID/../../../@pageID">
                <!-- level 3: treated as level 2 -->
                <xsl:value-of select="concat($callingPageID/../../../@pageID,'/',$callingPageID,'.html')" />
            </xsl:when>
            <xsl:when test="$callingPageID/../../@pageID">
                <!-- level 2 -->
                <xsl:value-of select="concat($callingPageID/../../@pageID,'/',$callingPageID,'.html')" />
            </xsl:when>
            <xsl:otherwise>
                <!-- level 1 -->
                <xsl:value-of select="concat($callingPageID,'/index.html')" />
            </xsl:otherwise>
        </xsl:choose>
	</xsl:template>

</xsl:stylesheet>