/**
 * Loads in a URL into a specified divName, and applies the function to
 * all the links inside the pagination div of that page (to preserve the ajax-request)
 * @param string href The URL of the page to load
 * @param string divName The name of the DOM-element to load the data into
 * @return boolean False To prevent the links from doing anything on their own.
 */
function loadPiece(href,divName) {
	$(divName).fadeOut("fast");
	$(divName).load(href, {}, function(){
		setTimeout("showPiece('" + divName + "')",500);
		var divPaginationLinks = divName+" .pagination a";
		$(divPaginationLinks).click(function() { 	
			var thisHref = $(this).attr("href");
			loadPiece(thisHref,divName);
			return false;
		});
	});
}

function showPiece(divName) {
	$(divName).fadeIn("slow");
}

function cycle(fist_id) {
	var id = $("#events > div:visible").next().attr("rel");
	if(!id){
		id = $("#events > div:first").attr("rel");
	}
	$("#events > div").fadeOut();
	if(!$("#events_" + id + " *").length){
		clearInterval(cycle_interval);
		$("#events_" + id).load('/events/show/' + id, {}, function(){
			$("#events_" + id).animate({opacity: 0}, 500).animate({opacity: 1.0}, 500).fadeIn();
			cycle_interval = setInterval(cycle,7000);
		});
	} else {
		$("#events_" + id).animate({opacity: 0}, 500).animate({opacity: 1.0}, 500).fadeIn();
	}
} 
