//Setup the carousel
$(".featured_module .entry").each(function(){
	
	//Grab the div position
	var id = $(this).index(); 
	
	//generate the links
	var page = id + 1; 
	if(page == 1){
		$(".pagination").append('<a href="" class="active">'+page+'</a>');
	} else {
		$(".pagination").append('<a href="">'+page+'</a>');
	}
	
	//Show the newest item
	if(id == 0){
		$(this).css("display", "block").addClass("active");
	}
	
});//each


//Clicking the pagination links
$(".featured_module .pagination a").click(function(){
	
	//Grab the div position
	var id = $(this).html() - 1;
	
	//Animations & handling
	$(".featured_module .entry.active").fadeOut("500", function(){
		$(this).removeClass("active");
	});
	$(".featured_module .entry:eq("+id+")").fadeIn("500", function(){
		$(this).addClass("active");
	});
	
	//
	$(".featured_module .pagination a").removeClass("active");
	$(this).addClass("active");
	
	return false;
	
});


//Carousel loop
setInterval("carouselLoop()", 8000);
function carouselLoop(){
	
	//Main Object
	var obj = ".featured_module .pagination a";
	
	//Next Object
	var next_obj = $(obj+".active").index()+1;
	
	// An undesired object get's added after the last page.
	// This function ensure's the loop restarts correctly.
	var exists = $(obj+":eq("+next_obj+")").length;
	if(exists == 0){
		var next_obj = '0';
	}
	
	//Switch the active class
	$(obj).removeClass("active");
	$(obj+":eq("+next_obj+")").addClass("active");
	
	//Animations & handling
	$(".featured_module .entry.active").fadeOut("500", function(){
		$(this).removeClass("active");
	});
	$(".featured_module .entry:eq("+next_obj+")").fadeIn("500", function(){
		$(this).addClass("active");
	});
	
}
