var topStories = {
	slideshowSpeed: 7, // in seconds
	$buttons: $('#top-stories').find('li'),
	$stories: $('#content-left').find('.top-story'),
	currentStory: 0,
	onReady: function() {
		// Show only the first story
		topStories.$stories.hide();
		topStories.highlight(0);
		topStories.$stories.eq(0).show();
		topStories.startTimer();
		
		// Initialize buttons
		topStories.$buttons
			.each(function(i) {
				$(this).data('index', i);
			})
			.hoverIntent(
				function () {
					topStories.stopTimer;
					topStories.highlight($(this).data('index'));
				},
				function () {}
			);
			
		// Play or pause the slideshow when the user hovers over the Top Stories area
		$('.top-story-container, #top-stories').hoverIntent(
			function () {
				topStories.stopTimer();
			},
			function () {
				topStories.startTimer();
			}
		);
	},
	highlight: function(storyNum) {
		if (!topStories.$buttons.eq(storyNum).hasClass('active')) {
			// Highlight the current button
			topStories.$buttons.filter('.active').removeClass('active');
			topStories.$buttons.eq(storyNum).addClass('active');
			
			// Highlight the current story
			topStories.$stories.filter('.active').fadeOut('fast').removeClass('active');
			topStories.$stories.eq(storyNum).fadeIn('fast').addClass('active');
			topStories.currentStory = storyNum;
		}
	},
	startTimer: function() {
		$('#top-stories').everyTime(topStories.slideshowSpeed * 1000, function() {
			nextStory = (topStories.currentStory < 3) ? topStories.currentStory + 1 : 0;
			topStories.highlight(nextStory);	
		});
	},
	stopTimer: function () {
		$("#top-stories").stopTime();
	}	
}

topStories.onReady();
