//Locks the carousel
var cLock = false;

function mycarousel_initCallback(carousel) {
    jQuery('#barNext').bind('click', function() {
        carousel.next();
		cLock = true;
        return false;
    });
    jQuery('#barPrev').bind('click', function() {
        carousel.prev();
		cLock = true;
        return false;
    });
};

//Remove active class from all the a tags in the jcarousel
function homeReset(){
	$("#barCont li").each(function(){
		$(this).removeClass("active");
	});
}

//Code for loop
var currentSlide = null;
function playNext() {
	if(currentSlide != null && cLock == false){
		//Moves carousel to next button
		jQuery('#barCont').jcarousel('next');
	}
	if(currentSlide==null || currentSlide==$('#barCont').jcarousel('size')){
		playSlide(1);
	}else{
		currentSlide++;
		playSlide(currentSlide);
	}
}
function playSlide(i) { 
	resetTO();
	$('#homeCont').cycle(i); 
	$('#banner-home').cycle(i); 
	homeReset();
	$(".goto"+(i)).parent().addClass("active");
	currentSlide = i;	
}
function resetTO(){
	clearTimeout ( loopTO );
	loopTO = setTimeout("playNext()", 9000);
}

$(document).ready(function() {
	
	//Setup Cycle
	$('#homeCont').cycle({fx:'fade', speed:1000, timeout: 0});
	$('#banner-home').cycle({fx:'scrollLeft', speed:1000, timeout: 0});
	
	//Setup jcarousel
    $('#barCont').jcarousel({
    	wrap: 'circular',
		scroll: 1,
		animation: 1000,
        initCallback: mycarousel_initCallback,
        // This tells jCarousel NOT to autobuild prev/next buttons
        buttonNextHTML: null,
        buttonPrevHTML: null
    });
	
	//Add click functions to all the a tags in the jcarousel
	$("#barCont a").each(function(i){
		$(this).click(function() { 
			$('#homeCont').cycle(i+1); 
			$('#banner-home').cycle(i+1);
			cLock = true;
			homeReset();
			$(this).parent().addClass("active");
			clearTimeout ( loopTO );
			return false; 
		}); 
	});
	loopTO = setTimeout("playNext()", 6000);
	
});
