var showIndex = new Boolean(false); // Enable index display
var indexSeparator = ' '; // String between index items
var showCaption = new Boolean(true); // Enable caption display
var preCache = new Boolean(true); // Activates pre-caching of next slide
var pictureID = 'slide'; // SPAN or DIV ID for slide
var captionID = 'caption'; // SPAN or DIV ID for caption
var captionClassName = "captiontext";
var indexID = 'index'; // SPAN or DIV ID for index
var wrapOn = new Boolean(false); // Wrapping from last slide 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(false); // Disable caption as image ALT tag text

var glbCacheTimer;
var glbSlideTimer;

// Contains index of the current slide, initialized to the first slide (1)
var glbCurrentSlide = 1;

// Array holding slide filenames
var slides = new Array ();

// Array holding slide captions
var captions = new Array ();

// Array holding link names
var linkNames = 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 showSlide(index)
{
  //alert("showSlide index="+index);
  // Get the slide object.
  var slideLocation = getObjectByID("mainImage");

  // Get the image source.
  var imgString = '';
  imgString = slides[index-1];

   //alert("showSlide imgString="+imgString);
  // Show the image.
  slideLocation.src = imgString;
  //alert("showSlide slideLocation.src="+slideLocation.src);

  // Set current slide.
  glbCurrentSlide = index;

  // Dislay the slide caption
  showSlideCaption();

  // Dislay the slide counter info
  slideCounter();

  // Pre-cache if enabled.
  preLoadNext();
}

function showNext() {
  if (glbCurrentSlide >= slides.length) {
    if (wrapOn == true) {
      glbCurrentSlide = 1;
      showSlide (glbCurrentSlide);
    }
  } else {
    glbCurrentSlide += 1;
    showSlide (glbCurrentSlide);
  }
}

function showPrevious() {
  if (glbCurrentSlide <= 1) {
    if (wrapOn == true) {
      glbCurrentSlide = slides.length;
      showSlide (glbCurrentSlide);
    }
  } else {
    glbCurrentSlide += -1;
    showSlide (glbCurrentSlide);
  }
}

function showFirst() {
	glbCurrentSlide = 1;
	showSlide (glbCurrentSlide);
}

function showLast() {
	glbCurrentSlide = slides.length;
	showSlide (glbCurrentSlide);
}

function showSlideCaption() {
  // Create caption if enabled.
  if (showCaption == true)
  {
    var slideCaption = getObjectByID(captionID);
	if (slideCaption)
	{
		var captionElement = "<div class=\"" + captionClassName + "\">" + captions[glbCurrentSlide-1] + "</div>";
		slideCaption.innerHTML = captionElement;
	}
  }
}

function slideCounter() {
  // Get the slideCounter object.
  var slideCounter = getObjectByID("slideCounter");
  if (slideCounter)
  {
	  slideCounter.innerHTML = "Slide " + glbCurrentSlide + " of " + slides.length;
  }
}

function initSlide() {
  // Display the slide
  var slideLocation = getObjectByID(pictureID);
  var imgString = '';

  if (clickMode == true) {imgString += "<a href='javascript:void(showNext());'>";}
  imgString += "<img border='0' id='mainImage' src='"+ slides[glbCurrentSlide-1] +"'";
  if (imageALT == true) {imgString += ' alt="'+captions[glbCurrentSlide-1].replace(/"/g,"'").replace(/<[^>]*>/g,"")+'"';}
  imgString += ">";
  if (clickMode == true) {imgString += "</a>";}
  slideLocation.innerHTML = imgString;

  // Dislay the slide caption
  showSlideCaption();

  // Dislay the slide counter info
  slideCounter();

  // Build the index if enabled.
  if (showIndex == true) {buildIndex();}

  // Pre-cache if enabled.
  preLoadNext();

  // Slideshow mode, if enabled.
  if (slideMode == true) {
    glbSlideTimer = setTimeout('autoNext();', (slideDelay * 1000));
  }
}

function preLoadNext() {
  // Pre-cache if enabled.
  if ((preCache == true) && (glbCurrentSlide < slides.length))
  {
    // Start timer for cache loader routine to check if main image is loaded
    glbCacheTimer = setTimeout('cache(' + glbCurrentSlide + ');', 500);
  }
}

function cache(slideID) {
  //alert("cache slideID="+slideID);
  // Check to see if main image has loaded
  if (getObjectByID('mainImage').complete) {
    // Clear the timer
    clearTimeout(glbCacheTimer);
    // Load the next image.
    //getObjectByID('cache').src= slides[slideID];
	var imageObj = new Image();
	imageObj.src= slides[slideID];
  } else {
    // Not loaded, so reset timer
    glbCacheTimer = setTimeout('cache(' + glbCurrentSlide + ');', 500);
  }
}

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

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

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

function autoNext ()
{
  // Invoked from the SlideMode Timer.
  showNext();

  glbSlideTimer=setTimeout('autoNext();',(slideDelay*1000));
}

function enableSlideMode (newDelay) {
  // Turns slide mode on
  slideMode=Boolean(true);
  if (newDelay > 0) {
    slideDelay = newDelay;
  }
  //showSlide(glbCurrentSlide); //necessary to reset URL parameters.
  //glbSlideTimer=setTimeout('showNext();',(slideDelay*1000));
}

function disableSlideMode() {
  slideMode = Boolean(false);
  clearTimeout (glbSlideTimer);
  //showSlide(glbCurrentSlide); //necessary to reset URL parameters.
}
