var tar, buf;
var tarMutato = 0;
var bufMutato = 0;
var timeOut   = 8000;

function nextBuf() {
    if (bufMutato >= buf.length-1) {
        bufMutato = 0;
        return;
    }
    bufMutato++;
}
function nextTar() {
    if (tarMutato >= tar.length-1) {
        tarMutato = 0;
        return;
    }
    tarMutato++;
}

function csere() {
    var tmp;
    t = tar[tarMutato].getElementsByTagName("DIV")[0];
    b = buf[bufMutato].getElementsByTagName("DIV")[0];
    tmp = t.cloneNode(true);
    tar[tarMutato].replaceChild(b, t);
    buf[bufMutato].appendChild(tmp);
}

function rotal() {
    for (i=1; i<=4; i++) {
        csere();
        nextTar();
        nextBuf();
    }
    window.setTimeout("rotal()", timeOut);
}


function startRotation() {
    if (!document.getElementById) return; 
    tar = getElementsByClassName("tar");
    buf = getElementsByClassName("buffer");
    // alert("Tárméret: "+tar.length+'\nBufferméret: '+buf.length);
    if (tar.length<4) return;
    if (buf.length == 0) return;
    window.setTimeout("rotal()", timeOut);
}


setEvent(window, "load", startRotation);
