var good_browser = document.getElementById ? true : false;

var montage_images          = null;
var montage_interval        = 1500;
var montage_base_z          = 10;
var montage_num_onscreen    = 10;
var montage_next_new        = 0;
var montage_next_old        = -montage_num_onscreen;
var montage_wrapped         = false;

if (good_browser)
{
    montage_images = new Array
    (
        { src: "/images/montage/djedis-house.jpg",                  x: 310, y: 190 },       // Second Life
        { src: "/images/montage/aav-home.jpg",                      x: 600, y: 197 },       // AAV
        { src: "/images/montage/spyvideo-lcd-display-01-sm.jpg",    x: 495, y: 180 },       // HMD
        { src: "/images/montage/penyon-site.jpg",                   x: 230, y: 185 },       // Penyon
        { src: "/images/montage/maxs-motors-sm.jpg",                x: 670, y: 215 },       // All In Automotive
        { src: "/images/montage/tuts-chair.jpg",                    x: 205, y: 195 },       // Second Life
        { src: "/images/montage/leoscreenshot-sm.jpg",              x: 530, y: 180 },       // Leonardo
        { src: "/images/montage/penyon-cd-front.jpg",               x: 455, y: 175 },       // Penyon
        { src: "/images/montage/kitchen-sm.jpg",                    x: 660, y: 180 },       // Leonardo (image)
        { src: "/images/montage/obelisk.jpg",                       x: 140, y: 181 },       // Second Life
        { src: "/images/montage/spyvideo-lcd-display-sm.jpg",       x: 345, y: 200 },       // HMD
        { src: "/images/montage/leonardo.jpg",                      x: 600, y: 175 },       // Leonardo (image)
        { src: "/images/montage/flowers.jpg",                       x: 690, y: 210 },       // Photography
        { src: "/images/montage/espy-ad.jpg",                       x: 265, y: 174 },       // Penyon
        { src: "/images/montage/spyvideo-lcd-sm.jpg",               x: 780, y: 185 },       // HMD
        { src: "/images/montage/kea.jpg",                           x: 190, y: 210 },       // Photography
        { src: "/images/montage/flea.jpg",                          x: 535, y: 180 },       // Photography
        { src: "/images/montage/menu-image-resize-sm.jpg",          x: 440, y: 175 },       // Leonardo
        { src: "/images/montage/3-room-house.jpg",                  x: 680, y: 195 },       // Second Life
        { src: "/images/montage/selection-tool-menu-screen-sm.jpg", x: 245, y: 190 },       // Leonardo
        { src: "/images/montage/aav-front.jpg",                     x: 300, y: 195 },       // AAV
        { src: "/images/montage/leoscreenshot-01-sm.jpg",           x: 600, y: 200 },       // Leonardo
        { src: "/images/montage/egyptian-reed-boat.jpg",            x: 766, y: 214 },       // Second Life
        { src: "/images/montage/gordo-card.jpg",                    x: 465, y: 185 },       // Penyon
        { src: "/images/montage/ourobengr-sm.jpg",                  x: 699, y: 194 },       // Ourobengr
        { src: "/images/montage/penyon-cd-back.jpg",                x: 220, y: 174 },       // Penyon
        { src: "/images/montage/fill-texture-dialog-sm.jpg",        x: 165, y: 185 },       // Leonardo
        { src: "/images/montage/lw-for-mac-screenshot-sm.jpg",      x: 375, y: 180 },       // Leonardo (LW Mac)
        { src: "/images/montage/texture-edit-with-texture-sm.jpg",  x: 444, y: 190 },       // Leonardo
        { src: "/images/montage/spyvideo-lcd-terminal.jpg",         x: 530, y: 174 },       // HMD
        { src: "/images/montage/lsl-door.jpg",                      x: 272, y: 200 },       // Second Life (code)
        { src: "/images/montage/image-list-view-sm.jpg",            x: 595, y: 180 },       // Leonardo
        { src: "/images/montage/glovecap.jpg",                      x: 705, y: 220 }        // KeyGlove
    );
}

function montage_step()
{
    if (!montage_wrapped)
    {
        for (var i = montage_next_new; i < montage_images.length && i < montage_next_new + 3; i++)
        {
            if (!montage_images[i].img.complete)
            {
                // One of the next three images isn't loaded,
                // delay 1/4 second then check again...
                setTimeout("montage_step()", 250);
                return;
            }
        }
    }

    var d = document;

    // fix z-index
    for (var i = montage_next_new - montage_num_onscreen, z = montage_base_z; i <= montage_next_new; i++, z++)
    {
        var n = (i < 0 ? montage_images.length + i : i);
        d.getElementById("m" + n).style.zIndex = z;
    }

    // show next image
    if (d.images["i" + montage_next_new].src != montage_images[montage_next_new].src)
    {
        d.images["i" + montage_next_new].src = montage_images[montage_next_new].src;
    }
    
    d.getElementById("m" + montage_next_new).style.visibility = "visible";
    montage_next_new++;
    if (montage_next_new >= montage_images.length)
    {
        montage_wrapped = true;
        montage_next_new = 0;
    }

    // hide old image
    if (montage_next_old >= 0)
    {
        d.getElementById("m" + montage_next_old).style.visibility = "hidden";
    }
    montage_next_old++
    if (montage_next_old >= montage_images.length)
    {
        montage_next_old = 0;
    }
    
    setTimeout("montage_step()", montage_interval);
}

function montage_run()
{
    if (!good_browser) return;
    for (var i in montage_images)
    {
        montage_images[i].img = new Image();
        montage_images[i].img.src = montage_images[i].src;
    }
    
    setTimeout("montage_step()", montage_interval);
}

