﻿// Parametri cookie
var cookieName = 'layoutPos';
var cookieDuration = 365; // days

// Percorso immagini per il tree menu
var subImgOpen = '/templates/template1/images/list_open.gif';
var subImgClose = '/templates/template1/images/list_close.gif';

window.addEvent('domready', function () {

    // Apro il cookie
    var layoutPos = new Hash.Cookie(cookieName, { autoSave: true, duration: cookieDuration, path: '/' });
    layoutPos.load();

    // Di default faccio aprire la seconda tab
    if (!layoutPos.get('posTab')) {
        layoutPos.set('posTab', 1);
        layoutPos.load();
    }

    // Box icone a scorrimento
    var iconsForRow = 2;
    var iconsList = $$('.evidenceContainer .evidenceBox');
    var iconsCount = iconsList.length;
    var currentIcon = iconsForRow - 1; // Ultima icona mostrata
    var currentIconPos = 0; // Ultimo posto dove è stata mostrata l'icona
    var overIcon = false;
    var timeIconDelay = 4000;
    var timeIconFade = 1200;

    // Cambia icona
    var changeIcon = function () {

        // Se sono sopra un'icona esco
        if (overIcon) {
            return;
        }

        // Calcolo la posizione dove mettere l'immagine
        var cLeft = iconsList[currentIconPos].getStyle('top').toInt();
        // Nascondo l'immagine che sta occupando il posto di quella nuova
        iconsList[currentIconPos].tween('opacity', 0);
        // Aggiorno la postazione
        currentIconPos = (currentIconPos + 1) % iconsCount;
        // Calcolo la nuova immagine di mostrate
        currentIcon = (currentIcon + 1) % iconsCount;
        // Imposto la posizione dell'immagine
        iconsList[currentIcon].setStyle('top', cLeft);
        // Mostro l'immagine corrente
        iconsList[currentIcon].tween('opacity', 1);

    }

    if (iconsCount > iconsForRow) {
        iconsList.each(function (el, i) {

            el.set('tween', {
                duration: timeIconFade
            }).addEvent('mouseenter', function () {
                overIcon = true;
            }).addEvent('mouseleave', function () {
                overIcon = false;
            });

            if (i >= iconsForRow) {
                el.setStyles({
                    'opacity': 0,
                    'display': 'block'
                });
            }

        });
        // Start con il ciclo
        changeIcon.periodical(timeIconDelay + timeIconFade);
    }

    // Gestione tabs inbank, news /////////////////////////////////////////////////////////////////////////////////////////////////////////
    var tabsTop = $$('.tabsTop'); // sono 2
    var boxTop = $$('.boxTop');

    boxTop.setStyles({ 'opacity': 0, 'display': 'block' });
    boxTop.set('tween', { duration: 400 });

    if (tabsTop.length > 0) {
        tabsTop[0].set('index', 0);
        tabsTop[1].set('index', 1);

        // Su explorer 6 è forzato inbank aperto per la gestioen delle immagini
        if (layoutPos.get('posTab') == 1 && !ie6) {
            tabsTop[1].addClass('selected');
            boxTop[1].setStyle('opacity', 1);
            layoutPos.set('posTab', 1);
        }
        else {
            tabsTop[0].addClass('selected');
            boxTop[0].setStyle('opacity', 1);
            layoutPos.set('posTab', 0);
        }

        tabsTop.addEvent('click', function () {
            boxTop.tween('opacity', 0);
            tabsTop.removeClass('selected');
            this.addClass('selected');
            layoutPos.set('posTab', this.get('index'));
            boxTop[this.get('index')].tween('opacity', 1);
        });
    }

});

window.addEvent('load', function () {

    // Gestione icone homepage
    $$('.navIcons').addEvent('mouseenter', function () {
        this.set('tween', { duration: 200 });
        this.tween('margin-top', '-10');
    }).addEvent('mouseleave', function () {
        this.set('tween', { duration: 300 });
        this.tween('margin-top', '0');
    })

    // Chiusura del LOAD
});
