<%!

// import statements are concentrated here rather than in the individual routines
import java.util.*;
import se.datadosen.util.*; 
import se.datadosen.jalbum.*;

%>
<ja:if not exists="userInterface">
	<%!
	//Double check and initalise user variables if they are not present (e.g. running from command line, or if some problem with onload.bsh) 
  	String upButtonAtRootIndexPage = "normal";
	boolean useThumbForFolderIcon = true;
	boolean fixedFormatIndexPage = false;
	boolean imageNameUnderThumbnail = false;
	boolean imageCommentUnderThumbnail = false;
	boolean protectIndexImages = false;
	boolean allowEmptyFolderDescription = false;
	boolean displayW3CLogosOnIndex = true;
	String dateFormat = "dd.MM.yyy";
	int folderAgeNew = 30;
	boolean keyboardShortcuts = true;
	boolean jumpDirectFullscreen = false;
	int numberIndexColumns = 1;
	String iconSet = "default";
	boolean displayFolderDateAdded = true;
	boolean imageDateUnderThumbnail = false;
	int folderThumbScale = 100;
	boolean promotionalLogos = true;
	String charityName = "Firefox";
	int imagePadding = 4;
	boolean multiNavIndex = true;
	boolean globalHeader = true;
	boolean globalFooter = true; 
	%>
</ja:if>

<%!
// Declare non-UI variables which may be changed within a method
boolean upButtonAtRootIndexPageNormal; boolean upButtonAtRootIndexPageForce; boolean upButtonAtRootIndexPageNever; String buttonSet = ""; String spacerDiv = ""; boolean rootLevel; boolean LinkOriginalsViaScaled; int thumbCount; String noFloatForFirstThumb = ""; boolean lastProcessedWasThumb; boolean upButtonActive; int rowWidth; String rowStart = ""; String rowStop = ""; String title1 = ""; String descript = ""; String promotionBanner = ""; int origOrigThumbWidth; int origOrigThumbHeight; int origThumbWidth; int origThumbHeight; int miniThumbContainer; String fName = ""; int thumbW; int thumbH; String thumbFormatStart = ""; String thumbFormatEnd = ""; String thumbStyle = ""; String thumbPath = "";  String promotionBanner = "";

// This sets up initial values for variables
void variableInit() {

	utils.updateVars();

  // Change variables which use multiple options as words into boolean variables for easier and faster logic later
  upButtonAtRootIndexPageNormal = false; upButtonAtRootIndexPageForce = false; upButtonAtRootIndexPageNever = false;
  if (upButtonAtRootIndexPage.equalsIgnoreCase("normal")) { upButtonAtRootIndexPageNormal = true; upButtonAtRootIndexPageForce = false; upButtonAtRootIndexPageNever = false; }
  if (upButtonAtRootIndexPage.equalsIgnoreCase("force")) { upButtonAtRootIndexPageNormal = false; upButtonAtRootIndexPageForce = true; upButtonAtRootIndexPageNever = false; }
  if (upButtonAtRootIndexPage.equalsIgnoreCase("never")) { upButtonAtRootIndexPageNormal = false; upButtonAtRootIndexPageForce = false; upButtonAtRootIndexPageNever = true; }

  buttonSet = (iconSet.equalsIgnoreCase("default") ? album.get("BUTTON_SET") : iconSet);
  spacerDiv = "<div style=\"clear: left\u003b height: 10px\"></div>";

  if (resSubPath != void) resPath = resPath + "/" + resSubPath;

  if (level == 0) { rootLevel = true; } else { rootLevel = false; }
  if (engine.getImageLinking().equalsIgnoreCase("LinkOriginalsViaScaled")) { linkOriginalsViaScaled = true; } else { linkOriginalsViaScaled = false; }

  // The following are not user variables but need to be initalised anyway
  thumbCount = 0;
  noFloatForFirstThumb = "clear: left "; 
  lastProcessedWasThumb = false;
  upButtonActive = false;

  rowWidth = (maxThumbWidth+(2*imagePadding))*cols + 3;
  if (fixedFormatIndexPage) {
  rowStart = "<div style=\"width:" + rowWidth + "px\">";
  rowStop = "</div>";
  } else {
  rowStart = "";
  rowStop = "";
  }
}

void iconFileCheck() {
  // Check to see if there are any icon files present in a button set which should overwrite the default buttons in the res directory
  if (level == 0) {
 	String[] fileNameArray = {"help.gif", "info.gif", "download.gif", "orderprints.gif"};
  	for (int i=0;i<fileNameArray.length;i=i+1) {
  		File sourceFile = new File(skinDirectory + "/button-sets/" + buttonSet, fileNameArray[i]);
		if (sourceFile.exists()) {
			if (resSubPath != void) {
				File outputFile = new File(resDirectory + File.separator + resSubPath, fileNameArray[i]);
			 	IO.copyFile(sourceFile, outputFile, true);
			} else {
				File outputFile = new File(resDirectory, fileNameArray[i]);
			 	IO.copyFile(sourceFile, outputFile, true);
			}
	  	}
	}
  }
}

// Generate drop box options for quick navigation
void addLinks() {
  if (totalIndexes == void) totalIndexes = 1;
  for (i=1; i<=totalIndexes; i++) {
    out.print("<option");
    if (i == indexNum) out.print(" selected=\"selected\"");
    out.println(" value=\"" + engine.getIndexPageName() + (i==1?"":i) + engine.getPageExtension() + "\">" +    "$text.page " + i + "</option>");
  }
}

// Install or remove filters
void filterCheck() {
  if (sameSizeFilterIndex == void) sameSizeFilterIndex = false;
  if (album.get("filterRemoved") == null) album.put("filterRemoved", true);
  if (sameSizeFilterIndex && album.get("filterRemoved")) {
    JAFilter f = new FixedShapeFilter();
    album.put("ourFilter", f);
    engine.addFilter(f, 4);
    album.put("filterRemoved", false);
  }
  if (!sameSizeFilterIndex && !album.get("filterRemoved")) {
    JAFilter f = album.get("ourFilter");
    engine.removeFilter(f, 4);
    album.put("filterRemoved", true);
  }
}

void readMetaFile() {
  // If meta.properties exists in the image directory and contains
  // a description of the folder, display it with the variable descript.
	  title1 = title;
  File propsFile = new File(imageDirectory, "meta.properties");
  if (propsFile.exists()) {
    Map props = IO.readMapFile(propsFile);
    String newTitle = props.get("title");
    if (newTitle != null && newTitle.length() >0) title1 = newTitle;
    else if (level == 0) title1 = texts.getString("rootFolderName");
    Map props = IO.readMapFile(propsFile);
    String newDescript = props.get("descript");
    if (newDescript != null && newDescript.length() > 0) {
    descript = newDescript;
    } else { 
    	if (!allowEmptyFolderDescription) descript = texts.getString("noDescript");
    }
  } else {
  if (!allowEmptyFolderDescription) descript = texts.getString("noDescript");
  }	
}

void firefoxCheck() {
  // This maps the charity names to their banner ads
  if (charityName.equals("Firefox")) promotionBanner = "<a href=\"http://www.spreadfirefox.com/?q=affiliates&amp;id=45292&amp;t=67\"><img alt=\"Get Firefox!\" title=\"Get Firefox!\" src=\"http://sfx-images.mozilla.org/affiliates/Buttons/88x31/trust.gif\" /></a>";
  if (charityName.equals("AVERT AIDS Charity")) promotionBanner = "<a href=\"http://www.avert.org\"><img src=\"http://www.avert.org/media/logo/8831.jpg\" alt=\"AVERT - AIDS charity\" /></a>"; 
  if (charityName.equals("Open Office")) promotionBanner = "<a href=\"http://www.openoffice.org\"><img src=\"http://marketing.openoffice.org/art/galleries/marketing/web_buttons/nicu/88x31_5.png\"  alt=\" Use OpenOffice.org\" title=\"Use OpenOffice.org\"></a>";
  // if we are advertising Firefox, introduce some randomness to show the different logos on all the pages
  String[] ffnames = { "trust", "rediscover", "take", "get", "safer" };
  if (charityName != void && charityName.length() > 0 && charityName.equals("Firefox")) promotionBanner = "<a href=\"http://www.spreadfirefox.com/?q=affiliates&amp;id=45292&amp;t=67\"><img alt=\"Get Firefox!\" title=\"Get Firefox!\" src=\"http://sfx-images.mozilla.org/affiliates/Buttons/88x31/" + ffnames[(int)(Math.random()*ffnames.length)] + ".gif\" /></a>";
}

void writePath(File dir, String prefix) {
  boolean topLevel = fileVariables.get(dir) == null;
  if (!topLevel) writePath(dir.getParentFile(), prefix + "../");
  if (!topLevel) out.print(" &raquo; ");
  title1 = (topLevel ? texts.getString("rootFolderName") : dir.getName());
  File propsFile = new File(((prefix.length() > 0) ? dir + "/" : dir), "meta.properties");
// out.println(propsFile);
  if (propsFile.exists()) {
    Map props = IO.readMapFile(propsFile);
    String newTitle = props.get("title");
    if (newTitle != null && newTitle.length() >0) title1 = newTitle;
  }
  if ((prefix.length() > 0) && (level != 0)) out.print("<a href=\"" + prefix + engine.getIndexPageName() + engine.getPageExtension() + "\" title=\"" + texts.getString("folderJump") + "\">"); else out.print("<a href=\"" + indexPage + "\" title=\"" + texts.getString("folderJump") + "\">");
  out.print(title1 + "</a>");
}

void indexLinks() {
  if (album.get("rootName") == null) album.put("rootName", title);
  if (level != 0) {
    writePath(imageDirectory, "");
    upButtonActive = true;
  }
}

void getFolderDescription () {

  // Get the folder description
  if (currentFile.isDirectory()) {
    File subDir = currentFile;
    // If meta.properties exists in the image directory and contains
    // a description of the folder, display it with the variable descript.
    File propsFile = new File(subDir, "meta.properties");
    title1 = label;
    if (propsFile.exists()) {
      Map props = IO.readMapFile(propsFile);
      String newTitle = props.get("title");
      if (newTitle != null && newTitle.length() >0) title1 = newTitle;
      Map props = IO.readMapFile(propsFile);
      String newDescript = props.get("descript");
      if (newDescript != null && newDescript.length() > 0) {
        descript = newDescript;
      } else { 
	  if (!allowEmptyFolderDescription) descript = texts.getString("noDescript");
      }
    } else {
    if (!allowEmptyFolderDescription) descript = texts.getString("noDescript");
    }	
  }
}

void thumbStuff () {

	// We save the label variable to fName, because we may change the label value using various config files
	fName = label;
	// Now check to see if the user wants to use a different label for their photo by checking for an entry in titles.properties, or a text file named with the same name as the filename

	// If meta.properties exists in the image directory and contains
	// a description of the folder, display it with the variable descript.
	File propsFile = new File(imageDirectory, "titles.properties");
      if (propsFile.exists()) {
        Map props = IO.readMapFile(propsFile);
        String newTitle = props.get(fileName);
        if (newTitle != null && newTitle.length() >0) label = newTitle;
	} 

	// If a text file with the same name as an image exists, use the contents of this for the image comment and/or label instead
  	File cFile = new File(imageDirectory, fName+".txt");
  	if (cFile.exists()) {
  	  Map captions = IO.readMapFile(cFile);
  	  String text = (String)captions.get("comment");
  	  if (text != null && text.length()> 0) comment = text;
  	  String text = (String)captions.get("title");
  	  if (text != null && text.length()> 0) label = text;
  	}
 
	  // If we use comments or descriptions under the image we have to enclose this in a <div> to make it work. This will change the object from an  
	  // inline element to a block element, meaning that it won't flow as nicely. Therefore we only use the <div> if we have to. See documentation for details.
	if ( sameSizeFilterIndex ) {
	  thumbW = maxThumbWidth;
	  thumbH = maxThumbHeight;
	} else { 
	  thumbW = thumbWidth;
	  thumbH = thumbHeight;
	}
	if ( album.get("lastProcessedWasThumb") != null ) lastProcessedWasThumb = album.get("lastProcessedWasThumb");
	if ( imageNameUnderThumbnail || imageCommentUnderThumbnail || imageDateUnderThumbnail || fixedFormatIndexPage ) {
		thumbFormatStart = "<div class=\"float\" style=\"width: " + ( fixedFormatIndexPage ? "100%" : thumbW + "px") + "\u003b text-align: center\u003b padding: " + imagePadding + "px\u003b " + noFloatForFirstThumb + "\">"; 
		thumbFormatEnd = "</div>";
		thumbStyle = "style=\"margin: 0 auto\u003b display: block\"";
		if (fixedFormatIndexPage) {
		}
	} else if (!lastProcessedWasThumb && !fixedFormatIndexPage ) {
	thumbFormatStart = "<div style=\"clear: left\"></div>";
	thumbFormatEnd = "";
	} else {
	thumbFormatStart = "";
	thumbFormatEnd = "";
	}
	if (!fixedFormatIndexPage) {
	thumbStyle = "style=\"margin: -2px -2px -1px -3px\u003b padding: " + imagePadding + "px\"";
	}
	if (noFloatForFirstThumb != void && noFloatForFirstThumb.length() > 0) out.print("<div style=\"clear: left\u003b\"></div>");
}

void thumbStuff2 () {
  if (fixedFormatIndexPage) {
    // End drop an end of row tag if our thumbnail is at the end of row, or if it is the last thumbnail on a page
    if (indexNum == void) indexNum = 1;
    if ( lastProcessedWasThumb && (thumbCount % cols == 0 || ( ( 1+ ((indexNum-1)*rows*cols+indexImageCount) - (1+(indexNum-1)*rows*cols) ) == (imageNum-(1+(indexNum-1)*rows*cols)+1) ) ) ) {
    out.print("</div>"); 
    }
  }
}

%>