var width = 268;		// width for each element (assuming every element has the same width)
var noElements = 4;		// number of elements
var curElement = 1;


function scroll(way) {
	var goMax, goMin;
	
	if (way == 'plus')
		curElement++;
	else if (way == 'min')
		curElement--;
		
	if (curElement > noElements) {
		curElement = 1;
		goMax = true;
	}
		
	if (curElement < 1) {
		curElement = noElements;
		goMin = true;
	}
		
	
	if (way == 'plus') {
		var new_start = (curElement-2)*width;
		var new_end = (curElement-1)*width;
	}
	else if (way == 'min') {
		var new_start = (curElement)*width;
		var new_end = (curElement-1)*width;
	}

	if (goMin) new_start = 0;
	else if (goMax) new_start = noElements*width;
	
	scrollStart(document.getElementById('scroller'), new_start, new_end, 'horiz');
}


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

function scrollStart(elem, start, end, 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++;
	}
}

function linear(t, b, c, d) {
	return c*t/d + b;
}

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

function fixScrollPos() {
	document.getElementById('scroller').scrollLeft = (curElement-1)*width;
}