// rotator script
var timer=0;
var divsadded = "N";
var lslideimgs = [];
var pauseGallery="N";

$(document).ready(function() {		
	
	//Execute the slideShow, set 4 seconds for each images
	slideShow(8000, 0);
	//slideShow(7000, 0);


});

function rotatorNavClick(idx) {
    //clearInterval(timer);

//    timer = null;
pauseGallery="Y";
    $('ul.slideshow').unbind("hover");
   // slideShow(3000, idx);
    slideShow(8000, idx);
}

function slideShow(speed, gallerySeed) {

    //this allows for seeding the image rotator and marking the proper one to init later
    var seeded = "N";
    $("ul.slideshow li").each(function(index) {
        if (gallerySeed >= 0) {
            $(this).removeClass("show");
            if (index == gallerySeed) { $(this).addClass("show"); seeded = "Y"; }
        }
        if (divsadded == "N") { lslideimgs[index] = $(this).find("img").attr("src"); }
    });
    
    if (divsadded == "N") {
        //append a LI item to the UL list for displaying caption
        $('ul.slideshow').append('<li id="slideshow-caption" class="caption"><div class="slideshow-caption-container"><h3></h3><p></p></div></li>');

        //append a LI item to the UL list for displaying caption
        $("ul.slideshow").append("<li id=\"slideshow-caption-navigator\"><div style=\"text-align:right;\"></div></li>");

        //do not add them again
        divsadded = "Y";
    }	

    //this adds all the custom navigators
    var numimages = $("ul.slideshow li").length;
    $("#slideshow-caption-navigator div").html("");
    $("ul.slideshow li").each(function(index) {
        if (index < (numimages - 2)) {
        	var limagesrc = $("img", $(this)).attr("src");
            $("#slideshow-caption-navigator div").append("<a href='javascript:rotatorNavClick(" + index + ");' title='image " + (index + 1) + "'><img class='slideshownav' src='" + limagesrc.replace(".jpg", "thumb.jpg")  + "' /></a>&nbsp;&nbsp;");
        }
    });

    //bind mouse over events to the images
    $("#slideshow-caption-navigator a img").hover(function() { $(this).addClass("slideshownavhover"); }, function() { $(this).removeClass("slideshownavhover"); });

	//Set the opacity of all images to 0
	$('ul.slideshow li').css({opacity: 0.0});

	if (seeded == "Y") {
	    //Get the first image and display it (set it to full opacity)
	    $('ul.slideshow li.show').css({ opacity: 1.0 });

	    //Get the caption of the first image from REL attribute and display it
	    $('#slideshow-caption h3').html($('ul.slideshow li.show img').attr('title'));
	    $('#slideshow-caption p').html($('ul.slideshow li.show img').attr('alt'));
	    toggleActiveNavigator(gallerySeed);
	}
	else {
	    //Get the first image and display it (set it to full opacity)
	    $('ul.slideshow li:first').css({ opacity: 1.0 });

	    //Get the caption of the first image from REL attribute and display it
	    $('#slideshow-caption h3').html($('ul.slideshow img:first').attr('title'));
	    $('#slideshow-caption p').html($('ul.slideshow img:first').attr('alt'));
	    toggleActiveNavigator(0);
	}	
		
	//Display the caption
	$('#slideshow-caption').css({ opacity: 1.0, bottom: 0 });
	$("#slideshow-caption-navigator").css({ opacity: 1.0, bottom: 0 });
if (timer ==0){
	//Call the gallery function to run the slideshow
	clearInterval(timer);
	timer = setInterval('gallery()', speed);
}
	//pause the slideshow on mouse over
	$('ul.slideshow').hover(
	    function() { pauseGallery="Y";},
	    function() { pauseGallery="N"; }
	
    );
}

function gallery() {

if (pauseGallery == "N"){
	//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'));

    //Toggle the currently active navigator
    var looper;
    for (looper = 0; looper < lslideimgs.length; looper++) {
        if (next.find("img").attr("src") == lslideimgs[looper]) { break; }
    }
    toggleActiveNavigator(looper);    

    //bind mouse over events to the images
    $("#slideshow-caption-navigator a img").hover(function() { $(this).addClass("slideshownavhover"); }, function() { $(this).removeClass("slideshownavhover"); });

	
	//Get next image caption
	var title = next.find('img').attr('title');	
	var desc = next.find('img').attr('alt');	

	//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
	$('#slideshow-caption').animate({bottom:-70}, 100, function () {
			//Display the content
			$('#slideshow-caption h3').html(title);
			$('#slideshow-caption p').html(desc);				
			$('#slideshow-caption').animate({bottom:0}, 1000);	
	});		

	//Hide the current image
	current.animate({ opacity: 0.0 }, 2000).removeClass('show');

	//Update navigation display
}

}

function toggleActiveNavigator(pvIndex) {
    $("#slideshow-caption-navigator a img").each(function(index) {
        if (index == pvIndex) { $(this).addClass("slideshownavactive"); }
        else { $(this).removeClass("slideshownavactive"); }
    });
}
