﻿google.load("maps", "2.x");

$(document).ready(function() {
$("#com_hues").accordion({autoheight: false });
  $("#com_hues").hide();

  $("#com_btn_pro").click(function() {
    $("#com_btn_pro").attr("class", "com_btn_act");
    $("#com_btn_hue").attr("class", "com_btn");
    $("#com_hues").fadeOut("fast");
    $("#com_prop").fadeIn("slow");
    return false;
  });

  $("#com_btn_hue").click(function() {
    $("#com_btn_pro").attr("class", "com_btn");
    $("#com_btn_hue").attr("class", "com_btn_act");
    $("#com_prop").fadeOut("fast");
    $("#com_hues").fadeIn("slow");
    return false;
  });

  $("a[rel^='pp']").prettyPhoto({
    animationSpeed: 'fast', /* fast/slow/normal */
    padding: 30,
    opacity: 0.45,
    showTitle: true,
    allowresize: true,
    theme: 'dark_square'
  });

});

function mostrarSeccion(boton, itemSeccion) {
  $('.con_ficha').hide();
  $('.btn_ficha_marcado').attr("class", "btn_ficha");
  $(itemSeccion).fadeIn();  
  $(boton).attr("class", "btn_ficha_marcado");

  if (itemSeccion == "#con_ficha_7") initialize();
}

// Llama a esta función cuando la página se ha cargado.
function initialize() {
  var map = new google.maps.Map2(document.getElementById("map"));
  map.setCenter(new google.maps.LatLng(aloLat, aloLon), 12);

  var mapControl = new GMapTypeControl();
  map.enableScrollWheelZoom();
  map.addControl(mapControl);
  map.addControl(new GSmallMapControl());

  var point = new GLatLng(aloLat, aloLon);
  map.addOverlay(new GMarker(point));

  gdir = new GDirections(map, document.getElementById("directions"));
  GEvent.addListener(gdir, "load", onGDirectionsLoad);
  GEvent.addListener(gdir, "error", handleErrors);
}

function setDirections(fromAddress, toAddress, locale) {
  $("#error_mapa").hide();
  gdir.load("from: " + fromAddress + " to: " + toAddress, { "locale": locale });
}

function handleErrors() {
  if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
    $("#error_mapa").text("No se ha podido encontrar la dirección dada, por favor trate de ser menos específico.");
  else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
    $("#error_mapa").text("No se ha podido procesar la búsqueda, por favor intentelo de nuevo más tarde.");    
  else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
    $("#error_mapa").text("Por favor rellene el Destino y el Origen antes de obtener la ruta.");
  else if (gdir.getStatus().code == G_GEO_BAD_KEY)
    $("#error_mapa").text("Ha habído un error al procesar la búsqueda, intentelo de nuevo más tarde. [KEY]");
  else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
    $("#error_mapa").text("No se entiende la dirección escrita, por favor trate de ser menos específico."); 
  else
    $("#error_mapa").text("Ha habido un error, por favor intentelo de nuevo más tarde.");

  $("#error_mapa").show();
}

function onGDirectionsLoad() { }

function intercambiarDirecciones() {
  var aux = $("#from").val();
  
  $("#from").val($("#to").val());
  $("#to").val(aux);
 
  $("#from").attr("disabled", $("#from").attr("disabled") ? "" : "disabled");
  $("#to").attr("disabled", $("#to").attr("disabled") ? "" : "disabled");

  var from = $('#from').val();
  var to = $('#to').val();

  if (from != "" && to != "")
    setDirections(from, to, 'es');
}

function verDatoContacto(ID, Tipo) {
	$.getJSON("/datosContacto.ashx", { id: ID, t: Tipo }, function(json) {
		if (json.t != undefined) {
			switch (json.t) {
				case "W":
					//$("#btnWeb").attr("href", json.d);
					//$("#btnWeb").attr("target", "_blank");
					//$("#btnWeb").attr("onclick", "");
					//$("#btnWeb").text(json.d.replace('http://', ''));
					$("#globoInfoWeb").html('<div align="center"><h3 style="margin: .7em 0;">Web</h3><a href="http://' + json.d.replace('http://', '') + '" target="_blank">' + json.d.replace('http://', '') + '</a></div>');
					break;
				case "E":
					//$("#btnEma").attr("href", "mailto:" + json.d + "?subject=Contacto desde la www.AntiquaNatura.com");
					//$("#btnEma").attr("onclick", "");
					//$("#btnEma").text(json.d);
					$("#globoInfoEmail").html('<div align="center"><h3 style="margin: .7em 0;">e-Mail</h3><a href="mailto:' + json.d + '?subject=Contacto desde la www.SpaNatura.com" target="_blank">' + json.d + '</a></div>');
					break;
				case "T":
					//$("#btnTel").hide();
					//$("#lblTel").text(json.d);
					$("#globoInfoTelefono").html('<div align="center"><h3 style="margin: .7em 0;">Teléfono</h3>' + json.d + '</div>');
					break;
			}
		}
		else {
			switch (Tipo) {
				case "W":
					//$("#btnWeb").hide();
					//$("#lblWeb").text("Este alojamiento no dispone de web propia");
					$("#globoInfoWeb").html('<div align="center"><h3 style="margin: .7em 0;">Web</h3>no disponible</div>');
					break;
				case "E":
					//$("#btnEma").hide();
					//$("#lblEma").text("Este alojamiento no dispone de e-mail");
					$("#globoInfoEmail").html('<div align="center"><h3 style="margin: .7em 0;">e-Mail</h3>no disponible</div>');
					break;
				case "T":
					//$("#btnTel").hide();
					//$("#lblTel").text("Este alojamiento no dispone teléfono de contacto");
					$("#globoInfoTelefono").html('<div align="center"><h3 style="margin: .7em 0;">Teléfono</h3>no disponible</div>');
					break;
			}
		}
	});
}

function enviarComentario(ID) {
  var _n = $("#nombre").val();
  var _e = $("#email").val();
  var _t = $("#titulo").val();
  var _c = $("#comentarios").val();
  var hayError = false;

  if (_n == "") { $("#nombre_err").show(); hayError = true; } else $("#nombre_err").hide();

  if (_e == "") { $("#email_err").show(); hayError = true; }
  else if (!_e.match(/^\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b$/i)) {
    $("#email_err").text('* Dirección no válida');
    $("#email_err").show();
    hayError = true;
  }
  else { $("#email_err").hide(); }

  if (_t == "") { $("#titulo_err").show(); hayError = true; } else $("#titulo_err").hide();
  if (_c == "") { $("#comentarios_err").show(); hayError = true; } else $("#comentarios_err").hide();

  if (hayError) return;

  $.getJSON("/nuevoComentario.ashx", { id: ID, a: _n, e: _e, t: _t, c: _c }, function(data) {
    if (data.ok == 1) {
      $("#form_com").hide();
      $("#form_com_res").attr("class", "res_ok");
      $("#form_com_res").html("Su comentario ha sido enviado correctamete.<br />Gracias.");
      $("#form_com_res").fadeIn();
    }
    else {
      $("#form_com").hide();
      $("#form_com_res").attr("class", "res_ko");
      $("#form_com_res").html("Ha habido un problema al enviar el comentario, por favor intentelo de nuevo más tarde.<br />Gracias.");
      $("#form_com_res").fadeIn();
    }
  });
}

function enviarAmigo(ID) {
  var _tn = $("#t_nombre").val();
  var _te = $("#t_email").val();
  var _sn = $("#a_nombre").val();
  var _se = $("#a_email").val();
  var _c = $("#a_comentarios").val();
  var hayError = false;

  if (_tn == "") { $("#t_nombre_err").show(); hayError = true; } else $("#t_nombre_err").hide();
  if (_sn == "") { $("#a_nombre_err").show(); hayError = true; } else $("#a_nombre_err").hide();

  if (_te == "") { $("#t_email_err").show(); hayError = true; }
  else if (!_te.match(/^\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b$/i)) {
    $("#t_email_err").text('* Dirección no válida');
    $("#t_email_err").show();
    hayError = true;
  }
  else { $("#t_email_err").hide(); }

  if (_se == "") { $("#a_email_err").show(); hayError = true; }
  else if (!_se.match(/^\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b$/i)) {
    $("#a_email_err").text('* Dirección no válida');
    $("#a_email_err").show();
    hayError = true;
  }
  else { $("#a_email_err").hide(); }

  if (_c == "") { $("#a_comentarios_err").show(); hayError = true; } else $("#a_comentarios_err").hide();

  if (hayError) return;

  $.getJSON("/enviarAmigo.ashx", { id: ID, tn: _tn, te: _te, sn: _sn, se: _se, c: _c }, function(data) {
    if (data.ok == 1) {
      $("#form_amigo").hide();
      $("#form_amigo_res").attr("class", "res_ok");
      $("#form_amigo_res").html("Su correo ha sido enviado correctamete.<br />Gracias.");
      $("#form_amigo_res").fadeIn();
    }
    else {
      $("#form_amigo").hide();
      $("#form_amigo_res").attr("class", "res_ko");
      $("#form_amigo_res").html("Ha habido un problema al enviar el correo, por favor intentelo de nuevo más tarde.<br />Gracias.");
      $("#form_amigo_res").fadeIn();
    }
  });
}
