/**

Javascript Image Slideshow Script
Created by Aaron Montecalvo

**/


slideshowImage = "#promoImageSlideshow";
slideshowLink = "#promoImageLink";
slideshowController = "#slideshowController";
slideshowContainer = "#promoImageSlideshowContainer";
pauseButton = "#slideshowPauseButton";
previousButton = "#slideshowPreviousButton";
nextButton = "#slideshowNextButton";
pauseSrc = "";

fadeSpeed = 500;
imageVisible = 7000;
currentImage = 0;
running = true;
direction = "foward"
var slideshowImages = new Array();
var slideshowAltText = new Array();
var slideshowImageMap = new Array();
var slideshowURL = new Array();
var slideshowEnabled = false;
loopID1 = null;
loopID2 = null;
loopID3 = null;

function slideshowSetup() {
    i = 0;
    $(slideshowContainer + " img").each(function (index) {
        if ("#" + $(this).attr('id') != slideshowImage) {
            slideshowImages[i] = $(this).attr('src');
            slideshowAltText[i] = $(this).attr('alt');
            slideshowImageMap[i] = $(this).attr('usemap');
            slideshowURL[i] = $(this).attr('title');
            $(this).css({
                "display": "none"
            });
            i++;
        }
    });
	// if there is at least 1 image in the slideshow
    if(i > 0){
        slideshowEnabled = true;
        pauseSrc = $(pauseButton).attr('src');
        setImage();
        resetSlideshowControls();
		$(slideshowController).css({
        "display": "inline",
		});
		setTimeout("slideshowFadein()", 1);
    }
}

function resetSlideshowControls(){
    if(slideshowEnabled){
        var pos = $(slideshowImage).offset();
        var width = $(slideshowImage).width();
        var height = $(slideshowImage).height();
        $(slideshowController).css({
            "left": (pos.left + (width / 2) - 70) + "px",
            "top": (pos.top + height - 50) + "px"
        });
        $(slideshowController).children().css({
            'opacity': 0.5
        }).end().css({
            'opacity': 0.5
        });
    }
}

function slideshowFadein() {
    $(slideshowImage).fadeIn(fadeSpeed);
    loopID1 = setTimeout("slideshowFadeOut()", imageVisible);
}

function slideshowFadeOut() {
    $(slideshowImage).fadeOut(fadeSpeed);
    loopID2 = setTimeout("slideshowNextimage()", fadeSpeed);
}

function slideshowNextimage() {
    nextImage(direction);
    loopID3 = setTimeout("slideshowFadein()", 10);
}

function slideshowStopRestart() {
    if (running) {
        clearTimeout(loopID1);
        clearTimeout(loopID2);
        clearTimeout(loopID3);
        running = false;
        $(pauseButton).attr('src', $(nextButton).attr('src'));
    } else {
        setTimeout("slideshowFadeOut()", 1);
        running = true;
        $(pauseButton).attr('src', pauseSrc);
    }
}

function nextImage(direction) {

    if (direction == "foward") {
        if (currentImage == slideshowImages.length - 1) {
            currentImage = 0;
        } else {
            currentImage++;
        }
    } else if (direction == "backward") {
        if (currentImage == 0) {
            currentImage = slideshowImages.length - 1;
        } else {
            currentImage--;
        }
    }

    setImage();
}

function slideshowNext() {
    clearTimeout(loopID1);
    clearTimeout(loopID2);
    clearTimeout(loopID3);
    direction = "foward";
    slideshowNextimage();
}

function slideshowPrevious() {
    clearTimeout(loopID1);
    clearTimeout(loopID2);
    clearTimeout(loopID3);
    direction = "backward";
    slideshowNextimage();
}

function setImage() {
    $(slideshowImage).attr('src', slideshowImages[currentImage]);
    $(slideshowImage).attr('alt', slideshowAltText[currentImage]);
    $(slideshowImage).attr('title', slideshowAltText[currentImage]);
    $(slideshowImage).attr('usemap', slideshowImageMap[currentImage]);
    $(slideshowLink).attr('href', slideshowURL[currentImage]);
}

// wait until all of the images load to start the slideshow
$(window).load(
    function() {
        slideshowSetup();
    }
);

$(window).resize(
    function() {
        resetSlideshowControls();
    }
);
