// JavaScript Document

var pagina;

var seccion;

var imagenes = new Array("sala.jpg", "agua.jpg", "cartel.jpg", "pileta.jpg", "pileta2.jpg");

$(document).ready(inicializar);

function inicializar(){
	
	$(document).pngFix(); 
    
	//$("#contenido").load("hotel.php");
	
	$("#menu_sup_items a").click(call_page);
	
	$("#hotel").click(call_page);
	
	$("#hotel").click();
	
	$("#menu_sup_items li a").focus(blurear);	
	
}

function blurear()
{

$(this).blur();
	
}



function call_page(){
	
	seccion = $(this).attr("id");
	
	pagina = seccion + ".php";

    $("#contenido").html('<div style="text-align:center"><img src="imgs/logo/logo.png" width="235" height="104" /><br />Cargando '+seccion+'</div>');

    $("#contenido").load(pagina, "" , retorno);

    n = Math.floor(Math.random() * imagenes.length);

    $("#cabezal_img").css("background-image", "url('imgs/cabezal/" + imagenes[n] + "')");

    //$("#cabezal_img").css("background-image", "../imgs/cabezal/" + imagenes[n]);
	
}



function retorno(datos){
	
	$(document).pngFix(); 

    switch(seccion){
		
        case "bungalows":

            $("#p2 a").lightBox();
			$("#p3 a").lightBox();
			$("#p5 a").lightBox();

        break;
        
        case "contacto":
		
			$("#btnEnviar").click(enviarDatos);
			
        break;
		
        case "hotel":
		
			$("#par1 a").lightBox();

        break;

        case "reservas":

        $('.datepicker').datepicker({
		changeMonth: true,
		changeYear: true,
		dateFormat: "DD',' d 'de' MM 'de' yy",
		minDate: 0,
		maxDate: '+12M +10D',
		dayNames: ['Domingo', 'Lunes', 'Martes', 'Miercoles', 'Jueves', 'Viernes', 'Sabado'],
		dayNamesMin: ['Do', 'Lu', 'Ma', 'Mi', 'Ju', 'Vi', 'Sa'],
		monthNames: ['Enero','Febrero','Marzo','Abril','Mayo','Junio','Julio','Agosto','Setiembre','Octubre','Noviembre','Diciembre'],
		monthNamesShort: ['Ene','Feb','Mar','Abr','May','Jun','Jul','Ago','Set','Oct','Nov','Dic']

        }
        );
		
		$("#botonEnvio").click(enviarReserva);

        break;


    }

}


function enviarDatos(){

    //alert($("#nombre").attr("value"));

	if(($("#nombre").attr("value") != "")&& ($("#mail").attr("value") != "")&& ($("#telefono").attr("value") != "")){
		
		if (/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test($("#mail").attr("value"))){

			 dataToSend = ({
				 nombre : $("#nombre").attr("value"),
				 mail : $("#mail").attr("value"),
				 telefono : $("#telefono").attr("value"),
				 comentarios : $("#comentarios").attr("value")
	
	
			});
	
			conexAjax("Scripts/contacto.php", "POST", "json", dataToSend, mostrarMensaje, mostrarOK, verError);
			
			$("#nombre").attr("value", "");
			$("#mail").attr("value", "");
			$("#telefono").attr("value", "");
			$("#comentarios").attr("value", "");
			
		}else{
			
			$("#aviso").html("El mail tiene formato incorrecto.");
        	$("#aviso").fadeIn();
		
		}

    }else{

		$("#aviso").html("Debes completar todos los campos.");
        $("#aviso").fadeIn();


    }
	
}




function enviarReserva(){

	if(($("#nombre").attr("value") != "")&& ($("#mail").attr("value") != "")&& ($("#telefono").attr("value") != "")){
		
		if (/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test($("#mail").attr("value"))){

			 dataToSend = ({
				 nombre : $("#nombre").attr("value"),
				 mail : $("#mail").attr("value"),
				 telefono : $("#telefono").attr("value"),
				 desde : $("#desde").attr("value"),
				 hasta : $("#hasta").attr("value"),
				 personas : $("#select").attr("value"),
				 comentarios : $("#comentarios").attr("value")
	
	
			});

       		conexAjax("Scripts/reserva.php", "POST", "json", dataToSend, mostrarMensaje, mostrarOK, verError);
			
			$("#nombre").attr("value", "");
			$("#mail").attr("value", "");
			$("#telefono").attr("value", "");
			$("#desde").attr("value", "");
			$("#hasta").attr("value", "");
			$("#select").attr("value", "");
			$("#comentarios").attr("value", "");
			
		}else{
			
			$("#aviso").html("El mail tiene formato incorrecto.");
        	$("#aviso").fadeIn();
			
		}


    }else{

		$("#aviso").html("Debes completar todos los campos.");
        $("#aviso").fadeIn();


    }
	
}



function conexAjax(dirUrl, tipoCon, tipoDatos, datos, antesEnv, exitoRec, errores){

    $.ajax({
        url: dirUrl,
        type: tipoCon,
        dataType:tipoDatos,
        data: datos,
        beforeSend: antesEnv,
        success: exitoRec,
        error: errores

    })

}

function mostrarMensaje()
{

    $("#aviso").html("Enviando datos...");
    $("#aviso").fadeIn();

}

function mostrarOK(datos)
{

    if(datos){

        $("#aviso").html("Gracias, su mensaje ha sido enviado, a la brevedad nos pondremos en contacto.");
        $("#aviso").css("color","#0C0");
		$("#aviso").fadeIn();

    }else{

        $("#aviso").html("Ocurri&oacute; un error al enviar los datos.");
		$("#aviso").fadeIn();

    }

}

function verError()
{

    $("#aviso").html("Ocurri&oacute; un error al enviar los datos.");
	$("#aviso").fadeIn();

}
