/*
======= Image Viewer 2.30 ========
==== Created by Josh Freedman ====
====  Web 1 Marketing, Inc.   ====
====   Copyright (C) 2005	====
====   All rights reserved.   ====
====  www.web1marketing.com   ====
==================================

Use of these scripts is free so long as you include proper
attribution (the block above) and a link from your site to
www.web1marketing.com.

Use these scripts at your own risk. The author assumes no
responsibility for support or for consequences of their use.

Comments and suggestions welcome: www.joshfreedman.com.

IT IS HIGHLY RECOMMENDED THAT NO CHANGES BE MADE TO THIS FILE AND
THAT ALL PARAMETER VALUES BE CUSTOMIZED IN THE PHOTO_LIST.JS FILES.

==== Version Notes ====
Version 2.30 Added optional index parameter
Version 2.22 Fixed clickMode and imageAlt behaviors
Version 2.21 Moved precaching image to bottom of page
Version 2.20 Added showFirst, showLast, click image to advance, caption as image ALT text
Version 2.13 Added slideshow features
Version 2.12 Parameterized ID's, wrapping optional
Version 2.11 Added caching of next image
Version 2.10 Added index functionality
Version 2.00 Functional browsing of images forward and back
*/

// ====================
// ==== Parameters ====
// ====================
var showIndex = new Boolean(true); // Enable index display
var indexSeparator = ''; // String between index items
var navBegin = '<tr>';
var navEnd = '</tr>';
var showTitle = new Boolean(true); // Enable title display
var showCaption = new Boolean(true); // Enable caption display
var showRef = new Boolean(true); // Enable reference display
var preCache = new Boolean(true); // Activates pre-caching of next image
var pictureID = 'image'; // SPAN or DIV ID for image
var titleID = 'title'; // SPAN or DIV ID for caption
var captionID = 'caption'; // SPAN or DIV ID for caption
var indexID = 'index'; // SPAN or DIV ID for index
var refID = 'ref'; // SPAN or DIV ID for reference
var wrapOn = new Boolean(true); // Wrapping from last image to first and vice-versa
var slideMode = new Boolean(false); // Enables slideshow mode
var slideDelay = 10; // Default delay between slides inseconds
var clickMode = new Boolean(true); // Disable image clicking to advance
var imageALT = new Boolean(true); // Disable caption as image ALT tag text

// ==========================
// ==== Third Party Code ====
// ==========================

/*
Webmonkey GET Parsing Module
Language: JavaScript 1.0

Source: Webmonkey Code Library
(http://www.hotwired.com/webmonkey/javascript/code_library/)

Author: Patrick Corcoran
Author Email: patrick@taylor.org
*/

function createRequestObject() {
 FORM_DATA = new Object();
 // The Object ("Array") where our data will be stored.
 separator = ',';
 // The token used to separate data from multi-select inputs
 query = '' + this.location;
 query = query.substring((query.indexOf('?')) + 1);
 // Keep everything after the question mark '?'.
 if (query.length < 1) { return false; }  // Perhaps we got some bad data?
 keypairs = new Object();
 numKP = 1;
 while (query.indexOf('&') > -1) {
  keypairs[numKP] = query.substring(0,query.indexOf('&'));
  query = query.substring((query.indexOf('&')) + 1);
  numKP++;
 }
 keypairs[numKP] = query;
 for (i in keypairs) {
  keyName = keypairs[i].substring(0,keypairs[i].indexOf('='));
  // Left of '=' is name.
  keyValue = keypairs[i].substring((keypairs[i].indexOf('=')) + 1);
  // Right of '=' is value.
  while (keyValue.indexOf('+') > -1) {
   keyValue = keyValue.substring(0,keyValue.indexOf('+')) + ' ' + keyValue.substring(keyValue.indexOf('+') + 1);
   // Replace each '+' in data string with a space.
  }
  keyValue = unescape(keyValue);
  // Unescape non-alphanumerics
  if (FORM_DATA[keyName]) {
   FORM_DATA[keyName] = FORM_DATA[keyName] + separator + keyValue;
  } else {
   FORM_DATA[keyName] = keyValue;
  }
 }
 return FORM_DATA;
}

FORM_DATA = createRequestObject();

// =============================
// ==== The heart of it all ====
// =============================


var glbCacheTimer;
var glbSlideTimer;

// Contains index of the current image, initialized to the first image (1)
var glbCurrentImage = 1;

// Array holding image filenames
var images = new Array ();

// Array holding image extensions
var exts = new Array ();

// Array holding references
var refs = new Array ();

// Array holding image title
var titles = new Array ();

// Array holding image captions
var captions = new Array ();

// Array holding link names
var linkNames = new Array ();

// Array holding index images
var navNames = new Array ();

function getObjectByID(id) {
  // Cross-browser function to return the object with the specific id

  if (document.all) { // IE
    return document.all[id];
  } else { // Netscape
    return document.getElementById(id);
  }
}


function showImage(index) {
  // Shows the image with identified index
  var theURL = "" + this.location;

  // Strip parameters, if any present, from end of URL.
  if (theURL.indexOf("?")>0) {
    theURL = theURL.substring(0,theURL.indexOf("?"));
  }

  // Append the new image index as a parameter.
  theURL += "?image=" + index;

  // Append the slideshow mode as a parameter.
  if (slideMode == true) {
    theURL += "&slideMode=true";
    theURL += "&slideDelay=" + slideDelay;
  }

  // Go to the constructed URL which has the new
  // image's index as a parameter.
  this.location = theURL;
}

function showNext() {
  if (glbCurrentImage >= images.length) {
    if (wrapOn == true) {
	 glbCurrentImage = 1;
	 showImage (glbCurrentImage);
    }
  } else {
    glbCurrentImage += 1;
    showImage (glbCurrentImage);
  }
}

function showPrevious() {
  if (glbCurrentImage <= 1) {
    if (wrapOn == true) {
	 glbCurrentImage = images.length;
	 showImage (glbCurrentImage);
    }
  } else {
    glbCurrentImage += -1;
    showImage (glbCurrentImage);
  }
}

function showFirst() {
	glbCurrentImage = 1;
	showImage (glbCurrentImage);
}

function showLast() {
	glbCurrentImage = images.length;
	showImage (glbCurrentImage);
}

function initImage() {
  // Display the image
  var imageLocation = getObjectByID(pictureID);
  var imgString = '';

  if (clickMode == true) {imgString += "<a href='javascript:void(showNext());'>";}
  imgString += "<img border='0' id='mainImage' src='img/"+ images[glbCurrentImage-1] + exts[glbCurrentImage-1] + "'";
  if (imageALT == true) {imgString += ' alt="'+captions[glbCurrentImage-1].replace(/"/g,"'").replace(/<[^>]*>/g,"")+'"';}
  imgString += ">";
  if (clickMode == true) {imgString += "</a>";}
  imageLocation.innerHTML = imgString;

  // Create caption if enabled.
  if (showCaption == true) {
    var imageCaption = getObjectByID(captionID);
    imageCaption.innerHTML = captions[glbCurrentImage-1];
  }

  // Create title if enabled.
  if (showTitle == true) {
    var imageTitle = getObjectByID(titleID);
    imageTitle.innerHTML = titles[glbCurrentImage-1];
  }

  // Create reference if enabled.
  if (showRef == true) {
    var imageRef = getObjectByID(refID);
    imageRef.innerHTML = refs[glbCurrentImage-1];
  }

  // Build the index if enabled.
  if (showIndex == true) {buildIndex();}

  // Pre-cache if enabled.
  if ((preCache == true) && (glbCurrentImage < images.length)) {
    // Start timer for cache loader routine to check if main image is loaded
    glbCacheTimer = setTimeout('cache(' + glbCurrentImage + ');', 500);
  }

  // Slideshow mode, if enabled.
  if (slideMode == true) {
    glbSlideTimer = setTimeout('showNext();', (slideDelay * 1000));
  }
}

function cache(imageID) {
  // Check to see if main image has loaded
  if (getObjectByID('mainImage').complete) {
    // Clear the timer
    clearTimeout(glbCacheTimer);
    // Load the next image.
    getObjectByID('cache').src= images[imageID];
  } else {
    // Not loaded, so reset timer
    glbCacheTimer = setTimeout('cache(' + glbCurrentImage + ');', 500);
  }
}

// t=type
	// gco-geo,cty;
	// gec-geo,ear,cty;
	// gao-geo,ear;
	// gpo-geo,prv;
	// gep-geo,ear,prv;
	// gne-geo,ear,cnt;
// g= geo entry
// c= cty count
// cpA-cpF= cty image number
// p= province
// pin= province image number
// n= country
// nin= country image number
// e= ear entry
// clA-clF= city map entry

function oce(t,g,c,cpA,cpB,cpC,cpD,cpE,cpF,p,pin,n,nin,e,clA,clB,clC,clD,clE,clF)

// http://localhost/chinesehistory/contents/06dat/geo.html#xianggang
// http://localhost/chinesehistory/contents/04ear/c01.html
// http://localhost/chineseart/contents/atls/c02p05.htm
{
var hr = '<hr align="center" width="100%" color="#000000" noshade style="color: #000000" size="1">';
var pb = '<p class="a2">SEE Also:<ul class="a2">';
var gl = '/chinesehistory/contents/06dat/geo.html#';
var gt = 'Exploring Chinese History :: Geographical Database Entry';
var el = '/chinesehistory/contents/04ear/c';
var et = 'Exploring Chinese History :: East Asian Region :: ';
var ce = '.htm?image=';
var pt = 'Atlas :: Provincial Maps :: ';
var ct = 'Atlas :: City Maps :: ';
var nt = 'Atlas :: Country Maps :: ';
var pl = '/chineseart/contents/atls/c01.htm?image=';
var cl = '/chineseart/contents/atls/c02.htm?image=';
var nl = '/chineseart/contents/atls/c03.htm?image=';
var lb = '<li><a class="contents" target="_blank" href="';
var eu = '</a></li>';
var ep = '</ul></p>'
var sp = ' :: ';

	if(t=="gco"){
		if(c=="1"){
		return hr + pb + lb + gl + (g) + '">' + gt + eu + lb + cl + (cpA) + '">' + ct + (clA) + eu + ep;}
		if(c=="2"){
	     return hr + pb + lb + gl + (g) + '">' + gt + eu + lb + cl + (cpA) + '">' + ct + (clA) + eu
		+ lb + cl + (cpB) + '">' + ct + (clB) + eu + ep;}
		if(c=="3"){
	     return hr + pb + lb + gl + (g) + '">' + gt + eu + lb + cl + (cpA) + '">' + ct + (clA) + eu
		+ lb + cl + (cpB) + '">' + ct + (clB) + eu
		+ lb + cl + (cpC) + '">' + ct + (clC) + eu + ep;}
		if(c=="4"){
	     return hr + pb + lb + gl + (g) + '">' + gt + eu + lb + cl + (cpA) + '">' + ct + (clA) + eu
		+ lb + cl + (cpB) + '">' + ct + (clB) + eu
		+ lb + cl + (cpC) + '">' + ct + (clC) + eu
		+ lb + cl + (cpD) + '">' + ct + (clD) + eu + ep;}
		if(c=="5"){
		return hr + pb + lb + gl + (g) + '">' + gt + eu + lb + cl + (cpA) + '">' + ct + (clA) + eu
		+ lb + cl + (cpB) + '">' + ct + (clB) + eu
		+ lb + cl + (cpC) + '">' + ct + (clC) + eu
		+ lb + cl + (cpD) + '">' + ct + (clD) + eu
		+ lb + cl + (cpE) + '">' + ct + (clE) + eu + ep;}
		if(c=="6"){
		return hr + pb + lb + gl + (g) + '">' + gt + eu + lb + cl + (cpA) + '">' + ct + (clA) + eu
		+ lb + cl + (cpB) + '">' + ct + (clB) + eu
		+ lb + cl + (cpC) + '">' + ct + (clC) + eu
		+ lb + cl + (cpD) + '">' + ct + (clD) + eu
		+ lb + cl + (cpE) + '">' + ct + (clE) + eu
		+ lb + cl + (cpF) + '">' + ct + (clF) + eu + ep;}
	;}
	if(t=="gen"){
	return pb + lb + gl + (g) + '">' + gt + eu
	+ lb + el + (e) + '.html">' + et + (n) + eu
	+ lb + nl + (nin) + '">' + nt + (n) + eu + ep;}
	if(t=="pen"){
	return pb + lb + gl + (g) + '">' + gt + eu
	+ lb + el + (e) + '.html">' + et + (n) + eu
	+ lb + nl + (nin) + '">' + nt + (n) + eu + ep;}
	if(t=="gec"){
	return pb + lb + gl + (g) + '">' + gt + eu
	+ lb + el + (e) + '.html">' + et + (n) + eu
	+ lb + cl + (cpA) + '">' + ct + (clA) + eu + ep;}
	if(t=="gao"){
	return pb + lb + gl + (g) + '">' + gt + eu
	+ lb + el + (e) + '.html">' + et + (n) + eu + ep;}
	if(t=="ecc"){
	return pb + lb + gl + (g) + '">' + gt + eu
	+ lb + el + (e) + '.html">' + et + (n) + eu
	+ lb + nl + (nin) + '">' + nt + (n) + eu
	+ lb + cl + (cpA) + '">' + ct + (clA) + eu + ep;}
	if(t=="geo"){
	return pb + lb + gl + (g) + '">' + gt + eu + ep;}
	if(t=="epn"){
	return pb + lb + gl + (g) + '">' + gt + eu
	+ lb + el + (e) + '.html">' + et + (n) + eu
	+ lb + pl + (pin) + '">' + pt + (p) + eu
	+ lb + nl + (nin) + '">' + nt + (n) + eu + ep;}
	if(t=="epc"){
	return pb + lb + gl + (g) + '">' + gt + eu
	+ lb + el + (e) + '.html">' + et + (n) + eu
	+ lb + pl + (pin) + '">' + pt + (p) + eu
	+ lb + nl + (cpA) + '">' + ct + (clA) + eu + ep;}
	if(t=="epcT"){
	return pb + lb + gl + (g) + '">' + gt + eu
	+ lb + el + (cpE) + '.html">' + et + (clE) + eu
	+ lb + el + (cpF) + '.html">' + et + (clF) + eu
	+ lb + cl + (cpA) + '">' + ct + (clA) + eu + ep;}
	if(t=="gep"){
	return pb + lb + gl + (g) + '">' + gt + eu
	+ lb + el + (e) + '.html">' + et + (n) + eu
	+ lb + pl + (pin) + '">' + pt + (p) + eu + ep;}
	if(t=="gpo"){
	return pb + lb + gl + (g) + '">' + gt + eu
	+ lb + pl + (pin) + '">' + pt + (p) + eu + ep;}
}

function colref(tb,l,d,ta,c) //tb= text before link; ta=text after link; l=link location; d=text to display
{
	return '<hr align="center" width="100%" color="#000000" noshade style="color: #000000" size="1">'
	+ '<p class="a2">Image graciously on loan from: <br>' + (tb) + '<a class="contents" target="_blank" '
	+ 'href="' + (l) + '">' + (d) + '</a>' + (ta) + '<br><br>Please see <a class="contents" href="#' + (c) + '">important copyright information</a> regarding this image.</p>';
}

function phtref(tb,l,d,ta) //tb= text before link; ta=text after link; l=link location; d=text to display
{
	return '<hr align="center" width="100%" color="#000000" noshade style="color: #000000" size="1">'
	+ '<p class="a2">Phtograph by: <br>' + (tb) + '<a class="contents" target="_blank" '
	+ 'href="' + (l) + '">' + (d) + '</a>' + (ta) + '</p>';
}

function frmref(tb,l,d,ta) //tb= text before link; ta=text after link; l=link location; d=text to display
{
	return '<hr align="center" width="100%" color="#000000" noshade style="color: #000000" size="1">'
	+ '<p class="a2">From: <br>' + (tb) + '<a class="contents" target="_blank" '
	+ 'href="' + (l) + '">' + (d) + '</a>' + (ta) + '</p>';
}

function echbiodat(t,s) 
{
    return '<hr align="center" width="100%" color="#000000" noshade style="color: #000000" size="1">'
    + '<p class="a2">SEE also:<ul class="a2">'
    + '<li><a class="contents" target="_blank" '
    + 'href="/chinesehistory/contents/06dat/bio.' + (t) + '.html#' + (s) + '">'
    + 'Exploring Chinese History :: Biographical Database Entry : ' + (s) + '</a></li>'
    + '</ul></p>';
}

function addImage(filename, ftype, linkName, title, caption, ref) {
  // Add filenames and captions to their respective arrays; ftype= j= .jpg, g= .gif, p= .png
  var len = images.length;
  images[len] = filename;
  exts[len] = ftype;
  titles[len] = title;
  captions[len] = caption;
  refs[len] = ref;
  if (typeof linkName == "undefined") {
	linkNames[len] = len + 1;
  } else {
	linkNames[len] = linkName;
  }
  if (typeof filename == "undefined") {
	navNames[len] = len + 1;
  } else {
	navNames[len] = filename;
  }
  if (typeof ftype == "undefined") {
	exts[len] = len + 1;
  } else {
	exts[len] = ftype;
  }
  if (typeof title == "undefined") {
	titles[len] = len + 1;
  } else {
	titles[len] = title;
  }
  if (typeof caption == "undefined") {
	captions[len] = len + 1;
  } else {
	captions[len] = caption;
  }
  if (typeof ref == "undefined") {
	refs[len] = len + 1;
  } else {
	refs[len] = ref;
  }
  }
  
function buildIndex() {
  // Creates a clickable list of image numbers.
  var indexString = '';
  var i;

  for (i = 1; i < images.length+1; i++) {
    // If not the first image, add separator
    if (i>1 && i!=12 || i!=23 || i!=34 || i!=45) {indexString += indexSeparator}
    if (i==12 || i==23 || i==34 || i==45) {indexString += '</tr><tr>'}
    // If the first image, add a tr tag.
    if (i==1) {indexString += navBegin}
    // Make current image # bold and don't make it a link
    if (i == glbCurrentImage) {
	 indexString += '<td class="alb_navc"><img border="0" src="img/nv/' + navNames[i-1] + 'nv.jpg' + '"><br><span class="galnav">' + linkNames[i-1] + '</span></td>';
    } else { // Make all other numbers links
	 indexString += '<td class="alb_navc"><a class="galnav" href="javascript:void(showImage(' + i + '));"><img border="0" src="img/nv/' + navNames[i-1] + 'nv.jpg' + '"><br>' + linkNames[i-1] + '</a></td>';
    }
  }
  // Display the index
  getObjectByID(indexID).innerHTML = indexString;
}

function enableSlideMode (newDelay) {
  // Turns slide mode on
  slideMode=Boolean(true);
  if (newDelay > 0) {
    slideDelay = newDelay;
  }
  showImage(glbCurrentImage); //necessary to reset URL parameters.
//  glbSlideTimer=setTimeout('showNext();',(slideDelay*1000));
}

function disableSlideMode() {
  slideMode = Boolean(false);
  clearTimeout (glbSlideTimer);
  showImage(glbCurrentImage); //necessary to reset URL parameters.
}

// Get image index from URL, if there is one, otherwise start at 1
if (FORM_DATA["image"]>0) {
  glbCurrentImage = Number(FORM_DATA["image"]);
} else {
  glbCurrentImage = 1;
}

// Get slide mode from URL, if there is one
if (FORM_DATA["slideMode"] == "true") {
  slideMode = Boolean(true);
  slideDelay = FORM_DATA["slideDelay"];
}
