var screenShotTimeOut = null;
var screenShotAlpha = 0.0;
var slideShowRunning = true;
var imgPreLoad = new Image();

// called to start the process of showing the next screen shot
function nextScreenShot()
{
    // find the screen shot number
    var screenShotImg = document.getElementById('screenShot');
    if (!screenShotImg) return;
    var imgSrc = screenShotImg.src;
    imgSrc = imgSrc.substr(imgSrc.lastIndexOf("BlindPoint")+"BlindPoint".length, 1);
    runAjax("./src/pages/games/util/loadNextScreen.php?next&lastScreen="+imgSrc, screenShotLoaded);
}

function screenShotLoaded(_urlText)
{
    // start loading in the image
    imgPreLoad.src = _urlText;    
    // start fading out old screen shot once this image loads
    imgPreLoad.onLoad = screenShotFadeCallback(_urlText);
}

function screenShotFadeCallback(_urlText)
{
    // get the image
    var screenShotImg = document.getElementById('screenShot');
    if (!screenShotImg) return;

    // find the alpha for the image
    if (screenShotImg.src.indexOf(_urlText) == -1)  // fade out old image
    {
        screenShotAlpha -= 0.1;
        if (screenShotAlpha <= 0.0)   // finished fading out?
        {
            screenShotAlpha = 0.0;
            screenShotImg.src = _urlText;
        }
    }
    else // fading in new image
    {
        screenShotAlpha += 0.1;
        if (screenShotAlpha >= 1.0)   // finished fading in
            screenShotAlpha = 1.0;
    }

    // set the image to the alpha calculated
    if (navigator.appName.indexOf("Microsoft")!=-1)
        screenShotImg.style.filter = 'alpha(opacity=' + screenShotAlpha*100 + ')';
    else
        screenShotImg.style.opacity = screenShotAlpha;

    if (screenShotAlpha < 1.0)  // still fading in or out
        screenShotTimeOut = setTimeout("screenShotFadeCallback('"+_urlText+"');", 40);
    else // finished fading
        screenShotTimeOut = setTimeout("nextScreenShot();", 3000);
}

function startSlideShow()
{
    // the first slide is turned off when we start so set the images opacity to 0
    screenShotAlpha = 0.0;
    if (navigator.appName.indexOf("Microsoft")!=-1)
        document.getElementById('screenShot').style.filter = 'alpha(opacity=0)';
    else
        document.getElementById('screenShot').style.opacity = 0;

    // start the slide show
    nextScreenShot();
}