window.onload = initAll;

var curImg = 0;
var climbPics = new Array(
    "climbPics/Wedge-1.jpg","climbPics/Wedge-2.jpg",
    "climbPics/Wedge-3.jpg", "climbPics/Wedge-4.jpg",
    "climbPics/Wedge-5.jpg", "climbPics/Lake-Arrowhead-1.jpg",
    "climbPics/buildering.jpg", "climbPics/Ralph-Stover-1.jpg");
var climbCaptions = new Array(
    "Lake Wedge with Wedge Mountain in background.",
    "On the glacier of Wedge Mountain, BC",
    "Sunrise on Wedge Mountain climb.",
    "Near the summit of the Wedge Mountain climb.",
    "A view of the backcountry from Wedge Mountain.",
    "A secret swimming hole near Lake Arrowhead, CA.",
    "Mark bouldering in Princeton, NJ.",
    "Shirley and Mark climbing in Ralph Stover, PA.");

function initAll() {
    // set-up the slide-show buttons
    if (document.getElementById) {
        document.getElementById("previousPhotoButton").innerHTML = "Previous";
        document.getElementById("nextPhotoButton").innerHTML = "Next";
        document.getElementById("randomPhotoButton").innerHTML = "Random";
        document.getElementById("showAnimationButton").innerHTML = "Start Slideshow";
	document.getElementById("previousPhotoButton").onclick = previousPic;
	document.getElementById("nextPhotoButton").onclick = nextPic;
	document.getElementById("randomPhotoButton").onclick = randomPic;
	document.getElementById("showAnimationButton").onclick = startAutoSlideShow;
    }
    else {
	alert("Sorry, your browser doesn't support this script.");
    }
    randomPic();  // load a random picture
}

function previousPic() {
    stopAutoSlideShow();
    curImg--;
    if (curImg < 0) {
	curImg = climbPics.length-1;
    }
    document.getElementById("climbPics").src = climbPics[curImg];
    document.getElementById("climbCaption").innerHTML =
	climbCaptions[curImg] + "&nbsp &nbsp (photo "
	+ (curImg + 1) + " of " + climbPics.length + ")";
}

function nextPic() {
    stopAutoSlideShow();
    advancePic();
}

function advancePic() {
    curImg++;
    if (curImg == climbPics.length) {
	curImg = 0;
    }
    document.getElementById("climbPics").src = climbPics[curImg];
    document.getElementById("climbCaption").innerHTML = 
	climbCaptions[curImg] + "&nbsp &nbsp (photo "
	+ (curImg + 1) + " of "	+ climbPics.length + ")";
}

function randomPic() {
    stopAutoSlideShow();
    // This function chooses (uniformly) a random image other than the current
    //   image.
    var numPics = climbPics.length;
    var randArray = new Array(numPics);
    var i=0;
    for (i=0; i< numPics; i++) randArray[i]=i;
    randArray[numPics-1]=curImg;
    randArray[curImg]=numPics-1;
    curImg = randArray[Math.floor((Math.random()*(numPics-1)))];
    document.getElementById("climbPics").src = climbPics[curImg];
    document.getElementById("climbCaption").innerHTML =
	climbCaptions[curImg] + "&nbsp &nbsp (photo "
	+ (curImg + 1) + " of " + numPics + ")";
}

var globalCntr = 0;
var countUpTimer = null;

function startAutoSlideShow() {
    x = 2000;
    document.getElementById("showAnimationButton").onclick = stopAutoSlideShow;
    document.getElementById("showAnimationButton").innerHTML = "Stop Slideshow";
    advancePic();
    countUpTimer = setInterval('advancePic()',x);
    setTimeout(stopAutoSlideShow,x*(climbPics.length-1)-x/2);
}

function stopAutoSlideShow() {
    document.getElementById("showAnimationButton").onclick = startAutoSlideShow;
    document.getElementById("showAnimationButton").innerHTML = "Start Slideshow";
    clearInterval(countUpTimer);
    countUpTimer = null;
}

