var pause = false;
var timer; 
var currentLocation = 1;
var nextLocation = 1; 
var picsLoaded = 0;
var images = new Array;

function changeElementText(id, newText) {
  element = document.getElementById(id);
  if (element)
	  element.innerHTML = newText;
}
function play() {
  go2NextPhoto();
}
function preloadComplete() {
}
function resetTimer() {
  clearTimeout(timer);
	timer = setTimeout('go2NextPhoto()', 5000);
}
function wait4CurrentPhoto() {
  if (!showCurrentPhoto()) {
		clearTimeout(timer);
		timer = setTimeout('wait4CurrentPhoto()', 500);
		return 0;
  } else {
		preloadNextPhoto();
		resetTimer();
  }
}
function go2NextPhoto() {
  currentLocation = nextLocation;
  if (!showCurrentPhoto()) {
		wait4CurrentPhoto();
		return 0;
  }
  preloadNextPhoto();
  resetTimer();
}
function preloadNextPhoto() {
  nextLocation = (parseInt(currentLocation) + 1);
  if (nextLocation > photoCount) {
		nextLocation = 1;
  }
  if (nextLocation == 0) {
    nextLocation = photoCount;
  }
  preloadPhoto(nextLocation);
}
function showCurrentPhoto() {
  if (!images[currentLocation] || !images[currentLocation].complete) {
		preloadPhoto(currentLocation);
		return 0;
  }
  if (browserCanBlend){
//		document.images.slide.style.filter="progid:DXImageTransform.Microsoft.Fade(duration=1)";
//		document.images.slide.filters[0].Apply();
		document.images.slide.style.filter="blendTrans(duration=1)";
		document.images.slide.style.filter="blendTrans(duration=crossFadeDuration)";
		document.images.slide.filters.blendTrans.Apply();
  }
  document.slide.src = images[currentLocation].src;
  setCaption(photoCaptions[currentLocation]);
  if (browserCanBlend) {
		document.images.slide.filters[0].Play();
  }

  return 1;
}
function preloadPhoto(index) {
  if (picsLoaded < photoCount) {
		if (!images[index]) {
	    images[index] = new Image;
	    images[index].onLoad = preloadComplete();
	    images[index].src = document.getElementById("photo_urls_" + index).href;
	    picsLoaded++;
		}
  } 
}
function setCaption(text) {
  changeElementText("caption", text);
}
function doPause()  {
	if(pause) {
		pause = false;
		document.getElementById("pauseBtn").value = "pause";
		play();
	} else {
		pause = true;
		document.getElementById("pauseBtn").value = "play";
		clearTimeout(timer);
	}
}
