
// ====================
// ==== Parameters ====
// ====================
var showIndex = new Boolean(false); // Enable index display
var indexSeparator = ' '; // String between index items
var showCaption = new Boolean(false); // Enable caption display
var preCache = new Boolean(true); // Activates pre-caching of next photo
var pictureID = 'photo'; // SPAN or DIV ID for photo
var captionID = 'caption'; // SPAN or DIV ID for caption
var indexID = 'index'; // SPAN or DIV ID for index
var wrapOn = new Boolean(true); // Wrapping from last photo to first and vice-versa
var slideMode = new Boolean(true); // Enables slideshow mode
var slideDelay = 5000; // Default delay between slides in miliseconds
var clickMode = new Boolean(true); // Disable image clicking to advance
var imageALT = new Boolean(true); // Disable caption as image ALT tag text


// =============================
// ==== The heart of it all ====
// =============================


var glbCacheTimer;
var glbSlideTimer;

// Contains index of the current photo, initialized to the first photo (1)
var glbCurrentPhoto = 1;

// Array holding photo filenames
var photos = new Array ();

// Array holding photo captions
var captions = new Array ();

// Array holding link names
var linkNames = new Array ();

function showPhoto(index) {
  glbCurrentPhoto = index;
  var photoLocation = getObjectByID(pictureID);
  var imgString = '';

  if (clickMode == true) {imgString += "<a href='javascript:void(showNext());'>";}
  imgString += "<img border='0' id='mainImage' src='"+ photos[glbCurrentPhoto-1] +"'";
  if (imageALT == true) {imgString += ' alt="'+captions[glbCurrentPhoto-1].replace(/"/g,"'").replace(/<[^>]*>/g,"")+'"';}
  imgString += " width='301px' height='247'>";
  if (clickMode == true) {imgString += "</a>";}
  photoLocation.innerHTML = imgString;

  // Create caption if enabled.
  if (showCaption == true) {
    var photoCaption = getObjectByID(captionID);
    photoCaption.innerHTML = captions[glbCurrentPhoto-1];
  }

  // Build the index if enabled.
  if (showIndex == true) {buildIndex();}

  // Pre-cache if enabled.
  if ((preCache == true) && (glbCurrentPhoto < photos.length)) {
    // Start timer for cache loader routine to check if main image is loaded
    glbCacheTimer = setTimeout('cache(' + glbCurrentPhoto + ');', 500);
  }
	slideDelay = 10000 - s.getValue();
	//get the user configurable delay setting
//  slideDelay = getValue('slideShowDelay');
  // Slideshow mode, if enabled.
  if (slideMode == true) {
    clearTimeout (glbSlideTimer);
    glbSlideTimer = setTimeout('showNext();', (slideDelay));
  }
}

function showNext() {
  if (glbCurrentPhoto >= photos.length) {
    if (wrapOn == true) {
      glbCurrentPhoto = 1;
      showPhoto (glbCurrentPhoto);
    }
  } else {
    glbCurrentPhoto += 1;
    showPhoto (glbCurrentPhoto);
  }
}

function showPrevious() {
  if (glbCurrentPhoto <= 1) {
    if (wrapOn == true) {
      glbCurrentPhoto = photos.length;
      showPhoto (glbCurrentPhoto);
    }
  } else {
    glbCurrentPhoto += -1;
    showPhoto (glbCurrentPhoto);
  }
}

function showFirst() {
	glbCurrentPhoto = 1;
	showPhoto (glbCurrentPhoto);
}

function showLast() {
	glbCurrentPhoto = photos.length;
	showPhoto (glbCurrentPhoto);
}

function initPhoto() {
  // Display the photo
  if (slideMode == true) {
 	  writeToDiv('playPause','<a href="javascript:disableSlideMode();"><img src="/imagenes/pause.jpg" border="0"></a>');
  } else {
	  writeToDiv('playPause','<a href="javascript:enableSlideMode();"><img src="/imagenes/play.jpg" border="0"></a>');
  }
  showPhoto(glbCurrentPhoto);
}

function cache(photoID) {
  // Check to see if main image has loaded
  if (getObjectByID('mainImage').complete) {
    // Clear the timer
    clearTimeout(glbCacheTimer);
    // Load the next image.
    getObjectByID('cache').src= photos[photoID];
  } else {
    // Not loaded, so reset timer
    glbCacheTimer = setTimeout('cache(' + glbCurrentPhoto + ');', 500);
  }
}

function addPhoto(filename, caption, linkName) {
  // Add filenames and captions to their respective arrays.
  var len = photos.length;
  photos[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 < photos.length+1; i++) {
    // If not the first photo, add separator
    if (i>1) {indexString += indexSeparator}
    // Make current photo # bold and don't make it a link
    if (i == glbCurrentPhoto) {
      indexString += '<b>' + linkNames[i-1] + '</b>';
    } else { // Make all other numbers links
      indexString += '<a href="javascript:void(showPhoto(' + i + '));">' + linkNames[i-1] + '</a>';
    }
  }
  // Display the index
  getObjectByID(indexID).innerHTML = indexString;
}

function enableSlideMode () {
  // Turns slide mode on
  slideMode=Boolean(true);
 	writeToDiv('playPause','<a href="javascript:disableSlideMode();"><img src="/imagenes/pause.jpg" border="0"></a>');
  if (glbCurrentPhoto >= photos.length) {
    glbCurrentPhoto = 1;
  } else {
    glbCurrentPhoto += 1;
  }
  showPhoto(glbCurrentPhoto); 
}

function disableSlideMode() {
  slideMode = Boolean(false);
	writeToDiv('playPause','<a href="javascript:enableSlideMode();"><img src="/imagenes/play.jpg" border="0"></a>');
  clearTimeout (glbSlideTimer);
  showPhoto(glbCurrentPhoto); //necessary to reset URL parameters.
}

//added functionality
function writeToDiv(divId,text) {
  x = getObjectByID(divId);
	x.innerHTML = '';
	x.innerHTML = text;
}

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 getValue(id) {
  if (x = getObjectByID(id)) {
    return x.value;
	} else {
	  return false;
	}
}

