// Create/destroy popup window containing a larger version of a thumbnail
// image.  Requires template "zoom_popup.html" in the javascript source
// directory.  References global variable WebRoot from setroot.js, so
// call the SetRoot function before calling this script.

// When calling InitZoomWin, include the full path name of the image
// relative to the root.  E.g.: "ferret/images/.../...jpg", where
// "ferret" is a subdirectory below the root.

var ZoomWin = null;             // the popup window
var ZoomWinName = "ZoomWin";    // name of popup window
var ZoomWinTitle = null;        // title for popup window

var ZoomImg = null;             // saved path of image
var ImgCount = 0;               // number of images (for generating names)

var ImgList = new Array();      // list of loaded images
var ListCount = 0;              // number of images in list
var IsLoaded = false;           // flag indicating if loading is needed

function InitZoomWin(ImgName, WindowTitle, WindowWidth, WindowHeight) {
  var i = 0;

  if(ZoomImg == ImgName && IsWinOpen()) {   // image already displayed!
    ZoomWin.focus();
  }
  else {
    IsLoaded = false;

    for(i=0; i<=ListCount; i++) {           // see if we loaded it before
      if (ImgList[i] == ImgName) {
        IsLoaded = true;
      }
    }

    if(!IsLoaded) {                         // if not, add it to list
      ImgList[ListCount++] = ImgName;
    }

    CloseZoomWin();                         // close previous window, if any

    ZoomImg = "../" + ImgName;              // save image path relative to script
    ZoomWinTitle = WindowTitle;             // save window title
    ZoomWinName = "ZoomWin" + ImgCount++;   // give window a unique name

    ZoomWin = OpenZoomWin(ZoomWinName, WindowWidth, WindowHeight);
  }
}

function OpenZoomWin(ZoomWinName, WindowWidth, WindowHeight) {
  var WinFeatures = "toolbar=0,scrollbars=0,resizable=0,width="
      + WindowWidth + ",height=" + WindowHeight;

  var ZoomWinTemplate = WebRoot + "jsource/zoom_popup.html";

  return window.open(ZoomWinTemplate, ZoomWinName, WinFeatures);
}

function IsWinOpen() {        // test to see if popup is open
  var IsOpen = false;

  if(ZoomWin != null) {
    if(ZoomWin.closed != true) {
    	IsOpen = true;
    }
  }

  return IsOpen;
}

function CloseZoomWin() {     // close popup window if it is open
  if(IsWinOpen()) {
    ZoomWin.close();
  }
}

function SetStatus(msg) {     // put message in status bar
  status = msg;
  return true;
}

