
var slideChangeInterval = 10000;
var currentSlideItem = 0;
var splashTimer = 0;
var splashChanging = false;
var splashPaused = false;

function splashPlayPause()
{
    if (splashPaused) {
        // console.log("playing...");
        splashPaused = false;
        $("button#playpause").html("Pause");
        splashNext();
    }
    else {
        // console.log("pausing...");
        splashPaused = true;
        $("button#playpause").html("Play");
        clearTimeout(splashTimer);
    }
}

function splashPrevious()
{
    // console.log("-- previous - currentSlideItem:" + currentSlideItem + " (" + splashChanging + ")");
    clearTimeout(splashTimer);
    if (splashChanging) {
        return;
    }
    var slideToHide = currentSlideItem--;
    var slideToShow = currentSlideItem;
    if (slideToShow < 0) {
        slideToShow = currentSlideItem = splashItems.length - 1;
    }
    splashChangeSlide(slideToHide, slideToShow);    
}

function splashNext()
{
    // console.log("-- next - currentSlideItem:" + currentSlideItem + " (" + splashChanging + ")");
    clearTimeout(splashTimer);
    if (splashChanging) {
        return;
    }
    var slideToHide = currentSlideItem++;
    var slideToShow = currentSlideItem;
    if (slideToShow >= splashItems.length) {
        slideToShow = currentSlideItem = 0;
    }
    splashChangeSlide(slideToHide, slideToShow);
}

function splashChangeSlide(hideIndex, showIndex)
{
    splashChanging = true;
    // console.log("hiding:" + hideIndex + ", showing:" + showIndex + " (" + splashChanging + ")");
    
    // fade old item out completely before fading new item in
    $("div#splash_content").fadeOut("slow", function() {
        // console.log("done hiding");
        splashShowSlide(showIndex);
    });
}

function splashShowSlide(showIndex)
{
    // console.log("showing:" + showIndex + " (" + splashChanging + ")");
    splashChanging = true;

    // clone our placeholder html and fill in item values
    // console.log(splashItems[showIndex].ShortDescription);
    var $item = $("div#splash_placeholder").clone();
    $item.find("div#splash_shortdescription").text(splashItems[showIndex].ShortDescription);
    $item.find("div#splash_fulldescription").text(splashItems[showIndex].FullDescription);
    $item.find("img#splash_thumbnail").attr("src", splashItems[showIndex].Thumbnail);
    $item.find("a#splash_iteminfolink").attr("href", splashItems[showIndex].InfoLink);
    $item.find("span#splash_price").text(splashItems[showIndex].Price);
    if (!splashItems[showIndex].BuyItNow) {
        $item.find("div#splash_buyitnow").hide();
    }

    // assign placeholder html to div being shown and upate index display
    $("div#splash_content").html($item.html());
    $("div#splash_indexdisplay").text((showIndex + 1) + " / " + splashItems.length);

    // bring it up
    $("div#splash_content").fadeIn("slow", function() {

        if (!splashPaused) {
            // console.log("starting splashTimer: " + slideChangeInterval);
            splashTimer = setTimeout("splashNext()", slideChangeInterval);
        }
        splashChanging = false;
        // console.log("done showing");
    });   
}