var scrolled;
var cp;
var delay = 6000;
var si;
var st;
var totProds;
var scroll4;

function initSlide() {


    // Slider prodotti
    var allProds = getElementsByClassName(document, 'div', 'cont_rool')[0];
    allProds.style.display = 'inline';
    allProds = allProds.getElementsByTagName('a');
    totProds = allProds.length;
    scrolled = getElementsByClassName(document, 'div', 'wp_prodwrapper')[0];
    scrolled.style.overflow = 'hidden';
    scrolled.style.height = 'auto';

    scroll4 = new Fx.Scroll(scrolled, {
        wait: false,
        duration: 1000,
        offset: {'x': 0, 'y': 0},
        transition: Fx.Transitions.Quad.easeInOut
    });
    
    for (var i = 0; i < totProds; i++) {
        var a = allProds[i];
        a.id = 'pb_'+i;
        a.onclick = function(event) {
            clearTimeout(st);
            clearTimeout(si);
            var lastCurrentIco = getElementsByClassName(document, 'a', 'activeImage')[0];
            if (lastCurrentIco != this) {
                var allSquares = getElementsByClassName(document, 'div', 'cont_rool')[0].getElementsByTagName('a');
                
                if (allSquares[allSquares.length-1] == this) {
                    this.className = 'activeImage nomargin';
                } else {
                    this.className = 'activeImage';
                }
                
                if (allSquares[allSquares.length-1] == lastCurrentIco) {
                    lastCurrentIco.className = 'inactiveImage nomargin';
                } else {
                    lastCurrentIco.className = 'inactiveImage';
                }
                
                event = new Event(event).stop();
                scroll4.toElement('wp_prodbox'+this.id.substr(3,1));
            }
            return false;
        };
    }
    cp = 1;
    st = setTimeout("goToDelayed()", delay);
}

function goToDelayed() {
    clearTimeout(si);
    if (cp < totProds - 1) {
        scroll4.toElement('wp_prodbox'+cp);
        getElementsByClassName(document, 'a', 'activeImage')[0].className = 'inactiveImage';
        getElementsByClassName(document, 'a', 'inactiveImage')[cp].className = 'activeImage';
        cp++;
        si = setInterval("goToDelayed()", delay);
    } else if (cp == totProds-1) {
        scroll4.toElement('wp_prodbox'+cp);
        getElementsByClassName(document, 'a', 'activeImage')[0].className = 'inactiveImage';
        getElementsByClassName(document, 'a', 'inactiveImage')[cp].className = 'activeImage nomargin';
        si = setTimeout("reset()", delay);
    }
}

function newProdSlider() {
    scroll4 = new Fx.Scroll(scrolled, {
        wait: false,
        duration: 1000,
        offset: {'x': 0, 'y': 0},
        transition: Fx.Transitions.Quad.easeInOut,
        wheelStops: false
    });
}

function reset() {
    clearTimeout(si);
    cp = 4;
    couples = getElementsByClassName(document, 'div', 'wp_prodbox');
    for (var j = 0; j < couples.length; j++) {
        toClone1 = couples[j];
        clone1 = toClone1.cloneNode('true');
        clone1.id = 'wp_prodbox'+(cp+j);
        scrolled.getElementsByTagName('div')[0].appendChild(clone1);
    }
    scroll4 = new Fx.Scroll(scrolled, {
        wait: false,
        duration: 1000,
        offset: {'x': 0, 'y': 0},
        transition: Fx.Transitions.Quad.easeInOut,
        wheelStops: false,
        onComplete: function() {
            if (getElementsByClassName(document, 'div', 'wp_prodbox').length > couples.length) {
                for (var j = 0; j < couples.length; j++) {
                    scrolled.getElementsByTagName('div')[0].removeChild(couples[j]);
                }
                var newProds = getElementsByClassName(document, 'div', 'wp_prodbox');
                for (var j = 0; j < couples.length; j++) {
                    newProds[j].id = 'wp_prodbox'+j;
                }
                cp = 1;
                scroll4.set(0,0);
                newProdSlider();
            }
        }
    });
    scroll4.toElement('wp_prodbox'+cp);
    getElementsByClassName(document, 'a', 'activeImage')[0].className = 'inactiveImage nomargin';
    getElementsByClassName(document, 'a', 'inactiveImage')[0].className = 'activeImage';
    cp++;
    si = setInterval("goToDelayed()", delay);
}

if (window.attachEvent) {
    window.attachEvent("onload", initSlide, false);
}

if (window.addEventListener) {
    window.addEventListener("load", initSlide, false);
}