﻿Date.prototype.goto = function(n) {
    this.setTime(this.getTime() + n * 86400000);
    return this;
};

Date.prototype.DateDiff = function(endDate) {
    var ONE_DAY = 1000 * 60 * 60 * 24;
    var date1_ms = this.getTime();
    var date2_ms = endDate.getTime();
    var difference_ms = Math.abs(date2_ms - date1_ms);
    return Math.round(difference_ms / ONE_DAY);
};

$(function() {
	$.simpleWeather({
		zipcode: 'SPXX0165',
		unit: 'c',
		success: function(weather) {
			html = '<h4>El tiempo en '+weather.city+'</h4>';
			html +='<img src="'+weather.thumbnail+'" width="45" height="25"><span>Hoy:</span> Máx. ' + weather.high + ' &deg; | Mín. ' + weather.low + '&deg;<br />';
			html +='<img src="'+weather.tomorrow.thumbnail+'" width="45" height="25"><span style="color:yellow;font-weight:bold;">Mañana:</span> Máx. ' + weather.tomorrow.high + ' &deg; | Mín. ' + weather.tomorrow.low + '&deg; ';
			$("#weather").html(html);
		},
		error: function(error) {
			$("#weather").html('<p>'+error+'</p>');
		}
	});
	
	
    $('input.date').datepicker({
        dateFormat: 'dd/mm/yy',
        firstDay: 1,
        minDate: +1,
		showOn: "button",
		buttonImage: "js/calendar.png",
		buttonImageOnly: true,
        numberOfMonths: [2, 2],
        onSelect: function(dateText) {
            var newDate = $.datepicker.parseDate('dd/mm/yy', dateText);
            if ($('.date').index(this) == 0) {
                $('.date:last').datepicker('option', 'minDate', newDate.goto(1));
                var checkOut = $('.date:last').datepicker('getDate');
                if (newDate > checkOut)
                    $('.date:last').datepicker('setDate', newDate.goto(1));
            }
            else {
                $('.date:first').datepicker('option', 'maxDate', newDate.goto(-1));
                var checkIn = $('.date:first').datepicker('getDate');
                if (newDate < checkIn)
                    $('.date:first').datepicker('setDate', newDate.goto(-1));
            }

            var newStage = $('.date:first').datepicker('getDate').DateDiff($('.date:last').datepicker('getDate'));
            $('#s').val(newStage);
        }
    });

    var lang = "es";
    $('#datepicker').datepicker('option', $.extend({ showMonthAfterYear: false }, $.datepicker.regional[lang]));


    $('input.date:first').datepicker('setDate', +7);
    $('input.date:last').datepicker('setDate', +12);	
	 
	$('#webs').change(function() { document.location = $(this).val(); });
	
	SexyLightbox.initialize({color:'black', dir: 'estilos/sexyimages'});
	
	
	function valido(){
		var formato=ES_es;
		if (!isEmail(document.forms[0].mail, 'E-mail', true , true, true, true, null, 250)) 
			return false;
		if (!isTexto(document.forms[0].nombre, 'Nombre', true , true, true, true, null, 255)) 
			return false;		
		/*if (!isTexto(document.forms[0].telefono, 'Teléfono', true , true, true, true, null, 255)) 
			return false;*/
		if (!isTexto(document.forms[0].observaciones, 'Observaciones', true , true, true, true, null, 255)) 
			return false;			
		return true;
	}
	
	function comprobar(){
		if (valido() && confirm("¿Enviar Formulario?")){
			document.forms[0].accion.value="n";
			document.forms[0].submit();
		}
	}
	
	$(".enviar").click(function(evento){
		evento.preventDefault();
		comprobar();
	});	
	
	$(".resetear").click(function(evento){
		evento.preventDefault();
		$("#formularioacceso")[0].reset();
	});
	
	$('#galeria').cycle({
		fx: 'fade',
		timeout: 5000,
		speed: 1750
	});	
});
