var lastSection = null;

function scrollDirection(scroll, direction)
{
	var element = document.getElementById("scrollcontent");
	var children = element.childNodes;
	
	sectionNames = new Array();
	
	for (var i = 0; i < children.length; i++)
	{
		if( element.childNodes[i].className == "section" )
		{
			sectionNames.push(element.childNodes[i].id);
		}
	}
	
	if( lastSection == null )
	{
		sectionFrom = 0;
	}
	else
	{
		for( var i = 0; i < sectionNames.length; i++)
		{
			if( sectionNames[i] == lastSection )
			{
				sectionFrom = i;
			}
		}
	}

	if( direction == "next" )
	{
		sectionTo = sectionFrom+1;
		if( sectionTo > sectionNames.length-1 )
		{
			sectionTo = 0;
		}
	}
	else
	{
		sectionTo = sectionFrom-1;
		if( sectionTo < 0 )
		{
			sectionTo = sectionNames.length-1;
		}
	}	

	sectionFrom = sectionNames[0];
	sectionTo = sectionNames[sectionTo];

	var scrollElement = document.getElementById(scroll);
	var posFrom = findElementPos(document.getElementById(sectionFrom));
	var posTo = findElementPos(document.getElementById(sectionTo));

	lastSection = sectionTo;
	
	scrollStart(scrollElement, scrollElement.scrollLeft, posTo[0]-posFrom[0], "horiz");
}

function scrollSection(scroll, scrollFrom, scrollTo)
{
	var scrollElement = document.getElementById(scroll);
	var posFrom = findElementPos(document.getElementById(scrollFrom));
	var posTo = findElementPos(document.getElementById(scrollTo));

	lastSection = scrollTo;
	
	scrollStart(scrollElement, scrollElement.scrollLeft, posTo[0]-posFrom[0], "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;
	if(!scrollanim.duration)
	{
		scrollanim.duration = 20;
	}
	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 findElementPos(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);
}

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