﻿function cycleImages() {
    if ($('MainImage') == null) return;
    var images = $('MainImage').select('a');
    if (images.length > 1) {
        var currentDelay = 0;
        var delay = 8000;
        var fadeDuration = 1.5;
        var firstImage = images[0];
        images.each(function(image) {
            window.setTimeout(function() {
                this.fade({ duration: fadeDuration });
            } .bind(image), currentDelay);
            var nextImage = image.next('a');
            if (!nextImage) nextImage = firstImage;
            if (nextImage) {
                window.setTimeout(function() {
                    this.appear({ duration: fadeDuration });
                } .bind(nextImage), currentDelay);
            }
            currentDelay += delay;
        });
    };
    window.setTimeout(cycleImages, currentDelay);
}
Event.observe(window, 'load', function() {
    window.setTimeout(cycleImages, 3000);
});