// Scroll: Setup Scrolling Stuff

function Scroll(totalSections, visibleSections, moveAmt, scrollArea){
	this.currentPosition = 1;
	this.lastSection = 1;
	this.moveAmt = moveAmt; // We could make this dynamic, but for now...
	this.numberContents = totalSections - (visibleSections - 1); // this is finding out the number of moves we should allow to be made.
	this.theScroll = document.getElementById(scrollArea);
	this.scrollanim = {time:0, begin:0, change:0.0, duration:0.0, element:null, timer:null};
}

// Scroll the page manually to the position of element "link", passed to us.

Scroll.prototype.sineInOut = function (t, b, c, d){

	return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
}

Scroll.prototype.findElementPos = function (elemFind){

	var elemX = 0;
	var elemY = 0;
	do {
		elemX += elemFind.offsetLeft;
		elemY += elemFind.offsetTop;
	} while ( elemFind = elemFind.offsetParent )

	//console.log("Found element "+elemFind+" at "+elemY+"/"+elemX);

	return Array(elemX, elemY);
}

Scroll.prototype.ScrollSection = function (position,orientation){

	// Store the last section, and update the current section

	this.lastPosition = this.currentPosition;
	this.currentPosition = position;

	// Get the element we want to scroll, get the position of the element to scroll to

	offset = this.moveAmt * (this.currentPosition - 1);
	if (orientation == "horiz") {
		this.scrollStart(this.theScroll, this.theScroll.scrollLeft, offset, orientation);
	} else {
		this.scrollStart(this.theScroll, this.theScroll.scrollTop, offset, orientation);
	}
	// return false;
}

// Scroll the page using the arrows

Scroll.prototype.ScrollArrow = function (direction){

	// Now iterate through our array of tab names, find matches, and determine where to go.

	i = this.currentPosition;

	switch (direction) {

		case "left":
			if (i - 1 < 1) {
			i = this.numberContents;
			} else {
				i = i - 1;
			}
			orientation = "horiz";
		break;
		case "right":
			if ((i + 1) > this.numberContents) {
			i = 1;
			} else {
				i = i + 1;
			}
			orientation = "horiz";
		break;
		case "up":
			if (i - 1 < 1) {
			i = this.numberContents;
			} else {
				i = i - 1;
			}
			orientation = "vert";
		break;
		case "down":
			if ((i + 1) > this.numberContents) {
			i = 1;
			} else {
				i = i + 1;
			}
			orientation = "vert";
		break;

	}

	// Go to the section name!
	this.ScrollSection(i,orientation);

}

Scroll.prototype.scrollStart = function(elem, start, end, direction){

	//console.log("scrollStart from "+start+" to "+end+" in direction "+direction);

	if (this.scrollanim.timer != null) {
		clearInterval(this.scrollanim.timer);
		this.scrollanim.timer = null;
	}
	this.scrollanim.time = 0;
	this.scrollanim.begin = start;
	this.scrollanim.change = end - start;
	this.scrollanim.duration = 25;
	this.scrollanim.element = elem;

	this.scrollHorizAnim();

	dumb = this;

	if (direction == "horiz") {
		this.scrollanim.timer = setInterval(function(){dumb.scrollHorizAnim()}, 15);
	} else {
		this.scrollanim.timer = setInterval(function(){dumb.scrollVertAnim()}, 15);
	}
}

Scroll.prototype.scrollVertAnim = function (){

	if (this.scrollanim.time > this.scrollanim.duration) {
		clearInterval(this.scrollanim.timer);
		this.scrollanim.timer = null;
	}
	else {
		move = this.sineInOut(this.scrollanim.time, this.scrollanim.begin, this.scrollanim.change, this.scrollanim.duration);
		this.scrollanim.element.scrollTop = move;
		this.scrollanim.time++;
	}
}

Scroll.prototype.scrollHorizAnim = function(){

	if (this.scrollanim.time > this.scrollanim.duration) {
		clearInterval(this.scrollanim.timer);
		this.scrollanim.timer = null;
	}
	else {
		move = this.sineInOut(this.scrollanim.time, this.scrollanim.begin, this.scrollanim.change, this.scrollanim.duration);
		this.scrollanim.element.scrollLeft = move;
		this.scrollanim.time++;
	}

}
