﻿/*****

Image Cross Fade Redux
Version 1.0
Last revision: 02.15.2006
steve@slayeroffice.com

Please leave this notice intact. 

Rewrite of old code found here: http://slayeroffice.com/code/imageCrossFade/index.html


*****/

window.addEventListener ? window.addEventListener("load", so_xfade_init, false) : window.attachEvent("onload", so_xfade_init);

var so_document = document, so_imgs = new Array(), so_zInterval = null, so_current = 0, so_pause = false;

function so_xfade_init() {
    if (!so_document.getElementById || !so_document.createElement) return;

    // DON'T FORGET TO GRAB THIS FILE AND PLACE IT ON YOUR SERVER IN THE SAME DIRECTORY AS THE JAVASCRIPT!
    // http://slayeroffice.com/code/imageCrossFade/xfade2.css
    //css = d.createElement("link");
    //css.setAttribute("href","xfade2.css");
    //css.setAttribute("rel","stylesheet");
    //css.setAttribute("type","text/css");
    //d.getElementsByTagName("head")[0].appendChild(css);

    if (!so_document.getElementById("imageContainer"))
        return;

    so_imgs = so_document.getElementById("imageContainer").getElementsByTagName("img");
    for (i = 1; i < so_imgs.length; i++) so_imgs[i].xOpacity = 0;
    so_imgs[0].style.display = "block";
    so_imgs[0].xOpacity = .99;

    setTimeout(so_xfade, 1000);
}

function so_xfade() {
    cOpacity = so_imgs[so_current].xOpacity;
    nIndex = so_imgs[so_current + 1] ? so_current + 1 : 0;

    nOpacity = so_imgs[nIndex].xOpacity;

    cOpacity -= .05;
    nOpacity += .05;

    so_imgs[nIndex].style.display = "block";
    so_imgs[so_current].xOpacity = cOpacity;
    so_imgs[nIndex].xOpacity = nOpacity;

    setOpacity(so_imgs[so_current]);
    setOpacity(so_imgs[nIndex]);

    if (cOpacity <= 0) {
        so_imgs[so_current].style.display = "none";
        so_current = nIndex;
        setTimeout(so_xfade, 1000);
    } else {
        setTimeout(so_xfade, 50);
    }

    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) + ")";
    }

}