//The following variables are set externally
//slideSeconds, scrollSpeed, nextPage, imageSource, pause
var lastx = 0, lasty = 0;
var scrollInterval;
image = new Image();
image.src = imageSource;

//Initialize scrolling if image is wide enough
//Set up timeout for next slide.
function initialize(){
	if(!pause && slideSeconds > 0 && scrollSpeed >= 0){
		if(image.width > 1000 && scrollSpeed > 0)
			setTimeout("startScroll(true)", slideSeconds * 1000);
		else if(image.height > 750 && scrollSpeed > 0)
			setTimeout("startScroll(false)", slideSeconds * 1000);
		else
			setTimeout("loadNextPage()", slideSeconds * 1000);
	}
}

//When called loads the next slide unless we are scrolling through a panorama
//In which case we set a flag for the scroll function to check when it is done before loading the panorama.
function loadNextPage(){
	window.location = nextPage;
}

function startScroll(right){
	lastx = lasty = 0
	if(right){
		scrollInterval = setInterval("scrollWindowRight()",30);
		window.scroll(1,60);
	}
	else{
		scrollInterval = setInterval("scrollWindowDown()",30);
		window.scroll(5,1);
	}
	if (document.layers && document.layers['notes'])
		document.layers['notes'].visibility = 'hide';
	else if (document.all && document.all['notes'])
		document.all['notes'].style.visibility = 'hidden';
	else if (document.getElementById && document.getElementById('notes'))
		document.getElementById('notes').style.visibility = 'hidden';
}

//Stops the scroll and sets pause link to play
function stopScroll(){
	clearInterval(scrollInterval);
	if (document.layers && document.layers['pause']){
		document.layers['pause'].src = 'play.gif';
		document.layers['pauselink'].href = playLink;
	}
	else if (document.all && document.all['pause']){
		document.all['pause'].src = 'play.gif';
		document.all['pauselink'].href = playLink;
	}
	else if (document.getElementById && document.getElementById('pause')){
		document.getElementById('pause').src = 'play.gif';
		document.getElementById('pauselink').href = playLink;
	}
}

//Scrolls the window to the right and loads the next slide when done if enough time has passed.
function scrollWindowRight(){
	var xpos,ypos;
	if (document.all && !document.getElementById){
		xpos=document.body.scrollLeft;
		ypos=document.body.scrollTop;
	}
	else{
		xpos=window.pageXOffset;
		ypos=window.pageYOffset;
	}
	if(xpos == lastx){
		clearInterval(scrollInterval);
		window.location = nextPage;
	}
	else if(xpos < lastx || xpos > lastx + scrollSpeed)
		stopScroll();
	else{
		window.scroll(xpos+scrollSpeed, ypos);
		lastx = xpos;
	}
}

//Scrolls the window to down and loads the next slide when done if enough time has passed.
function scrollWindowDown(){
	var xpos,ypos;
	if (document.all && !document.getElementById){
		xpos=document.body.scrollLeft;
		ypos=document.body.scrollTop;
	}
	else{
		xpos=window.pageXOffset;
		ypos=window.pageYOffset;
	}
	if(ypos == lasty){
		clearInterval(scrollInterval);
		window.location = nextPage;
	}
	else if(ypos < lasty || ypos > lasty + scrollSpeed)
		stopScroll();
	else{
		window.scroll(xpos, ypos+scrollSpeed);
		lasty = ypos;
	}
}

window.onload=initialize;
