﻿window.addEventListener ? window.addEventListener("load", so_init, false) : window.attachEvent("onload", so_init);

var d = document, imgs, descs, thumbs, current = 0, playingmovie, autorun = true;

function setOpacity(obj) {
    if (obj.xOpacity > .99) {
        obj.xOpacity = .99;
        return;
    }
    obj.style.opacity = obj.xOpacity;
    obj.style.MozOpacity = obj.xOpacity;
    obj.style.filter = "alpha(opacity=" + (obj.xOpacity * 100) + ")";
}

function so_init() {
    if (!d.getElementById || !d.createElement) return;

    imgs = d.getElementById("screenshots").getElementsByTagName("img");
    for (i = 0; i < imgs.length; i++) imgs[i].xOpacity = 0;
    
    descs = d.getElementById("captions").getElementsByTagName("div");

    thumbs = d.getElementById("thumbs").getElementsByTagName("img");
    for (i = 0; i < thumbs.length; i++) thumbs[i].xOpacity = 0.5;

    var screencast = d.getElementById("screencastmovie");
    if (screencast) {
        // use the movie instead of screenshots
        // d.getElementById("screenshots").style.display = "none";
        playingmovie = true;
    }
    else {
        //d.getElementById("screenshots").style.display = "inherit";
        playingmovie = false;

        if (thumbs.length > 0) {
        
            imgs[0].style.display = "block";
            imgs[0].xOpacity = .99;
            thumbs[0].xOpacity = .99;
            setOpacity(thumbs[0]);
            descs[0].style.display = "block";

            setTimeout(so_autorun, 8000);
        }
    }    
}

function so_autorun() {
    if (!autorun) return;

    cOpacity = imgs[current].xOpacity;
    var nIndex = imgs[current + 1] ? current + 1 : 0;
    descs[current].style.display = "none";
    descs[nIndex].style.display = "block";

    nOpacity = imgs[nIndex].xOpacity;

    cOpacity -= .1;
    nOpacity += .1;

    imgs[nIndex].style.display = "block";
    imgs[current].xOpacity = cOpacity;
    imgs[nIndex].xOpacity = nOpacity;        
    setOpacity(imgs[current]);
    setOpacity(imgs[nIndex]);

    thumbs[current].xOpacity = 0.5 + cOpacity / 2;
    thumbs[nIndex].xOpacity = 0.5 + nOpacity / 2;
    setOpacity(thumbs[current]);
    setOpacity(thumbs[nIndex]);

    if (cOpacity <= 0) {
        imgs[current].style.display = "none";
        current = nIndex;
        setTimeout(so_autorun, 4000);
    } else {
        setTimeout(so_autorun, 20);
    }
}


function so_show(nIndex) {

    if (playingmovie) {
        d.getElementById("screencastmovie").style.display = "none";
        playingmovie = false;

        imgs[nIndex].style.display = "block";
        imgs[nIndex].xOpacity = .99;
        setOpacity(imgs[nIndex]);
        thumbs[nIndex].xOpacity = .99;
        setOpacity(thumbs[nIndex]);
        descs[nIndex].style.display = "block";
    } 

    autorun = false;
    // if (nIndex == current) return;
    descs[current].style.display = "none";
    descs[nIndex].style.display = "block";

    imgs[current].style.display = "none";
    imgs[nIndex].style.display = "block";
    imgs[current].xOpacity = 0;
    imgs[nIndex].xOpacity = 0.99;
    setOpacity(imgs[current]);
    setOpacity(imgs[nIndex]);

    thumbs[current].xOpacity = 0.5;
    thumbs[nIndex].xOpacity = 0.99;
    setOpacity(thumbs[current]);
    setOpacity(thumbs[nIndex]);
    
    current = nIndex;
}
