// menu motions

$(document).ready(function() {
	
	// #menu animations
	menu_position = $('#menu').position();
	
	var active = -1;
	var default_left = menu_position.left;
	var default_width;
	$('#menu li a').each(function (i) {
			if($(this).parent('li').attr('class') == "active")
				active = i;
		
			default_width = (active<0) ? $('#menu li a').eq(0).outerWidth() +8 : $('#menu li a').eq(active).outerWidth() +8;
			active_position = $('#menu li a').eq(active).position();
			default_left = (active<0) ? default_left : active_position.left + menu_position.left;
				
			$('#cursor').css({'left':default_left+'px','width':default_width+'px'})
			$('#cursor #dx').css({'left':default_width-28+'px'})
			
			$(this).hover(function() {
				position = $(this).position();
				left = position.left + menu_position.left;
				width = $(this).outerWidth()+8;
				
				$('#cursor').stop().animate({'left':left+'px'},{queue:true, duration:600})
				$('#cursor').animate({'width':width+'px'},{queue:false, duration:300})
				$('#cursor #dx').stop().animate({'left':width-28+'px'},{queue:false, duration:300})
				
				if(i!=active && active>=0) 
				{
					$('#menu li a').eq(active).stop().animate({'color':'#f5effd'},600);
					$(this).stop().animate({'color':'#fff'},600)
				}
				else 
					$('#menu li a').eq(active).stop().animate({'color':'#fff'},300);
			}, 
			function () {
				$('#cursor').stop().animate({'left':default_left+'px','width':default_width+'px'},{queue:false, duration:500})
				$('#cursor #dx').stop().animate({'left':default_width-28+'px'},{queue:false, duration:500})
				$('#menu li a').eq(active).stop().animate({'color':'#fff'},600);
				
				if(i!=active)
					$(this).stop().animate({'color':'#b0afcf'},600)
		})
	})


	/** temporizzatore ed eventi #carousel **/
	var t = setInterval("carousel($('#pointers li').length);",4000);

	$('#pointers li').each(function (i) {
		$(this).click(function () {
			clearInterval(t);
			$('#pointers li').removeClass('active');
			$(this).addClass('active');
			$('#slideunder').attr('src', $('#slideover').attr('src')).stop().css({'opacity':'1'})
			$('#slideover').attr('src', $(this).children('a').attr('href')).stop().css({'opacity':'0'}).load(function () {
				$(this).stop().animate({'opacity':'1'}, {duration : 500, queue : true })
			})
		})
	})

	$('#slideover').load(function () {
		$(this).stop().animate({'opacity':'1'}, {duration:500, queue:false})
	});
	
	

	$('.gallery-link').hover(function () {
		var lang = $(this).children('img').attr('src');
		var pos = lang.indexOf('_') + 1 ;
		lang = lang.substr(pos, 2);
		$(this).children('img').attr('src', '../images/gallery-button_'+lang+'-hover.png');
	}, function() {
		var lang = $(this).children('img').attr('src');
		var pos = lang.indexOf('_') + 1 ;
		lang = lang.substr(pos, 2);
		$(this).children('img').attr('src', '../images/gallery-button_'+lang+'.png');
	})


});

/** animazione #carousel **/
var count = 0;
function carousel(n)
{
	count = (count == 0) ? n : count;
	count = (count-- > 0) ? count-- : n;
	$('#pointers li').removeClass('active');
	$('#pointers li').eq(count).addClass('active');
	$('#slideunder').attr('src', $('#slideover').attr('src')).css({'opacity':'1'});
	$('#slideover').attr('src', $('#pointers li').eq(count).children('a').attr('href')).css({'opacity':'0'});
}
