function slideshowPrevious(current, previous)
{
    var imageUri = document.getElementById('imageuri').value;

    var eCurrent = document.getElementById('slideshowCurrent');
    var ePrevious = document.getElementById('slideshowPrevious');
    var eNext = document.getElementById('slideshowNext');

    if (!eCurrent || !eCurrent.innerHTML)
    {
        return true;
    }

    if (!ePrevious || !ePrevious.innerHTML)
    {
        return true;
    }

    if (!eNext)
    {
        return true;
    }

    eNext.innerHTML = eCurrent.innerHTML;
    eCurrent.innerHTML = ePrevious.innerHTML;
    ePrevious.innerHTML = '';

    findPrevious(previous);

    eNext = document.getElementById('slideshowThumbnailNext');

    if (eNext)
    {
        eNext.innerHTML = '<a href="?image=' + current + '" onclick="return slideshowNext(\'' + previous + '\', \'' + current + '\');"><img src="' + imageUri + current + '?width=100" alt="" class="thumbnail" /></a>\n';
    }

    ePrevious = document.getElementById('slideshowThumbnailPrevious');

    if (ePrevious)
    {
        ePrevious.innerHTML = '';
    }

    return false;
}


function findPrevious(current)
{
    if (!current)
    {
        return;
    }

    var basedir = document.getElementById('basedir').value;
    var imageuri = document.getElementById('imageuri').value;
    var url = basedir + '/slideshow_findimage.php';
    var args =
        'which=-1' +
        '&current=' + current;
    HTTPRequest(_findPrevious, url, 'GET', args, current);
}


function _findPrevious(response, current)
{
    var imageuri = document.getElementById('imageuri').value;
    var basedir = document.getElementById('basedir').value;
    var ePrevious = document.getElementById('slideshowPrevious');
    var data = (response ? response.split('%%') : Array());

    previous = (data[0] ? data[0] : '');
    description = (data[1] ? data[1] : '');
    caption = (data[2] ? data[2] : '');
    copyright = (data[3] ? data[3] : '');

    if (ePrevious)
    {
        ePrevious.innerHTML = (previous ?
            '<h2>' + description + '</h2>\n' +
            '<a href="' + basedir + '/images/' + previous + '" title="' + description + '"><img src="' + imageuri + previous + '?width=400" alt="' + description + '" /></a><br />\n' +
            '<div class="slideshow-copyright">&copy; ' + copyright + '</div>\n' +
            '<small>' + caption + '</small><br />\n' :
            '');
    }

    ePrevious = document.getElementById('slideshowThumbnailPrevious');

    if (ePrevious)
    {
        ePrevious.innerHTML = (previous ? '<a href="?image=' + previous + '" onclick="return slideshowPrevious(\'' + current + '\', \'' + previous + '\');" title="' + description + '"><img src="' + imageuri + previous + '?width=100" alt="' + description + '" title="' + description + '" class="thumbnail" /></a>' : '');
    }

    highlightThumbnail(current);
}


function slideshowNext(current, next)
{
    var imageUri = document.getElementById('imageuri').value;

    var eCurrent = document.getElementById('slideshowCurrent');
    var ePrevious = document.getElementById('slideshowPrevious');
    var eNext = document.getElementById('slideshowNext');

    if (!eCurrent || !eCurrent.innerHTML)
    {
        return true;
    }

    if (!eNext || !eNext.innerHTML)
    {
        return true;
    }

    if (!ePrevious)
    {
        return true;
    }

    ePrevious.innerHTML = eCurrent.innerHTML;
    eCurrent.innerHTML = eNext.innerHTML;
    eNext.innerHTML = '';

    findNext(next);

    ePrevious = document.getElementById('slideshowThumbnailPrevious');

    if (ePrevious)
    {
        ePrevious.innerHTML = '<a href="?image=' + current + '" onclick="return slideshowPrevious(\'' + next + '\', \'' + current + '\');"><img src="' + imageUri + current + '?width=100" alt="" class="thumbnail" /></a>\n';
    }

    eNext = document.getElementById('slideshowThumbnailNext');

    if (eNext)
    {
        eNext.innerHTML = '';
    }

    return false;
}


function findNext(current)
{
    if (!current)
    {
        return;
    }

    var basedir = document.getElementById('basedir').value;
    var imageuri = document.getElementById('imageuri').value;
    var url = basedir + '/slideshow_findimage.php';
    var args =
        'which=1' +
        '&current=' + current;
    HTTPRequest(_findNext, url, 'GET', args, current);
}


function _findNext(response, current)
{
    var basedir = document.getElementById('basedir').value;
    var imageuri = document.getElementById('imageuri').value;
    var eNext = document.getElementById('slideshowNext');
    var data = (response ? response.split('%%') : Array());

    next = (data[0] ? data[0] : '');
    description = (data[1] ? data[1] : '');
    caption = (data[2] ? data[2] : '');
    copyright = (data[3] ? data[3] : '');

    if (eNext)
    {
        eNext.innerHTML = (next ?
            '<h2>' + description + '</h2>\n' +
            '<a href="' + basedir + '/images/' + next + '" title="' + description + '"><img src="' + imageuri + next + '?width=400" alt="' + description + '" /></a><br />\n' +
            '<div class="slideshow-copyright">&copy; ' + copyright + '</div>\n' +
            '<small>' + caption + '</small><br />\n' :
            '');
    }

    eNext = document.getElementById('slideshowThumbnailNext');

    if (eNext)
    {
        eNext.innerHTML = (next ? '<a href="?image=' + next + '" onclick="return slideshowNext(\'' + current + '\', \'' + next + '\');" title="' + description + '"><img src="' + imageuri + next + '?width=100" alt="' + description + '" title="' + description + '" class="thumbnail" /></a>' : '');
    }

    highlightThumbnail(current);
}


function highlightThumbnail(current)
{
    if (!current)
    {
        return;
    }

    var re = new RegExp(current);

    for (var idx = 0; idx < 999; idx++)
    {
        var e = document.getElementById('slideshowThumbnailList' + idx);

        if (!e)
        {
            break;
        }

        var highlight = (e.innerHTML.search(re) > -1);
        e.style['backgroundColor'] = (highlight ? '#6f0e01' : '#ffffff');

        if (e = document.getElementById('slideshowThumbnailListLink' + idx))
        {
            e.style['color'] = (highlight ? '#ffffff' : '#000000');
        }
    }
}
