$(document).ready(function() {
	var animating = false;
	var impactWrapper = $('#impact_pane .impact_wrapper');
	var impactTimer = window.setInterval(nextImpact, 5000);;
	var maxPixels = (208 * (impactWrapper.children().length - 1));
	
	function stopAnimating()
	{
		window.animating = false;
	}
	
	function nextImpact()
	{
		var currentOffset = impactWrapper.css('margin-left').match(/\d+/)[0];
		
		if(currentOffset < maxPixels)
			impactWrapper.animate({marginLeft:'-=208px'}, 'slow', 'linear', stopAnimating);
		else
			impactWrapper.animate({marginLeft:'0px'}, 'slow', 'linear', stopAnimating);
	}
	
	function previousImpact()
	{
		var currentOffset = impactWrapper.css('margin-left').match(/\d+/)[0];
		
		if((currentOffset > 0) && (currentOffset <= maxPixels))
			impactWrapper.animate({marginLeft:'+=208px'}, 'slow', 'linear', stopAnimating);
		else
			impactWrapper.animate({marginLeft: '-'+maxPixels+'px'}, 'slow', 'linear', stopAnimating);
	}
	
	$('.impact_next').click( function()
	{
		if(!window.animating)
		{
			window.animating = true;
			nextImpact();
		}
		return false;
	} );
	
	$('.impact_previous').click( function()
	{
		if(!animating)
		{
			window.animating = true;
			previousImpact();
		}
		return false;
	} );
	
	$('.impact_block').mouseover( function()
	{
		clearInterval(window.impactTimer);
		window.impactTimer = null;
	} );
	
	$('.impact_block').mouseout( function()
	{
		if(is_null(window.impactTimer))
			window.impactTimer = window.setInterval(nextImpact, 5000);
	} );
	
	$('#slides_previous').click(function()
	{
		var panelWrapper = $('#panels .current_panel').parent();
		var currentOffset = panelWrapper.css('margin-left');
		
		$('#tabs .current').removeClass('current');
		if(currentOffset == '0px')
		{
			panelWrapper.animate({marginLeft:'-1740px'}, "slow");
			$('#help_tab').addClass('current');
			$('#tab_background').animate({
				left:"285px",
				width:"165px"
			},"slow");
		} else if(currentOffset == '-870px')
		{
			panelWrapper.animate({marginLeft:'0px'}, "slow");
			$('#overview_tab').addClass('current');
			$('#tab_background').animate({
				left:"0",
				width:"103px"
			},"slow");
		} else
		{
			panelWrapper.animate({marginLeft:'-870px'}, "slow");
			$('#difference_tab').addClass('current');
			$('#tab_background').animate({
				left:"130px",
				width:"127px"
			},"slow");
		}
		
		return false;
	});
	
	$('#slides_next').click(function() {
		var panelWrapper = $('#panels .current_panel').parent();
		var currentOffset = panelWrapper.css('margin-left');
		
		$('#tabs .current').removeClass('current');
		if(currentOffset == '0px')
		{
			panelWrapper.animate({marginLeft:'-=870px'}, "slow");
			$('#difference_tab').addClass('current');
			$('#tab_background').animate({
				left:"130px",
				width:"127px"
			},"slow");
		} else if(currentOffset == '-870px')
		{
			panelWrapper.animate({marginLeft:'-=870px'}, "slow");
			$('#help_tab').addClass('current');
			$('#tab_background').animate({
				left:"285px",
				width:"165px"
			},"slow");
		} else
		{
			panelWrapper.animate({marginLeft:'0px'}, "slow");
			$('#overview_tab').addClass('current');
			$('#tab_background').animate({
				left:"0",
				width:"103px"
			},"slow");
		}
		
		return false;
	});
	
	$('#overview_tab a').click(function() {
		if(!$(this).parent().hasClass('current'))
		{
			$('#panels .panels_wrapper').animate({marginLeft: "0px"}, "slow");
			
			$('#tabs .current').removeClass('current');
			$(this).parent().addClass('current');
			$('#tab_background').animate({
				left:"0",
				width:"103px"
			},"slow");
		}
		return false;
	});
	
	$('#difference_tab a').click(function() {
		if(!$(this).parent().hasClass('current'))
		{
			$('#panels .panels_wrapper').animate({marginLeft: "-870px"}, "slow");
			
			$('#tabs .current').removeClass('current');
			$(this).parent().addClass('current');
			$('#tab_background').animate({
				left:"130px",
				width:"127px"
			},"slow");
		}
		return false;
	});
	
	$('#help_tab a').click(function() {
		if(!$(this).parent().hasClass('current'))
		{
			$('#panels .panels_wrapper').animate({marginLeft: "-1740px"}, "slow");
			
			$('#tabs .current').removeClass('current');
			$(this).parent().addClass('current');
			$('#tab_background').animate({
				left:"285px",
				width:"165px"
			},"slow");
		}
		return false;
	});
});