//
// 	Code taken and adapted from CodaEffects.js - (C) 2007 Panic, Inc.
// 	http://www.panic.com
//

var codaEffects_currentSection = false; // The default loaded section on the page
var codaEffects_scrollanim = {time:0, begin:0, change:0.0, duration:0.0, element:null, timer:null};

function codaEffects_scrollSection(currID, scrollArea, offset) {

	if (!codaEffects_currentSection) {
		codaEffects_currentSection = offset;
	}

	if (codaEffects_currentSection == currID) {
		return;
	}
	lastSection = codaEffects_currentSection;
	codaEffects_currentSection = currID;
	
	theScroll = document.getElementById(scrollArea);
	position = codaEffects_findElementPos(document.getElementById(currID));
	
	if (offset != "") {
		offsetPos = codaEffects_findElementPos(document.getElementById(offset));
		position[0] = position[0] - offsetPos[0];
	}

	
	codaEffects_scrollStart(theScroll, theScroll.scrollLeft, position[0], "horiz");
}

function codaEffects_scrollStart(elem, start, end, direction) {

	if (codaEffects_scrollanim.timer != null) {
		clearInterval(codaEffects_scrollanim.timer);
		codaEffects_scrollanim.timer = null;
	}
	codaEffects_scrollanim.time = 0;
	codaEffects_scrollanim.begin = start;
	codaEffects_scrollanim.change = end - start;
	codaEffects_scrollanim.duration = 25;
	codaEffects_scrollanim.element = elem;
	
	if (direction == "horiz") {
		codaEffects_scrollanim.timer = setInterval("codaEffects_scrollHorizAnim();", 15);
	}
	else {
		codaEffects_scrollanim.timer = setInterval("codaEffects_scrollVertAnim();", 15);
	}
}

function codaEffects_scrollVertAnim() {
	if (codaEffects_scrollanim.time > codaEffects_scrollanim.duration) {
		clearInterval(codaEffects_scrollanim.timer);
		codaEffects_scrollanim.timer = null;
	}
	else {
		move = codaEffects_sineInOut(codaEffects_scrollanim.time, codaEffects_scrollanim.begin, codaEffects_scrollanim.change, codaEffects_scrollanim.duration);
		codaEffects_scrollanim.element.scrollTop = move; 
		codaEffects_scrollanim.time++;
	}
}

function codaEffects_scrollHorizAnim() {
	if (codaEffects_scrollanim.time > codaEffects_scrollanim.duration) {
		clearInterval(codaEffects_scrollanim.timer);
		codaEffects_scrollanim.timer = null;
	}
	else {
		move = codaEffects_sineInOut(codaEffects_scrollanim.time, codaEffects_scrollanim.begin, codaEffects_scrollanim.change, codaEffects_scrollanim.duration);
		codaEffects_scrollanim.element.scrollLeft = move;
		codaEffects_scrollanim.time++;
	}
}

function codaEffects_findElementPos(elemFind) {
	var elemX = 0;
	var elemY = 0;
	do {
		elemX += elemFind.offsetLeft;
		elemY += elemFind.offsetTop;
	} while ( elemFind = elemFind.offsetParent )

	return Array(elemX, elemY);
}

function codaEffects_sineInOut(t, b, c, d) {
	return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
}

