//====================================================================
//                           enlarge.js
//     
// Author:      Bradley Odom
// Email:       dopevector@hotmail.com
// Date:        6/25/2003
// 
// A JavaScript/PHP implementation of a popup window system
// for displaying an enlarged image of a thumbnail in a popup window.
//
// Files:  enlarge.js   This JavaScript file used for preloading the
//                      enlarged images and creating the popup.
//         enlarge.php  The PHP page used for controlling the popup 
//                      layout.
//
// Usage:  enlarge_path must be set to the absolute path to
//         enlarge.php.
//         
//         preload_images should be set to run on page load i.e.
//         <body onload="preload_images();">.
//
//         enlarge should be used as the link around the image
//         thumbnails and requires that the enlarged image name
//         and path be passed to it i.e.
//         <a href="javascript:enlarge('/path/to/image/image.jpg');">
//
// Functions:
// - preload_images:  This function preloads the full size versions
//     of the thumbnail images.  The function makes the following 
//     assumptions:
//
//     1.  The thumbnails are in the format Xs.jpg or Xs.gif
//         where X is any number of valid filename characters.
//     2.  The full sized images are in the format Xl.jpg or Xl.gif
//         where X is any number of valid filename characters.
//     3.  The filetype of the thumbnail is the same as the full
//         sized image.
//     4.  The thumbnail and the full sized image are in the same
//         directory.
//
//     Arguments:
//     - None
//
//     Returns:
//     - Void
//
// - enlarge:  This function creates the popup window.
//     
//     Arguments:
//     - image_name (required):  The name including full path of the 
//         full sized image.
//     - title (optional):  The title of the popup window.  Defaults
//         to "Enlarged Image" if no argument is passed.
//
//     Returns:
//     - Void
//====================================================================

//======= Begin user editable variables.
var enlarge_path = "/php/popup/";

// Set following parameters to "yes" to turn on by default and "no" to turn off.
var location_default = "no";  // Display address bar
var status_default = "no";  // Display status bar
var menubar_default = "no";  // Display menu bar (File, Edit, View, etc.)
var scrollbars_default = "no";  // Display a scroll bar when content requires it
var resizable_default = "no";  // Allow the window to be resized
var toolbar_default = "no";  // Display Back, Forward, etc.
var directories_default = "no";  // Display bar with bookmarks and other 
                                 // misc buttons

//======= End user editable variables.

var enlarged_images = new Array();

function preload_images() {
  // Loop through the images in the page.  If they match the format 
  // mentioned above then create a pointer to a new Image and set its
  // source to the corresponding full sized image string.
  for (i = 0; i < document.images.length; i++) {
    if (document.images[i].src.indexOf("s.jpg") > 0) {
      image_src = document.images[i].src.substring(0,document.images[i].src.indexOf("s.jpg")) + "l.jpg";
      image_index = return_image_name(image_src);      

      enlarged_images[image_index] = new Image();
      enlarged_images[image_index].src = image_src;
    } else if (document.images[i].src.indexOf("s.gif") > 0) {
      image_src = document.images[i].src.substring(0,document.images[i].src.indexOf("s.gif")) + "l.gif";

      enlarged_images[image_src] = new Image();
      enlarged_images[image_src].src = image_src;
    } 
  }
} 

function return_image_name(image_name) {
  var index = 0;

  while ((index = image_name.indexOf("/")) > -1) {
    image_name = image_name.substring(index + 1, image_name.length);
  }

  return image_name;
}

function enlarge(image_name) {
  // Set parameters to default values for this call.
  var location_ = location_default;
  var status_ = status_default;
  var menubar_ = menubar_default;
  var scrollbars_ = scrollbars_default;
  var resizable_ = resizable_default;
  var toolbar_ = toolbar_default;
  var directories_ = directories_default;

  // If a second argument is passed, set title to its value else
  // default title to "Enlarged Image".
  var title = arguments.length > 1 ? arguments[1] : "Enlarged Image";

  if (arguments.length > 2) {
    var split_array = new Array();
    for (var i = 2; i < arguments.length; i++) {
      if (arguments[i].indexOf("=") > 0) {
        split_array = arguments[i].split("=");
        
        switch (split_array[0]) {
          case "location":
            location_ = split_array[1];
            break;
          case "status":
            status_ = split_array[1];
            break;
          case "menubar":
            menubar_= split_array[1];
            break;
          case "scrollbars":
            scrollbars_ = split_array[1];
            break;
          case "resizable":
            resizable_ = split_array[1];
            break;
          case "toolbar":
            toolbar_ = split_array[1];
            break;
          case "directories":
            directories_ = split_array[1];
            break;
          default:
            break;
        }
      }
    }
  }

  // Calculate the new window's dimensions accounting for the width 
  // and height of the "Close Window" text at the bottom of the page. 
  var win_width = (enlarged_images[image_name].width > 115) ? enlarged_images[image_name].width : 115;
  var win_height = enlarged_images[image_name].height + 30;
  
  // Calculate the top and left placement to center the window on the
  // screen.
  var win_top = (screen.availHeight - win_height - 10)/2;
  var win_left = (screen.availWidth - win_width - 30)/2;

  // Assemble the URL to the php script.
  var url = enlarge_path + "enlarge.php?image_name=" + enlarged_images[image_name].src + "&title=" + title;

  // Assemble the parameter string.
  var parameters = "location=" + location_ + ",status=" + status_ + 
                   ",menubar=" + menubar_ + ",scrollbars=" + scrollbars_ + 
                   ",resizable=" + resizable_ + ",toolbar=" + toolbar_ + 
                   ",directories=" + directories_ +
                   ",width=" + win_width + ",height=" + win_height + ",top=" +
                   win_top + ",left=" + win_left;

  // Open a new popup window with all window decorations off.
  var enlarge_window = window.open(url, "enlarge_window", parameters);

  // Ensure the popup is brought into focus.
  enlarge_window.focus();
}

