/* Image slideshow
 * Hacked together by Brian Bernas from various slideshow examples.
 * 2010-2011
 * Version 0.1
 */

var pause=5000; // 5 seconds
var playSlideshow = true;
var activeShow = null;

function bslideImgFade(loadImg)
{
	var $active = $('#slideshow img.active');

	if ($active.length == 0) {
		$active = $('#slideshow img:last');
	}

	var $next;
	
	if (loadImg != null) {
		$next = $("#slideshow img:eq("+loadImg+")");
	} else {
		$next = $active.next().length ? $active.next() :
			$('#slideshow img:first');
	}

	if ($active.index() == $next.index()) return;

	$active.addClass('last-active');

	$next.css('display', 'block');
	$next.css({opacity: 0.0});
	$next.addClass('active');
	$next.animate({opacity: 1.0}, 1000, function() {
		$active.removeClass('active last-active');
		$active.css({opacity: 0.0});
		$active.css('display', 'none');
		});
}

function bslideOnImg(loadImg)
{
	if (activeShow) {
		clearInterval(activeShow);
		activeShow = null;
		playSlideshow = false;
	}
	bslideImgFade(loadImg-1);
	
	bslideTogglePlayButton();
}

function bslideTogglePlayButton()
{
	var $play = $('#thumb-nav-left .nav-play-pause a');
	
	if ($play.hasClass('nav-play')) {
		$play.removeClass('nav-play');
	}
	if ($play.hasClass('nav-pause')) {
		$play.removeClass('nav-pause');
	}
	
	if (playSlideshow) {
		// Display a Pause Button
		$play.addClass('nav-pause');
	} else {
		// Display the Play Button
		$play.addClass('nav-play');
	}
		
}

function bslideToggleShow()
{
	if (activeShow) {
		clearInterval(activeShow);
		activeShow = null;
		playSlideshow = false;
	} else {
		activeShow = setInterval("bslideImgFade()", pause);
		playSlideshow = true;
	}
	
	bslideTogglePlayButton();
}

function bslideSetThumbnailWidth()
{
	var $thumb = $('#thumbnails-list li');
	var twidth = 0;
	
	while ($thumb.length) {
		twidth = twidth + $thumb.width();
		
		$thumb = $thumb.next();
	}
	
	$('#thumbnails-list').css('width', twidth);
}

function bslideShowThumbnails()
{
	var $thumbs = $("#thumbnails");

	$thumbs.animate({top: 0}, 300);
}

function bslideHideThumbnails()
{
	var $thumbs = $("#thumbnails");

	$thumbs.animate({top: 200}, 300);
}

function bslideThumbsLeft()
{
	var $thumbs = $("#thumbnails-list");
	var thumbAdv = ($(document).width()-250);

	if (thumbAdv < 200) thumbAdv = 200;

	var tleft = ($thumbs.position().left+thumbAdv);
	
	if (tleft > 110) tleft = 110;
	
	$thumbs.animate({left: tleft}, 300);
}
	
function bslideThumbsRight()
{
	var $thumbs = $("#thumbnails-list");
	var dwidth = (-($thumbs.width()+50) + $(document).width());
	var thumbAdv = ($(document).width()-250);
	
	var tleft = ($thumbs.position().left-thumbAdv);
	
	if (tleft < dwidth) tleft = dwidth;
	
	$thumbs.animate({left: tleft}, 300);
}

function bslideStartShow()
{
	var currentURL = document.location.href;
	var targetImg = parseInt(currentURL.split("#")[1]);
	var currentImg = null;

	// Initialize the Width of the Thumbnail Row
	$('#thumbnail-div').removeClass('hide');
	bslideSetThumbnailWidth();

	// Determine which image to display first
	if (targetImg) {
		currentImg = $("#slideshow img:eq("+(targetImg-1)+")");
		playSlideshow = false; // Assume that the user only wants this image
	} else {
		currentImg = $("#slideshow img:first");
	}

	if (currentImg) {
		currentImg.addClass('active');
		if (playSlideshow) {
			activeShow = setInterval("bslideImgFade()", pause);
		}
	}

	// Give the user a clue on how to use the site
	// Display the thumbnail row for 2 seconds then hide it.
	setTimeout("bslideHideThumbnails()", 2000);
	
	$("#thumbnail-div").hover(bslideShowThumbnails, bslideHideThumbnails);
	
	bslideTogglePlayButton();
}
