//Locks the carousel
var cLock = false;

//Remove active class from all the a tags in the jcarousel
function homeReset(){
	$("#filmstrip li").each(function(){
		$(this).removeClass("active");
	});
}

//Code for loop
var currentSlide = 0;
function playNext() {
	currentSlide++;
	jQuery('#filmstrip').jcarousel('next');
	if(currentSlide==$('#filmstrip').jcarousel('size')){
		playSlide(0);
	}else{
		playSlide(currentSlide);
	}
}
function playSlide(i) { 
	resetTO();
	$('#image-info').cycle(i); 
	$('#photoSlider').cycle(i); 
	homeReset();
	$(".goto"+(i+1)).parent().addClass("active");
	currentSlide = i;	
}
function resetTO(){
	clearTimeout ( loopTO );
	loopTO = setTimeout("playNext()", 8000);
}

$(document).ready(function() {
	
	//Setup Cycle
	$('#image-info').cycle({fx:'fade', speed:800, timeout: 0});
	$('#photoSlider').cycle({fx:'scrollLeft', speed:800, timeout: 0});
	
	//Setup jcarousel
    $('#filmstrip').jcarousel({
    	wrap: 'circular',
		scroll: 1,
		animation: 800,
        // This tells jCarousel NOT to autobuild prev/next buttons
        buttonNextHTML: null,
        buttonPrevHTML: null
    });
	
	//Add click functions to all the a tags in the jcarousel
	$("#filmstrip a").each(function(i){
		$(this).click(function() { 
			$('#image-info').cycle(i); 
			$('#photoSlider').cycle(i);
			cLock = true;
			homeReset();
			$(this).parent().addClass("active");
			clearTimeout ( loopTO );
			return false; 
		}); 
	});
	loopTO = setTimeout("playNext()", 6000);
	
});
