﻿var intervalloCicloEnfatizza;
var durataIntervallo = 5000;
var attivaCicloEnfatizza = true;

function avviaHomepage()
{
	var hl = $('.current-highlight h2');
	hl.css('background', 'transparent none');
	$('<div class="highlight-background"/>').insertAfter(hl)
	.css({ height: hl.height(), opacity: 0.4 });

	$('li.highlight-item a.title').mouseover
	(
		function() { enfatizzaElemento($(this)); }
	);
	$('.highlight-items-list').hover(fermaCicloEnfatizza, avviaCicloEnfatizza);
	avviaCicloEnfatizza();
	$('.news-list ul li').hover
	(
		function(){ $(this).addClass('hovered') },
		function(){ $(this).removeClass('hovered') }
	);
}
function cambiaVoceMenuApprofondimento(link)
{
	cambiaVoceMenu(link).click(function() { cambiaVoceMenuApprofondimento($(this)); return false; });
}
function cambiaVoceMenuClassifiche(link)
{
	cambiaVoceMenu(link).click(function() { cambiaVoceMenuClassifiche($(this)); return false; });
}
function cambiaVoceMenu(link)
{
	var selected = link.parent().parent().find('.selected');
	var span = selected.children('span');
	selected.removeClass('selected');
	span.replaceWith('<a href="#" title="' + span.html() + '">' + span.html() + '</a>');
	link.parent().addClass('selected');
	link.replaceWith('<span>' + link.html() + '</span>')
	return selected.children('a');
}
function fermaCicloEnfatizza()
{
	window.clearInterval(intervalloCicloEnfatizza);
}
function avviaCicloEnfatizza()
{
	fermaCicloEnfatizza();
	if (attivaCicloEnfatizza)
	{
		intervalloCicloEnfatizza = window.setInterval(enfatizzaProssimoElemento, durataIntervallo);
	}
}
function enfatizzaProssimoElemento()
{
	var next = $('.highlight-items-list li.selected').next();
	if (next.length == 0) {
		enfatizzaElemento($('.highlight-items-list li a.title').eq(0));
	} else {
		enfatizzaElemento(next.children('a.title'));
	}
}
function enfatizzaElemento(link) {

	var title = link.children('strong').html();
	$('li.highlight-item').removeClass('selected');
	link.parent().addClass('selected');
	$('.current-highlight img')
		.attr('src', link.attr('class').split('title ').join(''))
		.attr('alt', title)
	;
	$('.current-highlight a')
		.attr('href', link.attr('href') )
		.attr('title', link.attr('title') )
	;
	$('.current-highlight h2 strong').remove();
	$('.current-highlight h2').append( '<strong>' + link.children('span.hidden').html() + '</strong>' );
	$('.current-highlight h2 a.item-area').html( title );
	$('.highlight-background').css('height', $('.current-highlight h2').height());
}
$(document).ready(avviaHomepage);