// JavaScript Document

<!-- Adapted from http://jonraasch.com/blog/a-simple-jquery-slideshow -->

$(document).ready(function() {
	
	//hover function to activate caption.  Note that the behavior for IE 7 and under involves the use of a rollup instead of a fade
	$('#slideshow').hover(function(){
			if ($.browser.msie){
				if ($.browser.version < 7) {
					$(this).find('span').animate({bottom:'6px'},{queue:false,duration:ie_showHideDuration});
					//alert("IE 6 Check Active");
				}
				else {
					$(this).find('span').animate({bottom:'8px'},{queue:false,duration:ie_showHideDuration});
					//alert("IE Check Active");
				}
			} else {
				$(this).find('span').animate({opacity: 0.99},{queue:false,duration:showHideDuration});
				//alert("IE Check NOT Active");
			}
			resume = 1;
			clearInterval(intervalVar);
		}, function(){
			if ($.browser.msie){
				$(this).find('span').animate({bottom:'-68px'},{queue:false,duration:ie_showHideDuration});
				//alert("IE Check Active");
			} else {
				$(this).find('span').animate({opacity: 0.0},{queue:false,duration:showHideDuration});
				//alert("IE Check NOT Active");
			}
			startSlideshow();
	});
	
	
	// develop array of slides so that a random ordering can be applied
	slide_count = $("#slideshow li").length - 1;
	
	var i = 0;
	for (i = 0; i <= slide_count; i++)
		{
		slide_array[i] = i;
		}
		
	//apply random if set in base variables	
	if (shuffle == 1) {
	slide_array.sort( randOrd );
	}
	
	//reset first image based on randomized array
	var active = $('#slideshow LI.active');
	var full_list = $('#slideshow LI')
	var new_active = $( full_list[ slide_array[ 0 ] ] );
	new_active.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 0, function() {
            active.removeClass('active last-active');
			});

	//activate slideshow, with runs as it's own closed loop-- only broken by the hover behavior, which restarts the slideshow on hover=false
	startSlideshow();
});


function startSlideshow() {
	
	if (slide_count > 0) 
	{
		//checks for resume value-- if slideshow is resuming after a hover event, shorten the remaining time that the current slide is displayed
		if (resume > 0) {
			intervalVar = setInterval("slideSwitch()", (slideDuration / 4));
		} else {
			intervalVar = setInterval("slideSwitch()", slideDuration);
		}
	}
}


function slideSwitch() {
	//determines next slide by array key-- if at end of array, restarts at the first slide
    if (current_slide_key == slide_count) {
		nextSlide_key = 0;
	} else {
		nextSlide_key = current_slide_key + 1;
	}
	
	//sets active and next as objects that can be referenced by jquery code
	var active = $('#slideshow LI.active');
	var full_list = $('#slideshow LI')
	var next = $( full_list[ slide_array[ nextSlide_key ] ] );
	
	// do the slide swap with animation and class changes
	active.addClass('last-active'); 

	next.css({opacity: 0.0})
		.addClass('active')
		.animate({opacity: 1.0}, transitionDelay, function() {
			active.removeClass('active last-active');
		});

	//set variables for next time through
	current_slide_key = nextSlide_key;
	resume = 0;
	
	//reset the interval and restart the slideshow
	clearInterval(intervalVar);
	startSlideshow();
}

//returns random value, used in setting random order of array
function randOrd(){
	return (Math.round(Math.random())-0.5);
	} 
