$(document).ready(function(){
		
		$('.arriba_button').click(function () { 
		  window.scrollTo(0,0);
		  return false;
		});
		
		$('#nav li').hover(function(){
		  $(this).find('div').show();
		},function(){
		  $(this).find('div').hide();
		});
		
		if ($.browser.safari) {
    		$('textarea').css('resize', 'none');
			$('input.text').css('resize', 'none');
 		}
		
		$('#print').click(function(){
			window.print();
			return false;
		});
		
		$("#link_zoom").toggle(
		  function () {
			$('body').addClass('font_mayor');
		  },
		  function () {
			$('body').removeClass('font_mayor');
		  }
		);
	});
//****************************AVAILABILITY CALENDAR***********************************************
var m1,m2,m3,y1,y2,y3; // years and months of selected calendars

function sendReservationForm(id,always_send){
	addAjaxLoader("reservation_form");
	var id_house 		= $("#houses").val();
	var house      		= encodeURIComponent($("#houses option[value='"+$("#houses").val()+"']").text());
	var checkin    		= $("#checkin").val();
	var checkout   		= $("#checkout").val();
	var adults	   		= parseInt($("#adults").val());
	var under_age  		= parseInt($("#under_age").val());
	var name 	   		= encodeURIComponent($("#name").val());
	var country    		= encodeURIComponent($("#country").val());
	var phone      		= $("#phone").val();
	var email      		= $("#email").val();
	var code			= $("#promotional_code").val();
	var observations    = encodeURIComponent($("#observations").val());
	var details			= encodeURIComponent($("#nights").html()+$("#amount").html().replace("€","&euro;"));
	var msg = "";

	if(checkin=='')msg+="\n"+$("#checkin").attr("title");
	if(checkout=='')msg+="\n"+$("#checkout").attr("title");
	if(adults==0)msg+="\n"+getTranslateEntry("select_one_adult_at_least");
	if(name=='')msg+="\n"+$("#name").attr("title");
	if(!emailCheck(email))msg+="\n"+$("#email").attr("title");
	if(country=='')msg+="\n"+$("#country").attr("title");
	
	if(msg==''){
	
		if(parseInt(always_send) == 1 || !isBooked(id, checkin, checkout)){
			$.ajax({
				   async: false,
				   type: "POST",
				   url: "_ajax.php",
				   contentType: "application/x-www-form-urlencoded;charset=UTF-8",
				   data: ("opc=sendReservationForm&id="+id_house+"&house="+house+"&checkin="+checkin+"&checkout="+checkout+"&adults="+adults+"&under_age="+under_age+"&name="+name+"&country="+country+"&email="+email+"&phone="+phone+"&observations="+observations+"&details="+details+"&code="+code),
				   success: function(data){
						removeAjaxLoader("reservation_form");
						$("#reservation_form").remove();
						$("#reservation_form_complete").show();
			   	   }
			});
			
		}
		else{
			alert(getTranslateEntry("not_available"));
			removeAjaxLoader("reservation_form");
		}
	}
	else{
		alert(getTranslateEntry("complete_required_fields")+"\n"+msg);
		removeAjaxLoader("reservation_form");
	}
}


function updateTotalNights(id){
	if($("#checkin").val()!='' && $("#checkout").val()!=''){
		addAjaxLoader("reservation_form");
		var total = getDaysCount($("#checkin").val(),$("#checkout").val());
		$("#nights").html(total+" "+(total>1 || total==0?getTranslateEntry("nights"):getTranslateEntry("night")));
		updateAmount(id);
		removeAjaxLoader("reservation_form");
		
	}
}
function checkGuests(id,capacity,adults,under_age,el){
	
	var txt_exceeds_capacity = getTranslateEntry("guest_amount_exceeds_capacity");
	if(parseInt(adults)+parseInt(under_age) > parseInt(capacity)){
		$(el).val("0");
		alert(txt_exceeds_capacity+".");
	}
	updateAmount(id);
	
}
function updateAmount(id){
	var adults = $("#adults").val();
	var under_age = $("#under_age").val();
	var checkin = $("#checkin").val();
	var checkout = $("#checkout").val();
	if(checkin!='' && checkin!=undefined && checkout!='' && checkout!=undefined){
		if(adults>0 || under_age>0){
			addAjaxLoader("reservation_form");
			var total = calculateStay(id,checkin,checkout,adults,under_age);
			if(parseInt(total)>0){
				$("#amount").html(" - €"+total);
			}
			else{
				$("#amount").html(getTranslateEntry("rates_not_available"));	
				$("#amount").css("textAlign","left");
			}
			removeAjaxLoader("reservation_form");
		}
	}
}
function calculateStay(id,checkin,checkout,adults,under_age){
	
	var total="";
	$.ajax({
		   async: false,
		   type: "GET",
		   url: "_ajax.php",
		   data: "opc=calculateStay&checkin="+checkin+"&checkout="+checkout+"&id="+id+"&adults="+adults+"&under_age="+under_age,
		   success: function(data){
				total=data;
   	   	   }
	});
	return total;
}

function getDaysCount(checkin,checkout){
	var count="";
	$.ajax({
		   async: false,
		   type: "GET",
		   url: "_ajax.php",
		   data: "opc=getDaysCount&checkin="+checkin+"&checkout="+checkout,
		   success: function(data){
				count=data;
   	   	   }
	});
	return count;
}

function isBooked(id,checkin,checkout){
	var booked=false;
	$.ajax({
	   async: false,
	   type: "GET",
	   url: "_ajax.php",
	   data: "opc=isBooked&checkin="+checkin+"&checkout="+checkout+"&id="+id,
	   success: function(data){
		  if(data==1){
			booked=true;
		  }
		  else{
			booked=false;
		  }
			
   	   }
	});
	
	return booked;
}

function setDates(year,month){
	
	
	m1 = parseInt(month);
	y1 = parseInt(year);

	if(m1<12){
		m2=m1+1;
		y2=y1;
	}
	else{
		m2=1;
		y2 = y1+1;
	}
	if(m2<12){
		m3 = m2+1;
		y3 = y2;
	}
	else{
		m3 = 1;
		y3 = y2+1;	
	}

}
function highlightDays(id_apartment){
	var cont=0;
	var booked,offer;
	addAjaxLoader("calendar_content",true);
	$("#calendar_box *").removeClass("ui-corner-all").removeClass("ui-corner-left").removeClass("ui-corner-right"); 
	$("#calendar_box .ui-datepicker-calendar .ui-state-default").css("background","none");
	$("#calendar_box .ui-datepicker-calendar .ui-state-default").css("border","1px solid #D4CCB0");
	//$(".ui-datepicker-calendar .ui-state-default").css("color","red");
	$("#calendar_box .ui-datepicker-calendar").each(function(){
	
			var auxyear=eval("y"+(cont+1));
			var auxmonth=eval("m"+(cont+1));
			
			var booked=getDaysByState(auxyear,auxmonth,1,id_apartment);
			var reserved=getDaysByState(auxyear,auxmonth,2,id_apartment);
			$(this).find(".ui-state-default").each(function(){
				$(this).parents("td").attr("onclick","");
				$(this).css("cursor","default");
				$(this).attr("href","javascript:void(0);");
				if(in_array(booked,parseInt(Math.round($(this).html())))){
					$(this).addClass("day_state_booked");
				}
				else if (in_array(reserved,parseInt(Math.round($(this).html())))) {
					$(this).addClass("day_state_booked");
				}
				else{
					$(this).addClass("day_state_available");
				}
			});
			
		cont++;
	});
	if($("#calendar_box .ui-datepicker-prev").length ==0){
		$("#calendar_box .ui-datepicker-group-first .ui-state-default").each(function(){
			if(parseInt($(this).html()) < new Date().getDate()){
				
				$(this).removeClass("day_state_available").addClass("day_state_booked");
			}
		});
	}
	removeAjaxLoader('calendar_content');
}
function getDaysByState(year,month,state,id_apartment){
	var mat;
	$.ajax({
		   async: false,
		   type: "GET",
		   url: "_ajax.php",
		   data: "opc=getDaysByState&year="+year+"&month="+month+"&state="+state+"&id="+id_apartment,
		   success: function(data){
				if(data!=''){
					mat = data.split(",");
				}
				else{
					mat= new Array();
				}
	   	   }
	});
	return mat;
}
//****************************END AVAILABILITY CALENDAR***********************************************
function showRecommendForm(description,href){
	$.ajax({
	   async: true,
	   type: "POST",
	   url:  "_ajax.php",
	   data:  "opc=showRecommendForm&href="+href+"&description="+encodeURIComponent(description),
	   success: function(data){
			MySimpleLightBox.loadHTML(data);
	   }	
	});
}

function sendRecommend(description,href){
	if(checkFormFields('recommend_form',1,0,'#5A5A5A','#FFFFFF')){
		addAjaxLoader('recommend_form');
		var name  = $("#recommend_name").val();
		var email = $("#recommend_email").val();	
	
		$.ajax({
		   async: true,
		   type: "POST",
		   url:  "_ajax.php",
		   data:  "opc=sendRecommend&href="+href+"&description="+encodeURIComponent(description)+"&name="+encodeURIComponent(name)+"&email="+encodeURIComponent(email),
		   success: function(data){
				MySimpleLightBox.close();
		   }	
		});
	}
}

function showRecommendSite(){
	$.ajax({
	   async: true,
	   type: "POST",
	   url:  "_ajax.php",
	   data:  "opc=showRecommendSiteForm",
	   success: function(data){
			MySimpleLightBox.loadHTML(data);
	   }	
	});
}

function sendRecommendSite(){
	if(checkFormFields('recommend_form',1,0,'#5A5A5A','#FFFFFF')){
		addAjaxLoader('recommend_form');
		var name  = encodeURIComponent($("#recommend_name").val());
		var email = encodeURIComponent($("#recommend_email").val());	
	
		$.ajax({
		   async: true,
		   type: "POST",
		   url:  "_ajax.php",
		   data:  "opc=sendRecommendSite&name="+name+"&email="+email,
		   success: function(data){
				MySimpleLightBox.close();
		   }	
		});
	}
}
function showNewsletterSubscriptionForm(){
	$.ajax({
	   async: true,
	   type: "POST",
	   url:  "_ajax.php",
	   data:  "opc=showNewsletterSubscriptionForm",
	   success: function(data){
			MySimpleLightBox.loadHTML(data);
	   }	
	});
}
function sendNewsletterSubscription(){
	
	if(checkFormFields('newsletter_form',1,0,'#5A5A5A','#FFFFFF')){
		addAjaxLoader('newsletter_form');
		var name  = encodeURIComponent($("#newsletter_name").val());
		var surname  = encodeURIComponent($("#newsletter_surname").val());
		var email = encodeURIComponent($("#newsletter_email").val());	
		
		$.ajax({
		   async: false,
		   type: "POST",
		   url:  "_ajax.php",
		   data:  "opc=sendNewsletterSubscription&name="+name+"&surname="+surname+"&email="+email,
		   success: function(data){
				
				switch(parseInt(data)){
					case 1:
						alert(getTranslateEntry('existent_account'));
						removeAjaxLoader('newsletter_form');
						break;
					case 2:
						MySimpleLightBox.loadHTML('<div style="padding: 10px;background-color: #FFF;border:1px solid #999999;width:483px;">'+getTranslateEntry('newsletter_registration_successfull')+'</div>');
						//$("#newsletter_form").html(getTranslateEntry('newsletter_registration_successfull'));
						
						break;
					
				}
			
		   }	
		});
	}
}
