<%
import se.datadosen.util.*;  

int thumbCommentLength = 30; //max amount of comment that is displayed underneath Thumbnail

//Code needed to include files from the res directory  
File root = (File)album.get("root");  
if (root == null) 
{
  root = outputDirectory;
  album.put("root", root);  
  rootName = title;
  album.put("rootName", rootName);
}
File resDir = new File(root, "res");
album.put("resDir", resDir);

//method to show multilevel index links
void writePath(File dir, String prefix) { 
  boolean topLevel = fileVariables.get(dir) == null; 

  if (!topLevel) 
    writePath(dir.getParentFile(), prefix + "../"); 

  if (!topLevel) out.print(" > ");

  if (prefix.length() > 0) 
    out.print("<a href=\"" + prefix + engine.getIndexPageName() + engine.getPageExtension() + "\">"); 
  else
    out.print("<a href=\"" + indexPage + "\">"); 

  out.print((topLevel ? "<b>" + album.get("rootName") + "</b>" : dir.getName()) + "</a>");
} 


%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
<TITLE>$title</TITLE>
<LINK HREF="$stylePath" rel=stylesheet>
</HEAD>

<BODY ID="index">

<!-- Include header.inc from image directory if present -->
<ja:include page="<%= resDir %>/header.inc" />
<table width="100%" border="0">
  <tr class="headerRow">
    <td>
      <div class="indexLinks" align="left"><%= writePath(imageDirectory, "") %> </div>
    </td>
  </tr>
  <tr class="titleRow">
    <td align="center">
      <div class="indexTitle" align="center">$title</div>
      <ja:if exists="subtitle" >
        <span class="subtitle" align="center">$subtitle</span>
      </ja:if>
    </td>
  </tr>
  <tr>
    <td>      
      <!-- Iterate through images and produce an index table -->
      <table align="center" border="0" cellspacing="10">
        <tr>
          <td colspan="3" class"pageNavCell" align="center">
          <ja:if test=<%= level > 0 %> >
            <div align="center"><a href="../"><img src="$resPath/up.gif" alt="Back to Previous Directory" border="0"></a></div>
          </ja:if>
          <ja:if test=<%= totalIndexes != void %>>
          <table align="center" border="0">
            <tr>
              <td>
              <ja:if exists="previousIndexPage">
                <div><a href="$previousIndexPage"><img align="texttop" src="$resPath/previous.gif" border="0" alt="Previous Page"></a></div>
              </ja:if>
              <ja:else>
                <div><img src="$resPath/previous_disabled.gif" border="0" alt="No Previous Page" align="texttop"></div>
               </ja:else>
              </td>
              <td class="pageNavCell">    
                <%
                  //Produce fast Google-like numbered links to other index pages
                  if (totalIndexes == void) return;  // totalIndexes only exist if several index pages
                  {
                    out.print("Pages: ");
                    for (i=1; i<=totalIndexes; i++) 
                    {
                      if (i == indexNum) 
                        out.print("<span class='currentPageNum'> " + i + " </span>");  // Emphasize current index page
                      else {
                        out.print(" <a class='pageNumLink' href=\"" + engine.getIndexPageName() + (i==1?"":i)
                         + engine.getPageExtension() + "\">" + i + "</a> ");
                      }
                    }
                  }
                %>  
              </td>
              <td class="pageNavCell" align="center"> 
              <ja:if exists="nextIndexPage">
                <a href="$nextIndexPage"><div><img src="$resPath/next.gif" border="0" alt="Next Page" align="texttop"></div>
                
                </a>
              </ja:if>
              <ja:else>
                <div><img src="$resPath/next_disabled.gif" border="0" alt="No Next Page"></div>
                
              </ja:else>  
              </td> 
             </tr>
            </table>
            </ja:if>
            <br>
           </td>
        </tr>
      <ja:rowiterator>
        <TR>
        <ja:coliterator>
          <% 
             String currentComment = ""; //container to hold potentially truncated comment
             // write comment (or part thereof).  
             // _<filename>.inc comment takes precedence over comment within image.
             if (comment != void)
              {
                currentComment = comment;
              }
             
             //see if there is a _<filename>.inc file, and if so extract the contents
             String filename = fileName;
             if (fileName != void && fileName.length() >= 4)   
             {
               filename =  fileName.substring(0, fileName.length()-4);
             }
             
             File captionFile = new File(imageDirectory, "_" + filename + ".inc");
             if (captionFile.exists())
             {
               String commentString = IO.readTextFile(captionFile);
               
               if (commentString != null) 
               {
                  //replaceAll is a Java 1.4 method, so make sure version is verified (see init.bsh)
                  if (((String)album.get("versionVerified")).equals("true"))
                  {
                    //strip all HTML tags out of comment so that it displays correctly
                    commentString = commentString.replaceAll("\\<.*?\\>","");
                  }
                  currentComment = commentString;
               }                
             }             
             
             //set comment to max length if necessary
             if (currentComment.length() > thumbCommentLength)  //thumbCommentLength defined at beginning of file
             {
                currentComment = currentComment.substring(0,thumbCommentLength-4);
                currentComment = currentComment + "...";
             }             
          %> 
          
            <ja:if exists="iconPath">
              <%
              //For directories, use the corresponding thumbnail image if it exists
              File customThumb = new File(imageDirectory,"_" + fileName + ".jpg");
              %>
              <ja:if test= <%= customThumb.exists() %>>
                <%
                  File thumbDest = new File(outputDirectory, customThumb.getName());                  
                  if (!customThumb.equals(thumbDest))
                    IO.copyFile(customThumb, thumbDest);
                %>
                <TD class="indexThumbCell" align="left"  WIDTH="<%=100/cols%>%">              
                <A HREF="$closeupPath" class="thumb">
                <IMG BORDER=0 alt="Click to Enter '$label' Section" class="image" SRC="<%= "_" + fileName + ".jpg"%>"><BR>
                <div class="thumbLabel"><b>$label Section</b></div></A>
              </ja:if>
              <ja:else>
                  <TD class="indexThumbCell" align="center" valign="bottom" WIDTH="<%=100/cols%>%">              
                  <A HREF="$closeupPath" class="thumb">
                  <IMG SRC="$thumbPath" WIDTH="$thumbWidth" HEIGHT="$thumbHeight" alt="Click to Enter '$label' Page" border="0"><BR>
                  <div class="thumbLabel"><b>$label Directory</b></div></A>
                <br>
              </ja:else>
            </ja:if>
            <ja:else>          
              <TD class="indexThumbCell" align="left" WIDTH="<%=100/cols%>%">
              <A HREF="$closeupPath">
              <IMG CLASS="image" SRC="$thumbPath" WIDTH="$thumbWidth" HEIGHT="$thumbHeight" BORDER=0 ALT="Click for large version"><BR>
              <div class="thumbLabel"><b>$label</b></div></A>
              <div class="thumbDetails">$fileDate, <SMALL><%= fileSize/1024 %> KB</SMALL></div>
               <div class="thumbComment"><%= currentComment %></div>
               <BR>
            </ja:else>
            <br>
          </TD>
        </ja:coliterator>
        </TR>
      </ja:rowiterator>
          <tr>
          <td colspan="3" class="pageNavCell" align="center">
          <ja:if test=<%= totalIndexes != void %>>
          <table align="center" border="0">
            <tr>
              <td>
              <ja:if exists="previousIndexPage">
                <div><a href="$previousIndexPage"><img align="texttop" src="$resPath/previous.gif" border="0" alt="Previous Page"></div>
                
                </a>
              </ja:if>
              <ja:else>
                <div><img src="$resPath/previous_disabled.gif" border="0" alt="No Previous Page" align="texttop"></div>
                
               </ja:else>
              </td>
              <td class="pageNavCell">    
                <%
                  //Produce fast Google-like numbered links to other index pages
                  if (totalIndexes == void) return;  // totalIndexes only exist if several index pages
                  {
                    out.print("Pages: ");
                    for (i=1; i<=totalIndexes; i++) 
                    {
                      if (i == indexNum) 
                        out.print("<span class='currentPageNum'> " + i + " </span>");  // Emphasize current index page
                      else {
                        out.print(" <a class='pageNumLink' href=\"" + engine.getIndexPageName() + (i==1?"":i)
                         + engine.getPageExtension() + "\">" + i + "</a> ");
                      }
                    }
                  }
                %>  
              </td>
              <td class="pageNavCell" align="center"> 
              <ja:if exists="nextIndexPage">
                <a href="$nextIndexPage"><div><img src="$resPath/next.gif" border="0" alt="Next Page" align="texttop"></div>
                
                </a>
              </ja:if>
              <ja:else>
                <div><img src="$resPath/next_disabled.gif" border="0" alt="No Next Page"></div>
                
              </ja:else>  
              </td> 
             </tr>
            </table>
            </ja:if>
           </td>
        </tr>
</TABLE>
    </td>
  </tr>
</table>
<!-- Include footer.inc from image directory if present -->
<ja:include page="<%= resDir %>/footer.inc" />

<P>
  <SMALL>Generated by <A HREF="$generatorUrl" TARGET="_blank"> $generator</A>
  </SMALL>
</P>
</BODY>
</HTML>