// JavaScript Document
$(document).ready( function (){
if($('#slideshow').length) { //if the slideshow element exists. Continue with code

//build the array of images within the id slideshow
slide=new Array();
slide = $("#slideshow img").get();

//call function slideshow(X) X can equal either F (forward) or R (reverse)

//show first image in slideshow
$(slide[0]).show();
var caption_string = $(slide[0]).attr("alt");
$("#caption").html("<p class='caption'>" + caption_string + "</p>");

//set global variables
var current_position=0;
var F="F"
var R="R"
var cycle_speed=5000 //slideshow speed in milliseconds


//slideshow function
function slideshow(direction){
	//direction is Forward

	if (direction=="F"){

		if(current_position == (slide.length)-1){
			var next_slide=0;
		}else{
			var next_slide=current_position+1;
		}
	}else if (direction=="R"){
	//direction is Reverse
	if(current_position == 0){
			var next_slide=(slide.length)-1;
		}else{
			var next_slide=current_position-1;
		}
	}
		
	$(slide[next_slide]).css("z-index","200");
	$(slide[next_slide]).fadeIn("slow", function(){
		$(slide[current_position]).hide( function(){
			$(slide[next_slide]).css("z-index","100");
			current_position=next_slide;
			}
		);
	});

	caption_string=$(slide[next_slide]).attr("alt");
	$("#caption").html("<p class='caption'>" + caption_string + "</p>");

}

//start the playhead
play_slideshow = setInterval ( function(){slideshow(F);}, cycle_speed );

//pause
$("#pause_btn").click(function () { 
	$(this).hide();
	$("#play_btn").show();
      clearInterval(play_slideshow);
    });
    
//play
   $("#play_btn").click(function () { 
   $(this).hide();
	$("#pause_btn").show();
      slideshow(F);
      play_slideshow = setInterval ( function(){slideshow(F);}, cycle_speed );
    });
    
//next
$("#nxt_btn").click(function () { 
       clearInterval(play_slideshow);
	   if($("#pause_btn").is(':visible')){
		$("#pause_btn").hide();
		$("#play_btn").show();
	   }
    //  play_slideshow = setInterval ( function(){slideshow(F);}, cycle_speed );
      slideshow(F);
    });
//prev    
$("#prev_btn").click(function () { 
       clearInterval(play_slideshow);
	    if($("#pause_btn").is(':visible')){
		$("#pause_btn").hide();
		$("#play_btn").show();
	   }
     // play_slideshow = setInterval ( function(){slideshow(F);}, cycle_speed );
      slideshow(R);
    });

}//end if
else{
	null;
	
}

});
