﻿// JavaScript Document

$(window).resize(function() {
	// SetWidthHorizontaleMenu();
});
function OpenPopUp(pageurl) {

	window.open("" + pageurl + "", "mywindow", "menubar=1,resizable=1,width=500,height=600");

}

function SetWidthHorizontaleMenu() {

	var w = $('#horizontalmenu').parent().outerWidth();
	var w_inner = $('#horizontalmenu').innerWidth();

	var size = $('#horizontalmenu').children('li').size();
	var w2 = 0;

	$('#horizontalmenu > li').each(function() {
		w2 += $(this).innerWidth();
		//w2 += $(this).width();
	});
	var padding = parseInt(((w - w2 - 6) / size) / 2);
	//alert(w+":"+w2+":"+padding);

	$('#horizontalmenu li').each(function(index) {
		$(this).css('padding-right', '' + (padding - 1) + 'px');
		$(this).css('padding-left', '' + (padding) + 'px');
	});
}


$(document).ready(function() {

	var browser = $.browser;

	if($.browser.webkit) {

		$("head").append('<link rel="stylesheet" href="css/safari.css" type="text/css"/>');

	} else if($.browser.msie) {

	} else if($.browser.safari) {

	}

	SetWidthHorizontaleMenu();

	if(document.getElementById("mycarousel")) {

		$("#mycarousel").jcarousel({
			scroll : 1,
			initCallback : mycarousel_initCallback,
			itemVisibleInCallback : {
				onBeforeAnimation : mycarousel_itemVisibleInCallbackBeforeAnimation,
				onAfterAnimation : mycarousel_itemVisibleInCallbackAfterAnimation
			},
			buttonNextHTML : null,
			buttonPrevHTML : null
		});

	}

	/// elimino il terzo livello dal menu principale
	$("ul#horizontalmenu ul ul").remove();

	///////////////////////////// briciole di pane

	$(".navigation span").last().remove();
	var lastspan = $(".navigation a").last().text();
	$(".navigation a").last().remove();
	$(".navigation").append('<span>' + lastspan + '</span>');

	/////////////////////////////// FORM RICERCA

	$("#MenuTop ul li.search a, #BoxFormSearch a#close").click(function() {
		/*
		 if($("#BoxFormSearch").hasClass("open")){
		 $("#BoxFormSearch").addClass("closed");
		 $("#BoxFormSearch").removeClass("open");
		 $("#BoxFormSearch").css("display","none");
		 }else{
		 $("#BoxFormSearch").addClass("open");
		 $("#BoxFormSearch").removeClass("closed");
		 $("#BoxFormSearch").css("display","block");

		 }

		 return false;
		 */
	});
	/////////////////////////////// FORM CONTATTI

	function VerifyEmail(emailstring) {
		var status = false;
		var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
		if(emailstring.search(emailRegEx) == -1) {
			//alert("Please enter a valid email address.");
		} else {
			//alert("Woohoo!  The email address is in the correct format and they are the same.");
			status = true;
		}
		return status;
	}


	$("#FormContact").removeAttr("onsubmit");
	$("#FormContact").attr("action", "sendmail.php?action=send");
	$("#FormContact input[name=submit]").addClass("more");
	$("#FormContact input[name=reset]").addClass("reset");
	$("#FormContact input[name=reset]").removeAttr("onclick");

	$("#FormContact input[name=reset]").click(function() {///// reset form

		$(':input', '#FormContact').not(':button, :submit, :reset, :hidden').val('').removeAttr('checked').removeAttr('selected');

	});

	$("#FormContact").submit(function() {

		var theForm = $("#FormContact");
		var email = $("#FormContact #email").val();
		var consenso = $("#FormContact input[name=consenso]").attr("checked");
		var messaggio = $("#FormContact textarea[name=messaggio]").val();
		var lang = $("#FormContact input[name=lang]").val();

		if(!lang || (lang == "it")) {//// italiano

			if(email == "") { Alert("Attenzione, il campo EMAIL è obbligatorio ai fini dell'invio.", "email", theForm);
				return false;
			}
			if((email != "") && !VerifyEmail(email)) { Alert("Attenzione, indicare un indirizzo EMAIL valido!", "email", theForm);
				return false;
			}
			if(!consenso) { Alert("Attenzione: senza il consenso al Trattamento dei dati personali non è possibile inviare la sua richiesta.", "consenso", theForm);
				return false;
			}

		} else if(lang == "en") {///// inglese

			if(email == "") { Alert("Warning: please fill the field Email, it is compulsory.", "email", theForm);
				return false;
			}
			if((email != "") && !VerifyEmail(email)) { Alert("Warning: indicate a valid email address!", "email", theForm);
				return false;
			}
			if(!consenso) { Alert("Warning: You must agree to the terms and conditions under the Privacy Act.", "consenso", theForm);
				return false;
			}

		} else if(lang == "de") {////// tedesco

			if(email == "") { Alert("Achtung: Bitte füllen Sie das Feld Email, ist es obligatorisch.", "email", theForm);
				return false;
			}
			if((email != "") && !VerifyEmail(email)) { Alert("Warnung: Geben Sie eine gültige Email-Adresse!", "email", theForm);
				return false;
			}
			if(!consenso) { Alert("Warnung: Sie müssen die Bedingungen, unter das Datenschutzgesetz einverstanden.", "email", theForm);
				return false;
			}

		} else if(lang == "fr") {/////// francese

			if(email == "") { Alert("Attention: s'il vous plaît remplir le champ Email, il est obligatoire.", "email", theForm);
				return false;
			}
			if((email != "") && !VerifyEmail(email)) { Alert("Attention: indiquer une adresse email valide!", "email", theForm);
				return false;
			}
			if(!consenso) { Alert("Attention: Vous devez accepter les termes et conditions en vertu de la Loi sur la protection des renseignements personnels.", "consenso", theForm);
				return false;
			}

		} else if(lang == "es") {/////// spagnolo

			if(email == "") { Alert("Advertencia: por favor, rellene el campo Email, es obligatorio.", "email", theForm);
				return false;
			}
			if((email != "") && !VerifyEmail(email)) { alert("Advertencia: indicar una dirección válida de correo electrónico!", "email", theForm);
				return false;
			}
			if(!consenso) { Alert("Advertencia: Debe aceptar los términos y condiciones en la Ley de Privacidad.", "email", theForm);
				return false;
			}

		}

		$("#FormContact").hide();
		$("#loading_form").fadeIn();

		$.post("sendmail.php", {
			action : "send",
			lang : lang,
			email : email,
			messaggio : messaggio,
			sendtojquery : "yes"
		}, function(data) {

			$("#loading_form").hide();
			$("#result_form").fadeIn();
			$("#result_form").html(data);

			$("#result_form #button_back").click(function() {

				$("#FormContact").fadeIn();
				$("#result_form").hide();
				$("#loading_form").hide();

				/// elimino tutti i messaggi di errore
				$('span.error', theForm).remove();

				/// annullo tutti i campi
				$(':input', theForm).not(':button, :submit, :reset, :hidden').val('').removeAttr('checked').removeAttr('selected');

			});
		});
		return false;
	});
	/////////////////// navigazione per lettere pagina archive index
	
	/*var $window = $(window),
       $stickyEl = $('.title_page');

   $window.scroll(function() {
        var windowTop = $window.scrollTop(),
            elTop = $stickyEl.offset().top;

        $stickyEl.toggleClass('sticky', windowTop > elTop);
    });*/
   try {
   $(".archive .title_page").jScroll();
   } catch (e){}

	$("#navigation_by_letter a").each(function() {
		var letter = $(this).text();
		$(this).attr("href", "javascript:void(0);");

		$(this).click(function() {
			$('html,body').animate({scrollTop: $("."+letter+":first").offset().top - 110},'slow');
			return false;

		});
	});
	/////// stampa errore di compilazione form

	function Alert(msg, inputName, theForm) {
		//alert(msg);
		$("span.error", theForm).remove();
		$("input[name=" + inputName + "]", theForm).focus();
		$("input[name=" + inputName + "]", theForm).after('<span class="error">' + msg + '</span>');
		$("input[name=" + inputName + "]", theForm).parent().find(".error").css("display", "none");
		$("input[name=" + inputName + "]", theForm).parent().find(".error").fadeIn();
	}

});

