$(function(){
	startRotator();
});
function startRotator()
{
	setInterval("showNextItem(-1)", 5000);	
	
	// Hide afbeeldingen
	$(".homeImg").find("img").eq(0).nextAll("img").hide();
	
	// Sla de text op
	$(".subOverItem").each(function(i){
		$(this).find(".subOverTxt").data("text", $(this).find(".subOverTxt").html());
		if( i != 0 )
		{
			$(this).find(".subOverTxt").html($(this).find(".subOverDesc").html());	
		}
	});
}

function showNextItem(i)
{
	var curIndex = $(".subOverItem").index($(".subOverItem.selected"));
	var nextIndex = $(".subOverItem").length == curIndex + 1 ? 0 : curIndex + 1;
	nextIndex = i == -1 ? nextIndex : i;
	// Set new index
	$(".subOverItem").removeClass("selected");
	$(".subOverItem").eq(nextIndex).addClass("selected");
	
	// Fade afbeelding
	$(".homeImg").find("img").eq(curIndex).fadeOut();
	$(".homeImg").find("img").eq(nextIndex).fadeIn();	
	
	// Schuiven
	if(nextIndex != 0)
	{
		var oldOffset = parseInt($(".subOverItem").eq(curIndex).css("bottom")) + 57;
		$(".subOverItem").eq(curIndex).animate({ height : 22, bottom : oldOffset}, 500, function(){
			$(this).addClass("short");	
		});
		
		$(".subOverItem").eq(nextIndex).removeClass("short").height(22).animate({ height : 79}, 500);
		
		// Zet text van element
		$(".subOverItem").eq(curIndex).find(".subOverTxt").html($(".subOverItem").eq(curIndex).find(".subOverDesc").html());
		$(".subOverItem").eq(nextIndex).find(".subOverTxt").html($(".subOverItem").eq(nextIndex).find(".subOverTxt").data("text"));
	}
	else
	{
		var newOffset = parseInt($(".subOverItem").eq(nextIndex).css("bottom")) - 57;
		$(".subOverItem").eq(nextIndex).removeClass("short").height(22).animate({ height : 79, bottom : newOffset}, 500);	
		
		$(".subOverItem").eq(curIndex).animate({ height : 22}, 500, function(){
			$(this).addClass("short");	
		});
		
		// Zet text van element
		$(".subOverItem").eq(curIndex).find(".subOverTxt").html($(".subOverItem").eq(curIndex).find(".subOverDesc").html());
		$(".subOverItem").eq(nextIndex).find(".subOverTxt").html($(".subOverItem").eq(nextIndex).find(".subOverTxt").data("text"));
		
		// Alles er tussenin verplaatsen naar onder
		for(i=nextIndex + 1;i<curIndex ; i++)
		{
			var newOffset = parseInt($(".subOverItem").eq(i).css("bottom")) - 57;
			$(".subOverItem").eq(i).height(22).animate({bottom : newOffset}, 500);	
			
		}
	}
}
