//http://www.queness.com/post/1450/jquery-photo-slide-show-with-slick-caption-tutorial-revisited

//20110915 - dk modified to support captions pulled from divs
//20111017 - dk modified to support multiple slideshows on one page via ID

function slideShow(iSlideshowCnt, speed, captionspeed) {

	//append a LI item to the UL list for displaying caption
	$('ul#slideshow_' + iSlideshowCnt).append('<li class="slideshow-caption" id="slideshow-caption_' + iSlideshowCnt + '" class="caption"><div class="slideshow-caption-container"><h3></h3><p></p></div></li>');

	//Set the opacity of all images to 0
	$('ul#slideshow_' + iSlideshowCnt + ' li').css({opacity: 0.0});
	
	//Get the first image and display it (set it to full opacity)
	$('ul#slideshow_' + iSlideshowCnt + ' li:first').css({opacity: 1.0});
	
	//Get the caption of the first image from REL attribute and display it
	//$('#slideshow-caption_' + iSlideshowCnt h3').html($('ul#slideshow_' + iSlideshowCnt + ' a:first').find('img').attr('title'));
	//$('#slideshow-caption_' + iSlideshowCnt p').html($('ul#slideshow_' + iSlideshowCnt + ' a:first').find('img').attr('alt'));

//Get next image caption
var title = $('ul#slideshow_' + iSlideshowCnt + ' a:first').find('img').attr('title');
var desc = $('ul#slideshow_' + iSlideshowCnt + ' a:first').find('img').attr('alt');

//Get caption div (if any)
var sTemp = title.toLowerCase();
var iPos = sTemp.indexOf("@cap");
if (iPos >= 0) {
	var iImageCnt = title.substr(iPos+4, 99);
	title = "";
	desc = document.getElementById('divSlideshowCaption' + iImageCnt).innerHTML;
}

if ((title) ||(desc)) {
	$('#slideshow-caption_' + iSlideshowCnt + ' h3').html(title);
	$('#slideshow-caption_' + iSlideshowCnt + ' p').html(desc);

	//Display the caption
	$('#slideshow-caption_' + iSlideshowCnt).css({opacity: 0.9, bottom:0});
}

	//Call the gallery function to run the slideshow	
	var timer = setInterval("gallery('"+ iSlideshowCnt + "','" + captionspeed + "')", speed);	
	
	//pause the slideshow on mouse over
	$('ul#slideshow_' + iSlideshowCnt + '').hover(
		function () {
			clearInterval(timer);	
		}, 	
		function () {
			//timer = setInterval('gallery(' + captionspeed + ')',speed);
			timer = setInterval("gallery('"+ iSlideshowCnt + "','" + captionspeed + "')", speed);	
		}
	);
	
}

function gallery(iSlideshowCnt, captionspeed) {

	//if no IMGs have the show class, grab the first image
	var current = ($('ul#slideshow_' + iSlideshowCnt + ' li.show')?  $('ul#slideshow_' + iSlideshowCnt + ' li.show') : $('#ul#slideshow_' + iSlideshowCnt + ' li:first'));

	//Get next image, if it reached the end of the slideshow, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().attr('id') == 'slideshow-caption_' + iSlideshowCnt)? $('ul#slideshow_' + iSlideshowCnt + ' li:first') :current.next()) : $('ul#slideshow_' + iSlideshowCnt + ' li:first'));
		
	//Get next image caption
	var title = next.find('img').attr('title');	
	var desc = next.find('img').attr('alt');	

//Get caption div (if any)
var sTemp = title.toLowerCase();
var iPos = sTemp.indexOf("@cap");
if (iPos >= 0) {
	var iImageCnt = title.substr(iPos+4, 99);
	title = "";
	desc = document.getElementById('divSlideshowCaption' + iImageCnt).innerHTML;
}

	//Set the fade in effect for the next image, show class has higher z-index
	next.css({opacity: 0.0}).addClass('show').animate({opacity: 1.0}, 1000);
	
if ((title) || (desc)) {
	//Hide the caption first, and then set and display the caption
	$('#slideshow-caption_' + iSlideshowCnt).slideToggle(captionspeed, function () { 
		$('#slideshow-caption_' + iSlideshowCnt + ' h3').html(title); 
		$('#slideshow-caption_' + iSlideshowCnt + ' p').html(desc); 
		$('#slideshow-caption_' + iSlideshowCnt).slideToggle(captionspeed); 
	});		
}

	//Hide the current image
	current.animate({opacity: 0.0}, 1000).removeClass('show');

}
