var currentScroll = 1;
var scroll = true;
var marginTop = 0;
var scrollTimer;

function initScrollTab() {
    //aggiungo il sencondo div nel secondo tab
    currentScroll = 2;
    addScrollDiv();
    
    //risetto il tab corrente e vi aggiungo un tab
    currentScroll = 1;
    addScrollDiv();
    
    //e via...
    doScroll();
    $$('.scrollTab')[currentScroll-1].className = "scrollTab currentTab";
}

function stopScroll() {
    clearTimeout(scrollTimer);
    scroll = false;
}

function startScroll() {
    if(!scroll) {
        scroll = true
        doScroll();
    }
}

function changeTab(i) {
    if(currentScroll != i) {
    
        stopScroll();
        
        $('scroll'+currentScroll).style.cssText = "display: none;";
        $('scroll'+i).style.cssText = "";
        $$('.scrollBoxImg')[i-1].style.cssText = "";
        $$('.scrollBoxImg')[currentScroll-1].style.cssText = "display: none;";
        $$('.scrollTab')[currentScroll-1].className = "scrollTab otherTab";
        $$('.scrollTab')[i-1].className = "scrollTab currentTab";
        
        
        new Effect.Morph($('scroll'+i).firstChild,{ style:"margin-top: 0px;", duration: 0.0 });
        marginTop = 0;
        currentScroll = i;
        scrollTimer = setTimeout('startScroll()',3000);
    }
}

function addScrollDiv() {
    var newDiv = $('scroll'+currentScroll).firstChild.cloneNode(true);
    new Effect.Morph(newDiv,{ style:"margin-top: 0px;", duration: 0.0 });
    $('scroll'+currentScroll).insert(newDiv);
}

function doScroll() {
    if(scroll) {
        marginTop++;
        var height = 1000;
        try {var height = $('scroll'+currentScroll).firstChild.getHeight(); } catch(e) {};
        if(marginTop == height) {           
            addScrollDiv();
            marginTop = 0;
            $('scroll'+currentScroll).firstChild.remove();
        }
        new Effect.Morph( $('scroll'+currentScroll).firstChild,{ style:"margin-top: -"+marginTop+"px;", duration: 0.0 });
        scrollTimer = setTimeout('doScroll()',40);
    }
}



