/* #background_scritte POSITION */
var scrittePosition = function() {
	var winW = $(window).width();
	var scritteW = $('#background_scritte').width();

	if( winW > scritteW ) {
		newLeft = winW/2 - scritteW/2;
		$('#background_scritte').css({left: newLeft});
	}
}


$(document).ready(function(){
	var defaultAnimEasing = 'easeInOutElastic';
	var defaultAnimDuration = 1300;
	var timer = null;

	var mainWinW = 940;

	var Pointer = $('#menu_indicator_pointer');

	
	/* LARGHEZZA TOTALE DELLE PAGINE PRINCIPALI */
	var totPages = $('.one_page').length;
	$('#body_all').width( totPages * mainWinW );
	
	/* Pagina Contatti è indipendente */
	totPages = totPages - 1;
	

	/* POINTER */
	var initialPos = $('#menu_home').position().left;
	var initialDim = $('#menu_home').width();
	
	Pointer.css({left: initialPos, width: initialDim});
	
	Pointer.attr({'aria-element': 'menu_home'});
	
	/* POINTER */
	$('#menu_voices td img').hover(function(){
		/* Stop possible current animation */
		Pointer.stop();
		clearTimeout(timer);
		timer = null;
		
		var thisOne = $(this);
		var newPos = thisOne.position().left;
		var newDim = thisOne.width();
		
		Pointer.animate({left: newPos, width: newDim}, defaultAnimDuration, defaultAnimEasing);
		
	}, function(){
		timer = window.setTimeout(function(){
			var whichOne = $('#' + Pointer.attr('aria-element'));
			
			var returnPos = whichOne.position().left;
			var returnDim = whichOne.width();
			
			Pointer.animate({left: returnPos, width: returnDim}, defaultAnimDuration, defaultAnimEasing);
			
		}, 650)
	})
	
	$('#menu_voices td img').click(function(){
		var thisOne = $(this);
		var thisId = thisOne.attr('id');
		
		Pointer.attr({'aria-element': thisId});
		
		var windowIndex = $('#body_all').find('.one_page[aria-page="'+ thisId +'"]').index();
		var newWindowLeft = windowIndex * mainWinW;
		
		$('#body_all').animate({left: -newWindowLeft}, defaultAnimDuration, defaultAnimEasing);
	})
	
	
	$('#body_change_left').mouseup(function(){
		var nowLeft = parseInt( $('#body_all').css('left') );
		
		if( nowLeft == 0 ) {
			return;
		}
		
		nowLeft = nowLeft * (-1);
		
		var nowIndex = nowLeft / mainWinW;
		var newElement = $('#menu_voices td:nth-child('+ parseInt( nowIndex ) +')').find('img');
		
		newElement.trigger('mouseover');
		newElement.trigger('click');
	})
	
	
	$('#body_change_right').mouseup(function(){
		var nowLeft;
		
		if( $('#body_all').css('left') == 'auto' ) {
			nowLeft = 0;
		} else {
			nowLeft = parseInt( $('#body_all').css('left') );
		}

		if( nowLeft == (totPages * mainWinW) ) {
			return;
		}
		
		nowLeft = nowLeft * (-1);
		
		var nowIndex = nowLeft / mainWinW;
		var newElement = $('#menu_voices td:nth-child('+ parseInt( nowIndex + 2 ) +')').find('img');
		
		newElement.trigger('mouseover');
		newElement.trigger('click');
	})
	
	
	$('#menu_contatti').mouseup(function(){
		var newLeft = ( totPages ) * mainWinW;
		
		$('#body_all').animate({left: -newLeft}, defaultAnimDuration, defaultAnimEasing);
	})
	
	
	/* INVIO EMAIL CONTATTI */
	$('#refresh_simage').click(function(){
		document.getElementById('siimage').src = 'show_simage.html?sid=' + Math.random();
		$('input[name="comment_captcha"]').val('');
		return false;
	})
	
	$('#part_contatti_form form').submit(function(e){
		e.preventDefault();
		
		var theLink = $(this).attr('action');
		var theData = $(this).serialize();
		
		doShowLoading(function(){
			var popupTitle, popupContents;
			$.getJSON(theLink, theData, function(resVal){
				if( resVal.error == 0 ) {
					popupTitle = 'Grazie!';
					popupContents = resVal.msg;
					$('#refresh_simage').trigger('click');
					
				} else {
					popupTitle = 'Errore';
					popupContents = '<div class="error_msg">' + resVal.msg + '</div>';
					$('#refresh_simage').trigger('click');
				}
				
				doOpenPopup(popupContents, popupTitle, false);
			})
		})
	})
	
	
	/* LINKS TO POPUP */
	$('a[rel="to_popup"]').click(function(e){
		e.preventDefault();
		
		var theTitle = $(this).attr('title');
		var theLink = $(this).attr('href');
		var theContents = '';
		
		doShowLoading(function(){
			$.get(theLink, function(resVal){
				theContents = resVal;

				doOpenPopup(theContents, theTitle, false);
			})
		})
	})


	scrittePosition();
	
	doShowLoading();
})



$(window).load(function(){
	doHideLoading();
})


$(window).resize(function(){
	scrittePosition();
})
