/*
===============================================================
---------------------- Slide Show 0.1 -------------------------
===============================================================
*/

//-------------------------------------- PARSER BY PATRICK CORCORAN
function createRequestObject() {
 FORM_DATA = new Object();
 separator = ',';
 query = '' + this.location;
 query = query.substring((query.indexOf('?')) + 1);
 // Keep everything after the question mark '?'.
 if (query.length < 1) { return false; }  // 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();
//-------------------------------------- END PARSER

function getObjectByID(id) {
  // Cross-browser function to return the object with the specific id
  if (document.all) { // IE
    return document.all[id];
  } else { // Mozilla
    return document.getElementById(id);
  }
}

// ----------- GLOBALS -----------
var gCurrentPhoto=1; // Contains index of the current photo, set at 1
var gCacheTimer;
var gSlideTimer;


// ---------- VARIABLES ----------
var pDisplayIndex = new Boolean(true); // index display
var pSeparator = ' '; // index item separator
var pShowCaption = new Boolean(true); // caption display
var preCache = new Boolean(true); // pre-caching of next photo
var pWrap = new Boolean(true); // last photo to first
var pSlideShow = new Boolean(false); // slideshow mode
var pDelay = 10; // delay in secs
var pClickImage = new Boolean(true); // disable image clicking
var pALTtag = new Boolean(true); // disable caption as image ALT tag text

// ------- SPAN / DIV IDs --------
var pictureID = 'photo';
var captionID = 'caption';
var indexID = 'index';

// ----------- ARRAYS ------------
var photos = new Array ();
var captions = new Array ();
var links = new Array ();

// --------- FUNCTIONS -----------
function showImage(index) {
  // Shows the photo 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 photo index as a parameter.
  theURL += "?photo=" + index;

  // Append the slideshow mode as a parameter.
  if (pSlideShow == true) {
    theURL += "&pSlideShow=true";
    theURL += "&pDelay=" + pDelay;
  }
  // Go to appended URL
  this.location = theURL;
}

function goNext() {
  if (gCurrentPhoto >= photos.length) {
    if (pWrap == true) {
      gCurrentPhoto = 1;
      showImage (gCurrentPhoto);
    }
  } else {
    gCurrentPhoto += 1;
    showImage (gCurrentPhoto);
  }
}

function goPrev() {
  if (gCurrentPhoto <= 1) {
    if (pWrap == true) {
      gCurrentPhoto = photos.length;
      showImage (gCurrentPhoto);
    }
  } else {
    gCurrentPhoto += -1;
    showImage (gCurrentPhoto);
  }
}

function goFirst() {
	gCurrentPhoto = 1;
	showImage (gCurrentPhoto);
}

function goLast() {
	gCurrentPhoto = photos.length;
	showImage (gCurrentPhoto);
}

function initImage() {
  // Display the photo
  var photoLocation = getObjectByID(pictureID);
  var imgString = '';

  if (pClickImage == true) {imgString += "<a href='javascript:void(goNext());'>";}
  imgString += "<img border='1' id='mainImage' src='"+ photos[gCurrentPhoto-1] +"'";
  if (pALTtag == true) {imgString += ' alt="'+captions[gCurrentPhoto-1].replace(/"/g,"'").replace(/<[^>]*>/g,"")+'"';}
  imgString += ">";
  if (pClickImage == true) {imgString += "</a>";}
  photoLocation.innerHTML = imgString;

  // Create caption TRUE
  if (pShowCaption == true) {
    var photoCaption = getObjectByID(captionID);
    photoCaption.innerHTML = captions[gCurrentPhoto-1];
  }

  // Slideshow mode TRUE
  if (pSlideShow == true) {
    gSlideTimer = setTimeout('goNext();', (pDelay * 1000));
  }

  // Build index TRUE
  if (pDisplayIndex == true) {buildIndex();}

  // Pre-cache TRUE
  if ((preCache == true) && (gCurrentPhoto < photos.length)) {
    // Start timer for imageCache loader routine to check if main image is loaded
    gCacheTimer = setTimeout('imageCache(' + gCurrentPhoto + ');', 500);
  }
}

function imageCache(photoID) {
  // Check to see if main image has loaded
  if (getObjectByID('mainImage').complete) {
    // Clear the timer
    clearTimeout(gCacheTimer);
    // Load the next image.
    getObjectByID('imageCache').src= photos[photoID];
  } else {
    // Not loaded, so reset timer
    gCacheTimer = setTimeout('imageCache(' + gCurrentPhoto + ');', 500);
  }
}

function appendImage(filename, caption, link) {
  // Add filenames and captions to their respective arrays.
  var len = photos.length;
  photos[len] = filename;
  captions[len] = caption;
  if (typeof link == "undefined") {
	links[len] = len + 1;
  } else {
	links[len] = link;
  }
}

function buildIndex() {
  // Creates a clickable list of image numbers.
  var indexString='';
  var i;

  for (i=1; i<photos.length+1; i++) {
    // If not the first photo, add separator
    if (i>1) {indexString += pSeparator}
    // Make current photo # bold and don't make it a link
    if (i == gCurrentPhoto) {
      indexString += '<b>' + links[i-1] + '</b>';
    } else { // Make all other numbers links
      indexString += '<a href="javascript:void(showImage(' +i+ '));">' +links[i-1]+ '</a>';
    }
  }
  getObjectByID(indexID).innerHTML = indexString;  // Display the index
}

function enablepSlideShow (newDelay) {
  // Turns slide mode on
  pSlideShow=Boolean(true);
  if (newDelay > 0) {
    pDelay = newDelay;
  }
  showImage(gCurrentPhoto); //necessary to reset URL parameters.
//  gSlideTimer=setTimeout('goNext();',(pDelay*1000));
}

function disablepSlideShow() {
  pSlideShow = Boolean(false);
  clearTimeout (gSlideTimer);
  showImage(gCurrentPhoto); //necessary to reset URL parameters.
}

// Get photo index from URL, if there is one, otherwise start at 1
if (FORM_DATA["photo"]>0) {
  gCurrentPhoto = Number(FORM_DATA["photo"]);
} else {
  gCurrentPhoto = 1;
}

// Get slide mode from URL, if there is one
if (FORM_DATA["pSlideShow"] == "true") {
  pSlideShow = Boolean(true);
  pDelay = FORM_DATA["pDelay"];
}
