// bmbSlide is a slideshow built on the Jquery platform. It needs Jquery to work. Adjust any parameters where code comments prompt you.


function bmbSlide($speed,$timeOut,$bgColor){



$(document).ready(function() {

// Create Hover Effects -correct
$("#slideRw,#slidePlay,#slidePause,#slideFf").hover(function(){
	$(this).addClass("hover")},
	function(){
	$(this).removeClass("hover")}
	);


//Declare variables - correct
var $rewind = 0; 
var $paused = 0; // Slideshow Playing by default - 0:Playing - 1:Paused
var $slideImgs = new Array();
var $killClicks = 0;
var $intervalId = 0;
var $imgPosition = $("img.active").index("#slideImgs img");
var $nextActPos = $("img.next-active").index("#slideImgs img");

// Initialise Slideshow once active image is loaded - correct
$("div#slideImgs img.active").one('load', function() {
 

// Find Number of images in SlideShow -correct
var $imgQty = $("div#slideImgs img").length;



			// FUNCTION Get Active Image Position - correct
			function findActive(){
			$imgPosition = $("img.active").index("#slideImgs img");
			};

findActive();			
	
$("#bmbSlideControls").hide();
$("div#bmbSlide").hover(function(){
		$("#bmbSlideControls").fadeIn($speed);
		},
		function(){
		$("#bmbSlideControls").fadeOut($speed);
		});


			
// Change bg color of control bar.			
$("#bmbSlideControls").css({backgroundColor: $bgColor});




// Assign next-active for first time


if(($imgQty - 1) == $imgPosition) {
	var $nextImg = 0;
	}
else { var $nextImg = $imgPosition + 1;
};


// Assign Next Active for the first time.
var $nextCheck = $("div#slideImgs img.active").next().length;
if ($nextCheck == 0){
	$("div#slideImgs img").first().addClass("next-active");
	}
else { $("div#slideImgs img.active").next().addClass("next-active");
	};




			// FUNCTION Get Next Active Image Position - correct
			function findNextAct() {
			$nextActPos = $("img.next-active").index("#slideImgs img");
			};
						
findNextAct();		


			



// FUNCTION Find Next Image Position - 
function findNext(){

findActive();
	if ($rewind == 1){
			if($imgPosition == 0){
					$nextImg = ($imgQty-1);
					}
			else {
					$nextImg = $imgPosition - 1;
					};
		}
	else {
			if($imgPosition == ($imgQty - 1)){
					$nextImg = 0;
					}
			else {
					$nextImg = $imgPosition + 1;
				};
		};
			
		// Find image with next-active class and remove class
		findNextAct();
		$("div#slideImgs img:eq(" + $nextActPos + ")").removeClass("next-active");
		// Find chosen image and add class next active
		$("div#slideImgs img:eq(" + $nextImg + ")").addClass("next-active").css({ opacity: 1 });
		$nextActPos = $nextImg;
};


 function findCaption(){
	$slideLink = $("div#slideImgs img.active").attr("name");  
	var $caption = $("div#slideImgs img.active").attr("alt");
			$("#slideCaption").html($caption);
};

// Fade and Kill Loader
findCaption();
$("div#bmbSlideImgCover").fadeOut($speed,0);


//  Play Pause Toggle
$("#slidePlay").click(function(){
if ($killClicks == 0){
	$(this).hide();
	$("#slidePause").show();
	$paused = 0;
	$rewind = 0;
	clearInterval($intervalId);
	slideLoop();
}else{ return 0;};	});

$("#slidePause").click(function(){
if ($killClicks == 0){
	$(this).hide();
	$("#slidePlay").show();
	$paused = 1;
	clearInterval($intervalId);
}else{ return 0;};	});
	
$("#slideFf").click(function(){
if ($killClicks == 0){
	$paused = 0;
	$rewind = 0;
	$("#slidePlay").hide();
	$("#slidePause").show();
	clearInterval($intervalId);
	bmbSlideSwitch();
	slideLoop();
}else{ return 0;};	});

$("#slideRw").click(function(){
if ($killClicks == 0){
	$paused = 0;
	$rewind = 1;
	$("#slidePlay").hide();
	$("#slidePause").show();
	clearInterval($intervalId);
	bmbSlideSwitch();
	slideLoop();
}else{ return 0;};	});






function bmbSlideSwitch(){
if ($paused == 1){
	return 0;}
else{
	$killClicks = 1;
	findNext();
	$rewind = 0;
	// Add next-active class to next image
	$("div#slideImgs img.active").fadeTo($speed,0,function(){
	$("div#slideImgs img.active").removeClass("active");
	$("div#slideImgs img.next-active").removeClass("next-active").addClass("active");
	$killClicks = 0;
	findCaption();
	});
	};
};






function slideLoop(){
if ($paused == 0){

$(function() {
	$intervalId = setInterval( bmbSlideSwitch, $timeOut );
});
}
else { return 0; };
};

slideLoop();


}).each(function() {
  if(this.complete) $(this).load();
});


});
};

