// rotator script
var timer;
var divsadded = "N";
var lslideimgs = [];

/*This is needed to know which navigator is currently active*/
lslideimgs[0] = "/portals/2/skins/lvedc/images/bannerimage1.jpg";
lslideimgs[1] = "/portals/2/skins/lvedc/images/bannerimage2.jpg";
lslideimgs[2] = "/portals/2/skins/lvedc/images/bannerimage3.jpg";

$(document).ready(function() {		
	
	//Execute the slideShow, set 4 seconds for each images
	slideShow(7000, 0);

});

function rotatorNavClick(idx) {
    clearInterval(timer);
    timer = null;
    $('ul.slideshow').unbind("hover");
    slideShow(3000, 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)) {
            $("#slideshow-caption-navigator div").append("<a href='javascript:rotatorNavClick(" + index + ");' title='next " + "'><img class='slideshownav' src='portals/2/skins/lvedc/images/circle1.gif' class='rotatorimgnav' /></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'));
	    
	    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'));
	   
	    toggleActiveNavigator(0);
	}	
		
	//Display the caption
	$('#slideshow-caption').css({ opacity: 0.7, bottom: 0 });
	$("#slideshow-caption-navigator").css({ opacity: 0.7, bottom: 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() { /*mouse over pauses slideshown*/clearInterval(timer); timer = null; },
	    function() { /*mouse out restarts slideshow*/if (timer == null) { timer = setInterval('gallery()', speed); } }
    );
}

function gallery() {

	//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"); });

	
	//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}, 1000);
	
	//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);
						
	//Get next image caption
	var title = next.find('img').attr('title');	
	var desc = next.find('img').attr('alt');	

			$('#slideshow-caption').animate({bottom:1},1000);	
	});		

	//Hide the current image
	current.animate({ opacity: 0.0 }, 1000).removeClass('show');

	//Update navigation display


}

function toggleActiveNavigator(pvIndex) {
    $("#slideshow-caption-navigator a img").each(function(index) {
        if (index == pvIndex) {
		$(this).addClass("slideshownavactive");
		$(this).attr("src", "portals/2/skins/lvedc/images/circle2.gif");

	}
        else {
		$(this).removeClass("slideshownavactive");
		$(this).attr("src", "portals/2/skins/lvedc/images/circle1.gif");
	}
    });
}
