/****************************************************************************
  The Popuppic Script

  Designed for Netscape 4, IE 5, and Netscape 6.

  Programmer: Andrew Drawneek

  Requires: Is.js

  26-Feb-2001, First version.
  21-Apr-2005, Small fixes made to support browser version checks (screen),
    added click to close (for both left & right clicks).
  28-Apr-2005, Fixed bugs for IE: popupclick script now written into the
    popup window, instead of being loaded from a src= line (which crashed IE).
    Window width/height inner/outer differences are set to a default for IE
    on Windows (because IE does not support inner/outer width properties).
    This may have varying results on other platforms.
*****************************************************************************/

// -------------------------------------------------------------
// User switchable variables:

// loaderVisible, true or false
// Set to true to show a popup "loading" message while the image
// is being downloaded (may be preferable with large images).
// If this is false, no message is displayed (may be preferable
// with small images).
var loaderVisible = true;

// the width & height of the loader message, if displayed
var loaderWidth = 200;
var loaderHeight = 100;

var xpos = -1;
var ypos = -1;

var popupoptions = "resizable=yes,scrollbars=no";

// -------------------------------------------------------------

// default screen width and height

var screenWidth = 640;
var screenHeight = 480;

//if (is.nav4up || is.ie5up) {
if (screen) {
  screenWidth = screen.availWidth;
  screenHeight = screen.availHeight;
}
//}


// variables used internally by this script

var pic = null;
var picID = 0;
var picW = null;
var picname = "";
var picbgcolour = "";
var xdiff = 0;
var ydiff = 0;



function openPopup(url, wtitle, x, y, w, h, optionstr) {
// opens a popup window and returns the object.
// the popup is centred in capable browsers.

  if (x < 0)
    x = (screenWidth - w) / 2;
  if (y < 0)
    y = (screenHeight - h) / 2;

  if (optionstr)
    optionstr = ","+optionstr;

  var popup = window.open(url, wtitle,
      "screenX="+x
    +",screenY="+y
    +",left="+x
    +",top="+y
    +",width="+w
    +",height="+h

    +",innerWidth="+w
    +",innerHeight="+h

    +optionstr);

//  popup.moveTo(x, y);  // For browsers that don't know the open attributes
//  popup.resizeTo(w, h);


  return popup;
} // openPopup


function closePopup(popup) {
  if (popup != null && !popup.closed)
    popup.close();
} // closePopup


function cancelpic() {
  closePopup(picW);
  if (picID != 0) {
    clearInterval(picID);
    picID = 0;
  }
} // cancelpic


function showpic(url, name, bgcolour) {

  // clear any previous loader
  cancelpic();

  // start loading the new pic
  picname = name || url;
  picbgcolour = bgcolour || "white";


  pic = new Image();
  pic.src = url;

  // show the loader message
  if (loaderVisible) {
    picW = openPopup('', 'popuppic', xpos, ypos, loaderWidth, loaderHeight, popupoptions);
    picW.document.open();
    picW.document.writeln("<html><head><title>Please wait...</title></head>");
    picW.document.writeln("<body><center>");
    picW.document.writeln("<table width=100% height=100%><tr><td align=center valign=middle>");
    picW.document.writeln("<p style=\"font: bold 8pt sans-serif;\">Loading &quot;"+picname+"&quot;</p>");
    picW.document.writeln("<p style=\"font: normal 8pt sans-serif;\"><a href=\"javascript:window.close();\">Cancel</a></p>");
    picW.document.writeln("</td></tr></table>");
    picW.document.writeln("</center></body></html>");
    picW.document.close();

    // set inner/outer width/heigth differences
    // defaults are for IE on Windows
    xdiff = 12;
    ydiff = 30;
//    if (is.nav6up) {
    if (picW.outerWidth) {
      xdiff = picW.outerWidth - picW.innerWidth;
      ydiff = picW.outerHeight - picW.innerHeight;
    }

  }

  // start the loader
  picID = setInterval(loadpic, 500);
} // showpic


function loadpic() {

  var w;
  var h;


  if (loaderVisible && picW != null && picW.closed) {
    cancelpic();

    // user closed the loader window, so stop the loading in Netscape:
    if (is.nav4up && !is.nav6up) {
      window.stop();
      pic = null;
    }

  }
  else if (pic.complete) {
    clearInterval(picID);
    picID = 0;

    if (is.ie5up)
      closePopup(picW);

    var w = pic.width + xdiff;
    var h = pic.height + ydiff;

    if (!loaderVisible || is.ie5up)
      picW = openPopup('', 'popuppic', xpos, ypos, w, h, popupoptions);

    if (picW != null && !picW.closed) {

      if (loaderVisible && !is.ie5up) {
        // clear window contents
        picW.document.open();
        picW.document.writeln("<html><body></body></html>");
        picW.document.close();

        // re-centre popup window
        picW.moveTo(xpos < 0 ? (screenWidth - w) / 2 : xpos,
                    ypos < 0 ? (screenHeight - h) / 2 : ypos);
        picW.resizeTo(w, h);
      }

      // write out the popup's image contents
      picW.document.open();
      picW.document.writeln("<html><head>");
      picW.document.writeln("<title>"+picname+"</title>");

      picW.document.writeln("<script language=javascript>");
      picW.document.writeln("<!--");
      picW.document.writeln("function right(e) {");
      picW.document.writeln("  if (document.layers && e.which == 3) {");
      picW.document.writeln("    return false;");
      picW.document.writeln("  }");
      picW.document.writeln("  else if (document.all && event.button == 2) {");
      picW.document.writeln("    self.close();");
      picW.document.writeln("    return false;");
      picW.document.writeln("  }");
      picW.document.writeln("  return true;");
      picW.document.writeln("} // right");
      picW.document.writeln();
      picW.document.writeln("function left(e) {");
      picW.document.writeln("  self.close();");
      picW.document.writeln("  return true;");
      picW.document.writeln("} // left");
      picW.document.writeln();
      picW.document.writeln("document.onmousedown = right;");
      picW.document.writeln("document.onmouseup = left;");
      picW.document.writeln();
      picW.document.writeln("if (document.layers) {");
      picW.document.writeln("  window.captureEvents(Event.MOUSEDOWN);");
      picW.document.writeln("  window.captureEvents(Event.MOUSEUP);");
      picW.document.writeln("}");
      picW.document.writeln("// -->");
      picW.document.writeln("</script>");

      picW.document.writeln("<meta http-equiv=imagetoolbar content=no>");
      picW.document.writeln("</head><body bgcolor="+picbgcolour+">");
      picW.document.writeln("<div style=\"position:absolute; left:0px; top:0px;\">");
      picW.document.writeln("<img border=0 src=\""+pic.src+"\" width="+pic.width+" height="+pic.height+" alt=\""+picname+"\">");
      picW.document.writeln("</div></body></html>");
      picW.document.close();

    }
  }
} // loadpic
