//
// These functions modify the gallery so that images appear in appropriately-
// sized popup windows when clicked
//

function showPopupImage(path, width, height, has_caption)
{ // 35 = 15 for view frame + 20 for extra padding
  var x_width = 35
  var x_height = 35
  var scrollbar = 'no'
  if (has_caption || width > 992 || height > 768) {
    // 992 is for a common screen width of 1024 and 768 is for a common height of 800
    scrollbar = 'yes'
    x_width += 15 // for vertical scrollbar
    if (has_caption) x_height += 75
  }
  var image = window.open(path, "_blank", "width=" + (width + x_width) + ",height=" + (height + x_height) + ",scrollbars=" + scrollbar + ",status=no,toolbar=no");
  image.focus();
}

function initializeGallery()
{
  $$('#gallery .snapshot_image').each(function(snapshot) {
    var id = snapshot.id
    var link = snapshot.getElementsBySelector('.image_bay a').first()
    var image = snapshot.getElementsBySelector('.image_bay img').first()
    var width = id.match(/_(\d+)x\d+$/)[1]
    var height = id.match(/x(\d+)$/)[1]
    var href = "javascript:showPopupImage('" + link.href + "', " + width + ", " + height
    if (snapshot.down('.caption')) href += ", true"
    link.href = href + ")"
  })
}

Event.observe(window, 'load', initializeGallery)
