﻿var _imageIndex = 1;
var _images = new Array();
var _intervalID = null;
var _rootPath = '';
var _firstImage = true;

function setRoot(root) {
    _rootPath = root;
}

function addImage(imageID) {
    if (_images.length == 0) {
        // If the first image is being added, set this as image 1 source
        setImage1Source(imageID);
    }

    _images[_images.length] = imageID;
}

function setImage1Source(imageID) {
    var img = document.getElementById("sideImage1");

    img.src = "getimage.ashx?id=" + imageID;
}

function setImage2Source(imageID) {
    var img = document.getElementById("sideImage2");

    img.src = "getimage.ashx?id=" + imageID;
}

function setSize(width, height) {
    var cont = document.getElementById("imageRotatorContainer");
    var img1 = document.getElementById("sideImage1");
    var img2 = document.getElementById("sideImage2");

    if (cont == null)
        return;

    cont.style.height = height + "px";
    cont.style.width = width + "px";

    img1.style.height = height + "px";
    img1.style.width = width + "px";
    img2.style.height = height + "px";
    img2.style.width = width + "px";
}

function fadeImages(opacity, increment) {
    var img1 = document.getElementById("sideImage1");
    var img2 = document.getElementById("sideImage2");
    var delay = 1;

    opacity += increment;

    // Fade in/out image 1
    img1.style.filter = "alpha(opacity=" + opacity + ")";
    img1.style.opacity = opacity / 100;

    // Fade in/out image 2
    img2.style.filter = "alpha(opacity=" + (100 - opacity) + ")";
    img2.style.opacity = (100 - opacity) / 100;

    if (opacity == 100 || opacity == 0) {
        // Change direction
        increment = increment * -1;
        delay = 5000;

        // Select the next image to load
        _imageIndex += 1;

        if (_imageIndex > (_images.length - 1))
            _imageIndex = 0;

        // Pre-load the next image into the transparent image
        if (_firstImage) {
            img2.removeAttribute("opacity");
            img2.removeAttribute("filter");
            setImage1Source(_images[_imageIndex]);
        }
        else {
            img1.removeAttribute("opacity");
            img1.removeAttribute("filter");
            setImage2Source(_images[_imageIndex]);
        }
        // Switch between images
        _firstImage = !_firstImage;
    }

    setTimeout("fadeImages(" + opacity + ", " + increment + ")", delay);
}

window.onload = function () {
    if (_images.length > 1) {
        // Preload the second image
        setImage2Source(_images[1]);

        setTimeout("fadeImages(100, -4)", 5000);
    }
}
