function theRotator() {
	//Call the rotator function to run the slideshow, 5000 = change to next image after 5 seconds
	rotate_interval = setInterval('rotate()',5000);
}

function rotate() {	
	var current = $('.video_switcher .switcher .selected');
	// Get the next, if it exists; else, go back to the beginning
	var next = ((current.next().length) ? ((current.next().hasClass('selected')) ? $('.video_switcher .switcher .first') :current.next()) : $('.video_switcher .switcher .first'));	

	// Click the next.
	next.click();			
};





/* Copied from frontpage.js */

// jQuery
$(document).ready(function() {
	
	theRotator();

	/* Autofill pre-open tab */
	$.get('/?ajax/vid_stats/li_video/most_recent', function(data){
		$('#tab_most_recent_content ul').html(data);	
	});

	
	/* Add click listener to each tab in video_switcher */
	$(".video_switcher .switcher a").click(function(event){
		event.preventDefault();
		
		if($(this).hasClass('selected'))
		{
			// Already selected. Don't do anything.
		}
		else 
		{
			id = this.getAttribute('id');

			// Change tab selected status
			$('.video_switcher .switcher .selected').removeClass('selected');
			$(this).addClass('selected');
			
			/* Insert JS to change thumbs. */	
			new_id = 'preview_'+id;
			$('.video_switcher .video.current').removeClass('current');
			$('#'+new_id).addClass('current');

		} // End if(hasClass(selected))		
	}); // End tabbed_box click listener
	
	/* Add click listener to each tab in tabbed_box */
	$(".tabbed_box .tabs a").click(function(event){
		event.preventDefault();
		
		if($(this).parent().hasClass('selected'))
		{
			// Already selected. Don't do anything.
		}
		else 
		{
			id = this.getAttribute('id');

			// Change tab selected status
			$('.tabbed_box .tabs .selected').removeClass('selected');
			$(this).parent().addClass('selected');
			
			// Change content selected ('current') status
			this_content = id+'_content';
			$('.tabbed_box  .current').removeClass('current').addClass('notcurrent');
			$('#'+this_content).removeClass('notcurrent').addClass('current');
			
			// If content hasn't been loaded yet, load it.
			stat_cat = id.substr(4);
			
			current_html = $('#'+this_content+' ul').html();
			if((current_html == 'Loading...') || (current_html == ''))
			{
				$.get('/?ajax/vid_stats/li_video/'+stat_cat+'/4', function(data){
					$('#'+this_content+' ul').html(data);	
				});
			}

		} // End if(hasClass(selected))		
	}); // End tabbed_box click listener
	
	$("#popover a").click(function(event){
		event.preventDefault();
		$("#popover").remove();
	});
});