$(document).ready(function() {

	var maxIndex = $('.impact_block').length - 1;
	$('.impact_block').css({'margin-left':'160px','display':'none'})
	var currentIndex = 0;
	$('#impact_block'+currentIndex).css({'margin-left':'0px','display':'block'})
	var impactTimer = window.setTimeout(nextImpact, 5000);

	
	function nextImpact()
	{
		$('#impact_block'+currentIndex).animate({marginLeft:'-160px'}, 'slow', 'linear',function() 
			{
				hideImpactBlock(currentIndex);
				if(currentIndex < maxIndex)
					currentIndex = currentIndex + 1;
				else
					currentIndex = 0;
				
				$('#impact_block'+currentIndex).css({'margin-left':'160px','display':'block'})
				$('#impact_block'+currentIndex).animate({marginLeft:'0px'}, 'slow', 'linear');
			}
		);
		
		clearTimeout(impactTimer);
		impactTimer = window.setTimeout(nextImpact, 5000);
	}
	
	function previousImpact()
	{
		$('#impact_block'+currentIndex).animate({marginLeft:'160px'}, 'slow', 'linear',function()
			{ 
				hideImpactBlock(currentIndex);
				
				if(currentIndex > 0)
					currentIndex = currentIndex - 1;
				else
					currentIndex = maxIndex;
				
				$('#impact_block'+currentIndex).css({'margin-left':'-160px','display':'block'})
				$('#impact_block'+currentIndex).animate({marginLeft:'0px'}, 'slow', 'linear');
			}
		);
		
		clearTimeout(impactTimer);
		impactTimer = window.setTimeout(previousImpact, 5000);
	}
	
	function hideImpactBlock(blockIndex)
	{
		$('#impact_block'+blockIndex).css({'display':'none'})
	}
	
	$('.impact_next').click( function()
	{
		nextImpact();
		return false;
	} );
	
	$('.impact_previous').click( function()
	{
		previousImpact();
		return false;
	} );
	
});