// Modal window
function ___getHeightSize() {
	var yScroll;
	if (window.innerHeight && window.scrollMaxY) 
		yScroll = window.innerHeight + window.scrollMaxY;
	else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		yScroll = document.body.offsetHeight;
	}
	var windowHeight;
	
	if (self.innerHeight)
		windowHeight = self.innerHeight;
	else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowHeight = document.body.clientHeight;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	return pageHeight;
}

//Lightbox
$(function() {
    $('#slide a').lightBox();
	$('#foto_cont a').lightBox();
});

//Camere
$(document).ready(function(){
	
	var topModal = parseInt((document.body.clientHeight - 580)/2);
	var leftModal = parseInt((document.body.clientWidth - 747)/2)-20;
	
	$(".modal").click(function(){
		
	 	$("#foo").append('<div id="blackbckg"></div><div id="modal" style="top: ' + topModal + 'px;left: ' + leftModal + 'px;"><a id="closemodal" href="#" onclick="fadeOut();">X</a><iframe width="747" height="580" frameborder="0" src="richiedi-disponibilita.php"></iframe></div>');
		
	 	$("#blackbckg").css({height:___getHeightSize()}).fadeTo(750, 0.85,function callback() {
			$("#modal").slideDown(550);
		}
		);
	});
	
	$(".modal_corteverde").click(function(){
		
		$("#foo").append('<div id="blackbckg"></div><div id="modal" style="top: ' + topModal + 'px;left: ' + leftModal + 'px;"><a id="closemodal" href="#" onclick="fadeOut();">X</a><iframe width="747" height="580" frameborder="0" src="corte-verde.php"></iframe></div>');
		
		$("#blackbckg").css({height:___getHeightSize()}).fadeTo(750, 0.85,function callback() {
			$("#modal").slideDown(550);
		}
		);
		
	});
});

function fadeOut(){
	$("#modal").slideUp(550,function callback() {
		$("#blackbckg").fadeTo(750, 0,function callback() {
			$("#blackbckg").remove();
			$("#modal").remove();
			}
		);
	}
	);
}


$(document).ready(function(){
	
	//Partners
	$(".partner_tit").click(function(){
		$(".partner_tit").css('color','#fff');
		
		if($(this).parent().next().is(":visible"))
			$(this).parent().next().hide(450);
		else{
			$(".partner_txt:visible").hide(450);
			$(this).parent().next().show(450);
			$(this).css('color','#98363f');
		}
	
	});
	
	//Guestbook
	$("a.leave").click(function(){
		
		if ($("#comments").is(":visible")) {
			var link = $(this);
			$("#comments:visible").fadeOut(150,function(){
				$(".leave").text('close');
			});
			$("#add_comment:hidden").slideDown(450);
		}else{
			$(".leave").text('Leave a comment');
			$("#add_comment:visible").slideUp(450);
			$("#comments:hidden").fadeIn(150);
			
			
		}
		
	});
	
	//come arrivare
	$("a.link_ca").click(function(){
		
		if($(this).next().is(":visible")){
			$(this).next().slideUp(250);
			$(this).css("color","#fff");
		}else{
			$("a.link_ca").css("color","#fff");
			$(this).css("color","#97373f");
			$(".ca_txt:visible").slideUp(250);
			$(this).next().slideDown(250);
		}
		
	});
	
	
	$("#calcola_percorso").click(function(){
		
		$("#calcola_percorso_txt").slideDown(200);
		
	});
	
	
	// dintorni / enogastronomia
	$(".link_eno").click(function(){
		$(".link_eno").css('color','#fff');
		
		if($(this).next().next().is(":visible"))
			$(this).next().next().hide();
		else{
			$(".hidden_ul:visible").hide();
			$(this).next().next().show();
			$(this).css('color','#98363f');
		}
	
	});
	
	// eventi
	$(".view_event").click(function(){
		
		$(".view_event").css("color","#fff");
		
		if($(this).parent().next().is(":visible"))
			$(this).parent().next().hide(250);
		else{
			$(this).css("color","#98363f");
			$(".events_all:visible").slideUp(250);
			$(this).parent().next().slideDown(250);
		}
		
	});

	
});

// captcha
var isCaptchaCorrect = false;

$(document).ready(function(){
	
	$("input[name=code]").keyup(function(){
		codeInserted = $(this).val();
		if (codeInserted != '') {
			$.ajax({
			   type: "POST",
			   url: "../get-captcha.php",
			   success: function(code){
				 if(code.toUpperCase() == codeInserted.toUpperCase()){
					isCaptchaCorrect = true;
				 }else
				 	isCaptchaCorrect = false;
			   }
			 });
		}	
	});
});

//validazione form Guestbook
function validateGuestbook(form){
	
	if(form.nome.value.length < 1){
		alert('Please enter name');
		form.nome.style.background = '#ff0';
		form.nome.focus();
		return false;
	}
	if(form.cognome.value.length < 1){
		alert('Please enter surname');
		form.cognome.style.background = '#ff0';
		form.cognome.focus();
		return false;
	}
	if(form.citta.value.length < 1){
		alert('Please enter city');
		form.citta.style.background = '#ff0';
		form.citta.focus();
		return false;
	}
	if(form.mese.value == ''){
		alert('Please enter month');
		form.mese.focus();
		return false;
	}
	if(form.anno.value == ''){
		alert('Please enter year');
		form.anno.focus();
		return false;
	}
	if(form.commento.value == ''){
		alert('Please write your comment');
		form.commento.focus();
		return false;
	}
	
	// captcha
	if(form.code.value=='') {
	  alert('Please insert the code');
	  form.code.value='';
	  form.code.focus();
	  return false;
	 }else{
	 	if(!isCaptchaCorrect){
			alert('The code is not correct');
			form.code.focus();
			return false;
		}
	 }
	return true;
}
