/**********************************************************
 * Suzy's Javascript Photo Album, version 8.0, March 17, 2004
 *
 * Changed this version:  javascript is now in two seperate external
 * files, and put "spaces" in category names.
 *
 * This javascript may not be used on other sites without prior permission.
 *
 * You may contact me at photokity@hotmail.com for assistance. 
 *    
 **********************************************************/

self.name = "main"; // If you are using frames, change "main" to the name of the frame that the photoalbum is in.

current = 0; // Sets the current picture being shown to the first one.

ActiveVar = 0; // Sets up the variable that counts the pictures.
var ActiveArray = new Array() // Sets up the active array.
for (loop=0; loop < MainVar; loop++) {
  ActiveArray[ActiveVar++] = new Fix( MainArray[loop].DatVal, 
                                      MainArray[loop].PicVal, 
                                      MainArray[loop].TitVal, 
                                      MainArray[loop].CatVal, 
                                      MainArray[loop].TxtVal,
                                      MainArray[loop].BigPicVal )
}

function DisplayCategories() { // Lists out the categories.
  TotalCategories = SuzyCategory.length;
  for (loop=0; loop < TotalCategories; loop++)  {
    document.write("<option value=" + SuzyCategory[loop] + ">" + ReplaceChars(SuzyCategory[loop]) + "</option>");
  }
}

function FindPic() { // The search for a photo feature.
  TotalFound = 0;
  SearchString = document.SuzyForm.SearchWord.value;
  SearchString = SearchString.toLowerCase();
  WriteResults = window.open("","resultwindow","height=400, width=380, toolbar=0, status=0, menubar=0, resizable=1, scrollbars=1");
  WriteResults.document.open();
  WriteResults.document.write('<html><head><title>Search results for ' + SearchString + '</title>');
  WriteResults.document.write('<style type=text/css>body{background:url(images/parchment.jpg)}');
  WriteResults.document.write('img{border:2px ridge #ffaaaa; height:50px; vertical-align:middle}</style>');
  WriteResults.document.write('</head><body>');
  WriteResults.document.write('You searched for:  <i>' + SearchString + '</i><p><b>Results:</b><br>');
  for (loop=0; loop < ActiveVar ; loop++) {
    Keyword = ActiveArray[loop].TxtVal;
    Keyword = Keyword.toLowerCase();
    URL = ActiveArray[loop].PicVal;
    title = ActiveArray[loop].TitVal;
    lc_title = title.toLowerCase();
    SearchResult = Keyword.indexOf(SearchString);
    SearchResult2 = lc_title.indexOf(SearchString);
    if (SearchResult != "-1" || SearchResult2 != "-1") {
      WriteResults.document.write('<p><a href="javascript:ShowSuzyPicRefocus(' + loop + ');" target="main">');
      WriteResults.document.write('<img src=' + ActiveArray[loop].PicVal + '></a>&nbsp;&nbsp;');
      WriteResults.document.write('<a href="javascript:ShowSuzyPicRefocus(' + loop + ');" target="main">');
      WriteResults.document.write(title + '</a>' );
      TotalFound++;
    }
  }
  WriteResults.document.write('<p><b>Returned ' + TotalFound + ' results.</b>');
  WriteResults.document.write('<p><a href="javascript:window.close();">close window</a></body></html>');
  WriteResults.document.close();
  if ( window.focus ) {
    WriteResults.focus()
  }

}

function LoadPiclist() { // Loads initial list of pictures on web page.
  for (loop=0; loop < MainVar; loop++)  {
    document.write("<option value=" + ActiveArray[loop].PicVal + ">" + ActiveArray[loop].TitVal + "</option>");
  }
}

function LoadNextPic()  { // Loads next picture for faster operation.
  NextImage = new Image();
  NextPic = current + 1;
  if (NextPic>=ActiveVar) NextPic = 0;
  NextImage.src = ActiveArray[NextPic].PicVal;
}

function NextSuzyPic() { // Flips to the next photo.
  TotalImages = document.SuzyForm.SuzyDropdown.options.length;
  current++;
  if (current>=ActiveVar) current = 0;
  ShowSuzyPic(current);
}

LoadThis = 0;
function PreLoader() { // If checked, preloads all images into cache.  Five second interval between pics.
  if (SuzyForm.PreloadPics.checked && ++LoadThis < MainVar) {
    ShowingImage = new Image();
    ShowingImage.src = MainArray[LoadThis].PicVal;
    window.status="Pre-Loading image... '" + MainArray[LoadThis].PicVal + "'";
    RunLoader();
  } else {
    window.status=" ";
  }
}

function PreviousSuzyPic() { // Flips to the previous photo.
  current--;
  if(current<0) current = ActiveVar - 1;
  ShowSuzyPic(current);
}

function RandomSuzyPic() { // Shows a random photo.
  randompic = Math.floor(Math.random()*ActiveVar);
  ShowSuzyPic(randompic);
}

function ReplaceChars(entry) { // Replaces the _'s in cat names to make it pretty.
  out = "_"; // replace this
  add = " "; // with this
  temp = "" + entry; // temporary holder
  while (temp.indexOf(out)>-1) {
    pos= temp.indexOf(out);
    temp = "" + (temp.substring(0, pos) + add + 
    temp.substring((pos + out.length), temp.length));
  }
return temp;
}

function RunLoader() { // Pre-loads all images every 5 seconds, if checkbox is selected.
  timerLoad = setTimeout("PreLoader()",5000)
}

function AdvanceSlideShow() { // Show the next photo, if a slide show is running.
  if (document.SuzyForm.SelectedMode[0].checked) {
    timerSlideShow = setTimeout("AdvanceSlideShow();",document.SuzyForm.Timer.value*1000)
    NextSuzyPic();
  }
}

function RunSlideShow() { // Starts running a slide show of the photos.
  AdvanceSlideShow()
}

function ShowSuzyCategory(picked) { // Shows the pictures in that category.

  // Note what the current picture is.
  var ThisPicVal = 0
  if ( ActiveArray.length > 0 ) {
    ThisPicVal = ActiveArray[current].PicVal
  }
  ActiveArray.length = 0;
  PicList = "<SELECT size=13 name='SuzyDropdown' onChange='ShowSuzyPic(SuzyForm.SuzyDropdown.options.selectedIndex);'>";
  ActiveVar = 0;
  current = 0;
  for (loop=0; loop < MainVar; loop++) {
    if (MainArray[loop].CatVal.toLowerCase() == picked.toLowerCase() || picked == "all") {
      if ( ThisPicVal == MainArray[loop].PicVal ) {
        current = ActiveVar;
      }
      ActiveArray[ActiveVar++] = new Fix( MainArray[loop].DatVal, 
                                          MainArray[loop].PicVal, 
                                          MainArray[loop].TitVal, 
                                          MainArray[loop].CatVal, 
                                          MainArray[loop].TxtVal,
                                          MainArray[loop].BigPicVal )
      PicList = PicList + "<option value=" + MainArray[loop].PicVal + ">" + MainArray[loop].TitVal + "</option>"; 
    } 
  }
  PicList = PicList + "</select>";
  document.getElementById("PicSpot").innerHTML = PicList;
  ShowSuzyPic( current );
}

function ShowSuzyPicRefocus(newpic) { // Returns focus to main window and shows the photo and text.

  window.focus();
  ShowSuzyPic(newpic);
}

HeightLimit = 225;
WidthLimit  = 400;
function ShowSuzyPic(newpic) { // Shows the photo and text on the page.
  current = newpic;
  ShowingImage = new Image();
  ShowingImage.src = ActiveArray[current].PicVal;
  SelectionBox = document.SuzyForm.SuzyDropdown;
  SelectionBox.options[current].selected = true;
  WidthScale = WidthLimit / ShowingImage.width;
  HeightScale = HeightLimit / ShowingImage.height;
  if ( WidthScale < HeightScale )
    ClickableImage = 
      '<a href=javascript:SuzyPicRemote("' 
      + ActiveArray[current].PicVal 
      + '");><img name="PicShowing" src=' 
      + ActiveArray[current].PicVal + ' width=' + WidthLimit + ' border=0></a>';
  else
    ClickableImage = 
      '<a href=javascript:SuzyPicRemote("' 
      + ActiveArray[current].PicVal 
      + '");><img name="PicShowing" src=' 
      + ActiveArray[current].PicVal + ' height=' + HeightLimit + ' border=0></a>';
  document.getElementById("SuzySpot").innerHTML = ClickableImage 

  document.getElementById("LargerSpot").innerHTML = 
      '<a href=javascript:SuzyPicRemote("' 
      + ActiveArray[current].PicVal 
      + '");>See the picture bigger, with captions</a>';

  document.getElementById("Journal").innerHTML = 
    '<p style="text-align:center; margin-top: 0px; margin-bottom: 12px"><b>' + ActiveArray[current].DatVal + '<br>' + ActiveArray[current].TitVal + '</b></p>' 
    + ActiveArray[current].TxtVal;

  if ( ActiveArray[current].BigPicVal != null )
    document.getElementById("FullSizeSpot").innerHTML = 
      '<a href=javascript:SuperSizePicWindow("' 
      + ActiveArray[current].BigPicVal
      + '");>See the picture really big, no captions</a>';
  else
    document.getElementById("FullSizeSpot").innerHTML = ""
    
  
  if (document.SuzyForm.WhereView[1].checked) {
    timerID = setTimeout("SuzyPicRemote(ActiveArray[current].PicVal)",1000)
  }
  LoadNextPic();
}


function SuzyPicRemote(picName) { // Pops up the photo in a remote window.
  ShowingImage = new Image();
  ShowingImage.src = picName;
//~  wid = ShowingImage.width + 50;
//~  hei = ShowingImage.height + 230;
//~  if (wid < 30 || hei < 30) {
//~    wid=650;
//~    hei=490;
//~  }
//~  if (screen.width - 50 < wid) {
//~    wid = screen.width - 50;
//~  }
//~  if (screen.height - 50 < hei) {
//~    hei = screen.height - 50;
//~  }
  OpenWindow = window.open("", "remoteWin", "resizable=1,scrollbars=1,toolbar=0,left=15,top=10");
  OpenWindow.document.open();
  TitleString = ActiveArray[current].DatVal + ' &mdash; ' + ActiveArray[current].TitVal
  OpenWindow.document.write('<html><head><title>Larger view of ' + ActiveArray[current].TitVal + '</title></head>');
  OpenWindow.document.write('<body style="background:url(images/parchment.jpg);">');
  OpenWindow.document.write('<center><h2>' + TitleString + '</h2>');
  OpenWindow.document.write('<img src=' + picName + '><br>' + ActiveArray[current].TxtVal + '<br>');
  OpenWindow.document.write('<a href="javascript:window.close();">close window</a>');
  OpenWindow.document.write('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
  OpenWindow.document.write('<a href="javascript:window.print()">print window</a>');
  OpenWindow.document.write('</center></body></html>');
  OpenWindow.document.close();
//~  OpenWindow.resizeTo( wid, hei )
  if ( window.focus ) {
    OpenWindow.focus()
  }
}


function SuperSizePicWindow( picName ) { // Pops up the big picture in a second remote window.

  OpenWindow = window.open(picName, "SuperSizeWindow", "resizable=1,scrollbars=1,toolbar=0,left=15,top=10");
  OpenWindow.focus()

}
