var intervalID;
var interval2;
var maxLeft = 0;
var leftAmt = 0;
var rate = 4;
var amountLeft = 0;
var direction = 0;
var currentPic = 0;
var currentSec = 0;

function boxSetUp(){
    var counter = 1;
	while(document.getElementById("p"+counter) != null){
		counter++;
	}
	var theWidth = counter * 195;        
	document.getElementById("pane").style.width = theWidth+"px";    
    maxLeft = theWidth - 606;
	if(maxLeft < 0){
		maxLeft = 0;
	}
	maxLeft = maxLeft * -1 - 30;
	showPic(1);
	currentSec = 1;
	interval2 = setInterval("moveLeft()",4500);
}

function showPic(num){
	//check if pic can be seen. must be within -80 to 550
	var picPos = (num -1) * 195 + leftAmt;
	if(currentPic != num && picPos > -80 && picPos < 550){
		//remove focus current pic
		if(currentPic != 0){
			var pic = document.getElementById("p"+currentPic);
			pic.className = "pic";
			document.getElementById("t"+currentPic).style.display = "none";
		}
		//remove opacity
		var pic = document.getElementById("p"+num);
		pic.className = "picClear";
		//show arrow
		var arrow = document.getElementById("pointer");
		arrow.style.display = "block";
		arrowPos = picPos + 98;
		arrow.style.left = arrowPos + "px";
		//show text		
		document.getElementById("t"+num).style.display = "block";
		
		currentPic = num;
	}
	
}

function moveLeft(){
	if(amountLeft <= 0 && leftAmt > maxLeft ){
		var arrow = document.getElementById("pointer");
		arrow.style.display = "none";
		amountLeft = 195 * 2;
		if(leftAmt - amountLeft < maxLeft){
			amountLeft = leftAmt - maxLeft;	
		}
		direction = 0;
		intervalID = setInterval('accelerateMove()',50); 
		currentSec += 2;
		
	}	
	
}

function moveRight(){
	if(amountLeft <= 0 && leftAmt < 0){
		var arrow = document.getElementById("pointer");
		arrow.style.display = "none";
		amountLeft = 195 * 2;
		if(leftAmt + amountLeft > 0){
			amountLeft = leftAmt * -1;	
		}
		
		direction = 1;
		intervalID = setInterval('accelerateMove()',50);
		currentSec -= 2;
		if(interval2 != null){
			clearInterval(interval2);	
		}
	}
}

function accelerateMove(){
 	if(direction == 0){
		if(amountLeft < 5) {
			leftAmt -= amountLeft;
			amountLeft = 0;
						
		}
		else {
			toMove = Math.round(amountLeft/rate);
			amountLeft -= toMove;
			leftAmt -= toMove;
		}
	}
	else {
		if(amountLeft < 5) {
			leftAmt += amountLeft;
			amountLeft = 0;
						
		}
		else {
			toMove = Math.round(amountLeft/rate);
			amountLeft -= toMove;
			leftAmt += toMove;
		}
	}
	if(amountLeft == 0){
		clearInterval(intervalID);
		if(direction == 0){
			if(currentSec < 10){
				showPic(currentSec);	
			}	
		}
		else {
			if(currentSec >=1){
				showPic (currentSec);	
			}
		}
	}
 	document.getElementById("pane").style.left = leftAmt + "px";
}

