//////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2000 by Franky Wong <franky@kuentos.guam.net>
//
// SYNTAX
//
// <body>
// <script language="Javascript" src="http://www.88-cellular.com/88-cellular.js"></script>
// ...
// <a href="xxx.jpg" onClick="return show_picture(this)">...</a>
//
//////////////////////////////////////////////////////////////////////
// USER CONFIGURATION
//
// set this to true or false
//
// true - the program will always try to resize the
// picture window to the size of the image
//
// false - the program will resize the picture window
// to the size of the image, but will use the default
// size if the browser is not finished loading the image
//
var resizeWindowToImageSize = true;
//
// default image size and margin
//
var defaultWidth = 400;
var defaultHeight = 400;
var marginWidth = 15;
var marginHeight = 15;
//
// debug should be set to false
//
//var debug = true;
var debug = false;
//////////////////////////////////////////////////////////////////////

//var myImage, myWindow, myWidth, myHeight;

//
// open a image in the picture window
//
// SYNTAX:
//
// <a href="abc.jpg" onClick="return show_picture(this)">xxx</a>
//
var myImage, myWindow;


function show_picture(obj) {
  if (!obj || !obj.href) { return true; }

  var myWidth, myHeight;

  myImage = new Image()
  if (resizeWindowToImageSize) { myImage.onLoad = resize_window; }
  myImage.src = obj.href;

  if (myImage.width > 1 && myImage.height > 1) {
    myWidth = myImage.width + marginWidth;
    myHeight = myImage.height + marginHeight;
  } else {
    myWidth = defaultWidth;
    myHeight = defaultHeight;
  }

  if (debug) {
    alert(
      'myImage is ' + myImage + '\n' +
      'myImage.src is: ' + myImage.src + '\n' +
      'width is: ' + myImage.width +
      ' height is: ' + myImage.height + '\n' +
      ' window.open ' + myWidth + 'x' + myHeight + '\n' +
      (myImage.complete ? 'image loaded' : 'image not yet loaded')
    );
  }

  /*
  if (myWindow) {
    myWindow.innerWidth = myWidth;
    myWindow.innerHeight = myHeight;
    myWindow.location = myImage.src;
  } else {
  }
  */
  if (myWindow) myWindow.close();
  myWindow = window.open(myImage.src, 'pictureWindow',
    'resizable=yes,toolbar=no,width=' +
     myWidth + ',height=' + myHeight);

  // if (myWindow) myWindow.focus();
  return false;
}


//
// calculate size of the picture window and open one
//
function resize_window() {
  var myHeight, myHeight;

  if (!myImage || !myWindow) { return true; }
  if (myImage.width > 1 && myImage.height > 1) {
    myWidth = myImage.width + marginWidth;
    myHeight = myImage.height + marginHeight;
  } else {
    myWidth = defaultWidth;
    myHeight = defaultHeight;
  }
  if (debug) {
    alert(
      'myImage is ' + myImage + '\n' +
      'myImage.src is: ' + myImage.src + '\n' +
      'width is: ' + myImage.width +
      ' height is: ' + myImage.height + '\n' +
      ' window.open ' + myWidth + 'x' + myHeight + '\n' +
      (myImage.complete ? 'image loaded' : 'image not yet loaded')
    );
  }
  myWindow.innerWidth = myWidth;
  myWindow.innerHeight = myHeight;
}


function show_picture2(obj) {
  if (!obj || !obj.href) { return true; }

  myWindow = null;

  myImage = new Image()
  if (resizeWindowToImageSize) { myImage.onLoad = resize_window; }
  myImage.src = obj.href;

  if (debug) { alert('show_picture() done'); }
  return resize_window();
}


//
// resize the window to the size of the image if possible
//
function open_window() {
  if (myWindow) {
    if (debug) { alert('innerWidth and Height'); }
    myWindow.innerWidth = myWidth;
    myWindow.innerHeight = myHeight;
  } else {
    if (debug) alert('window.open ' + myWidth + 'x' + myHeight);
    myWindow = window.open(myImage.src, 'pictureWindow',
      'resizable=yes,toolbar=no,width=' +
      myWidth + ',height=' + myHeight);
  }
  if (debug) alert('window.focus');
  myWindow.focus();
}


//
// calculate size of the picture window and open one
//
function resize_window2() {
  if (!myImage) {
    if (debug) alert('problem');
    return true;
  }
  if (debug) {
    alert(
      'myImage is ' + myImage + '\n' +
      'myImage.src is: ' + myImage.src + '\n' +
      'width is: ' + myImage.width +
      ' height is: ' + myImage.height);
    alert(myImage.complete ? 'image loaded' : 'image not yet loaded');
  }
  if (myImage.complete && myImage.width > 0 && myImage.height > 0) {
    myWidth = myImage.width + marginWidth;
    myHeight = myImage.height + marginHeight;
  } else {
    myWidth = defaultWidth;
    myHeight = defaultHeight;
  }
  open_window();
  return false;
}
