<!-- 1toM.xsl: Copy all elements, but convert nested A elements into pop-up
menus (1-to-many links) as described at
http://developer.netscape.com/viewsource/smith_menu/smith_menu.html. 

Bob DuCharme 2003  No warrantee expressed or implied. 
 -->

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     version="1.0">

  <!-- Assume that document has a head element. Copy its contents then
       add menu scripts. -->
  <xsl:template match="head">
    <xsl:copy>

    <xsl:apply-templates/>

    <script language="JavaScript1.2" 
       src="http://www.snee.com/xml/linking/menu.js">
    </script>

    <script language="JavaScript1.2">
    function onLoad() {

		  <!-- For all a elements that have a children, -->
        <xsl:for-each select="//a[a]">
          <!-- menu name = "menu" + generated ID -->
	  	    <xsl:variable name="windowName">menu<xsl:value-of select="generate-id()"/>
          </xsl:variable>

			 <!-- JavaScript (JS) declaration to create window -->
			 <xsl:text>window.</xsl:text><xsl:value-of select="$windowName"/>
			 <xsl:text> = new Menu();
</xsl:text>

          <!-- For each 'a' child of the 'a' element that the outer for-each
			      is iterating over, output a JS line adding it as a choice
					to the menu, creating a line following this model:

               myMenu.addMenuItem("W3C specs","location='http://www.w3.org/TR/'");

             -->
		    <xsl:for-each select="a">
            <xsl:value-of select="$windowName"/>
				<xsl:text>.addMenuItem("</xsl:text><xsl:value-of select="normalize-space(text())"/>
				<xsl:text>","location='</xsl:text>
				<xsl:value-of select="@href"/>
				<xsl:text>'");
</xsl:text>
			 </xsl:for-each>

          <!-- Don't let the menu be dragged. -->
          <xsl:value-of select="$windowName"/><xsl:text>.disableDrag = true;
</xsl:text>
        </xsl:for-each>

          <!-- Following saves all (not just named) created menus. $windowName is
               now out of scope, so we have to regenerate the name of the last one. -->
          <xsl:text>menu</xsl:text>
			 <xsl:value-of select="generate-id(//a[a][last()])"/>
          <xsl:text>.writeMenus();
</xsl:text>
	 }
	 // 
    </script>

    </xsl:copy>
  </xsl:template>


  <!-- Add compatibility code to end of body. -->
  <xsl:template match="body">
    <xsl:copy>
    <xsl:apply-templates/>

  <script language="JavaScript1.2">
	 if (document.all || document.getElementById) {
		  onLoad();
	 }
	 //
  </script>

    </xsl:copy>
  </xsl:template>


  <xsl:template match="a[a]">
    <!-- If the 'a' has 'a' children, generate the menu call instead of
         outputting the children. Let the other 'a' elements get copied
         as-is. Model:
         <a href="javascript:window.showMenu(window.myMenu)">anchor text</a> -->

	 <a href="javascript:window.showMenu(window.menu{generate-id()})"
       class="multi">
      <!-- text node(s) of this 'a' element as anchor text. -->
	   <xsl:value-of select="normalize-space(text())"/> 
    </a>
  </xsl:template>

  <!-- Just copy everything else. -->
  <xsl:template match="@*|node()">
	 <xsl:copy>
		<xsl:apply-templates select="@*|node()"/>
	 </xsl:copy>
  </xsl:template>


</xsl:stylesheet>
