var scroll2;
var scroll;
var scroll3;
var cp;
var st;
var si;
var totProds;
var prods_scroll;
var n = 2;
var o = 2;
var acc_container;
var link;
var toClone1;
var clone1;

var delay = 6000;

function initSlide() {


    // Slider prodotti
    var allProds = getElementsByClassName(document, 'div', 'cont_rool')[0];
    if (allProds) {
        allProds = allProds.getElementsByTagName('a');
        totProds = allProds.length;
        prods_scroll = document.getElementById('prods_scroll');
        prods_scroll.style.overflow = 'hidden';
        prods_scroll.style.minHeight = '138px';
        prods_scroll.style.height = '143px';
        var chooser = getElementsByClassName(document, 'div', 'prod_chooser')[0];
        chooser.style.display = 'inline';
        
        scroll = new Fx.Scroll(prods_scroll, {
            wait: false,
            duration: 1000,
            offset: {'x': 0, 'y': 0},
            transition: Fx.Transitions.Quad.easeInOut,
            wheelStops: false
        });
        
        for (var i = 0; i < totProds; i++) {
            var a = allProds[i];
            a.id = 'pr_'+i;
            a.onclick = function(event) {
                clearTimeout(st);
                clearTimeout(si);
                cp = parseInt(this.id.substr(3,1))+1;
                var lastCurrentIco = getElementsByClassName(document, 'a', 'activeImage')[0];
                if (lastCurrentIco != this) {
                    var allSquares = getElementsByClassName(document, 'div', 'prod_chooser')[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();
                    scroll.toElement('prodSlide_'+this.id.substr(3,1));
                }
                /*if (cp < totProds) {
                    si = setInterval("goToDelayed()",delay);
                } else {
                    si = setInterval("reset()",delay);
                }*/
                return false;
            };
        }
        cp = 1;
        st = setTimeout("goToDelayed()", delay);
    }
    
    
    
    // rullo continuo
    acc_container = document.getElementById('accord_wrapper');
    var acc_toggler = getElementsByClassName(document, 'a', 'title_accord');
    if (acc_container) {
        acc_toggler[1].className = 'title_left title_accord';
        getElementsByClassName(acc_container, 'a', 'link_right')[1].className += ' hidden';
    }
    var a_onclick_backup;    
    if (acc_container) {
        acc_container.style.height = '18.1em';
        acc_container.style.overflow = 'hidden';
        var accord0;
        var accord1;
        newRullo();

        for (var i = 0; i < acc_toggler.length; i++) {
            var a = acc_toggler[i];
            if (!a.id) {
                a.id = 'ac_'+i;
            }
            if (i != 0) {
                a.onclick = function(event) {
                    this.blur();
                    toClone = document.getElementById('accord_'+(n-2)).parentNode;
                    event = new Event(event).stop();
                    scroll2.toElement('accord_'+this.id.substr(3));
                    this.onclick = null;
                    this.onmouseover = null;
                    this.onmouseout = null;
                    this.className += ' selected';
                    this.parentNode.getElementsByTagName('a')[1].className = 'link_right';
                    if (o) {
                        var clone0 = toClone.cloneNode('true');
                        var a = getElementsByClassName(clone0, 'a', 'title_accord')[0];
                        getElementsByClassName(clone0, 'a', 'link_right')[0].className += ' hidden';
                        a.onmouseover = function() {
                            this.style.background = 'transparent url(/static/img_root/arrow_bottom_blue.gif) no-repeat scroll right 50%';
                            this.style.color = '#004990';
                        }
                        a.onmouseout = function() {
                            this.style.background = 'transparent url(/static/img_root/arrow_down_grey_666.gif) no-repeat scroll right 50%';
                            this.style.color = '#999999';
                        }
                        a.style.background = 'transparent url(/static/img_root/arrow_down_grey_666.gif) no-repeat scroll right 50%';
                        a.style.color = '#999999';
                        a.id = 'ac_'+(n);
                        a.className = "title_left title_accord";
                        a.onclick = a_onclick_backup;
                        getElementsByClassName(clone0, 'div', 'accord')[0].id = 'accord_'+n;
                        clone0.className = 'accord_box';
                        acc_container.appendChild(clone0);
                    }
                    n++;
                    return false;
                }
                a.onmouseover = function() {
                    this.style.background = 'transparent url(/static/img_root/arrow_bottom_blue.gif) no-repeat scroll right 50%';
                    this.style.color = '#004990';
                }
                a.onmouseout = function() {
                    this.style.background = 'transparent url(/static/img_root/arrow_down_grey_666.gif) no-repeat scroll right 50%';
                    this.style.color = '#999999';
                }
            } else {
                a.className += ' selected';
            }
            a_onclick_backup = a.onclick;
        }
    }
    
    
    
    
    // rullo offerte
    off_container = document.getElementById('offers_wrapper');
    var o_onclick_backup;
    if (off_container) {
        getElementsByClassName(off_container, 'a', 'link_right')[1].className += ' hidden';
        off_toggler = getElementsByClassName(document, 'a', 'offers_trigger');
        off_toggler[1].className = 'title_left offers_trigger';
        off_container.style.overflow = 'hidden';
        off_container.style.height = '18.1em';
        newOffRullo();
        for (var i = 0; i < off_toggler.length; i++) {
            var a = off_toggler[i];
            if (!a.id) {
                a.id = 'of_'+i;
            }
            if (i != 0) {
                a.onclick = function(event) {
                    this.blur();
                    this.style.marginLeft = '6px';
                    toClone = document.getElementById('offers_'+(o-2));
                    event = new Event(event).stop();
                    scroll3.toElement('offers_'+this.id.substr(3));
                    this.onclick = null;
                    this.onmouseover = null;
                    this.onmouseout = null;
                    this.className += ' selected';
                    this.parentNode.getElementsByTagName('a')[1].className = 'link_right';
                    if (o) {
                        var clone0 = toClone.cloneNode('true');
                        var a = getElementsByClassName(clone0, 'a', 'offers_trigger')[0];
                        getElementsByClassName(clone0, 'a', 'link_right')[0].className += ' hidden';
                        a.onmouseover = function() {
                            this.style.background = 'transparent url(/static/img_root/arrow_bottom_blue.gif) no-repeat scroll right 50%';
                            this.style.color = '#004990';
                        }
                        a.onmouseout = function() {
                            this.style.background = 'transparent url(/static/img_root/arrow_down_grey_666.gif) no-repeat scroll right 50%';
                            this.style.color = '#999999';
                        }
                        a.style.background = 'transparent url(/static/img_root/arrow_down_grey_666.gif) no-repeat scroll right 50%';
                        a.style.color = '#999999';
                        a.style.marginLeft = '6px';
                        a.id = 'of_'+(o);
                        a.className = "title_left offers_trigger";
                        a.onclick = o_onclick_backup;
                        clone0.id = 'offers_'+o;
                        clone0.className = 'offers_box';
                        off_container.appendChild(clone0);
                    }
                    o++;
                    return false;
                }
                a.style.background = 'transparent url(/static/img_root/arrow_down_grey_666.gif) no-repeat scroll right 50%';
                a.style.color = '#999999';
                a.style.marginLeft = '7px';
                a.onmouseover = function() {
                    this.style.background = 'transparent url(/static/img_root/arrow_bottom_blue.gif) no-repeat scroll right 50%';
                    this.style.color = '#004990';
                }
                a.onmouseout = function() {
                    this.style.background = 'transparent url(/static/img_root/arrow_down_grey_666.gif) no-repeat scroll right 50%';
                    this.style.color = '#999999';
                }
            } else {
                a.className += ' selected';
            }
            o_onclick_backup = a.onclick;
        }            
    }
}

function newOffRullo() {
    scroll3 = new Fx.Scroll(off_container, {
        wait: false,
        duration: 500,
        offset: {'x': 0, 'y': 26},
        transition: Fx.Transitions.Quad.easeInOut,
        wheelStops: false,
        onComplete: function() {
            off_container.removeChild(getElementsByClassName(document, 'div', 'offers_box')[0]);
            this.set(0,0);
            newOffRullo();
        }
    });
}

function newRullo() {
    scroll2 = new Fx.Scroll(acc_container, {
        wait: false,
        duration: 500,
        offset: {'x': 0, 'y': 26},
        transition: Fx.Transitions.Quad.easeInOut,
        wheelStops: false,
        onComplete: function() {
            acc_container.removeChild(getElementsByClassName(document, 'div', 'accord_box')[0]);
            this.set(0,0);
            newRullo();
        }
    });
}

function goToDelayed() {
    clearTimeout(si);
    if (cp < totProds - 1) {
        scroll.toElement('prodSlide_'+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) {
        scroll.toElement('prodSlide_'+cp);
        getElementsByClassName(document, 'a', 'activeImage')[0].className = 'inactiveImage';
        getElementsByClassName(document, 'a', 'inactiveImage')[cp].className = 'activeImage nomargin';
        si = setTimeout("reset()", delay);
    }
}

function newProdSlider() {
    scroll = new Fx.Scroll(prods_scroll, {
        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', 'prod_box');
    for (var j = 0; j < couples.length; j++) {
        toClone1 = couples[j];
        clone1 = toClone1.cloneNode('true');
        clone1.id = 'prodSlide_'+(cp+j);
        //getElementsByClassName(clone1, 'div', 'prods_chooser');
        prods_scroll.getElementsByTagName('div')[0].appendChild(clone1);
    }
    scroll = new Fx.Scroll(prods_scroll, {
        wait: false,
        duration: 1000,
        offset: {'x': 0, 'y': 0},
        transition: Fx.Transitions.Quad.easeInOut,
        wheelStops: false,
        onComplete: function() {
            if (getElementsByClassName(document, 'div', 'prod_box').length > couples.length) {
                for (var j = 0; j < couples.length; j++) {
                    prods_scroll.getElementsByTagName('div')[0].removeChild(couples[j]);
                }
                var newProds = getElementsByClassName(prods_scroll, 'div', 'prod_box');
                for (var j = 0; j < couples.length; j++) {
                    newProds[j].id = 'prodSlide_'+j;
                }
                cp = 1;
                scroll.set(0,0);
                newProdSlider();
            }
        }
    });
    scroll.toElement('prodSlide_'+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);
}