var currentImage = "global";
currentImage = 0;
var slideTimer = "global";
slideTimer = parseInt(slideTimer);
var slideshowIsPlaying = "global";
slideshowIsPlaying = 0;
	


function updateSlide() {
	if (currentImage < 0) {
		currentImage = imageArray.length-1;
	}
	document.getElementById("tourImage").src = imageArray[currentImage].path;
	document.getElementById("tourCutlineText").innerHTML = imageArray[currentImage].cutline;
}


function nextImage() {
	if (currentImage >= imageArray.length-1) {
		currentImage = 0;
	} else {
		currentImage++;
	}
	stopSlideshow();
	swapImage();
}



function previousImage() {
	if (currentImage <= 0) {
		currentImage = imageArray.length-1;
	} else {
		if (slideTimer) {
			currentImage = currentImage-2;
			stopSlideshow();
		} else {
			currentImage--;
		}
	}
	swapImage();
}



function playstopSlideshow() {
	if (slideshowIsPlaying == 1) {
		slideshowIsPlaying = 0;
		stopSlideshow();
	} else {
		slideshowIsPlaying = 1;
		playSlideshow();
	}		
}



function playSlideshow() {
	stopSlideshow();
	if (currentImage < imageArray.length) {
		updateSlide();
		currentImage++;
		wait(3000);
	} else {
		currentImage = 0;
		wait(0);
	}
}



function stopSlideshow() {
	if (slideTimer) {
		clearTimeout(slideTimer);
	    document.getElementById("slideshowStart").className = "slideshowNotPlaying";
	    slideshowIsPlaying = 0;
		slideTimer = 0;
	}
}


function swapImage(newImage) {
    newImage++;
	if (newImage && newImage >= 0) {
        newImage--;
		currentImage = newImage;
	}
	stopSlideshow();
	updateSlide();
}


function wait(lengthOfTime) {
	slideTimer = setTimeout("playSlideshow()",lengthOfTime);
	document.getElementById("slideshowStart").className = "slideshowIsPlaying";
	slideshowIsPlaying = 1;
}