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() {

    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];
        
        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';
                    } else {
                        this.className = 'activeImage';
                    }
                    
                    if (allSquares[allSquares.length-1] == lastCurrentIco) {
                        lastCurrentIco.className = 'inactiveImage';
                    } else {
                        lastCurrentIco.className = 'inactiveImage';
                    }
                    
                    event = new Event(event).stop();
                    scroll.toElement('prodSlide_'+this.id.substr(3,1));
                }
                return false;
            };
        }
        cp = 1;
        st = setTimeout("goToDelayed()", delay);
    }
	
}


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);
}
