// HOME SLIDESHOW JAVASCRIPT (w/ jQuery)

var activeSlide = 1;			// track current slide
var numSlides = 0;
var sliderInterval = null;
var clickable = true;			// flag to disable clicking during transition

$(document).ready(function() {


	var i = 1;
	$('.slideshowItem').each(function() {
		// hide all slides but the first one
		if(i != 1) {
			$(this).hide();
		}
		i++;
	});
	numSlides = i-1;

	var j = 1;
	$('#slideshowSelectBox a').each(function() {
		// assign id
		$(this).attr('buttonnumber', j);
		// highlight first dot
		if(j == 1) $(this).addClass('slideshowActiveButton');
		else $(this).addClass('slideshowButtonOff');
		// click action
		$(this).click(function() {
			clearInterval(sliderInterval);
			ChangeToSlide($(this));
			/*
			if(clickable) {
				clickable = false;
				$('#slideshowSelectBox a').each(function() {
					$(this).addClass('slideshowButtonOff');
					$(this).removeClass('slideshowActiveButton');
				});	
				$(this).addClass('slideshowActiveButton');				
				var newslide = $(this).attr('buttonnumber');
				// fade out current slide
				$('#slideshowItem'+activeSlide).fadeOut(function() {
					// fade in new slide
					$('#slideshowItem'+newslide).fadeIn(function() {
						clickable = true;
					});
				});
				activeSlide = $(this).attr('buttonnumber');
			}
			*/
		});
		j++;
	});

	//Rotate the slides
	if (numSlides > 0) {
		sliderInterval = setInterval(function(){
			var newbutton = ((activeSlide % numSlides) + 1);
			/*
			$('a[buttonnumber="' + newbutton + '"]').click();
			*/
			ChangeToSlide($('a[buttonnumber="' + newbutton + '"]'));
		}, 10000);
	}

});

/**
* Change to the slide presented by clicking on a link
* Pass in a jQuery instance of the link clicked on
*/
function ChangeToSlide($linkclicked){
	if(clickable) {
		clickable = false;
		$('#slideshowSelectBox a').each(function() {
			$(this).addClass('slideshowButtonOff');
			$(this).removeClass('slideshowActiveButton');
		});	
		$linkclicked.addClass('slideshowActiveButton');				
		var newslide = $linkclicked.attr('buttonnumber');
		// fade out current slide
		$('#slideshowItem'+activeSlide).fadeOut(function() {
			// fade in new slide
			$('#slideshowItem'+newslide).fadeIn(function() {
				clickable = true;
			});
		});
		activeSlide = $linkclicked.attr('buttonnumber');
	}
	
}
