$(document).ready(function(){

	var billboard_timer = null;
	billboardPrep();
	setTimeout(function() {
		billboardLoop(); 
	}, 10000);
});

function billboardLoop() {
	
	if($("#billboard").length > 1) {
		var workshop_count = $("#workshops li").length;
		var workshop_count_limit = (workshop_count - 1) * 320;
		var workshop_left_pos = $("#workshops ul").css('left');
		workshop_left_pos = workshop_left_pos.split("px");
		 
		if(workshop_left_pos[0] === String('-'+workshop_count_limit)) {
			
			$("#workshops ul").animate({ left: '0px' }, 1000);
		
		} else {
			
			$("#workshops ul").animate({ left: (workshop_left_pos[0] - 320)+'px' }, 1000);
			
		}
		
		var active_nav = $('#workshops-nav li.active');
		
		var next_nav = active_nav.next().length ? active_nav.next() : $('#workshops-nav li:first');
		
		next_nav.addClass('active');
    	active_nav.removeClass('active');
		
    	billboard_timer = setTimeout( "billboardLoop()", 10000 );
	}

}

function billboardPrep() {

	var width = $("#workshops ul li").length * 320;
	$("#workshops ul").css({ width: width });
	
	if($("#billboard").length > 1) {
		$("#billboard")
		  .mouseover(function(){
			clearTimeout(billboard_timer);
			$("#workshops-paused").show();	
		}).mouseout(function(){
			billboard_timer = setTimeout( "billboardLoop()", 10000);
			$("#workshops-paused").hide();
		});
		
		
		$("#workshops-nav a").click(function(){
			clearTimeout(billboard_timer);
			$("#workshops li").stop(true, true);
			
			var parent = $(this).parent("li");
			
			if(!parent.hasClass("active")) {
				var active_billboard = $("#workshops li.active");
				var active_nav = $("#workshops-nav li.active");
				var number = $(this).text();
				
				var selected = (number - 1) * 320;
				
				parent.addClass('active');
    			active_nav.removeClass('active');
				
				if(selected === 0) {
			
					$("#workshops ul").animate({ left: '0px' }, 1000);
				
				} else {
					
					$("#workshops ul").animate({ left: '-'+selected+'px' }, 1000);
					
				}
				
    	    }
			
			return false;
		});
	}
        
}
