var timer;
var speed = 10000;
var numBkgd = 5;

$(document).ready(function() {
    slideShow(speed);
});

function slideShow(speed) {

    //Set the opacity of all images to 0
    $('ul.slideshow li').css({ opacity: 0.0 });
    
	$('ul.slideshow').children('li').each(function(j) { 
		var k=j+1;
		$('div.slideshow-numbers').append("<a href='#' rel='"+k+"' onclick='go("+k+")');>"+k+"</a> ");
	});
		
	//Get the first image and display it (set it to full opacity)
	$('ul.slideshow li:first').animate({opacity: 1.0}, 2000);
	
	//first image
	$('div.slideshow-numbers a[rel="1"]').addClass('on');

	var captionInfo = $('ul.slideshow li:first div').html();
	$('#home-slideshow-info-caption').addClass('home-slideshow-caption-bkgd-1');
	$('#home-slideshow-info-caption').html(captionInfo);	
	
	/*$('div.home-slideshow-info').animate({bottom:10}, 800);*/
	//$("div.home-slideshow-info").animate({"top": "-=165px"}, "slow");
	//$("#home-slideshow-info-caption").slideToggle("slow");
	$("#home-slideshow-info-caption").show('blind', "slow");
	$('ul.slideshow li:first').addClass("show");
	
	
	//Call the gallery function to run the slideshow	
	timer = setInterval('gallery()',speed);
	
	
	//pause the slideshow on mouse over
/*	$('ul.slideshow').hover(
		function () {
			clearInterval(timer);	
		}, 	
		function () {
			timer = setInterval('gallery()',speed);
		}
	);
*/	
}

function gallery() {
    	//$('ul.slideshow').css({ 'background': '#464843' });
	//if no IMGs have the show class, grab the first image
	var current = ($('ul.slideshow li.show')?  $('ul.slideshow li.show') : $('#ul.slideshow 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')? $('ul.slideshow li:first') :current.next()) : $('ul.slideshow li:first'));
	
	showSlide(next);
}

function showSlide(next){
	//if no IMGs have the show class, grab the first image
	var current = ($('ul.slideshow li.show')?  $('ul.slideshow li.show') : $('#ul.slideshow li:first'));

	//Get next image caption
	var captionInfo = next.find('div').html();
	var nextpageNum = next.find('a').attr('rel');
	
	//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}, 2000);
	
	//Hide the caption first, and then set and display the caption
	//$('div.home-slideshow-info').animate({"top": "+=165px"}, "slow");
	/*	$("#home-slideshow-info-caption").slideToggle("slow", function(){
				$('#home-slideshow-info-caption').removeClass(); // num of backgrounds = 5
				$('#home-slideshow-info-caption').addClass('home-slideshow-caption-bkgd-'+(nextpageNum % 5));
				$('#home-slideshow-info-caption').html(captionInfo);				
				//$('div.home-slideshow-info').animate({bottom:10}, 800);
				//$("div.home-slideshow-info").animate({"top": "-=165px"}, "slow");
				$("#home-slideshow-info-caption").slideToggle("slow");
				$('div.slideshow-numbers a').removeClass('on');
				$('div.slideshow-numbers a[rel='+nextpageNum+']').addClass('on');
		});
	*/

	$("#home-slideshow-info-caption").toggle('blind', "slow", function(){
		$('#home-slideshow-info-caption').removeClass(); 
		$('#home-slideshow-info-caption').addClass('home-slideshow-caption-bkgd-'+(nextpageNum % numBkgd));
		$('#home-slideshow-info-caption').html(captionInfo);				
		$("#home-slideshow-info-caption").toggle('blind', "slow");
		$('div.slideshow-numbers a').removeClass('on');
		$('div.slideshow-numbers a[rel='+nextpageNum+']').addClass('on');
	});	

	
	//Hide the current image
	current.animate({opacity: 0.0}, 2000).removeClass('show');

}

function previous() {
	var current = ($('ul.slideshow li.show')?  $('ul.slideshow li.show') : $('#ul.slideshow li:first'));
	var prev = ((current.prev().length) ? ((current.prev().attr('id') == 'slideshow-caption')? $('ul.slideshow li:last') :current.prev()) : $('ul.slideshow li:last'));
	showSlide(prev);
	clearInterval(timer);	
	timer = setInterval('gallery()',speed);
}

function next(){
	clearInterval(timer);
	gallery();
	timer = setInterval('gallery()',speed);
}

function go(num){
	clearInterval(timer);
	var next = $('ul.slideshow li a[rel="'+num+'"]').parent();
	showSlide(next);
	timer = setInterval('gallery()',speed);
}
