/* These Javascript routines control the SIZES OF PHOTOS opened when
 * the user clicks on a thumbnail image: MEDIUM or BIG?
 * It is mostly functional in JavaScript 1.1 or later,
 * partially functional in JS 1.0. */
/* *** CODE BY FRANK PETTIT, COPYRIGHT 2001 FRANK K. PETTIT. *** */

// This indicates the size of photo opened when user clicks on 
// thumbnail image: 1 == medium photo, 2 == big photo.
// Changeable by user preference, via HTML radiobutton form,
// and is stored in/read from cookie called "picturemode".
var linked_pic_size = 1;

// This indicates the target window in which photos will be displayed
// when user clicks on thumbnail image: 1 == new window, 2 == this same window.
// Changeable by user preference, via HTML radiobutton form,
// and is stored in/read from cookie called "opentarget".
var opentarget = 1;

/* *** CODE BELOW IS DEPENDENT ON FILENAMING/ WEB SERVER ADDRESSING. *** */
// This is the path (within web server) under which cookies
// (that describe desired picture size) will be stored.
var cookiepathname = "/";

/* *** CODE BELOW IS DEPENDENT ON LOCAL FILENAMING SCHEME. *** */
function picture_url( img_link)
{ // This function returns the URL string for the medium images OR
  // big images displayed when the user clicks on a link that wraps a
  // thumbnail image. img_link is the Link object wrapping the thumbnail.
  // The href returned by this function depends on your local filenaming scheme.
  // By default img_link's href field should be url of MEDIUM image.
  // In MY filenaming scheme, the URL for a medium image ends with ".htm"
  // and the URL for a big image is the same but ends with ".bg.htm".

  if (linked_pic_size == 2 &&   /* big picture */
      img_link.href.lastIndexOf(".htm",img_link.href.length-4) != -1)
    return img_link.href.substring(0,img_link.href.length-4) + ".bg.htm";
  else
    return img_link.href;   /* medium picture */
}


/* ** CODE BELOW IS _NOT_ DEPENDENT ON FILENAMING/ WEB SERVER ADDRESSING. ** */

/* GLOBAL VARIABLES */
var photowin;   /* Persistent window displaying all photos. */

// This is value of desired picture size (on PREVIOUS click).
var prev_pic_size = linked_pic_size;

var all_loaded = false;

// These are max. dimensions of window for "medium" and "big" photos.
// Window size is ideal images size + 35-40 pixels each way.
var medium_xsize = 770, medium_ysize = 570;
var big_xsize = 1225, big_ysize = 928;

if (jsversn >= 1.1)  // Turn off display of Javascript errors.
  window.onerror = new Function("return true;");

/* CHECK USER'S SCREEN SIZE.  JS 1.2 only. */
if (jsversn >= 1.2 && window.screen != null &&
    window.screen.availHeight != null &&
    window.screen.availWidth != null)
  {
    if (window.screen.availWidth < big_xsize)
      {
	big_xsize = window.screen.availWidth;
	if (window.screen.availWidth < medium_xsize)
	  medium_xsize = window.screen.availWidth;
      }
    if (window.screen.availHeight < big_ysize)
      {
	big_ysize = window.screen.availHeight;
	if (window.screen.availHeight < medium_ysize)
	  medium_ysize = window.screen.availHeight;
      }
  }


/* READ COOKIE INDICATING DESIRED PICTURE SIZE. */
// See if cookie named "picturemode" has been set by the user.

function getcookieval( cookiename)
{ // Returns value of cookie as string.
  var allcookies = parent.document.cookie;
  var startval = allcookies.indexOf(cookiename+"=");
  if (startval == -1) return "";
  startval += cookiename.length + 1; // +1 is to skip "="
  var endval = allcookies.indexOf(";", startval);
  if (endval == -1) endval = allcookies.length;
  return allcookies.substring(startval,endval);
}

if (getcookieval("picturemode") == "big")
  linked_pic_size = prev_pic_size = 2;
else
  linked_pic_size = prev_pic_size = 1;

if (getcookieval("opentarget") == "inframe")
  opentarget = 2;
else
  opentarget = 1;

var pastyear = new Date(1986,1);
var yearstr = (jsversn >= 1.2) ? pastyear.toUTCString() : pastyear.toGMTString();

function setcookieval( cookiename, cookieval, pathname)
{ // Clears old cookie value and sets new one. All arguments are strings.
  // This is a KLUDGE to delete old cookie value:
  parent.document.cookie = cookiename+"="+cookieval + ";expires="+yearstr;
  parent.document.cookie = cookiename+"="+cookieval + ";path="+escape(pathname);
}

function picturemodeclick(picmodebutton)
{ // This changes the cookie that controls the user's desired picture size.  
  // picmodebutton is a radio button that got clicked.
  if (picmodebutton.value == "big")
    {
      setcookieval( "picturemode", "big", cookiepathname);
      if (window == parent)
	linked_pic_size = 2;
      else
	parent.picgall.linked_pic_size = 2;
    }
  else if (picmodebutton.value == "medium")
    {
      setcookieval( "picturemode", "medium", cookiepathname);
      if (window == parent)
	linked_pic_size = 1;
      else
	parent.picgall.linked_pic_size = 1;
    }
}

function opentargetclick(targbutton)
{ // This changes the cookie that controls the user's desired target window.
  if (targbutton.value == "inframe")
    {
      setcookieval( "opentarget", "inframe", cookiepathname);
      if (window == parent)
	opentarget = 2;
      else
	parent.picgall.opentarget = 2;
    }
  else if (targbutton.value == "photowin")
    {
      setcookieval( "opentarget", "photowin", cookiepathname);
      if (window == parent)
	opentarget = 1;
      else
	parent.picgall.opentarget = 1;
    }
}

function clikpik(img_link)
{ // Event handler for when a link (associated with picture) is clicked on.
  // Argument img_link is hyperlink object that would normally be followed.

  var photowin_xsize, photowin_ysize; // integers
  var new_href, size_str;  // strings.
  var window_props =
    ",resizable=yes,scrollbars=yes,status=no,menubar=no,toolbar=no,location=no,directories=no";

  new_href = picture_url( img_link);

  if (jsversn >= 1.1 && opentarget == 1)
    {
      if (photowin != null && photowin.closed == false)
	{ // Bring pre-existing window to top.
	  // Next line fixes focus() bug in Explorer 4.0 that piece of shit.
	  if (photowin.opener == null)
	    photowin.opener = self;
	  photowin.focus();
	}

      if (linked_pic_size == 2)
	{
	  photowin_xsize = big_xsize;
	  photowin_ysize = big_ysize;
	}
      else
	{
	  photowin_xsize = medium_xsize;
	  photowin_ysize = medium_ysize;
	}

      if (photowin == null || photowin.closed == true ||
	  (linked_pic_size != prev_pic_size && jsversn < 1.2))
	{ // this window has never been opened before,
	  // or the user changed his desired picture size in JS1.1.
	  size_str = "height="+photowin_ysize+",width="+photowin_xsize;
          photowin = window.open( new_href, "franksphoto",
                                  size_str+window_props);
	}
      else if (linked_pic_size != prev_pic_size && jsversn >= 1.2)
	{ // the user changed his desired picture size in JS1.2.
	  photowin.resizeTo( photowin_xsize, photowin_ysize);
	  photowin.location = new_href;
	}
      else // this window has been opened before. Do not resize.
	photowin.location = new_href;

      if (photowin.opener == null)
	photowin.opener = self;
      photowin.focus();  // MAKE SURE pre-opened window is on top.
    }
  else  // Javascript 1.0 cannot use focus(). Open in current window.
    window.parent.location = new_href;

  prev_pic_size = linked_pic_size;

  return false; // false prevents browser from following normal link.
}

function thumbnailsloaded()
{ // This function is invoked after the whole page full of thumbnails
  // has loaded.
  if (jsversn >= 1.2 && window.screen != null &&
      ((browserName == 'Microsof' && window.screen.colorDepth != null &&
	window.screen.colorDepth > 0 && window.screen.colorDepth < 16) ||
       (browserName == 'Netscape' && window.screen.pixelDepth != null &&
	window.screen.pixelDepth > 0 && window.screen.pixelDepth < 16)))
    // NOT ENOUGH SCREEN COLORS. PRINT ERROR DIALOG BOX.
    window.alert("PROBLEM! \nIt seems your monitor can only display"
		 + " 256 colors."
		 + " So, the full-color photos on this webpage are going to"
		 + " look lousy on your monitor!  Don\'t blame me!\n"
		 + "You should reset your display for 16-bit color"
		 + " [also known as 65,000 colors] or better.");

  all_loaded = true;
}

function thumbnailsunload()
{ // This function is invoked when the user exits the page full of
  // thumbnails.
  // Note: "unload" means exiting browser Window; doesn't exit on switching tabs.
  if (jsversn >= 1.1 && photowin != null && photowin.closed == false)
    photowin.close();
}
