String.prototype.lpad = function(padString, length) {
	var str = this;
    while (str.length < length)
        str = padString + str;
    return str;
}


function alta_newsletter(email){
    if (email == ''){
        $('alta_news').setStyle({'color': 'red'});
        $('alta_news').value = 'Su Correo Electrónico';
    }
    else{
        var regMail = /^[A-Za-z][A-Za-z0-9_\-\.]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.\-]+[A-za-z]$/;
        if (regMail.test(email)){
            var url = "includes/guarda_newsletter.php?email=" + email;
            new Ajax.Request(url, {
              method: 'get',
              onComplete: function(transport) {
                    new Effect.BlindDown($('news_alta'), {duration: 0.5});
              }
            });
        }
        else{
            alert("Introduzca su direccion de correo electronico");
            return;
        }

    }
}

function cerrar_news(){
    new Effect.BlindUp($('news_alta'), {duration: 0.5});
}

function changeLang(idioma){
    var url = "traducciones/changeLang.php?lang=" + idioma;
    new Ajax.Request(url, {
      method: 'get',
      onComplete: function(transport) {
            window.location.reload();
      }
    });
}

function quitarLoading(){
  $('sombra_traduccion').hide();
  $('traducir').hide();
}


function mostrar_video(){
    new Effect.Parallel([
        new Effect.Morph('vision_video', {
          style: 'height: 435px;',
          duration: 0.8,
          beforeStart: function(){
              new Ajax.Request('includes/cargar_video.php', {
                  method: 'get',
                  onComplete: function(transport) {
                        $('cont_video').update(transport.responseText);
                        
                  },
                  onSuccess: function(transport){
                      new Effect.Appear('cerrar_video', {duration: 0.3});
                  }
                });
          }
        }),
        new Effect.Morph('cont_video',{
            style: 'top: 0px;',
            duration: 0.8,
            beforeStart: function(){
                new Effect.Fade('cont_play', {duration: 0.7});
            }
        })
    ]);
    new Effect.ScrollTo('video');

}

function cerrar_video(){
    new Effect.Parallel([
        new Effect.Morph('vision_video', {
          style: 'height: 295px;',
          duration: 0.8
        }),
        new Effect.Morph('cont_video',{
            style: 'top: -435px;',
            duration: 0.8,
            beforeStart: function(){
                new Effect.Appear('cont_play', {duration: 0.7});
            },
            afterFinish: function(){
                $('cont_video').update('');
            }
        })
    ]);
    new Effect.ScrollTo('inicio');

}

function start_slideshow(start_frame, end_frame, delay, lis) {
	setTimeout(fadeInOut(start_frame,start_frame,end_frame, delay, lis), delay);
}


function fadeInOut(frame, start_frame, end_frame, delay, lis) {
	return (function() {
		lis = $('slide-images').getElementsByTagName('li');
		Effect.Fade(lis[frame]);
		if (frame == end_frame) { frame = start_frame; } else { frame++; }
		lisAppear = lis[frame];
		setTimeout("Effect.Appear(lisAppear, {duration: 0.9});", 0);
		setTimeout(fadeInOut(frame, start_frame, end_frame, delay), delay + 1850);
	})

}

function init() {
	var lis = $('slide-images').getElementsByTagName('li');

	for( i=0; i < lis.length; i++){
		if(i!=0){
			lis[i].style.display = 'none';
		}
	}
	end_frame = lis.length -1;
	start_slideshow(start_frame, end_frame, delay, lis);
    
	
}

function enviar_contacta(){
    if ($('nombre').value == ''){
        alert('Introduzca su nombre de contacto');
        return false;
    }
    if ($('apellidos').value == ''){
        alert('Introduzca sus apellidos de contacto');
        return false;
    }
    if ($('email').value == ''){
        alert('Introduzca su direccion de correo electronico');
        return false;
    }
    if ($('telefono').value == ''){
        alert('Introduzca su numero de telefono');
        return false;
    }
    
    var form = $('frm_contacto');

   var url = "includes/enviar_contacto.php?" + form.serialize();
   new Ajax.Request(url, {
          method: 'get',
          onCreate: function(){
              var url = "includes/enviando_mail.php";
            new Ajax.Request(url, {
                  method: 'get',
                  onComplete: function(transport) {
                        $('cont_form_contacto').update(transport.responseText);
                  }
                });
          },
          onComplete: function(transport) {
                if (transport.responseText == "OK"){
                    $('cont_form_contacto').update(transport.responseText.replace("OK", ""));
                }
                else{
                    $('cont_form_contacto').update(transport.responseText.replace("OK", ""));
                }
                //new Effect.BlindDown($('news_alta'), {duration: 0.5});
          }
        });
   return false;
}

function mostrar_informacion(idioma, elemento){
	var sombra = document.createElement('div');
	var contenedor = document.createElement('div');
	var contenedor_titulo = document.createElement('div');
	var contenedor_contenido = document.createElement('div');
	
	sombra.setAttribute('id', 'sombra_reservation');
	contenedor_titulo.setAttribute('id', 'titulo_reservation');
	contenedor_titulo.innerHTML="<ul><li style='margin-left: -30px;'>Tarifas y Disponibilidades Camping Almafra Resort</li><li onclick='cerrar_reserva()' id='btn_cerrar_reserva'><img onclick='cerrar_reserva()' src='imgs/cerrar_reservas.png' /></li></ul>";
	contenedor_contenido.setAttribute('id', 'contenido_reservation');
	
	contenedor.setAttribute('id', 'contenedor_reservation');
	
	contenedor.appendChild(contenedor_titulo);
	contenedor.appendChild(contenedor_contenido);
	
	Element.extend(sombra);
	sombra.addClassName('sombra_reserva').hide();

	Element.extend(contenedor);
	contenedor.addClassName('cont_reserva').hide();
	
	document.body.appendChild(sombra);
	sombra.observe('click', cerrar_reserva);
	new Effect.Appear(sombra,{duration: 0.5, from: 0, to: 0.7});
	
	document.body.appendChild(contenedor);
	new Effect.Appear(contenedor, {duration: 0.5,
		afterFinish: function(){
			if (idioma == 'es')
				idioma = 'e';
			else if (idioma == 'en')
				idioma = 'u';
			else if (idioma == 'fr')
				idioma = 'f';
			else if (idioma == 'de')
				idioma = 'a';
			else if (idioma == 'nl')
				idioma = 'h';
			ifrm = document.createElement("iframe");
			if (elemento == "mobilhome")
				ifrm.setAttribute("src", "http://www.secureholiday.net/info.asp?site_frame=1&nc=5311&lg=" + idioma + "&t_pro=L");
			else
				ifrm.setAttribute("src", "http://www.secureholiday.net/tariffdetail.asp?site_frame=1&nc=5311&lg=" + idioma + "&particular=" + elemento);
			ifrm.setAttribute("name", "ventana_reservas");
			ifrm.style.border = '0px';
			ifrm.style.width = 750+"px";
			ifrm.style.height = 475 +"px";
			$('contenido_reservation').appendChild(ifrm);
		}
	});
	
}

function mostrar_reserva(idioma, tipo){
	var sombra = document.createElement('div');
	var contenedor = document.createElement('div');
	var contenedor_titulo = document.createElement('div');
	var contenedor_contenido = document.createElement('div');
	
	sombra.setAttribute('id', 'sombra_reservation');
	contenedor_titulo.setAttribute('id', 'titulo_reservation');
	contenedor_titulo.innerHTML="<ul><li style='margin-left: -30px;'>Sistema de Reservas Camping Almafra Resort <span onclick='document.location.href=\"contacto.php?obs=Informacion desde Website\"' style='font-size: 0.6em; cursor: pointer;'>[ &iquest;Tiene alguna duda?, contacte con nosotros]</span></li><li onclick='cerrar_reserva()' id='btn_cerrar_reserva'><img onclick='cerrar_reserva()' src='imgs/cerrar_reservas.png' /></li></ul>";
	contenedor_contenido.setAttribute('id', 'contenido_reservation');
	
	contenedor.setAttribute('id', 'contenedor_reservation');
	
	contenedor.appendChild(contenedor_titulo);
	contenedor.appendChild(contenedor_contenido);
	
	Element.extend(sombra);
	sombra.addClassName('sombra_reserva').hide();

	Element.extend(contenedor);
	contenedor.addClassName('cont_reserva').hide();
	
	document.body.appendChild(sombra);
	sombra.observe('click', cerrar_reserva);
	new Effect.Appear(sombra,{duration: 0.5, from: 0, to: 0.7});
	
	document.body.appendChild(contenedor);
	new Effect.Appear(contenedor, {duration: 0.5,
		afterFinish: function(){
			var dia = $('dia').value.lpad("0", 2);
			var mes = $('mes').value.lpad("0", 2);
			var anyo = $('anyo').value;
			cargar_contenido_reserva(dia, mes, anyo, idioma, tipo);
		}
	});
	
	
}

function cerrar_reserva(){
	new Effect.Fade('contenedor_reservation', {duration: 0.5,
		afterFinish: function(){
			new Effect.Fade('sombra_reservation', {duration: 0.5,
				afterFinish: function(){
					try{
						document.body.removeChild($('contenedor_reservation'));
						document.body.removeChild($('sombra_reservation'));
					}
					catch (ex){
						null;
					}
				}
			});
			
		}
	});
	
}

function cargar_contenido_reserva(dia, mes, anyo, idioma, tipo){
	if (idioma == 'es')
		idioma = 'e';
	else if (idioma == 'en')
		idioma = 'u';
	else if (idioma == 'fr')
		idioma = 'f';
	else if (idioma == 'de')
		idioma = 'a';
	else if (idioma == 'nl')
		idioma = 'h';
	ifrm = document.createElement("iframe");
	if (tipo == null)
		ifrm.setAttribute("src", "http://www.secureholiday.net/reservation.asp?nc=5311&lg=" + idioma + "&re=E@" + dia +"@" + mes +"@" + anyo + "&site_frame=1&bloc_recherche=0");
	else if (tipo == 'mobilhome')
		ifrm.setAttribute("src", "http://www.secureholiday.net/reservation.asp?nc=5311&lg=" + idioma + "&re=L@" + dia +"@" + mes +"@" + anyo + "&site_frame=1&bloc_recherche=0");
		//ifrm.setAttribute("src", "http://www.secureholiday.net/reservation.asp?nc=5311&jd=" + dia +"&md=" + mes +"&ad=" + anyo + "&lg=" + idioma + "&site_frame=1&rec=L");
	else if (tipo != null)
		ifrm.setAttribute("src", "http://www.secureholiday.net/reservation.asp?nc=5311&lg=" + idioma + "&re=E@" + dia +"@" + mes +"@" + anyo + "&site_frame=1&bloc_recherche=0");
		//ifrm.setAttribute("src", "http://www.secureholiday.net/reservation.asp?nc=5311&jd=" + dia +"&md=" + mes +"&ad=" + anyo + "&lg=" + idioma + "&site_frame=1&rec=E");
	ifrm.style.border = 'none';
	ifrm.style.width = 750+"px";
	ifrm.style.height = 475 +"px";
	$('contenido_reservation').appendChild(ifrm); 
}

function load_ofertas(){
	//Effect.ScrollTo("cab_ofertas", { duration: 1.0, offset:-130 });
	new Effect.Appear('cont_ofertas_es', {duration: 1.0});
	
	new Effect.Appear('cont_ofertas_de', {duration: 1.0});
	
	new Effect.Appear('cont_ofertas_en', {duration: 1.0,
		afterFinish: function(){
			new Effect.Fade('cont_ofertas_es', {duration: 1.0});
		}
	});
	
	new Effect.Appear('cont_ofertas_nl', {duration: 1.0,
		afterFinish: function(){
			new Effect.Fade('cont_ofertas_de', {duration: 1.0});
		}
	});
	
	new Effect.Appear('cont_ofertas_fr', {duration: 1.0,
		afterFinish: function(){
			new Effect.Fade('cont_ofertas_en', {duration: 1.0,
				afterFinish: function(){
					new Effect.Fade('cont_ofertas_nl', {duration: 1.0,
						afterFinish: function(){
							new Effect.Fade('cont_ofertas_fr', {duration: 1.0,
								afterFinish: function(){
									
										new Effect.Parallel([
										    new Effect.Appear('caja1', {duration: 0.8}),
										    new Effect.Move('caja1', {x: -1000, y: 0})
										                     
										], {duration: 1.0,
											afterFinish: function(){
												new Effect.Move('sep_caja1', {x: -1300, y:0, duration: 0.5});
												new Effect.Parallel([
													    new Effect.Appear('caja2', {duration: 0.8}),
													    new Effect.Move('caja2', {x: -1000, y: 0})
													                     
													], {duration: 1.0,
														afterFinish: function(){
															new Effect.Move('sep_caja2', {x: -1300, y:0, duration: 0.5});
															new Effect.Parallel([
															    new Effect.Appear('caja3', {duration: 0.8}),
															    new Effect.Move('caja3', {x: -1000, y: 0})
															                     
															], {duration: 1.0,
																afterFinish: function(){
																	new Effect.Move('sep_caja3', {x: -1300, y:0, duration: 0.5});
																	
																}
															});
														}
													});
											}
										});
									
								}
							});
						}
					});
				}
			});
		}
	});
}

