var intInterval = 0;
var intInterval2 = 0;
var thepic = 0;
var f_index = 0;
var autor = new Array();
var descripcion = new Array();
var fotos = new Array();
var total = 0;
var cal1 = new CalendarPopup();
var ary = new Array();
ary[0] = "url(images/fachada_noche.jpg)";
ary[1] = "url(images/roseton.jpg)";
ary[2] = "url(images/exterior_detalle.jpg)";
ary[3] = "url(images/patio_masia.jpg)";
ary[4] = "url(images/habitacion_sencilla_2.jpg)";
ary[5] = "url(images/relieve_aseo.jpg)";
ary[6] = "url(images/jacuzzi_lleno.jpg)";
ary[7] = "url(images/comedor_copas.jpg)";
ary[8] = "url(images/sencilla1_red.jpg)";
ary[9] = "url(images/patio_2.jpg)";
ary[10] = "url(images/piscina.jpg)";
ary[11] = "url(images/piscina_bale.jpg)";
ary[12] = "url(images/piscina_sombrillas.jpg)";
function doChange()
{
	thepic = (thepic + 1) % 13;
	$('#content').css({"background-image":""+ary[thepic]+""});
}
function adaptBodyBack()
{
	var viewportwidth = $(window).width();  
	var viewportheight = $(window).height();  
	if(viewportwidth > 1920)
	{
		$('body').css('background-image' , 'url(images/1920x1200.jpg)');

	}	
	if((viewportwidth > 1680) &&  (viewportwidth <= 1920))
	{
		$('body').css('background-image' , 'url(images/1920x1200.jpg)');	
	}
	if((viewportwidth > 1600) &&  (viewportwidth <= 1680))
	{
		$('body').css('background-image' , 'url(images/1680x1050.jpg)');
	}
	if((viewportwidth > 1440) &&  (viewportwidth <= 1600))
	{
		$('body').css('background-image' , 'url(images/1680x1050.jpg)');
	}
	if((viewportwidth > 1360) &&  (viewportwidth <= 1440))
	{
		$('body').css('background-image' , 'url(images/1440x900.jpg)');
	}
	if((viewportwidth > 1280) &&  (viewportwidth <= 1360))
	{
		$('body').css('background-image' , 'url(images/1360x768.jpg)');
	}
	if((viewportwidth > 1152) &&  (viewportwidth <= 1280))
	{
		$('body').css('background-image' , 'url(images/1280x1024.jpg)');
	}
	if((viewportwidth >= 1024) &&  (viewportwidth <= 1152))
	{	
		$('body').css('background-image' , 'url(images/1152x864.jpg)');
	}
	if((viewportwidth > 800) &&  (viewportwidth < 1024))
	{
		$('body').css('background-image' , 'url(images/1024x768.jpg)');
		$('#wrapper').width = '800px';
		$('#topdiv').width(800);		
		$('#menu').width(800);
		$('#content').width(800);
		$('#submenu').width(800);
		$('#subcontent').width(800);
	}
}
function doChange2() 
{	
	var foto_val = f_index + 1;
	$('#area_title').hide();
	$('#f_controls').css('display', 'inline');
	$('#data_space').show();
	$('#f_number').html("<span class='epi'>FOTO "+foto_val+"</span>");
	$('#foto_book').css({"background-image":""+fotos[f_index]+""});
	$('#autor').html("<span class='epi'>Autor: </span>"+autor[f_index]);
	$('#descripcion').html("<em>"+descripcion[f_index]+"<em>");
	f_index = (f_index + 1) % total;
}
function controlFoto(accion)
{
	switch(accion)
	{
		case 'play':
			$('#play').hide();
			$('#pause').css('display', 'inline');
			intInterval2 = window.setInterval('doChange2()', 1000*10);
			break
		case 'pause':
			$('#pause').hide();
			$('#play').css('display', 'inline');
			intInterval2=window.clearInterval(intInterval2);
			break
		case 'fwd':
			doChange2();
			break
		case 'rwd':
			if(f_index > 1)
			{
				f_index = f_index - 2;
				doChange2();
			}
			if(f_index == 1)
			{
				f_index = 0;
				doChange2();				
			}
			if(f_index == 0)
			{
				f_index = total;
				doChange2();				
			}
			break			
	}
}
function resetSlide()
{
	f_index = 0;
}

function readXML(section) 
{ //fuction to return the xml http object
	var xmlFile = section+"/"+section+".xml";
	var xml = $.ajax(
	{
		type: "GET",
		url: xmlFile,
		dataType: "xml",
		success: function(xml) 
		{
			total = xml.getElementsByTagName("foto").length;
			for(x=0;x<total;x++)
			{
				autor[x] = xml.getElementsByTagName("autor")[x].firstChild.data;
				descripcion[x] = xml.getElementsByTagName("descripcion")[x].firstChild.data;
				fotos[x] = "url("+section+"/"+x+".jpg)";
				if(x == total - 1)
				{
					intInterval2 = window.setInterval('doChange2()', 1000*10);	
				}
			}
		}
	});
	
}	
function chooseTittle(section)
{
	switch (section) 
	{
		case '5_1':
		   return 'La Mas&iacute;a';
		   break
		case '5_2':
		   return 'Habitaciones';
		   break
		case '5_3':
		   return 'Instalaciones';
		   break
		case '5_4':
		   return 'El entorno';
		   break
	}
}
function loadContents(section) 
{
	intInterval=window.clearInterval(intInterval);
	intInterval2=window.clearInterval(intInterval2);
	var mi_fecha=new Date();
	var fresh = mi_fecha.getTime();
	var fresh_target = "contents2.php?section="+section+"&date="+fresh;
	$("#content").load(fresh_target, function()
	{
	$("#atras").click(function(event)
		{
			backHome();
		});
	$("#foto_frame").click(function(event)
		{ 
			hidePics();
		});
		if(section=='1')
		{
			$("#entrada").click(function(event)
			{ 
				showPics('entrada', 'v');
			});
			$("#general").click(function(event)
			{ 
				showPics('general', 'h');
			});
			$("#piscina_bale_3").click(function(event)
			{ 
				showPics('piscina_bale_3', 'h');
			});
			$("#general_entre_arboles").click(function(event)
			{ 
				showPics('general_entre_arboles', 'h');
			});
			$("#general_cerca").click(function(event)
			{ 
				showPics('general_cerca', 'h');
			});
			$("#fuente_terraza").click(function(event)
			{ 
				showPics('fuente_terraza', 'v');
			});
		}
		if(section=='2_1')
		{			
			$("#restaurante_general").click(function(event)
			{ 
				showPics('restaurante_general', 'h');
			});
			$("#patio_desde_ventana").click(function(event)
			{ 
				showPics('patio_desde_ventana', 'v');
			});
			$("#recepcion").click(function(event)
			{ 
				showPics('recepcion', 'h');
			});
			$("#patio_atardecer").click(function(event)
			{ 
				showPics('patio_atardecer', 'h');
			});
			$("#mesa_interior").click(function(event)
			{ 
				showPics('mesa_interior', 'v');
			});
		}
		if(section=='2_2')
		{			
			$("#antiguedad").click(function(event)
			{ 
				showPics('antiguedad', 'h');
			});
			$("#room1").click(function(event)
			{ 
				showPics('room1', 'h');
			});
			$("#room2").click(function(event)
			{ 
				showPics('room2', 'h');
			});
			$("#room3").click(function(event)
			{ 
				showPics('room3', 'h');
			});
			$("#room4").click(function(event)
			{ 
				showPics('room4', 'h');
			});
			$("#room5").click(function(event)
			{ 
				showPics('room5', 'h');
			});
			$("#room6").click(function(event)
			{ 
				showPics('room6', 'h');
			});
			$("#room7").click(function(event)
			{ 
				showPics('room7', 'h');
			});
			$("#room8").click(function(event)
			{ 
				showPics('room8', 'h');
			});
			$("#room9").click(function(event)
			{ 
				showPics('room9', 'h');
			});
			$("#room10").click(function(event)
			{ 
				showPics('room10', 'h');
			});
		}
		if(section=='2_3')
		{			
			$("#montaje1").click(function(event)
			{ 
				showPics('montaje1', 'h');
			});
			$("#montaje2").click(function(event)
			{ 
				showPics('montaje2', 'h');
			});
			$("#montaje3").click(function(event)
			{ 
				showPics('montaje3', 'h');
			});
			$("#montaje4").click(function(event)
			{ 
				showPics('montaje4', 'h');
			});
			$("#montaje5").click(function(event)
			{ 
				showPics('montaje5', 'h');
			});
			$("#montaje6").click(function(event)
			{ 
				showPics('montaje6', 'v');
			});
			}
		if(section=='2_4')
		{			
			$("#montaje1").click(function(event)
			{ 
				showPics('montaje1', 'h');
			});
			$("#montaje2").click(function(event)
			{ 
				showPics('montaje2', 'h');
			});
			$("#montaje3").click(function(event)
			{ 
				showPics('montaje3', 'h');
			});
			$("#montaje4").click(function(event)
			{ 
				showPics('montaje4', 'h');
			});
			$("#montaje5").click(function(event)
			{ 
				showPics('montaje5', 'h');
			});
			$("#montaje6").click(function(event)
			{ 
				showPics('montaje6', 'v');
			});
		}
		if((section=='5_1') || (section=='5_2') || (section=='5_3') || (section=='5_4'))
		{
			$('#foto_book').css('background-image' , 'url(images/5.jpg)');
			var title = chooseTittle(section);
			$('#area_title').html(title+"<br /><span class='loading'>Cargando im&aacute;genes...</span>");	
			readXML(section);
			$("#playb").click(function(event)
			{
				controlFoto('play');
			});
			$("#pauseb").click(function(event)
			{
				controlFoto('pause');
			});
			$("#fwdb").click(function(event)
			{
				controlFoto('fwd');
			});
			$("#rwdb").click(function(event)
			{
				controlFoto('rwd');
			});
		}
		else
		{
			$('#content').css({"background-image":"url(images/"+section+".jpg)"});
		}
	});
}
function backHome()
{
	intInterval = window.setInterval('doChange()', 1000*10);
	$('#content').html("<div id='content_band'></div>");
	$('#content').css('background-image' , 'url(images/roseton.jpg)');
}
function showPics(target, position) 
{
	$('#container').hide();	
	$('#foto_frame').show();
	if(position == 'v')
	{
		$('#foto_frame').html("<img src='images/full/"+target+".jpg' width='286' heigth='428'><div id='foto_frame_band'></div>");
	}
	if(position == 'h')
	{
		$('#foto_frame').html("<img src='images/full/"+target+".jpg' width='640' heigth='428'><div id='foto_frame_band'></div>");
	}
}
function hidePics()
{
	$('#container').show();
	$('#foto_frame').hide();
}
////AREA RESERVAS - VALIDADOR FORMULARIO Y CALENDARIOS
////////////////////////////////////////COMPROBAR MAIL
var errorList = new Array();

function compare_dates()  
{  
	var date1 = $("input[name=date1]").val();
	var date2 = $("input[name=date2]").val();
	var date1M = date1.substring(3, 5);  
	var date1D = date1.substring(0, 2);  
	var date1Y = date1.substring(6,10);  
	var date2M = date2.substring(3, 5);  
	var date2D = date2.substring(0, 2);  
	var date2Y = date2.substring(6,10);
	if (date2Y > date1Y)  
	{  
		return(true)
	}  
	else  
	{  
		if (date2Y == date1Y)  
		{   
			if (date2M > date1M)  
			{  
				return(true)  
			}  
			else  
			{   
				if (date2M == date1M)  
				{  
					if (date2D > date1D)
					{
						return(true);
					}
					else
					{
						//errorList.push("La fecha de salida no puede ser anterior a la de llegada");
						alert("La fecha de salida no puede ser anterior a la de llegada");
						return(false);
					}
				}
				else
				{
				//errorList.push("La fecha de salida no puede ser anterior a la de llegada");
				alert("La fecha de salida no puede ser anterior a la de llegada");
				return(false);
				}
			}
		}
		else
		{
			//errorList.push("La fecha de salida no puede ser anterior a la de llegada");
			alert("La fecha de salida no puede ser anterior a la de llegada");
			return(false);
		}
	//errorList.push("El año seleccionado es erroneo");	
	alert("El año seleccionado es erroneo");
	return false;
	}
} 
function emailValidator(){
var elem = $('#mail').val();
	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
	if((elem.match(emailExp))&&(elem.length != 0))
	{
		return true;
	}
	else
	{
		//errorList.push("El e-mail introducido tiene un formato incorrecto");
		alert("El e-mail introducido tiene un formato incorrecto");
		return false;
	}
}
////////////////////////////Más de dos caracteres y no vacio
function noEmpty(aTextField) 
{
	var thefield = $("input[name="+aTextField+"]").val();
	var totalchar = $("input[name="+aTextField+"]").val().length;
   	if ((totalchar != 0) || (thefield != null)) 
   	{
     	 return true;
   	}
   	else 
   	{ 
   		alert("El campo "+aTextField+" est&aacute; vacío; el contenido es "+thefield);
		return false; 
   	}
}
/////////////////////////////Comprobar fechas
function checkDate1()
{
	var patron = new RegExp("^[0-9]{2}/[0-9]{2}/[0-9]{4}$");
	var fecha = $("input[name=date1]").val();
	if(patron.test(fecha))
	{
		return true;
	}
	else
	{
		//errorList.push("Formato de fecha incorrecto.");
		alert("Formato de fecha incorrecto.");
		return false;
	}
}
function checkDate2()
{
	var patron = new RegExp("^[0-9]{2}/[0-9]{2}/[0-9]{4}$");
	var fecha = $("input[name=date1]").val();
	if(patron.test(fecha))
	{
		return true;
	}
	else
	{
		//errorList.push("Formato de fecha incorrecto.");
		alert("Formato de fecha incorrecto.");
		return false;
	}
}
/////////////////////////////
function checkGuests()
{
	var adultos = $('#adultos').val();
	if(adultos != 0)
	{
		return true;
	}
	else
	{
		//errorList.push("Debe seleccionar el n&uacute;mero de hu&eacute;spedes.");
		alert("Debe seleccionar el n&uacute;mero de hu&eacute;spedes.");
		return false;
	}
}
/////////////////////////////
function writeArrays(array)
{
	var errores = array.length;
	var theString = "Se han encontrado "+errores+" errores en su reserva:<br />";
	while ( array.length > 0 ) 
	{
		theString = theString+"<span class='loading'>- "+array.shift() + '</span><br />';
	}
	return theString;
}
function checkAllFields1() 
{
	//alert('comprobando formulario: acceso a verificación');	
	if(checkDate2())
	{
		//alert('Fecha 2 correcta');	
		if(checkDate1())
		{
			//alert('Fecha 1 correcta');	
			if(compare_dates())
			{
				//alert('Fecha 2 posterior a fecha 1');	
				if(checkGuests())
				{
					//alert('Número de invitados introducido');	
					if(noEmpty('nombre'))
					{
						//alert('Campo nombre rellenado');	
						if(noEmpty('apellido1'))
						{
							//alert('Campo apellido 1 rellenado');	
							if(noEmpty('apellido2'))
							{
								//alert('Campo apellido 2 rellenado');	
								if(emailValidator())
								{
									//alert('Campo email rellenado, formato correcto');	
									if(noEmpty('pais'))
									{
										//alert('Campo pais rellenado');
										if(noEmpty('provincia'))
										{
											//alert('Campo provincia rellenado');
											if(noEmpty('ciudad'))
											{
												//alert('Campo ciudad rellenado');
												if(noEmpty('calle'))
												{
													//alert('Campo calle rellenado');
													if(noEmpty('cp'))
													{
														//alert('Campo CP rellenado');
														$('#container').hide();
														$('#foto_frame').show();
														$('#foto_frame').css("padding-top", "30px");
														$('#foto_frame').css("padding-left", "50px");
														$('#foto_frame').html("Su reserva ha sido procesada<br /><span class='loading'>En breve nos pondremos en contacto con usted</span>");															
														return true;
													}														
												}												
											}	
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}
$('#container').hide();
$('#foto_frame').show();
$('#foto_frame').css("padding-top", "30px");
$('#foto_frame').css("padding-left", "50px");
$('#foto_frame').html(writeArrays(errorList));
return false;
}
/////////////////Fin área reservas
////////////////////////////////////
$(document).ready(function()
{
	$("#masia").click(function(event)
	{
		 loadContents('1');
	});
	$("#restaurante").click(function(event)
	{
		 loadContents('2_1');
	});
	$("#alojamiento").click(function(event)
	{
		 loadContents('2_2');
	});
	$("#eventos").click(function(event)
	{
		 loadContents('2_3');
	});
	$("#acceso").click(function(event)
	{
		 loadContents('3');
	});
	$("#tarifas").click(function(event)
	{
		 loadContents('4');
	});
	$("#masiafotos").click(function(event)
	{
		 loadContents('5_1');
		 resetSlide();
	});
	$("#habitaciones").click(function(event)
	{
		 loadContents('5_2');
		 resetSlide();
	});
	$("#entorno").click(function(event)
	{
		 loadContents('5_4');
		 resetSlide();
	});	
	$("#online").click(function(event)
	{
		 loadContents('6');
	});	
	$("#ofertas").click(function(event)
	{
		 loadContents('7');
	});
	$("#nochevieja").click(function(event)
	{
		 loadContents('8');
	});
	$("#novedades").click(function(event)
	{
		 loadContents('2_4');
	});
	intInterval = window.setInterval('doChange()', 300*10);
	adaptBodyBack();
	function doSomething() 
	{
		window.location.href = window.location.href;
	};
	var resizeTimer = null;
	$(window).bind('resize', function() 
	{
		if (resizeTimer) clearTimeout(resizeTimer);
		resizeTimer = setTimeout(doSomething, 100);
	});	
	$('li.headlink').hover(
		function() 
		{ 
			$('ul', this).css('display', 'block');
		},
		function() 
		{ 
			$('ul', this).css('display', 'none');
		}
	);
});
