$(document).ready(function() {

	$('article span a[rel]').click(function() {
		var rel=$(this).attr('rel');
		var $article=$(this).closest('article');
		var $focused=$article.find('div img.focused');
		if (!$focused.length) $focused=$article.find('img').eq(0);
		var $new=rel=='next'?$focused.next():$focused.prev();
		if (!$new.length) {
			console.log('different row');
			$article.find('div').animate({left:0}).find('img').removeClass('focused').eq(0).addClass('focused');
				
			if (rel=='next') {
				$new=$article.next().find('img').eq(0);
			}

			if (rel=='prev') {
				$new=$article.prev().find('img').eq(0);
			}

			if ($new.length) {
				$.scrollTo($new,{
					margin:true,
					duration:300,
					axis:'y'
				});
			}
		}
		
		if ($new.length) $new.click();
	});

	$('article.noimages').find('>span').remove().end().find('div').remove();

	$('article div img').click(function() {
		var $t=$(this);
		$t.siblings().removeClass('focused').end().addClass('focused')
			.parent().stop().animate({left:-$t.position().left});
	}).eq(0).addClass('focused');

});
