$(document).ready(function() {
	/* Carousel */	
	cslide = 1;
	paused = false;
	slides = $("#carousel div").length;
	
	$("#carousel-details .previous").click(function() {		
		if(paused){
			$("#carousel").show();
			$("#youtube").html('');
			paused = false;
		}
		
		$("#carousel div:nth-child("+cslide+")").fadeOut("slow");
		
		cslide = cslide==1 ? slides : cslide-1;
		$("#carousel div:nth-child("+cslide+")").fadeIn("slow");
		$("#carousel-title").html($("#carousel div:nth-child("+cslide+") img").attr("alt"));
		
		clearTimeout(carousel);
		carousel = setTimeout(next_slide, 5000);
        
		$("#carousel-numbers").html(cslide+" / "+slides);
	});

	$("#carousel-details .next").click(function() {
		if(paused){
			$("#carousel").show();
			$("#youtube").html('');
			paused = false;
		}
		
		next_slide();
	});
	
	function next_slide() {
		if(paused) return;
		
		$("#carousel div:nth-child("+cslide+")").fadeOut("slow");
		
		cslide = cslide==slides ? 1 : cslide+1;
		$("#carousel div:nth-child("+cslide+")").fadeIn("slow");
		$("#carousel-title").html($("#carousel div:nth-child("+cslide+") img").attr("alt"));
		
		clearTimeout(carousel);
		carousel = setTimeout(next_slide, 5000);
        
		$("#carousel-numbers").html(cslide+" / "+slides);
	}

	//Initialise
	$("#carousel div:nth-child("+cslide+")").show();
	$("#carousel-title").html($("#carousel div:nth-child("+cslide+") img").attr("alt"));
	
	var carousel = setTimeout(next_slide, 5000);
    
	$("#carousel-numbers").html(cslide+" / "+slides);
		
	
	//Thumbnail Rollover
	//On mouse over those thumbnail
	$('.item').hover(function() {

		//Display the caption
		$(this).find('img').animate({opacity: "0.6"}, 200);
		$(this).find('div.caption').stop(false,true).fadeIn(200);
	},
	function() {

		//Hide the caption
		$(this).find('img').animate({opacity: "1"}, 200);
		$(this).find('div.caption').stop(false,true).fadeOut(200);
	});
		
});

function viewYoutube(vid) {
	$("#youtube").html('<iframe width="666" height="100%" src="http://www.youtube.com/embed/'+vid+'?autoplay=1" frameborder="0" allowfullscreen></iframe>');
	
	$("#carousel, #carousel div").fadeOut();
	
	paused = true;
}


