(function($){
	$.fn.slideshow = function(options){
		var $this = this;
		var s = -1;
		var sto = false;
		var $slides = [];
		var intervals = [];
		var playing = false;
		var settings = $.extend({
			'delay':7000,
			'transition':500,
			'autoplay':true
		},options);
		function init(){
			$this.find('.slides .slide').each(function(n){
				if($(this).find('img').length>0){
					$slides[n] = $(this);
					$slides[n].find('img').first().data('n',n);
					intervals[n] = setInterval(load,500);
				}
			});
			$this.find('.arrow.left').bind('click',function(ev){ev.preventDefault();previous();});
			$this.find('.arrow.right').bind('click',function(ev){ev.preventDefault();next();});
			$(window).bind('resize',resize);
		}
		function load(){
			for(var i=0;i<$slides.length;i++){
				$img = $slides[i].find('img');
				if($img.width()>0){
					$img.data('width',$img.width());
					$img.data('height',$img.height());
					clearInterval(intervals[parseInt($img.data('n'),10)]);
					intervals[parseInt($img.data('n'),10)] = false;
					var $caption = $slides[i].find('.caption').first();
					var position = {top:'auto',right:'auto',bottom:'auto',left:'auto'};
					if($caption.attr('data-position-top')) position.top = $caption.attr('data-position-top');
					if($caption.attr('data-position-right')) position.right = $caption.attr('data-position-right');
					if($caption.attr('data-position-bottom')) position.bottom = $caption.attr('data-position-bottom');
					if($caption.attr('data-position-left')) position.left = $caption.attr('data-position-left');
					$caption.css(position);
					$slides[i].hide();
					$slides[i].css({'top':0,'left':0});
				}
			}
			resize();
			if(intervals[0]==false&&!playing&&settings.autoplay){
				if($slides.length>1) playing = true;
				next();
			}
		}
		function previous(){
			var upcoming = $slides[(s-1+$slides.length)%$slides.length];
			if(upcoming.find('img').first().data('width')&&upcoming.find('img').first().data('width')>0){
				s = (s-1+$slides.length)%$slides.length;
				show(s);
				if(playing) sto = setTimeout(next,settings.delay);
			}else{
				clearTimeout(sto);
				sto = setTimeout(previous,1000);
			}
		}
		function next(){
			var upcoming = $slides[(s+1)%$slides.length];
			if(upcoming.find('img').first().data('width')&&upcoming.find('img').first().data('width')>0){
				s = (s+1)%$slides.length;
				show(s);
				if(playing) sto = setTimeout(next,settings.delay);
			}else{
				clearTimeout(sto);
				sto = setTimeout(next,1000);
			}
		}
		function resize(){
			//$this.css('height',$(window).height()+'px');
			$this.find('.slides .slide').each(function(){
				var $img = $(this).find('img').first();
				if($img.data('width')>0){
					var ratio = $img.data('width')/$img.data('height');
					var w = $this.width();
					var h = w/ratio;
					if(h<$this.height()){
						h = $this.height();
						w = h*ratio;
					}
					$img.css({width:w+'px',height:h+'px',left:Math.round(($this.width()-w)/2)+'px',top:Math.round(($this.height()-h)/2)+'px'});
				}
			});
		}
		function show(n){
			if(sto!==false) clearTimeout(sto);
			sto = false;
			for(var i=0;i<$slides.length;i++){
				if(i==n){
					$slides[i].hide();
					$slides[i].css('z-index',10);
				}else if($slides[i].css('z-index')>2){
					$slides[i].css('z-index',2);
				}else{
					$slides[i].css('z-index',1);
				}
			}
			$slides[n].fadeIn(settings.transition);
			$('#wrapper').removeClass('light dark');
			if($slides[n].attr('data-theme')=='light') $('#wrapper').addClass('light');
			if($slides[n].attr('data-theme')=='dark') $('#wrapper').addClass('dark');
		}
		init();
	}
})(jQuery);

$(function(){
	$('#slideshow').slideshow({
		delay:10000,
		transition:1000
	});
	$('#press dt a').click(function(ev){
		//ev.preventDefault();
		$('#press dl#featured').html('');
		var e = $(this).parent().parent().clone();;
		$('#press dl#featured').append(e);
		e = $(this).parent().parent().next().clone();
		$('#press dl#featured').append(e);
	});	
	$('#press dt a').first().click();
	$('#books dt a').click(function(ev){
		//ev.preventDefault();
		$('#books dl#featured').html('');
		var e = $(this).parent().parent().clone();;
		$('#books dl#featured').append(e);
		e = $(this).parent().parent().next().clone();
		$('#books dl#featured').append(e);
	});
	$('#books dt a').first().click();
});


/*
var slides = [];
var s = -1;
var delay = 10000;
var fade = 800;
var sto = false;
function startSlides(){
	$('.slideshow .slides .slide img').each(function(n){
		$(this).data('n',n);
		slides[slides.length] = this;
		$(this).data('ratio',parseFloat($(this).attr('src').substring($(this).attr('src').indexOf('-')+1,$(this).attr('src').lastIndexOf('.'))));
	});
	$('.slideshow .arrow.left').click(function(){
		prevSlide();
	});
	$('.slideshow .arrow.right').click(function(){
		nextSlide();
	});
	nextSlide();
}
function showSlide(n){
	if(sto!==false) clearTimeout(sto);
	sto = false;
	for(var i=0;i<slides.length;i++){
		if(i==n){
			$(slides[i]).hide();
			$(slides[i]).css('z-index',10);
			$('.slideshow .captions .caption').eq(i).fadeIn(fade);
		}else if($(slides[i]).css('z-index')>2){
			$(slides[i]).css('z-index',2);
			$('.slideshow .captions .caption').eq(i).fadeOut(fade);
		}else{
			$(slides[i]).css('z-index',1);
		}
	}
	$(slides[n]).fadeIn(fade);
}function nextSlide(){
	s = (s+1)%slides.length;
	showSlide(s);
	sto = setTimeout(nextSlide,delay);
}
function prevSlide(){
	s = (s-1+slides.length)%slides.length;
	showSlide(s);
	sto = setTimeout(nextSlide,delay);
}
*/
