﻿window.addEvent('domready', function () {

    var currentNews = 0;
    var onHover = false;

    var news = $$('.item-news');

    news.set('tween', {
        'duration': 1000,
        'onComplete': function (el) {
            el.inject(el.getParent(), 'bottom');
            el.setStyle('height', el.retrieve('initheight'));
        }
    });

    news.each(function (el) {
        el.store('initheight', el.getStyle('height').toInt());
        el.addEvent('mouseenter', function () {
            onHover = true;
        }).addEvent('mouseleave', function () {
            onHover = false;
        });
    });

    var changeNews = function () {
        if (onHover) {
            return;
        }
        news[currentNews].tween('height', 0);
        currentNews = (currentNews + 1) % news.length;
    }

    if (news.length > 0) {
        changeNews.periodical(5000);
    }


    var voices = $('navbar').getFirst('ul').getChildren('li');
    voices.each(function (el) {
        var sub = el.getFirst('ul');
        if (sub) {
            sub.setStyles({ 'opacity': 0, 'display': 'block' });
            sub.set('tween', { duration: 500 });
            el.addEvent('mouseenter', function () {
                sub.tween('opacity', 0.9);
            });
            el.addEvent('mouseleave', function () {
                sub.tween('opacity', 0);
            });
        }
    });

    if ($('sidebar29')) {
        // Set opacity
        $('sidebar29').setStyle('opacity', 0.9);
    }

    // Box icone a scorrimento
    var iconsForRow = 4;
    var iconsList = $$('.item-evidence');
    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('left').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('left', 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);
    }


});

var resizeSidebar = function () {
    if ($('sidebar29')) {
        $('sidebar29').setStyle('min-height', '0');

        // Set height
        var cHeight = $('main-content').getStyle('height').toInt();
        var sPadding = $('sidebar29').getStyle('padding-top').toInt() + $('sidebar29').getStyle('padding-bottom').toInt();
        var sMargin = $('sidebar29').getStyle('margin-top').toInt() + $('sidebar29').getStyle('margin-bottom').toInt();

        $('sidebar29').setStyle('height', '');
        var sHeight = $('sidebar29').getStyle('height').toInt();
        //alert(cHeight);
        //alert(Number(sHeight + sPadding + sMargin));
        if (cHeight > Number(sHeight + sPadding + sMargin)) {
            $('sidebar29').setStyle('height', Number(cHeight - sPadding - sMargin));
            //alert('resize');
        }
    }
}

window.addEvent('load', function () {
    resizeSidebar.run();
    $$('span.opened', 'span.closed').addEvent('click', function () {
        resizeSidebar.run();
    });
});

