//NOTES: need to work on better method of determining IE vs. Netscape for variables like innerWidth and pageXOffset
//change variable name of x

var ie5 = (document.getElementById && document.all)? true: false;
var x = 0;
var scrollPageSpeed = 20;
var doorwayWidth = 182;
var paddingEnd = 244;//space between the last door and the end of the train
var scrollPageTimer = null;
var doorways = new Array(0,401,1007,1613,2219);

//scrolls the page horizontally
function scrollPage(destination,doorIndex) {
	stopScroll();
	if (ie5) innerWidth = document.body.offsetWidth;
	if (innerWidth) { //adjusts destination to be centered in the browswer window
		dest = eval(destination + doorwayWidth/2 - innerWidth/2);
	}
	minLeft = 0;
	maxRight = eval(doorways[doorways.length-1]+doorwayWidth+paddingEnd-innerWidth);
	if (dest<minLeft) dest=minLeft;
	if (dest>maxRight) dest=maxRight;
	if (x < dest) {
		scrollPageRight(dest,doorIndex);
	}
	else if (x > dest) {
		scrollPageLeft(dest,doorIndex);
	}
	else if (doorIndex) {openDoors(doorIndex);}
}
function scrollPageRight(dest,doorIndex) {
	if (x<dest-4) {
		distance = eval(dest - x);
		step = 0.15 * distance;
		if (step<4) step=4;
		x += step;
		window.scroll(x,0)
		scrollPageTimer = setTimeout('scrollPageRight('+dest+','+doorIndex+')',scrollPageSpeed);
		//if(x>dest-12 && doorIndex) openDoors(doorIndex);//start to open the doors early
	}
	else stopScroll(dest,doorIndex);
}
function scrollPageLeft(dest,doorIndex) {
	if (x>dest+4) {
		distance = eval(x - dest);
		step = 0.15 * distance;
		if (step<4) step=4;
		x -= step;
		window.scroll(x,0)
		scrollPageTimer = setTimeout('scrollPageLeft('+dest+','+doorIndex+')',scrollPageSpeed);
		//if(x<dest+12 && doorIndex) openDoors(doorIndex);//start to open the doors early
	}
	else stopScroll(dest,doorIndex);
}
function stopScroll(dest,doorIndex) {
	if(scrollPageTimer) clearTimeout(scrollPageTimer);
	scrollPageTimer = null;
	if(dest) window.scroll(dest,0);
	if(doorIndex) openDoors(doorIndex);
	checkScrollPage();
}
//gets the current scroll position of the page
function checkScrollPage() {
	if (ie5) window.pageXOffset = window.document.body.scrollLeft;
	if (window.pageXOffset) x = window.pageXOffset;
	else x = 0;
	return x;
}
function initScrollPage() {
	dest = 0;
	if (ie5) innerWidth = document.body.offsetWidth;
	if (innerWidth) {
		dest = eval(doorways[1] + doorwayWidth/2 - innerWidth/2);
	}
	//window.scroll(dest,0);
}