var index = 0;
var maxindex=0;
var tempo = 6000;

var animating = true;

function next(){
//	$("#slideshow img:eq("+index+")").fadeOut( {duration: 700, easing: 'easeOutSine', complete:

	if( animating ) { // something was already fading
		$("#slideshow img:eq("+(index)%maxindex+")").stop(true,true);
		$("#slideshow img").hide();
	}
	animating=true;
	$("#slideshow img:eq("+index+")").fadeOut( {duration: 700, easing: 'easeOutSine', complete:
			function(){
				index++; index=index%maxindex;
				$("#slideshow img:eq("+index+")").fadeIn({duration: 700, easing: 'easeInSine', complete: function(){ animating=false; }});
				$('#photo_description').html("<h1>"+$("#slideshow img:eq("+index+")").attr('title')+"</h1>"+$("#slideshow img:eq("+index+")").attr('alt'));
			}
		}
	);
	// program the next automated event
	$(document).stopTime("slideshow");
	$(document).oneTime(tempo, "slideshow", function(i){
		next();
	},0);
}

function previous(){
	if( animating ) { // something was already fading
		$("#slideshow img:eq("+(index)%maxindex+")").stop(true,true);
		$("#slideshow img").hide();
	}
	animating=true;
	$("#slideshow img:eq("+index+")").fadeOut( {duration: 700, easing: 'easeOutSine', complete:
//	$("#slideshow img:eq("+index+")").fadeOut(1000,
		function(){
			index--; index=(index+maxindex)%maxindex;
			$("#slideshow img:eq("+index+")").fadeIn({duration: 700, easing: 'easeInSine', complete:function(){animating=false;}}); 
			$('#photo_description').html("<h1>"+$("#slideshow img:eq("+index+")").attr('title')+"</h1>"+$("#slideshow img:eq("+index+")").attr('alt'));
		}
	});
	// program the next automated event
	$(document).stopTime("slideshow");
	$(document).oneTime(tempo, "slideshow", function(i){
		next();
	},0);
}

$(document).ready(function(){
	// setting up the automated passing of pictures
	
	maxindex = $('#slideshow img:not(.prev,.next)').size();
	if(maxindex>0){
		$('#slideshow img:not(.prev,.next)').hide();

		$('#prev,#next').css('opacity','0');
		$('#prev,#next').css('-moz-opacity','0');

		$("#prev").click(
			function(){
				previous();
			}
		);

		$("#next").click(
			function(){
				next();
			}
		);
	} // end if

	$("#slideshow").hover(function(){
		$('#prev,#next').css('opacity','.5');
		$('#prev,#next').css('-moz-opacity','.5');
		$("#photo_description").fadeIn(250);
	}, function(){
		$('#prev,#next').css('opacity','0');
		$('#prev,#next').css('-moz-opacity','0');
		$("#photo_description").fadeOut(250);
	});
	$("#prev,#next").hover(function(){
		$('#prev,#next').css('opacity','1');
		$('#prev,#next').css('-moz-opacity','1');
	}, function(){
		$('#prev,#next').css('opacity','0');
		$('#prev,#next').css('-moz-opacity','0');
	});

	$('#slideshow img:not(.prev,.next):first').load(function(){
		$('#slideshow img:not(.prev,.next):first').fadeIn(600);
		$('#photo_description').html("<h1>"+$("#slideshow img:eq("+index+")").attr('title')+"</h1>"+$("#slideshow img:eq("+index+")").attr('alt'));
	});
});

$(document).load(function(){
	$(document).oneTime(tempo-1000, "slideshow", function(){
		next();
	},0);
});

$(window).keydown(function(event){
  switch (event.keyCode) {
	case 37: // left arrow
		previous();
		break;
	case 39: // right arrow
		next();
		break;
  }
});