
var timer = new Array(null, null, null);
var theScroll = new Array();

var currentIndex = 0;
var timerInterval = 15;
var timerDelay = 800;

var arraypos = 0;
var no_end = 0;

var autoDelay1 = 60*300;
var autoDelay2 = 60*300;

var autoTimer = null;

autoTimer = setTimeout('AutoScrollIt();', autoDelay1);


function AutoScrollIt(){

	ScrollArrow('right','scroller','item_1', 0);

	autoTimer = setTimeout('AutoScrollIt();', autoDelay2);

}

function ScrollSection(nextIndex, scrollArea, offset, bool)
{

	// Store the last section, and update the current section

	if (currentIndex == nextIndex) {
		return;
	}
	lastIndex = currentIndex;
	currentIndex = nextIndex;


	for(var i = 1; i < 4; i++){
		// Get the element we want to scroll, get the position of the element to scroll to

		theScroll[i-1] = document.getElementById(scrollArea+'-'+i);
		position = findElementPos(document.getElementById('item_'+(currentIndex+1)+'-'+i));

		// Get the position of the offset div -- the div at the far left.
		// This is the amount we compensate for when scrolling

		if (offset != "") {
			offsetPos = findElementPos(document.getElementById(offset+'-'+i));
			position[1] = position[1] - offsetPos[1];
		}

		if(bool && no_end){
			timer[i-1] = setTimeout('scrollStart('+position[1]+', '+(i-1)+', '+lastIndex+', '+currentIndex+');', 0);
		}
		else{
			timer[i-1] = setTimeout('scrollStart('+position[1]+', '+(i-1)+', '+lastIndex+', '+currentIndex+');', (i-1)*timerDelay);
		}

		// return false;

	}


}

// Scroll the page using the arrows

function ScrollArrow(direction, scrollArea, offset, clearAuto) {

	if(clearAuto) clearTimeout(autoTimer);

	toolbarNames = new Array('item_1','item_2','item_3');

	var bool = 0;

	// Now iterate through our array of tab names, find matches, and determine where to go.

	for (var i = 0; i < toolbarNames.length; i++) {
		if (i == currentIndex) {
			if (direction == "left") {
				if (i - 1 < 0) {
					nextIndex = toolbarNames.length - 1;
					bool =1;
				} else {
					nextIndex = i - 1;
				}
			} else {
				if ((i + 1) > (toolbarNames.length - 1)) {
					nextIndex = 0;
					bool =1;
				} else {
					nextIndex = i + 1;
				}
			}
		}
	}

	// Go to the section name!
		ScrollSection(nextIndex, scrollArea, offset, bool);

	if(clearAuto) autoTimer = setTimeout('AutoScrollIt();', autoDelay1);

}



//
// Animated Scroll Functions
// Scrolls are synchronous -- only one at a time.
//

var scrollanim = new Array();

scrollanim[0] = {time:0, begin:0, change:0.0, duration:0.0, element:null, timer:null};
scrollanim[1] = {time:0, begin:0, change:0.0, duration:0.0, element:null, timer:null};
scrollanim[2] = {time:0, begin:0, change:0.0, duration:0.0, element:null, timer:null};

function scrollStart(end, index, startIndex, endIndex)
{
//	alert(index+' '+ startIndex+' '+ endIndex);


	elem = theScroll[index];
	start = theScroll[index].scrollTop;

	if (scrollanim[index].timer != null) {
		clearInterval(scrollanim[index].timer);
		scrollanim[index].timer = null;
	}
	scrollanim[index].time = 0;
	scrollanim[index].begin = start;
	scrollanim[index].change = end - start;
	scrollanim[index].duration = 25;
	scrollanim[index].element = elem;
	
	scrollanim[index].timer = setInterval("scrollVertAnim("+index+","+endIndex+");", timerInterval);
}


function scrollVertAnim(index, navIndex)
{

	if (scrollanim[index].time > scrollanim[index].duration) {
		clearInterval(scrollanim[index].timer);
		scrollanim[index].timer = null;
	}
	else {
		move = sineInOut(scrollanim[index].time, scrollanim[index].begin, scrollanim[index].change, scrollanim[index].duration);
		scrollanim[index].element.scrollTop = move;
		scrollanim[index].time++;
	}
}

// Utility: Find the Y position of an element on a page. Return Y and X as an array

function findElementPos(elemFind)
{
	var elemX = 0;
	var elemY = 0;
	do {
		elemX += elemFind.offsetLeft;
		elemY += elemFind.offsetTop;
	} while ( elemFind = elemFind.offsetParent )

	return Array(elemX, elemY);
}


function sineInOut(t, b, c, d)
{
	return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
}
