var counter = 0;
var slideduration = 450;
var timebetweenautoslides = 5000;
var forward = true;
var periodicalFunctionVar;
var numberofslidesinview = 1;

var cannon = function() 
{
	aSlideDivs = $$('#latestProjects div.project');
	
    if (counter == aSlideDivs.length - 1 - (numberofslidesinview - 1)) forward = false;
    else if (counter == 0) forward = true;
	
	if(forward == true) counter++;
	else counter--;	
	
	doSlide();
}

var nextSlide = function(event) 
{
	$clear(periodicalFunctionVar);
	aSlideDivs = $$('#latestProjects div.project');
	if (counter < aSlideDivs.length - 1 - (numberofslidesinview - 1))
	{ 	
		counter++;		
		doSlide();
	}
	event.stop();
}

var previousSlide = function(event) 
{
	$clear(periodicalFunctionVar);
	if (counter > 0)
	{	
		counter--;		
		doSlide();
	}
	event.stop();
}

var doSlide = function()
{
	aSlideDivs = $$('#latestProjects div.project');
	
	aSlideDivs.each(function(div){
		offset = div.getStyle('width').substring(0, div.getStyle('width').length - 2);
		div.set('tween', {duration: slideduration});
		div.tween('left', -(counter * offset) + (offset * (div.id.substring(7) - 1)) + 'px');
	});
}
 
window.addEvent('domready', function() 
{
    $('next').addEvent('click', nextSlide);
    $('previous').addEvent('click', previousSlide);
    periodicalFunctionVar = cannon.periodical(timebetweenautoslides);
});
