Index: /gs3-installations/intermuse/trunk/sites/intermuse/collect/programmes/transform/pages/document-iiif-manifest.xsl
===================================================================
--- /gs3-installations/intermuse/trunk/sites/intermuse/collect/programmes/transform/pages/document-iiif-manifest.xsl	(revision 37082)
+++ /gs3-installations/intermuse/trunk/sites/intermuse/collect/programmes/transform/pages/document-iiif-manifest.xsl	(revision 37082)
@@ -0,0 +1,738 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<xsl:stylesheet version="1.0"
+	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
+	xmlns:java="http://xml.apache.org/xslt/java"
+	xmlns:util="xalan://org.greenstone.gsdl3.util.XSLTUtil"
+	xmlns:gslib="http://www.greenstone.org/skinning"
+	xmlns:gsf="http://www.greenstone.org/greenstone3/schema/ConfigFormat"
+	extension-element-prefixes="java util"
+	exclude-result-prefixes="java util gsf">
+
+  
+  <!-- Inherit from document, but modified to drastically cut things down to what is needed:
+         (i)  focus on just the (potientailly hierachically nested) pages
+         (ii) so a bit like ed=1, but implied rather than needing to be set
+
+       Key goal: generate a IIIF v2 compliant manifest file in JSON format based on the document
+  -->
+
+  <!--
+  <xsl:import href="pages/document.xsl"/>
+  -->
+
+  <xsl:include href="layouts/header-core.xsl"/>
+
+  <xsl:include href="pages/document-shared.xsl" />
+  <xsl:include href="pages/document-core.xsl" />
+
+  <xsl:output method="html" version="1.0" encoding="utf-8"/>
+  
+  <!--
+  <xsl:output method="text" version="1.0" encoding="utf-8" omit-xml-declaration="yes"/>
+  <xsl:strip-space elements="*"/>
+  -->
+  
+    <!-- If the c parameter is empty then use the p.c parameter for the collection name-->
+  <xsl:variable name="collNameChecked">
+    <xsl:choose>
+      <xsl:when test="$collName = '' and /page/pageRequest/paramList/param[@name='p.c']/@value">
+	<xsl:value-of select="/page/pageRequest/paramList/param[@name='p.c']/@value"/>
+      </xsl:when>
+      <xsl:otherwise>
+	<xsl:value-of select="$collName"/>
+      </xsl:otherwise>
+    </xsl:choose>
+  </xsl:variable>
+
+
+  <xsl:variable name="httpDocument">
+    <xsl:value-of select="$library_name"/>
+    <xsl:text>/collection/</xsl:text>
+    <xsl:value-of select="/page/pageResponse/collection/@name"/>
+    <xsl:text>/document/</xsl:text>
+    <xsl:value-of select="/page/pageRequest/paramList/param[@name = 'd']/@value"/> <!-- or use $docID from default/transform/pages/document.xsl ???? -->
+  </xsl:variable>
+
+  <xsl:variable name="iiifPresentationRootIdentifier">http-greenstone://<xsl:value-of select="$site_name"/>/<xsl:value-of select="$collNameChecked"/>/<xsl:value-of select="$docID"/></xsl:variable>
+
+  <xsl:variable name="bookswitch">mirador</xsl:variable>
+    
+  <xsl:template match="/">
+    <html>
+      <body>
+	<xsl:apply-templates select="/page/pageResponse/document" />
+      </body>
+    </html>
+    
+  </xsl:template>
+
+
+  <xsl:template name="iiif-other-content">
+    <xsl:param name="optSection"/>
+    
+    <xsl:variable name="baseURL">https:<xsl:value-of select="/page/pageRequest/@baseURL"/></xsl:variable> <!-- **** had to hardwire https: -->    
+    <!-- **** test for a gsf-option before include !!!! -->    
+  	      "otherContent": [{
+		    "@id": "<xsl:value-of select="$baseURL"/><xsl:value-of select="$library_name"/>/sites/<xsl:value-of select="$site_name"/>/collect/<xsl:value-of select="$collNameChecked"/>/index/assoc/<gsf:metadata name="assocfilepath" select="root"/>/openannotation-list<xsl:value-of select="$optSection"/>.json",
+                    "@type": "sc:AnnotationList"
+              }],
+  </xsl:template>
+  
+  
+  <xsl:template name="iiif-links">
+    <xsl:param name="identifier"/>
+    <xsl:param name="docType"/>
+    
+    <xsl:variable name="iiifImageRootIdentifier">
+      <xsl:text>/gs-cantaloupe/iiif/2/</xsl:text><xsl:value-of select="$site_name"/>:<xsl:value-of select="$collNameChecked"/>:<xsl:value-of select="$identifier"/>
+    </xsl:variable>
+
+
+    <!--
+      var chosen_title = "untitled";
+
+      if (gs.variables.chosenTitle &amp;&amp; !gs.variables.docNodeID) {
+          // simple doc
+          chosen_title = gs.variables.chosenTitle;
+      }
+      else {
+          var docNodeID = gs.variables.docNodeID;
+          var page_pos = docNodeID.indexOf('.');      
+          chosen_title = (page_pos>=0) ? "page " + docNodeID.substring(page_pos+1) : docNodeID;
+      }
+
+    -->
+
+
+    
+    <!-- page/pageRequest[@baseURL]="//intermuse.sowemustthink.space/greenstone3/ -->
+    <!-- page/pageRequest[@fullURL]="//intermuse.sowemustthink.space/greenstone3/library?a=d.... -->
+
+    <!-- ***** need to work out from raw XML message, say processing something from the above -->
+    
+    <xsl:variable name="document-location-origin">https://intermuse.sowemustthink.space</xsl:variable>
+
+    <xsl:variable name="section"><xsl:value-of select="position()" /></xsl:variable>
+    <xsl:variable name="optSection">
+      <xsl:choose>
+	<xsl:when test="$docType = 'simple'"></xsl:when><!-- i.e. empty -->
+	<xsl:otherwise><xsl:value-of select="position()" /></xsl:otherwise>
+      </xsl:choose>
+    </xsl:variable>
+
+
+    <xsl:variable name="chosenTitle">
+      <xsl:choose>
+	<xsl:when test="$docType = 'simple'">
+	  <xsl:call-template name="choose-title"/>
+	</xsl:when>
+	<xsl:otherwise>page <xsl:value-of select="$section"/></xsl:otherwise>
+      </xsl:choose>
+    </xsl:variable>
+
+    <!--
+      docType   = <xsl:value-of select="$docType"/>
+      docID     = <xsl:value-of select="$docID"/>
+      position  = <xsl:value-of select="position()" />
+
+      identifer = <xsl:value-of select="$identifier"/>
+      section    = <xsl:value-of select="$section"/>
+      optSection = <xsl:value-of select="$optSection"/>
+
+      iiifPresRootIdentifier  = <xsl:value-of select="$iiifPresentationRootIdentifier" />
+      iiifImageRootIdentifier = <xsl:value-of select="$iiifImageRootIdentifier" />
+    -->
+    <!-- an array entry in 'canvases': { @content, @id, @type, label, width, height, images[{}], thumbnail {@id,@type} } -->
+    <pre>
+      <xsl:if test="$section &gt; 1">
+	  ,
+      </xsl:if>
+	    {
+	      "@context": "http://iiif.io/api/presentation/2/context.json",
+	      "@id":      "<xsl:value-of select="$iiifPresentationRootIdentifier" />/canvas/<xsl:value-of select="$section"/>",
+	      "@type":    "sc:Canvas",
+	      "label":    "<xsl:value-of select="$chosenTitle"/>",
+	      <xsl:call-template name="iiif-other-content">
+		<xsl:with-param name="optSection"><xsl:value-of select="$optSection"/></xsl:with-param>
+	      </xsl:call-template>	    
+              &quot;width&quot;:  <gsf:metadata name="ImageWidth"/>,
+              &quot;height&quot;: <gsf:metadata name="ImageHeight"/>,
+	      
+	      "images": [{
+                "image": {
+	          "@context":   "http://iiif.io/api/image/2/context.json",
+	          "@type":      "oa:Annotation",
+		  "motivation": "sc:painting",
+		  "on":         "<xsl:value-of select="$iiifPresentationRootIdentifier" />/annotation/<xsl:value-of select="$section"/>",
+
+		  "resource": {
+		    "@id":     "<xsl:value-of select="$document-location-origin" /><xsl:value-of select="$iiifImageRootIdentifier" />/full/full/0/default.jpg",
+		    "@type":   "dctypes:Image",
+		    "format":  "image/jpeg",
+		    "width":   <gsf:metadata name="ImageWidth"/>,
+		    "hieght":  <gsf:metadata name="ImageHeight"/>,
+		    "service": {
+		      "@context": "http://iiif.io/api/image/2/context.json",
+		      "@id":      "<xsl:value-of select="$document-location-origin" /><xsl:value-of select="$iiifImageRootIdentifier" />",
+		      "profile":  "https://iiif.io/api/image/2/profiles/level2.json"
+		    }
+		  }
+		}
+	      }],
+	      "thumbnail": {
+	        "@id":   "<xsl:value-of select="$document-location-origin" /><xsl:value-of select="$iiifImageRootIdentifier" />/full/,150/0/default.jpg",
+		"@type": "dctypes:Image"
+	      }
+            }      
+    </pre>
+    <!-- Another field to consider in the above JSON is: -->
+    <!--   "format": mime_type, // ???? or is it OK to always have this as image/jpeg and rely on IIIF Image Server coverting as/when needed??? -->
+  </xsl:template>
+
+
+  <xsl:template name="googlevision-ocr-jsonOLD">
+    <xsl:param name="assocfilepath"/>
+
+    <xsl:variable name="GVDocumentOCRJSON"><gsf:metadata name="GVDocumentOCRJSON"/></xsl:variable>
+    <xsl:variable name="docSectionID"><xsl:value-of select="util:replace(@nodeID, '.', '_')"/></xsl:variable>
+<!--
+    <gsf:variable name="GVDocumentOCRJSON"><gsf:metadata name="GVDocumentOCRJSON"/></gsf:variable>
+    <gsf:variable name="docSectionID"><xsl:value-of select="util:replace(@nodeID, '.', '_')"/></gsf:variable>
+-->
+
+<!--
+    <gsf:script>
+      load_gv_dococr_json(gs.variables.GVDocumentOCRJSON,gs.variables.docSectionID,display_gv_ocr_bounding_boxes);
+    </gsf:script>
+-->
+
+  </xsl:template>
+
+
+  <xsl:template name="wrappedSectionPost">
+
+    <!-- add in IIIF Server link for section image -->
+    <xsl:variable name="nodeID" select="@nodeID"/>
+    <xsl:call-template name="iiif-links">
+      <xsl:with-param name="identifier" select="$nodeID" />
+      <xsl:with-param name="docType" select="@docType" />
+    </xsl:call-template>
+
+    <!-- add in Google Vision OCR JSON link for section image -->
+    <!--
+    <xsl:call-template name="googlevision-ocr-json">
+      <xsl:with-param name="assocfilepath"><gsf:metadata name="assocfilepath" select="root"/></xsl:with-param>
+    </xsl:call-template>    
+    -->
+    
+  </xsl:template>
+
+  <xsl:template name="sectionImage">
+    <!-- OBSOLETE when removing forced bookswith = mirador -->
+    <!-- make it do nothing !! -->
+    <xsl:if test="$bookswitch != 'mirador'">
+      <gsf:image type="screen"/>
+    </xsl:if>
+  </xsl:template>
+
+  <xsl:template name="sectionContent">
+    <xsl:call-template name="wrappedSectionPost"/>
+  </xsl:template>
+
+  <xsl:template name="sectionContentOLD">
+
+    <xsl:choose>
+      <xsl:when test="$bookswitch = 'mirador'">
+	<gsf:variable name="screenImageWidth"><gsf:metadata name="ScreenWidth"/></gsf:variable>
+	<gsf:variable name="screenImageHeight"><gsf:metadata name="ScreenHeight"/></gsf:variable>
+	<gsf:variable name="imageWidth"><gsf:metadata name="ImageWidth"/></gsf:variable>
+	<gsf:variable name="imageHeight"><gsf:metadata name="ImageHeight"/></gsf:variable>
+	<gsf:variable name="docNodeID"><xsl:value-of select="@nodeID"/></gsf:variable> <!-- used to make each 'tidyDiv' unique -->
+      </xsl:when>
+
+      <!-- OBSOLETE when removing forced bookswith = mirador -->
+      <xsl:otherwise>
+	<xsl:call-template name="wrappedSectionImage"/>
+	
+	<div style="padding: 10px 0px 10px 0px">Ocr'd Text:</div>
+	<i>
+	  <xsl:call-template name="wrappedSectionText"/>
+	</i>
+      </xsl:otherwise>
+    </xsl:choose>
+    
+    <xsl:call-template name="wrappedSectionPost"/>
+  </xsl:template>
+  
+  <xsl:template name="documentPre">
+    <xsl:variable name="httpCollection">library/sites/<xsl:value-of select="$site_name"/>/collect/<xsl:value-of select="/page/pageResponse/collection/@name"/></xsl:variable>
+
+    <xsl:variable name="chosenTitle">
+      <xsl:choose>
+	<xsl:when test="@docType='simple'">
+	  <xsl:call-template name="choose-title"/>
+	</xsl:when>
+	<xsl:otherwise>
+	  <xsl:value-of disable-output-escaping="yes" select="/page/pageResponse/document/documentNode/metadataList/metadata[@name='Title']" />
+	</xsl:otherwise>
+      </xsl:choose>
+    </xsl:variable>
+
+    <!-- <gsf:variable name="chosenTitle"><xsl:call-template name="choose-title"/></gsf:variable> -->
+    <!-- <gsf:variable name="toplevelTitle"><gsf:metadata name="Title" select="root"/></gsf:variable> -->
+    <!-- <gsf:variable name="imageWidth"><gsf:metadata name="ImageWidth"/></gsf:variable> -->
+    <!-- <gsf:variable name="imageHeight"><gsf:metadata name="ImageHeight"/></gsf:variable> -->
+
+
+    <!-- iiif_manifest: -->
+    <pre>
+    {
+        "@context": "http://iiif.io/api/presentation/2/context.json",
+	"@type": "sc:Manifest",
+	"@id": "<xsl:value-of select="$iiifPresentationRootIdentifier"/>/manifest",
+	"label": "<xsl:value-of select="$chosenTitle"/>",
+	"attribution": "The Internet of Musical Events (InterMusE): Digital Scholarship, Community, and the Archiving of Performance",
+	"license": "https://**** To Be Determined ****",
+	"logo": "https://intermuse.datatodata.org/wp-content/uploads/sites/4/2021/03/cropped-cropped-cropped-1.png",
+        "metadata": [
+	  { "label": "creator", "value": ["aaa1","aaa2"] },
+	  { "label": "title", "value": ["chosen-title"] }
+	],
+	<!-- trival IIIF sequences => has one IIIF canvas in it -->
+	"sequences": [{
+	  "@context": "http://iiif.io/api/image/2/context.json",
+          "@id": "<xsl:value-of select="$iiifPresentationRootIdentifier"/>/sequence/normal",
+          "@type": "sc:Sequence",
+	  "canvases": [
+    </pre>
+
+    <!--
+    <gsf:script>
+      console.log("**** sa=iiif-manifest: chosenTitle = " + gs.variables.chosenTitle);
+    </gsf:script>
+    <gsf:script src="{$httpCollection}/js/document_viewer.js"/>
+    <style>
+       div.ocr-boundingbox-overlay {
+         position: absolute;
+         left: 0;
+         top: 0;
+       }
+
+       div.ocr-boundingbox {
+         position: absolute;
+         border: solid black 1px;
+       }
+
+    </style>
+    -->
+    
+    <!-- OBSOLETE when removing forced bookswith = mirador -->
+    <!-- the following is not needed, if only generating the required JSON IIIF Manifest file -->
+    <!--
+    <xsl:if test="$bookswitch = 'mirador'">
+      <gsf:script src="ext/structured-image/mirador3/dist/mirador-with-annotations.js"/>
+      <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500" />
+    </xsl:if>
+    -->
+    
+    <!--
+    <gsf:script>
+      var IIIF_Doc_Images = [];
+    </gsf:script>
+    -->    
+  </xsl:template>
+      
+
+  <!--
+{
+    "@context": "http://iiif.io/api/presentation/2/context.json",
+    "@id": "http-greenstone://intermuse/programmes/HMS_1/canvas/1",
+    "@type": "sc:Canvas",
+    "width": "3670",
+    "height": "4641",
+    "label": "page 1",
+    "images": [
+        {
+            "@context": "http://iiif.io/api/image/2/context.json",
+            "@id": "http-greenstone://intermuse/programmes/HMS_1/annotation/1",
+            "@type": "oa:Annotation",
+            "motivation": "sc:painting",
+            "on": "http-greenstone://intermuse/programmes/HMS_1/canvas",
+            "resource": {
+                "@id": "https://intermuse.sowemustthink.space/gs-cantaloupe/iiif/2/intermuse:programmes:HMS_1.1/full/full/0/default.jpg",
+                "@type": "dctypes:Image",
+                "format": "image/jpeg",
+                "width": "3670",
+                "height": "4641",
+                "service": {
+                    "@context": "http://iiif.io/api/image/2/context.json",
+                    "@id": "https://intermuse.sowemustthink.space/gs-cantaloupe/iiif/2/intermuse:programmes:HMS_1.1",
+                    "profile": "https://iiif.io/api/image/2/profiles/level2.json"
+                }
+            }
+        }
+    ],
+    "thumbnail": {
+        "@id": "https://intermuse.sowemustthink.space/gs-cantaloupe/iiif/2/intermuse:programmes:HMS_1.1/full/,150/0/default.jpg",
+        "@type": "dctypes:Image"
+    }
+}
+  -->
+  
+  <xsl:template name="documentContentSimpleIIIFImage">
+    <xsl:call-template name="iiif-links">
+      <xsl:with-param name="identifier" select="$docID" />
+      <xsl:with-param name="docType" select="@docType" />
+    </xsl:call-template>
+  </xsl:template>
+  
+  <xsl:template name="documentContent">
+    <!-- The way document.xsl templates are structured, only get to here is if @docType = 'simple' -->
+    <!-- A 'paged' (or 'hierarical') document goes throgh 'wrappedContent' -->
+    <xsl:call-template name="documentPre"/>
+    <xsl:call-template name="documentContentSimpleIIIFImage"/>
+    <xsl:call-template name="documentPost"/>
+    <xsl:call-template name="documentPostMirador3"/>
+  </xsl:template>
+
+  <xsl:template name="documentPost">
+    <!-- close of canvases -->
+    <pre>
+	  ]
+	}]	  
+    }    	
+    </pre>
+    
+  </xsl:template>
+  
+  <xsl:template name="documentPostOLD">
+    <xsl:if test="@docType='simple'">
+      <!-- add in Google Vision OCR JSON link for simple page image -->      
+      <xsl:call-template name="googlevision-ocr-jsonOLD">
+	<xsl:with-param name="assocfilepath"><gsf:metadata name="assocfilepath" pos="first"/></xsl:with-param>
+      </xsl:call-template>
+    </xsl:if>
+  </xsl:template>
+
+  <xsl:template name="documentPostMirador3">
+
+    <!-- Container element of Mirador whose id should be passed to the instantiating call as "id" -->    
+    <div style="position: relative; width: 100%; height: 800px;">
+      <div id="mirador3-viewer"> <xsl:comment>filler</xsl:comment></div>
+    </div>
+
+    <!--
+    <gsf:script>      
+      var iiifpres_root_id = gs.variables.iiifPresentationRootIdentifier;
+      console.log(gs);
+      var iiifpres_label   = (gs.variables.toplevelTitle) ? gs.variables.toplevelTitle : gs.variables.chosenTitle;
+
+      var iiif_manifest = initIIIFManifest(iiifpres_root_id,iiifpres_label, IIIF_Doc_Images);
+
+      var mirador3 = createMirador3Viewer();
+    </gsf:script>
+    -->
+    
+  </xsl:template>
+
+  <xsl:template name="wrappedDocumentTopLevel">
+
+    <xsl:choose>
+      <xsl:when test="$bookswitch = 'mirador'">
+	<!-- <xsl:call-template name="documentPre"/> -->
+	<div style="display:block;"> <!-- For embedded mirador3 viewer, used to be none  **** -->
+	  <xsl:call-template name="wrappedDocument"/> <!-- without ed=1, this template misses the binds in of documentPre and documentPost -->
+	</div>
+	<!-- <xsl:call-template name="documentPost"/> -->
+	  <xsl:call-template name="documentPostMirador3"/>
+      </xsl:when>
+
+      <!-- OBSOLETE when removing forced bookswith = mirador -->
+      <xsl:otherwise>
+	<xsl:call-template name="wrappedDocument"/> <!-- without ed=1, this template misses the binds in of documentPre and documentPost -->
+      </xsl:otherwise>
+
+    </xsl:choose>
+  </xsl:template>
+
+  
+	<!-- content of a simple document. Will not be used for editing mode -->
+	<xsl:template name="documentPostInProgress">
+
+
+			    <gsf:script src="https://unpkg.com/mirador@latest/dist/mirador.min.js"/>
+
+
+			    <!-- By default uses Roboto font. Be sure to load this or change the font -->
+			    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500" />
+			    <!-- Container element of Mirador whose id should be passed to the instantiating call as "id" -->
+			    
+			    <div style="position: relative; width: 100%; height: 800px;">
+			      <div id="my-mirador"> <xsl:comment>filler</xsl:comment></div>
+			    </div>
+			    
+			    <gsf:script>
+			      
+var mirador = Mirador.viewer({
+  "id": "my-mirador",
+  "manifests": {
+    "https://intermuse.sowemustthink.space/greenstone3/sample-manifest.json": {
+      "provider": "IntermusE"
+    }
+    },
+  "window" : { "panels" : { "annotations": true, "attribution": false } },
+  "annotations": {
+    "htmlSanitizationRuleSet": 'iiif', // See src/lib/htmlRules.js for acceptable values
+    "filteredMotivations": ['oa:commenting', 'oa:tagging', 'sc:painting', 'commenting', 'tagging'],
+  },    
+  "windows": [
+    {
+      "loadedManifest": "https://intermuse.sowemustthink.space/greenstone3/sample-manifest.json",
+      "canvasIndex": 1,
+      "thumbnailNavigationPosition": 'far-bottom'
+    }
+  ]
+});
+			    </gsf:script>
+
+	  
+	</xsl:template>
+	
+
+
+	<xsl:template name="wrapDocumentNodes">
+
+	  <xsl:choose>
+	    <xsl:when test="../../document">
+	      <xsl:call-template name="topLevelSectionContent"/>
+	    </xsl:when>
+	    <xsl:otherwise>
+	      <xsl:call-template name="sectionContent"/>
+	    </xsl:otherwise>
+	  </xsl:choose>
+	  
+	  <xsl:if test="documentNode">
+	    <xsl:for-each select="documentNode">
+	      <xsl:call-template name="wrapDocumentNodes"/>
+	    </xsl:for-each>
+	  </xsl:if>
+	</xsl:template>
+
+	<!-- Used to make sure that regardless what the collection designer uses for the title and content we can wrap it properly -->
+	<!-- If editing, be aware that the Document Basket looks for specific classes that this template bakes in (key points marked with ***) -->
+	<xsl:template name="wrapDocumentNodesOLD">
+	        <xsl:variable name="nodeID" select="@nodeID"/>
+		<a name="{@nodeID}"><xsl:text> </xsl:text></a>
+		
+		<!-- Section header -->
+		<table class="sectionHeader"><tr>
+					
+			<!-- Title -->
+			<td id="header{@nodeID}" class="headerTD sectionTitle"><!-- *** -->
+				<p>
+					<xsl:attribute name="class"><xsl:value-of select="util:hashToDepthClass(@nodeID)"/> sectionHeader</xsl:attribute>
+
+					<xsl:if test="util:hashToSectionId(@nodeID)">
+						<span class="sectionNumberSpan">
+							<xsl:value-of select="util:hashToSectionId(@nodeID)"/>
+							<xsl:text> </xsl:text>
+						</span>
+					</xsl:if>
+					<!-- Display the title for the section regardless of whether automatic section numbering is turned on -->
+					<span><xsl:call-template name="sectionHeading"/></span>
+				</p>
+			</td>
+			
+		</tr></table>
+
+		<div id="doc{@nodeID}"><!-- *** -->
+		  
+		  <xsl:choose>
+		    <xsl:when test="../../document">
+		      <xsl:call-template name="topLevelSectionContent"/>
+		    </xsl:when>
+		    <xsl:otherwise>
+		      <xsl:call-template name="sectionContent"/>
+		    </xsl:otherwise>
+		  </xsl:choose>
+
+		  <xsl:if test="documentNode">
+		    <xsl:for-each select="documentNode">
+		      <xsl:call-template name="wrapDocumentNodes"/>
+		    </xsl:for-each>
+		  </xsl:if>
+		</div>	
+	</xsl:template>
+
+
+
+
+	<xsl:template name="wrappedSectionImage">
+		<xsl:variable name="screenImageWidth"><gsf:metadata name="ScreenWidth"/></xsl:variable>
+		<xsl:variable name="screenImageHeight"><gsf:metadata name="ScreenHeight"/></xsl:variable>
+		<xsl:variable name="imageWidth"><gsf:metadata name="ImageWidth"/></xsl:variable>
+		<xsl:variable name="imageHeight"><gsf:metadata name="ImageHeight"/></xsl:variable>
+		<xsl:variable name="docNodeID"><xsl:value-of select="@nodeID"/></xsl:variable> <!-- used to make each 'tidyDiv' unique -->
+
+				<gsf:div id="image{@nodeID}">
+					<xsl:attribute name="style">
+						<xsl:choose>
+							<xsl:when test="/page/pageRequest/paramList/param[@name = 'view']/@value = 'text'">
+								<xsl:text>display:none;</xsl:text>
+							</xsl:when>
+							<xsl:otherwise>
+								<xsl:text>display:block;</xsl:text>
+							</xsl:otherwise>
+						</xsl:choose>
+					</xsl:attribute>
+					<xsl:call-template name="sectionImage"/><xsl:text> </xsl:text>
+				</gsf:div>
+
+
+	</xsl:template>
+	
+	<!-- The default template for displaying the document node text -->
+	<!-- equivalent to gsf:text -->
+	<xsl:template name="documentNodeText">
+	  <xsl:param name="force">0</xsl:param>
+		<!-- Hides the "This document has no text." message -->
+		<xsl:variable name="noText"><gsf:metadata name="NoText"/></xsl:variable>
+		<xsl:choose>
+		<xsl:when test="$force = '1' or not($noText = '1')">
+
+			<!-- Section text -->
+			<xsl:for-each select="nodeContent">
+			  <xsl:call-template name="displayMarkedUpTextAndAnnotations"/>
+			</xsl:for-each>
+		</xsl:when>
+		<xsl:when test="$noText = '1' and not(metadataList/metadata[@name='ImageType'])">
+			<gsf:link type="source"><gsf:metadata name="Source"/></gsf:link>
+		</xsl:when>
+		</xsl:choose>
+		<xsl:text> </xsl:text>
+	</xsl:template>
+
+
+
+
+
+	<xsl:template match="/page/pageResponse/document">
+		<xsl:if test="$bookswitch = 'off' or ($bookswitch != 'on' and $bookswitch != 'flashxml')">
+			<div id="bookdiv" style="visibility:hidden; height:0px; display:inline;"><xsl:text> </xsl:text></div>
+		
+			<div id="float-anchor" style="width: 30%; min-width:180px; float:right; margin: 0 0 10px 20px;">		
+
+			<!-- add in some text just in case nothing has been added to this div-->
+			<xsl:text> </xsl:text>
+			</div>
+		</xsl:if>
+		
+		<!-- display the document -->
+		<xsl:choose>
+			<xsl:when test="@docType='simple'">
+				<xsl:call-template name="documentHeading"/><br/>
+				<xsl:call-template name="documentContentTopLevel"/>
+			</xsl:when>	
+			<xsl:otherwise> <!-- display the standard greenstone document -->
+				<xsl:call-template name="wrappedDocumentTopLevel"/>
+			</xsl:otherwise>
+		</xsl:choose>
+	</xsl:template>
+
+
+
+
+	<xsl:template name="wrappedDocument">
+		<xsl:choose>
+			<!-- NOTE: alb = ajax load bypass -->
+			<!-- 
+			     If we have asked for expanded document, then do this.
+			     OR If the docType is hierarchy and we want to bypass the ajax load then do this 
+			     OR If the docType is hierarchy and we have asked for the top level document then do this 
+			-->
+			<xsl:when test="/page/pageRequest/paramList/param[@name = 'ed']/@value = '1' or (/page/pageResponse/document/@docType = 'hierarchy' and (/page/pageRequest/paramList/param[@name = 'alb']/@value = '1' or (string-length(/page/pageRequest/paramList/param[@name = 'd']/@value) > 0 and not(util:contains(/page/pageResponse/document/@selectedNode, '.')))))">
+				<div id="gs-document">
+					<xsl:call-template name="documentPre"/>
+					<div id="gs-document-text" class="documenttext" collection="{/page/pageResponse/collection/@name}"><!-- *** -->
+					  <xsl:for-each select="documentNode">
+							<xsl:call-template name="wrapDocumentNodes"/>
+						</xsl:for-each>
+					</div>
+					<xsl:call-template name="documentPost"/>					
+				</div>
+			</xsl:when>
+			<xsl:when test="/page/pageResponse/document/@docType = 'paged' or /page/pageResponse/document/@docType = 'pagedhierarchy'">
+			  <div id="gs-document">
+					<div id="tocLoadingImage" style="text-align:center;">
+						<img src="{util:getInterfaceText($interface_name, /page/@lang, 'loading_image')}"/><xsl:value-of select="util:getInterfaceText($interface_name, /page/@lang, 'doc.loading')"/><xsl:text>...</xsl:text>
+					</div>
+				</div>
+				<script type="text/javascript">
+					<xsl:text disable-output-escaping="yes">
+						$(window).on("load", function()
+						{
+							var sectionID = gs.cgiParams.d;
+							var callbackFunction = null;
+							if(sectionID.indexOf(".") != -1)
+							{
+								callbackFunction = function()
+								{
+									focusSection(sectionID);
+								};
+							}
+							else {
+							   callbackFunction = function()
+								{
+								
+								expandAndExecute(sectionID+".1", null, null, null);
+								};
+							}
+							var docID = sectionID.replace(/([^.]*)\..*/, "$1");
+							var url = gs.xsltParams.library_name + "?a=d&amp;c=" + gs.cgiParams.c + "&amp;excerptid=gs-document&amp;dt=hierarchy&amp;d=" + docID;
+							if(gs.cgiParams.p_s) { 
+							url += "&amp;p.s="+gs.cgiParams.p_s;
+							}
+							loadTopLevelPage(callbackFunction, url);
+						});
+					</xsl:text>
+				</script>
+			</xsl:when>
+			<xsl:otherwise>
+				<div id="gs-document">							
+					<div id="tocLoadingImage" style="text-align:center;">
+						<img src="{util:getInterfaceText($interface_name, /page/@lang, 'loading_image')}"/><xsl:value-of select="util:getInterfaceText($interface_name, /page/@lang, 'doc.loading')"/><xsl:text>...</xsl:text>
+					</div>
+				</div>
+				<script type="text/javascript">
+					<xsl:text disable-output-escaping="yes">
+						$(window).on("load", function()
+						{
+							loadTopLevelPage(function()
+							{
+								//Don't focus the section until the table of contents is loaded
+								var tocCheck = function()
+								{
+									if(gs.variables.tocLoaded)
+									{
+										focusSection("</xsl:text><xsl:value-of select="/page/pageResponse/document/@selectedNode"/><xsl:text disable-output-escaping="yes">");
+									}
+									else
+									{
+										setTimeout(tocCheck, 500);
+									}
+								}
+								tocCheck();
+							});
+						});
+					</xsl:text>
+				</script>
+			</xsl:otherwise>
+		</xsl:choose>
+		
+		<div class="clear"><xsl:text> </xsl:text></div>
+	</xsl:template>
+
+
+
+	
+</xsl:stylesheet>
