$(document).ready(function() {
	
	$("#news-nav li.nav-next").bind('click', function(){ nav('news', 'next'); } );
	$("#news-nav li.nav-previous").bind('click', function(){ nav('news', 'previous'); } );
	
	$("#solutions-nav li.nav-next").bind('click', function(){ 
		solutionsnav("next");  
	});
	$("#solutions-nav li.nav-previous").bind('click', function(){ 
		solutionsnav("previous"); 
	});
	
	$("#solution1").attr("href", "javascript:;");
	$("#solution2").attr("href", "javascript:;");
	$("#solution3").attr("href", "javascript:;");
	$("#solution4").attr("href", "javascript:;");
	
	$("#solution1").bind('click', function(){ 
		sidenav('solutions', 1);
	});
	$("#solution2").bind('click', function(){ 
		sidenav('solutions', 2);
	});
	$("#solution3").bind('click', function(){ 
		sidenav('solutions', 3);
	});
	$("#solution4").bind('click', function(){ 
		sidenav('solutions', 4);
	});
});

var animating;
var newsIndex = 1;
var solutionsIndex = 1;
var bigIndex = 1;
var thumbsIndex = 1;
var contentimagesIndex = 4;

function nav(kind, way, context, target)
{

	if(!context) context = "";
	if(animating) return;

	var curPosition = $("ul."+kind+"-list").position();
	
	// Calculate how much pixels the whole post list should go to the left or the right.
	if(way == "previous")
	{
	
		if(eval(kind+'Index') <= 1) return;
		
		eval(kind+'Index--'); // Put the index on the previous article. Do this before newPosition to use it's width in the calculation.
		
		var newPosition = (curPosition.left + $("ul."+kind+"-list > li:nth-child("+eval(kind+'Index')+")").innerWidth())+"px";
	} else if(way == "next") // way == next
	{
	
		if(eval(kind+'Index') >= $("ul."+kind+"-list > li").length / $("ul."+kind+"-list").length) return;
	
		var newPosition = (curPosition.left - $("ul."+kind+"-list > li:nth-child("+eval(kind+'Index')+")").innerWidth())+"px";
		
		eval(kind+'Index++'); // Put the index on the next article.
	}
	
	// Remove the post-focus-first class from all posts and add it to the new one.
	//$("."+kind+" .post").removeClass("post-focus-first");
	//$("."+kind+" .post:nth-child("+eval(kind+'Index')+")").addClass("post-focus-first");
	
	// Put animating on true, so the animations won't bug eachother.
	setTimeout(function() { animating = true; }, 25);
	$("ul."+kind+"-list").animate({
		left: newPosition
	}, 500, false, 
		function()
		{
			
			animating = false; 
			if($("ul."+kind+"-list").css("left") != newPosition)
			{
				$("ul."+kind+"-list").css("left", newPosition + "px");
			}
			
			if(context == "side")
			{
				
				sidenav(kind, target);
			}
		}
	);
}

function sidenav(kind, target)
{
	var delta = target - eval(kind+'Index');
	
	//alert(target);
	
	if(delta > 0)
	{
		solutionsnav("next", "side", target);
	} else if(delta < 0)
	{
		solutionsnav("previous", "side", target);
	} else {}
}

function solutionsnav(way, context, target)
{
	if(way == 'previous')
	{
		nav('solutions', 'previous', context, target); 
	
		nav('thumbs', 'previous'); 
		nav('contentimages', 'next'); 
	} else {
		nav('solutions', 'next', context, target); 
	
		nav('thumbs', 'next'); 
		nav('contentimages', 'previous'); 
	}
}
