$(document).ready(function(){	
	function recalculate_width(){
		var totalWidth = 0;
		$("div#featured-events-slider ul.event").each(function(){
			totalWidth+= $(this).width();
		})

		$("div#featured-events-slider").css("width", totalWidth + 10);		
	}
	
	var loadingTimeout;
	
	
	$(".whatson-block").fitted();
	$("div#featured-events-slider li").fitted();
	
	$("ul.category-filter a").click(function(){
		
		//don't allow the user to click on the same thing twice, breaking the scrolling
		if ( $(this).hasClass("selected") )
		{
			return;
		}
		var old_selected = $("ul.category-filter a.selected").parent().attr("id").replace(/category-idx-/, "");
		old_selected = parseInt(old_selected);
		
		loadingTimeout = setTimeout('$("div#featured-events-loading").css("display", "block")', 100);
		
		$("ul.category-filter a").removeClass("selected");
		$(this).addClass("selected");
		
		var new_selected = $(this).parent().attr("id").replace(/category-idx-/, "");
		new_selected = parseInt(new_selected);
				
		//clear up any animations already in progress
		$("div#featured-events-slider").stop(true);
		$("div#featured-events-slider ul:not(.active-event)").remove();
				
		var category_id = $(this).attr("id").replace(/category-/, "");
				
		$.get("/mmlib/includes/visit-birmingham/ajax.getfeaturedevents.php", {category: category_id}, function(response){
			var eventUL = $("div#featured-events-slider ul.active-event:first");
			var newLeftPosition;
			clearTimeout(loadingTimeout);

			if(new_selected > old_selected){
				$(eventUL).after(response);
				newLeftPosition = -908;//$(eventUL).width() * -1;
			}else if(new_selected < old_selected){
				$(eventUL).before(response);
				newLeftPosition = 0;
				$("div#featured-events-slider").css("left", -908);
			}else{
				return;
			}
			recalculate_width();
			
			$("div#featured-events-loading").css("display", "none");
			$("div#featured-events-slider").animate({
				left: newLeftPosition
			}, 500, "swing", function(){
								if(new_selected > old_selected){
									$(eventUL).next().addClass("active-event");
								}else{
									$(eventUL).prev().addClass("active-event");									
								}
								$(eventUL).remove();
								$("div#featured-events-slider").css("left", "0");
								$("div#featured-events-slider li").fitted();								
							});
		});
		
		
		
		return false;
	});
	$("div#featured-events-slider ul.event li").css("marginRight", "10px");
	recalculate_width();
});
