//
// Adapted from CodaEffects.js - (C) 2007 Panic, Inc.
//

var currentProject = "project1"; // The default loaded section on the page
var	projects = new Array("project1", "project2", "project3", "project4", "project5", "project6");

function initProjects() {
	
	if(!document.getElementById('home'))
		return;
		
	var divs = document.getElementById('promo').getElementsByTagName('div');
	var counter = 1;
	
	for(var i=0; i<divs.length; i++) {
		if(returnClass(divs[i]) == "section"){
			idName = "project" + counter++;
			divs[i].setAttribute('id',idName)
		}
	}
	
	/*for(var k=0; k<projects.length; k++)
		alert(projects[k].getAttribute('id'));*/
}

addLoadEvent(initProjects);

function scrollProjects() 
{
	// Now iterate through the three projects, find matches, and determine where to go.

	for (var i = 0; i < projects.length; i++) {
		if (projects[i] == currentProject) {
			if ((i + 1) > (projects.length - 1))
				link = projects[0];
			else
				link = projects[i + 1];
		}
	}
	
	var scrollArea = "promo";
	var offset = "project1";

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

	if (currentProject == link) {
		return;
	}
	lastSection = currentProject;
	currentProject = link;
    
	// Get the element we want to scroll, get the position of the element to scroll to
	
	theScroll = document.getElementById(scrollArea);
	position = findElementPos(document.getElementById(link));

	// 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));
		position[0] = position[0] - offsetPos[0];
	}

	scrollStart(theScroll, theScroll.scrollLeft, position[0], "horiz");
	// return false;
}

//
// Animated Scroll Functions
// Scrolls are synchronous -- only one at a time.
//

var scrollanim = {time:0, begin:0, change:0.0, duration:0.0, element:null, timer:null};

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

	if (scrollanim.timer != null) {
		clearInterval(scrollanim.timer);
		scrollanim.timer = null;
	}
	scrollanim.time = 0;
	scrollanim.begin = start;
	scrollanim.change = end - start;
	scrollanim.duration = 25;
	scrollanim.element = elem;
	
	if (direction == "horiz") {
		scrollanim.timer = setInterval("scrollHorizAnim();", 15);
	}
	else {
		scrollanim.timer = setInterval("scrollVertAnim();", 15);
	}
}

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

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