$(function(){
	
	$('body').addClass('js-enabled');
	
	$('#stories').gallerify();
	
	Shadowbox.init();

});

$.fn.gallerify = function() {
	return this.each(function(){
		var parentUl = $('>ul',this);
		var stories = $('li',parentUl);

		var switchTo = function(element) {
			$(element).next().fadeIn('fast').parent().addClass('showing').siblings().removeClass('showing').children('.content:visible').fadeOut('fast');
		}

		var swapOut = window.setInterval(function(){
			var index = stories.index($('>.showing',parentUl));
			switchTo(stories.eq((index+1) % stories.length).children('.thumb'));
		},4000);

		stories.each(function(){
			$('>.thumb',this).hover(
				function(){$(this).addClass('over')},
				function(){$(this).removeClass('over')}
			).click(function(){
					switchTo(this);
					window.clearInterval(swapOut);
			});
		});

		$('li:first',parentUl).addClass('showing').siblings('li').children('.content').hide();
	});
}