function sectionOver(e)
{
	if($('.bg',$(e.target).parent()).css('opacity') == 0 || $(e.target).data('active')){
		$(e.target).data('active',1);
		//$('.bg',$(e.target).parent()).stop(true, true).animate({opacity: .75},500);
		$('.bg',$(e.target).parent()).stop(true, true).animate({opacity: 1},500);
	}
}
function sectionOut(e)
{
	if($(e.target).data('active'))
	{
		$('.bg',$(e.target).parent()).stop(true, true).animate({opacity: 0},500);
	}
}

function abstractOver(e)
{
	//$('.bg',$(e.target).parent()).stop(true, true).animate({opacity: .75},500);
	$('.bg',$(e.target).parent()).stop(true, true).animate({opacity: 1},500);
}
function abstractOut(e)
{
	$('.bg',$(e.target).parent()).stop(true, true).animate({opacity: 0},500);
}

function projectOver(e)
{
	$('.over',$(e.target).parent()).stop(true, true).animate({opacity: .75},500);
	$('p',$(e.target).parent()).addClass('thumb_text_over');
}
function projectOut(e)
{
	$('.over',$(e.target).parent()).stop(true, true).animate({opacity: 0},500);
	$('p',$(e.target).parent()).removeClass('thumb_text_over');
}

function checkScroll()
{
	//alert($(window).width());
	if($(window).width()<960)
	{
		$('html').css({'overflow-x':'auto'});
	}
	else
	{
		$('html').css({'overflow-x':'hidden'});
	}
}

function handleNewsletterResponse(responseText)
{
	//hideLoading();
	response = jQuery.parseJSON(responseText);
	
	$('#newsletterMessage').html(response['message']);
	$('#newsletterMessage').hide();
	
	if (response['success'])
	{
		$('#UtentenewsletterEmail').fadeOut(200);
		$('.submit', '#newsletter').fadeOut(200);
		setTimeout(function() { $('#newsletterMessage').fadeIn();}, 300);
		setTimeout(function(){
			$('#newsletter').animate({'opacity':0});
			}, 3000);
		$('#open_newsletter').unbind('click', toggleNewsletter);
	}
	else
	{
		$('#newsletterMessage').fadeIn();
	}	
}

function toggleNewsletter()
{
	$('#newsletter').stop(true, true).slideToggle(1000,'easeInOutQuart');
}

$(document).ready(function()
{	
	$(function()
	{
		$('a','.h4').bind('mouseenter', sectionOver);
		$('a','.h4').bind('mouseleave', sectionOut);
		$('a','.thumb_progetto').bind('mouseenter', projectOver);
		$('a','.thumb_progetto').bind('mouseleave', projectOut);
		$(window).resize(function() {
			checkScroll();
		});
		$('.abstract').bind('mouseenter', abstractOver);
		$('.abstract').bind('mouseleave', abstractOut);
		
		// form asincrono
		$('#newsletterAdd').ajaxForm({success: handleNewsletterResponse});
		$('#UtentenewsletterEmail').defaultClear();
		
		//	NEWSLETTER
		$('#newsletter').hide();
		$('#open_newsletter').bind('click', toggleNewsletter)
		
		$('html').keydown(function(e) {
			if(e.keyCode == 39)
			{
				if($('#pagination_older').attr('href'))
				{
					window.location = $('#pagination_older').attr('href');
				}
			}
			else if(e.keyCode == 37)
			{
				if($('#pagination_newer').attr('href'))
				{
					window.location = $('#pagination_newer').attr('href');
				}
			}
		});
		
		checkScroll();
	});
});
