slideshow_loading = 0;
slideshow_ready = 0;
slideshow_retrigger = 0;
slideshow_next = null;
slideshow_unused = [];

function $g(id) {
    return document.getElementById(id);
}

function removeElement(elem)
{
    elem.parentNode.removeChild(elem);
}

function setOpacity(elem, value)
{
    elem.style.opacity = value;
    elem.style.filter = 'alpha(opacity=' + parseInt(value*100) + ')';
}

function getOpacity(elem)
{
    return parseFloat(elem.style.opacity);
}

function pick_image()
{
    if ( slideshow_unused.length == 0 )
        slideshow_unused = slideshow.slice();
    index = Math.floor(Math.random()*slideshow_unused.length);
    if ( index > slideshow_unused.length-1 )
        index = slideshow_unused.length-1;
    ret = slideshow_unused[index];
    slideshow_unused.splice(index, 1);
    return ret;
}

function preload_image()
{
    try {
        if ( !slideshow )
            return;
    } catch(e) {
        return;
    }
    slideshow_loading = 1;
    // instantiate another image
    var preloader = new Image();
    preloader.onload=function() {
        // onLoad: set ready flag; if show_image needs retrigerring, do it
        slideshow_ready = 1;
        slideshow_loading = 0;
        if ( slideshow_retrigger )
            show_image();
    };
    preloader.src = pick_image();
    slideshow_next = preloader.src;
}

function retrieve_image()
{
    // wait for slideshow if necessary
    try {
        if ( !slideshow )
        {
            setTimeout('show_image()', 1000);
            return;
        }
    } catch(e) {
        setTimeout('show_image()', 1000);
        return;
    }
    // restart preloader if necessary
    if ( !slideshow_loading && !slideshow_ready )
    {
        slideshow_retrigger = 1;
        preload_image();
        return;
    }
    // check if new image is ready
    if ( !slideshow_ready )
    {
        // if not: mark for retriggering of show_image and return false
        slideshow_retrigger = 1;
        return;
    }
    ret = slideshow_next;
    // preload another image
    preload_image();
    // return the existing image
    return ret;
}

function fade(elem, step, interval)
{
    if ( step == 0 || interval == 0 )
        return;
    if (step > 0 && getOpacity(elem) >= 1.0 )
        return;
    if (step < 0 && getOpacity(elem) <= 0.0 )
    {
        elem.style.visibility = 'hidden';
        return;
    }
    setOpacity(elem, getOpacity(elem) + step);
    setTimeout(function() { fade(elem, step, interval); }, interval);
}

function show_image()
{
    var src = retrieve_image();
    if ( !src )
        return;
    // remove old image
    if ( $g('old_photo') )
        removeElement($g('old_photo'));
    // set the current image to be the old image
    if ( $g('current_photo') )
    {
        $g('current_photo').style.zIndex = '1';
        $g('current_photo').id = 'old_photo';
    }
    // set the new image to be the current image
    $g('photo_container').innerHTML += '<div id="current_photo" style="position:absolute; display:block; width:120px; height:90px; text-align:center; z-index: 2; filter:alpha(opacity=0); opacity:0;"><img src="'+src+'" alt="" /></div>';
    // fade in the current image and fade out the old image
    setOpacity($g('current_photo'), 0.0)
    fade($g('current_photo'), 0.05, 25);
    if ( $g('old_photo') )
    {
        $g('old_photo').className = 'fading';
        setOpacity($g('old_photo'), 1.0);
        fade($g('old_photo'), -0.05, 25);
    }
    // retrigger in 3 seconds
    setTimeout(show_image, 3000);
}

preload_image();
window.onload = function() {
    if ( ! $g('photo_container') )
        return;
    show_image();
};

