//-----------------------------------------------------------------------
// Module name   : lpopupImg.js
// Author        :
//    Script Source: CodeLifter.com
//    Copyright 2003
//    Do not remove this notice.
//    Author: etLux
//    Modified by: Paul Battersby
// Creation Date : July 28/06
// Description   :
//    Use this script to launch a popup window that automatically loads an
//    image and resizes itself to fit neatly around that image. The script
//    also places a title you set in the titlebar of the popup window. Any
//    number of images can be launched from a single instance of the script.
//
//    A zoom factor may be supplied to artificially increase the size of the
//    image and window
//
// Example:
//
//  <script language="JavaScript" type="text/javascript" src="lpopupimg.js"></script>
//  <a href="javascript:LPOPUPIMG_show('largeImg.jpg','Large',1)">
//    <img src="smallImg.jpg" border="0">
//  </a>
//
//------------------------------------------------------------------------*/

/*---------------------------- INCLUDE FILES ----------------------------*/


/*----------------------------- CONSTANTS -------------------------------*/
var LPOPUPIMG_DEFAULT_WIDTH  = 0;
var LPOPUPIMG_DEFAULT_HEIGHT = 1;
var LPOPUPIMG_POSITION_X     = 2;
var LPOPUPIMG_POSITION_Y     = 3;
var LPOPUPIMG_AUTOCLOSE      = 4;

/*----------------------------- VARIABLES -------------------------------*/
// Set the horizontal and vertical position for the popup

var lpopupimg_PositionX = 100;
var lpopupimg_PositionY = 100;

// Set these value approximately 20 pixels greater than the
// size of the largest image to be used (needed for Netscape)

var lpopupimg_defaultWidth  = 600;
var lpopupimg_defaultHeight = 600;

// Set autoclose true to have the window close automatically
// Set autoclose false to allow multiple popup windows

var lpopupimg_AutoClose = true;

//************************************************************************
// Name   : LPOPUPIMG_set
// Author : Paul Battersby
// Description :
//  This allows the caller alter the default position, default size
//  and whether the window automatically closes when a different image
//  is to be displayed or instead opens a whole new window allowing
//  multiple popups
//
// Pre :
//  (nothing)
//
// Params :
//  field - the field to be set. See the constants defined above
//
// Post :
//  specified field has been set to the given value
//
// Returns :
//  (nothing)
//*************************************************************************/
function LPOPUPIMG_set(field,value)
{
  switch (field) {
    case LPOPUPIMG_DEFAULT_WIDTH :
      lpopupimg_defaultWidth = value;
      break;

    case LPOPUPIMG_DEFAULT_HEIGHT:
      lpopupimg_defaultHeight = value;
      break;

    case LPOPUPIMG_POSITION_X:
      lpopupimg_PositionX = value;
      break;

    case LPOPUPIMG_POSITION_Y:
      lpopupimg_PositionY = value;
      break;

    case LPOPUPIMG_AUTOCLOSE:
      lpopupimg_AutoClose = value;
      break;
  } /* end switch */
} /* end of LPOPUPIMG_set */

//************************************************************************
// Name   : LPOPUPIMG_show
// Author : Paul Battersby
// Description :
//  This takes an image url, and custom fits a new popup window to the size
//  of the image.
//
// Pre :
//  (nothing)
//
// Params :
//  imageURL   - the image to be dispayed
//  imageTitle - the title to display in the title bar of the window
//  zoomFactor - value by which image dimensions should be multiplied
// Post :
//  a popup window has been displayed
//
// Returns :
//  (nothing)
//*************************************************************************/
function LPOPUPIMG_show(imageURL,imageTitle,zoomFactor)
{
  if (parseInt(navigator.appVersion.charAt(0))>=4) {
    var isNN=(navigator.appName=="Netscape")?1:0;
    var isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;
  }

  var optNN='scrollbars=no,width='+lpopupimg_defaultWidth+',height='+lpopupimg_defaultHeight+',left='+lpopupimg_PositionX+',top='+lpopupimg_PositionY;
  var optIE='scrollbars=no,width=150,height=100,left='+lpopupimg_PositionX+',top='+lpopupimg_PositionY;

  if (isNN) {
    imgWin=window.open('about:blank','',optNN);
  }

  if (isIE) {
    imgWin=window.open('about:blank','',optIE);
  }

  with (imgWin.document) {
    writeln('<html><head><title>Loading...</title><style>body{margin:0px}</style>');
    writeln('<sc'+'ript>');
    writeln('var isNN,isIE;');
    writeln('var imgWidth;');
    writeln('var imgHeight;');
    writeln('if (parseInt(navigator.appVersion.charAt(0))>=4){');
    writeln('isNN=(navigator.appName=="Netscape")?1:0;');
    writeln('isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;}');
    writeln('function reSizeToImage(){');
    writeln('  if (isIE){');
    writeln('    window.resizeTo(100,100);');
    writeln('    imgWidth = document.images[0].width');
    writeln('    imgHeight = document.images[0].height');
    writeln('    winWidth=imgWidth;'); /* 20 = leave a border at the left/right edge */
    writeln('    winHeight=imgHeight;'); /* 40 = compensate for title bar */
    writeln('    window.resizeTo(winWidth+20,winHeight+40);');
    writeln('  }');
    writeln('  if (isNN){');
    writeln('    winWidth=document.images["George"].width');
    writeln('    winHeight=document.images["George"].height');
    writeln('    window.innerWidth=winWidth;');
    writeln('    window.innerHeight=winHeight;');
    writeln('  }');
    writeln('}');

    writeln('function doTitle(){document.title="'+imageTitle+'";}');

    writeln('function doZoom(zoomFactor) {');
    writeln('  var newImgWidth;');
    writeln('  var newImgHeight;');
    writeln('  if (zoomFactor > 1) {');
    writeln('    if (isIE){');
    writeln('      document.images[0].width=imgWidth*zoomFactor;');
    writeln('      document.images[0].height=imgHeight*zoomFactor;');
            /* 20 = leave a border at the left/right edge */
            /* 40 = compensate for title bar */
    writeln('      window.resizeTo((winWidth*zoomFactor)+20,(winHeight*zoomFactor)+40);');
    writeln('');
    writeln('    }');
    writeln('    if (isNN){');
    writeln('      document.images["George"].width = winWidth * zoomFactor;');
    writeln('      document.images["George"].height = winHeight * zoomFactor;');
    writeln('      window.innerWidth*=zoomFactor;');
    writeln('      window.innerHeight*=zoomFactor;');
    writeln('    }');
    writeln('    ');
    writeln('  }');
    writeln('}');
    writeln('</sc'+'ript>');

    if (!lpopupimg_AutoClose) {
      writeln('</head><body bgcolor=#ffffff scroll="no" onload="reSizeToImage();doZoom('+zoomFactor+');doTitle();self.focus()">')
    } else {
      writeln('</head><body bgcolor=#ffffff scroll="no" onload="reSizeToImage();doZoom('+zoomFactor+');doTitle();self.focus()" onblur="self.close()">');
    }
    writeln('<div align="center"><img name="George" src='+imageURL+'></div></body></html>');
    close();
  } /* end with */

} /* end of LPOPUPIMG_show */

