// JavaScript Document
//$(document).ready( function (){
							 
	function start_slideshow(){
		$("#close_button").show();
		
		if($('#slideshow_images').length) { //if the slideshow element exists. Continue with code
		
		/* JSON'd images				
		var slide = {
			"images": [
				{"width": "900px", "height": "304px", "source": "images/slideshow_images/slide1.jpg", "title": "Slide One", "caption": "Caption One"},
				{"width": "250px", "height": "150px", "source": "images/slideshow_images/slide2.jpg", "title": "Slide Two", "caption": "Caption Two"},
				{"width": "350px", "height": "250px", "source": "images/slideshow_images/slide3.jpg", "title": "Slide Three", "caption": "Caption Three"}
			]
		};
		//*/
		
		//build the array of images within the id slideshow
		//slide=new Array();
		//slide = $("#slideshow_images img").get();
		//
		
		
		var number_of_slides=slide.images.length;
		
		//call function slideshow(X) X can equal either F (forward) or R (reverse)
		
		//show first image in slideshow
		$("#slideshow_images").html("<img src='" + slide.images[0].source + "' />");
		$("#slideshow_images img").fadeIn();
		//var caption_string = $(slide[0]).attr("alt");
		var caption_string = slide.images[0].caption;
		$("#caption").html("<p class='caption'>" + caption_string + "</p>");
		$(".slide_num").html("1 of " + number_of_slides);
		
		//set global variables
		current_position=0;
		F="F"
		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.images.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.images.length)-1;
				}else{
					var next_slide=current_position-1;
					
				}
			}
				
			//$(slide[next_slide]).css("z-index","200");
			$("#slideshow_images").html("<img src='" + slide.images[next_slide].source + "' />");
			$("#slideshow_images img").hide( function(){
				$("#slideshow_images img").fadeIn("slow", function(){
//					$(slide[next_slide]).css("z-index","100");
					current_position=next_slide;
				
					}
				);
			});
						//caption_string=$(slide[next_slide]).attr("alt");
						caption_string = slide.images[next_slide].caption;
			$("#caption").html(caption_string);
			$(".slide_num").html(next_slide+1 + " of " + number_of_slides);
		
		}
		
		//start the playhead
		///////////////////////
		///////////////////////
		//uncomment to play auto
		//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(event) {
  		event.preventDefault();
			   //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(event) {
  		event.preventDefault();
			  // 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;
			
		}
	
	
	};//end start slideshow function
	
	
	function stop_slideshow(){
		//alert("end");
		/*for(m=0; m < slide.images.length; m++){
			$(slide.images[m]).hide();
		}*/
		$("#slideshow_images img").fadeOut();
		$("#caption").html("");
			$(".slide_num").html("");
			$("#close_button").hide();

		
	}
	
	

//});

