var interval;
var imageArray;
var imageCount ;
var current ;
var prev;
var next;

function init() {
    interval = 5;
//    delay = 3;
    imageArray = document.getElementsByTagName("img");
    imgCount = imageArray.length;
    current = 0;

    wait();
}

function wait() {
    setTimeout(flip, interval * 1000);
}

function flip() {
    prev=(current + imgCount-1) % imgCount;
    next=(current + 1) % imgCount;
    imageArray[prev].className = 'stay';
    imageArray[current].className = 'out';
    imageArray[next].className = 'in';
//    current = next ;
    
    imageArray[current].addEventListener(
        "webkitTransitionEnd", 
        flip_end, 
        false
    );
}

function flip_end() {
    imageArray[current].removeEventListener(
        "webkitTransitionEnd", 
        flip_end, 
        false);
    current = next ;
    wait(); 
}