/*
** 7/28/05: Initial version
**          finished scrolling and looping backwards.
*/

var scrollspeed = 33             <!-- Lower is faster, but jumpy at <40 -->
var scrollstep=1

var contentwidth = 0;
var topimage = 0;
var position = 0;
var scrolltimer = 0;
var images = Array(new Image(), new Image());
var contentobj = getElement("content");
var image1     = getElement("image");
var image2     = getElement("nimage");

function getElement(name) {
    return(document.getElementById ? 
            document.getElementById(name) : document.all[name]);
}


function nextImage() {
    // Load the next image, and start pre-loading the one after it.
	if (scrollstep < 0) {	// If we're going backwards, skip back two images
		topimage = topimage -2;
		if (topimage < 0) topimage += imageInfo.length;
	}
		
	images[0].onload = getContentWidth;
	images[0].src = imageInfo[topimage].filename;
	images[1].src = imageInfo[(topimage+1) % imageInfo.length ].filename;
    images[1].onload = function() {
        image2.src = images[1].src;
        image2.width = images[1].width;
    }
    // These don't need to be called here, but they are, in case
    // getContentWidth doesn't do the right thing later
    image1.src = images[0].src;
    image1.width = images[0].width;
    image2.src = images[1].src;
    image2.width = images[1].width;
 
	updateImageInfo();
	topimage = (topimage + 1) % imageInfo.length;

	if (scrollstep < 0) {	// if we're going backwards, start at the end
		position = -images[0].width+1
	} else {
		position = 0;
	}
	contentobj.style.left = position + "px";

	if (window.opera) {          // opera won't call the onload function
		getContentWidth();
	}
}


// When our main image is through loading, 
// get the image size, and start scrolling it.
function getContentWidth() {
    if (images[0].complete == 0) {				// Not ready yet.  Come back
        setTimeout("getContentWidth()",1000)
    } else {
		image1.src = images[0].src				// again, just in case.
		image1.width = images[0].width		
        contentwidth=images[0].width;
		if (scrolltimer) {
			clearTimeout(scrolltimer);
		}
        scrolltimer = setTimeout("scrollit()", scrollspeed);
    }
}
function doscroll() { 	// This is used to turn the timer back on when we stop
	if (scrolltimer == 0) {
		scrollit();
	}
}

function scrollit() {
	if (position > 0) { // Do nothing because we'll fall off the left end
        nextImage();
	} else if (position-scrollstep < -contentwidth) { // hit the right end
        nextImage();
    } else {
		position -= scrollstep;
        contentobj.style.left = position + "px";
        scrolltimer = setTimeout("scrollit()", scrollspeed);
    }
}
// Control button callbacks
function left() { scrollstep = scrollstep >= 0 ? -1 : scrollstep-1; doscroll();}
function right() { scrollstep = scrollstep <= 0 ? 1 : scrollstep+1; doscroll();}
function stop() { scrollstep =0 ; clearTimeout(scrolltimer); scrolltimer=0; }

function updateImageInfo() {
	getElement("c1").innerHTML = imageInfo[topimage].desc;
	getElement("c2").innerHTML = imageInfo[topimage].id ;
	getElement("c3").innerHTML = imageInfo[topimage].date;
	getElement("c4").innerHTML = imageInfo[topimage].loc;
}

// These two functions show and hide the navbar by changing it's inline style
function shownav() { getElement("navbar").style.display = "block"; }
function hidenav() { getElement("navbar").style.display = "none"; }

function init() {
	nextImage();
	// setTimeout("fade('innerrow')", 3000);
}

window.onload=init;
