// Random photo generator for Philip's Home Page
//     Copyright 2005 Philip J. Guo
//     http://alum.mit.edu/www/pgbovine/

// Note: Must include photo_path_data.js BEFORE this file
//       to initialize horizImages and vertImages arrays.

BASE_URL = "http://photos.pgrind.com/new-galleries/";

// To make sure that we open different windows and don't simply
// clobber the same window:
uniqueID = 0;

function OpenURLinNewWindow(url) {
  // If the screen is large enough, make it the optimal width/height for a gallery.
  // Otherwise, make it almost as large as the screen
  var maxScreenWidth = screen.width - 20;
  var maxScreenHeight = screen.height - 80;

  var optimalGalleryWidth = 1000;
  var optimalGalleryHeight = 920;

  var width = (optimalGalleryWidth < maxScreenWidth) ? optimalGalleryWidth : maxScreenWidth;
  var height = (optimalGalleryHeight < maxScreenHeight) ? optimalGalleryHeight : maxScreenHeight;

  var handle = window.open(url, 'galleryWin' + uniqueID, 
			   'scrollbars=yes,menubar=yes,resizable=yes,width=' +
			   width + 
			   ',height=' + height);
  if (window.focus) {
    handle.focus();
  }

  uniqueID++;
}


function InitRandomImages() {
  var horizImg1 = document.getElementById ? 
  document.getElementById("horizImg1") : document.all.horizImg1;

  var horizImg2 = document.getElementById ? 
  document.getElementById("horizImg2") : document.all.horizImg2;

  var horizImg3 = document.getElementById ? 
  document.getElementById("horizImg3") : document.all.horizImg3;

  var horizImg4 = document.getElementById ? 
  document.getElementById("horizImg4") : document.all.horizImg4;

  var horizImg5 = document.getElementById ? 
  document.getElementById("horizImg5") : document.all.horizImg5;

  var horizImg6 = document.getElementById ? 
  document.getElementById("horizImg6") : document.all.horizImg6;


  var vertImg1 = document.getElementById ? 
  document.getElementById("vertImg1") : document.all.vertImg1;

  var vertImg2 = document.getElementById ? 
  document.getElementById("vertImg2") : document.all.vertImg2;

  var vertImg3 = document.getElementById ? 
  document.getElementById("vertImg3") : document.all.vertImg3;

  InitImagesAndLinks(horizImages, horizImg1, horizImg2, horizImg3, horizImg4, horizImg5, horizImg6);
  InitImagesAndLinks(vertImages, vertImg1, vertImg2, vertImg3);
}


// TODO: Refactor and generalize - this code SUCKS
//   WOW THIS CODE REALLY STINKS!!!
function InitImagesAndLinks(images, img1, img2, img3, img4, img5, img6) {
  numImages = images.length;
  v1 = Math.round(Math.random() * numImages);

  // In rare cases, we hit the max, so don't go out of bounds:
  if (v1 == numImages) {v1 -= 1;}

  v2 = Math.round(Math.random() * numImages);
  if (v2 == numImages) {v2 -= 1;}

  // Don't have duplicates!
  while (v2 == v1) {
    v2 = Math.round(Math.random() * numImages);
    if (v2 == numImages) {v2 -= 1;}
  }

  v3 = Math.round(Math.random() * numImages);
  if (v3 == numImages) {v3 -= 1;}
  while ((v3 == v2) || (v3 == v1)) {
    v3 = Math.round(Math.random() * numImages);
    if (v3 == numImages) {v3 -= 1;}
  }

  v4 = Math.round(Math.random() * numImages);
  if (v4 == numImages) {v4 -= 1;}
  while ((v4 == v3) || (v4 == v2) || (v4 == v1)) {
    v4 = Math.round(Math.random() * numImages);
    if (v4 == numImages) {v4 -= 1;}
  }

  v5 = Math.round(Math.random() * numImages);
  if (v5 == numImages) {v5 -= 1;}
  while ((v5 == v4) || (v5 == v3) || (v5 == v2) || (v5 == v1)) {
    v5 = Math.round(Math.random() * numImages);
    if (v5 == numImages) {v5 -= 1;}
  }

  v6 = Math.round(Math.random() * numImages);
  if (v6 == numImages) {v6 -= 1;}
  while ((v6 == v5) || (v6 == v4) || (v6 == v3) || (v6 == v2) || (v6 == v1)) {
    v6 = Math.round(Math.random() * numImages);
    if (v6 == numImages) {v6 -= 1;}
  }

  // A sample entry in the images array looks like this:
  // "sum2004-alaska/thumbnails/img_1485.jpg"

  // If we take the entry before the first slash ("sum-2004-alaska")
  // and tack on an ".html" to the end, that should be the URL
  // for the gallery where this photo resides (if the naming conventions
  // are followed properly).
  link1part = images[v1].substring(0, images[v1].indexOf('/'));
  link2part = images[v2].substring(0, images[v2].indexOf('/'));
  link3part = images[v3].substring(0, images[v3].indexOf('/'));

  link4part = images[v4].substring(0, images[v4].indexOf('/'));
  link5part = images[v5].substring(0, images[v5].indexOf('/'));
  link6part = images[v6].substring(0, images[v6].indexOf('/'));


  // To get around stupid dynamic binding in Javascript, let's encode
  // the URL into the image name after a '?' (super hacko!!!):
  img1.src = BASE_URL + images[v1] + '?' + link1part;
  img2.src = BASE_URL + images[v2] + '?' + link2part;
  img3.src = BASE_URL + images[v3] + '?' + link3part;

  if (img4) {
    img4.src = BASE_URL + images[v4] + '?' + link4part;
  }
  if (img5) {
    img5.src = BASE_URL + images[v5] + '?' + link5part;
  }
  if (img6) {
    img6.src = BASE_URL + images[v6] + '?' + link6part;
  }


  // Ahh, I see, you need an anonymous function, but because
  // JavaScript has dynamic binding, we can't just simply use
  // link1part, etc. in these functions.  We must extract out the
  // filenames that we've encoded into img1.src, etc. by splitting
  // with '?' and taking the 2nd token.  Super hack, eh?
  img1.onclick = function() {OpenURLinNewWindow(BASE_URL + img1.src.split('?')[1] + ".html");}
  img2.onclick = function() {OpenURLinNewWindow(BASE_URL + img2.src.split('?')[1] + ".html");}
  img3.onclick = function() {OpenURLinNewWindow(BASE_URL + img3.src.split('?')[1] + ".html");}

  if (img4) {
    img4.onclick = function() {OpenURLinNewWindow(BASE_URL + img4.src.split('?')[1] + ".html");}
  }
  if (img5) {
    img5.onclick = function() {OpenURLinNewWindow(BASE_URL + img5.src.split('?')[1] + ".html");}
  }
  if (img6) {
    img6.onclick = function() {OpenURLinNewWindow(BASE_URL + img6.src.split('?')[1] + ".html");}
  }

}

