/*
 * Scroller.js
 *
 * Written by Michael Marner
 * Copyright (c) 2008 LoudWhisper
 *
 */


var currentCategory = 0;

/**
 * The current position of where we are (pixel value)
 */
var currentX = 0;

/**
 * The desired location
 */
var finalX = 0;

var startingX = 0;

/**
 * Current Velocity
 */
var velocity = 0;

/**
 * Initial move speed.
 * Note that we have to do this in pixel values!
 */
var startSpeed = 0.5;

/**
 * Accelleration factor
 */
var acceleration = 1.1;

/**
 * How far to travel while accellerating.
 * This will also be used to decide when to start slowing down
 */
var accelDistance = 160;

var indexArray = [];

var projectArray = [];

var animationDude;

var interval = 10;

function doMove() {
	currentX = currentX + velocity;
	distanceTravelled = (Math.abs(currentX - startingX));
	distanceToGo = (Math.abs(finalX - currentX));
	if (distanceTravelled < accelDistance && distanceToGo > distanceTravelled) {
		velocity = velocity*acceleration;
	}
	else if (distanceToGo <= 60) {
		velocity = velocity/acceleration;
	}
	var scroller = document.getElementById("ScrollerThumbnailContainer");
	if ((currentX <= finalX && velocity < 0) || (currentX >= finalX && velocity > 0)) {
		currentX = finalX;
		clearInterval(animationDude);
	}
	scroller.style.left = Math.floor(-currentX) + "px";
}

function jumpTo(category) {
	var scroller = document.getElementById("ScrollerThumbnailContainer");
	scroller.style.left = indexArray[category] + "px";
	currentX = indexArray[category];
}
	

function moveTo(position) {
	clearInterval(animationDude);
	startingX = currentX;
	//decide on direction
	direction = currentX - position;
	finalX = position;
	if (direction < 0 ) {
		velocity = 1*startSpeed;
	}
	else {
		velocity = -startSpeed;
	}
	animationDude = setInterval("doMove()", interval);
}

function changeCategory(catIndex) {
	for (i=0;i<projectArray.length;i++) {
		if (projectArray[i].category != catIndex) {
			projectArray[i].element.className = "ScrollerThumbnailDisabled";
		}
		else {
			projectArray[i].element.className = "ScrollerThumbnailActive";
		}
	}
	moveTo(indexArray[catIndex]);
}

function addCategory(index, offset) {
	indexArray[index] = offset;
}
function addProject(category, element) {
	project = new Object;
	project.category = category;
	project.element = document.getElementById(element);
	projectArray.push(project);
}

