var currSwitch = 1;
var intv;

var homeSwitch = {

    init: function() {

        currSwitch = 1;
        if(!$('cntlinks')) return;
        lnk = $('cntlinks').getElementsByTagName('a');
        for (i=0; i<lnk.length; i++) {
            if (lnk[i].className == 'switcher') {
                this.setClickEvt(lnk[i]);
            }
        }
    },

    setClickEvt: function(lnk) {

        //lnk.onclick = function() {
        lnk.onmouseover = function() {

            stopRotation = true;
            numlink = this.id.replace(/\D+/, '');

            if (numlink != currSwitch) {
                li = this.parentNode;
                $('crrlink').removeAttribute('id');
                if ($('cntprod1')) $('cntprod1').style.display = 'none';
                if ($('cntprod2')) $('cntprod2').style.display = 'none';
                if ($('cntprod3')) $('cntprod3').style.display = 'none';
                $('cntprod'.concat(numlink)).style.display = "block";
                currSwitch = numlink;
                li.id = 'crrlink';
            }

            return false;

        }

    }
}



function cubeswitch () {



    this.cubegroup = null;
    this.rotation_links = null;
    this.cubes = [];
    this.currCube = 0;
    this.cubeStart = false;
    this.intv = null;
    this.next_cube;
    this.prev_cube;

    this.init = function(id_cubegroup) {

        if (!document.getElementById(id_cubegroup.toString())) return;
        this.cubegroup = document.getElementById(id_cubegroup.toString());
        this.rotation_links = $(id_cubegroup).getElementsByTagName('ul')[0];
        //alert(this.rotation_links);

        if(!this.rotation_links) return;

        var cbset = this.cubegroup.getElementsByTagName('div');
        for (var i=0; i<cbset.length; i++) {
            if (cbset[i].className.match(/cubeset/)) {
                this.cubes.push(cbset[i]);
            }
        }

        if (this.cubes.length == 0) return;

        var lnk = this.cubegroup.getElementsByTagName('a');
        for (var i=0; i<lnk.length; i++) {
            this.setClickEvt(lnk[i], i)
        }

        this.rotate();

    }

    this.rotate = function() {

    //alert(this.cubegroup.id);

        var _self = this;
        if (this.currCube == (this.cubes.length-1)) {
            this.prev_cube = this.cubes[this.cubes.length-1];
            this.prev_cube.style.display = 'none';

            this.rotation_links.getElementsByTagName('li')[this.currCube].className = '';
            this.currCube = 0;
            this.rotation_links.getElementsByTagName('li')[this.currCube].className = 'curr_rlink';

            this.next_cube = this.cubes[this.currCube];
            this.next_cube.style.display = 'block';
            this.cubeStart = false;
        }


        if (this.cubeStart) {

            this.prev_cube = this.cubes[this.currCube];
            this.prev_cube.style.display = 'none';

            this.rotation_links.getElementsByTagName('li')[this.currCube].className = '';
            this.next_cube = this.cubes[++this.currCube];
            this.rotation_links.getElementsByTagName('li')[this.currCube].className = 'curr_rlink';
            this.next_cube.style.display = 'block';
        }

        //alert(this.cubegroup.id + ": corrente" + this.currCube);

        this.cubeStart = true;
        this.intv = setTimeout(function() {
            _self.rotate();
        }, 5000);

    }


    this.setClickEvt = function(l, i) {
        var _self = this;

        l.onclick = function() {

            for (var j=0; j<_self.cubes.length; j++)
                _self.cubes[j].style.display = 'none';

            clearTimeout(_self.intv);

            for (var k=0; k<_self.rotation_links.getElementsByTagName('li').length; k++) {
                _self.rotation_links.getElementsByTagName('li')[k].className = '';
            }

            this.parentNode.className = 'curr_rlink';
            _self.cubes[i].style.display = 'block';

            return false;
        }
    }

}

var nlinksTransp;
var stopRotation = false;

var transpLinkRotation = {


    init: function() {

        if (!$('cntlinks')) return;

        var cs = 1;
        var div = $('cntlinks');
        var _self = this;

        nlinksTransp = div.getElementsByTagName('li');
        if (nlinksTransp.length < 2) return;

        setTimeout(function() {
            _self.rotate(++cs);
        },  10000);
    },

    rotate: function(cs) {

        if (stopRotation) return;

        var _self = this;

        if (cs > (nlinksTransp.length)) cs = 1;
        ps = (cs == 1)? nlinksTransp.length : cs - 1;

        /*** rimuovo id ***/
        nlinksTransp[ps-1].removeAttribute('id');
        nlinksTransp[cs-1].id = 'crrlink';
        $('cntprod'.concat(ps)).style.display = "none";
        $('cntprod'.concat(cs)).style.display = "block";

        setTimeout(function() {
            _self.rotate(++cs);
        }, 10000);

    },

    stopRotate: function() {
        stopRotation = !stopRotation;
    }

}


