
var g_timer = 5;
var g_stoped = false;
var g_next = 0;
var g_liveReq = false;

function initRandom (_timer, _next)
{
    g_timer = _timer;
    g_next = _next;
    setTimeout ('nextRandom (false)', g_timer * 1000);
}

function nextRandom (_force)
{
    if (!g_stoped || _force)
    {
        document.location.href = '/random-' + g_timer + '-' + g_next;
    }
}

function stopRandom ()
{
    g_stoped = true;
}

function startRandom ()
{
    g_stoped = false;
    initRandom (g_timer, g_next);
}

function fasterRandom ()
{
    g_timer--;
    if (g_timer < 1)
    {
        g_timer = 1;
    }
    initRandom (g_timer, g_next);
}

function slowerRandom ()
{
    g_timer++;
    initRandom (g_timer, g_next);
}


// Async preload function
function preloadURLS ()
{
    if (g_liveReq && g_liveReq.readyState == 4)
    {
        g_preloadImages = new Array (); 
        urlList = g_liveReq.responseText.split (';');
        for (i = 0; i < urlList.length; i++)
        {
            if (urlList[i].length > 0)
            {
                g_preloadImages[i] = new Image ();
                g_preloadImages[i].src = urlList[i];
            }
        }
    }
}

function preloadImages ()
{
    idList = g_preloadFiles.join (';');
    // async request
    if (g_liveReq && g_liveReq.readyState < 4)
        g_liveReq.abort ();
    if (window.XMLHttpRequest)
        g_liveReq = new XMLHttpRequest ();
    else
        g_liveReq = new ActiveXObject ('Microsoft.XMLHTTP');
    g_liveReq.onreadystatechange = function () { preloadURLS (); };
    g_liveReq.open ('GET', 'urls.php?i=' + encodeURIComponent (idList));
    g_liveReq.send (null);
    return false;
}

// Exif functions

// Pour afficher/cacher le popup
function displayProperty (_styleelt, _prop)
{
    if (typeof (_styleelt.setAttribute) == "undefined")
        _styleelt.display = _prop;
    else
        _styleelt.setAttribute ("display", _prop);
}

function openExif ()
{
    var exif = document.getElementById ('exif');
    var button = document.getElementById ('exif_open');

    displayProperty (exif.style, 'block');
    displayProperty (button.style, 'none');
    return false;
}

function closeExif ()
{
    var exif = document.getElementById ('exif');
    var button = document.getElementById ('exif_open');

    displayProperty (exif.style, 'none');
    displayProperty (button.style, 'block');

    return false;
}

function decode (coded)
{
    cipher = "aZbYcXdWeVfUgThSiRjQkPlOmNnMoLpKqJrIsHtGuFvEwDxCyBzA1234567890";
    shift = coded.length;
    link = "";
    for (i = 0; i < coded.length; i++)
    {
        if (cipher.indexOf (coded.charAt (i)) != -1)
        {
            ltr = (cipher.indexOf (coded.charAt(i)) - shift + cipher.length) % cipher.length;
            link += cipher.charAt (ltr);
        }
        else
        {
            link += coded.charAt (i);
        }
    }
    return link;
}

