// JavaScript Document

window.addEvent('domready', function() {
	if($('container-agenda') != null){
		Calendar.setup(
		 {
		   flat         : "container-agenda", // ID of the parent element
		   flatCallback : dateChanged           // our callback function
		 }
		);
	}
	if($('contenedor-listado-videos') != null){
		obtenerListadoVideos(1);
	}
	
});

function obtenerListadoVideos(pagina){
	var req = new Request({
		url: '/ajax.php?accion=obtenerListadoVideos',
		method: 'post',
		data:
		{
			pagina: pagina
		},
		onSuccess: function(txt){
			$('contenedor-listado-videos').innerHTML = txt;
		},
		onFailure: function(){
			$('contenedor-listado-videos').set('text', 'Ocurrio un error.');
		}
	}).send();
	$('contenedor-listado-videos').innerHTML = '<div style="padding-left: 70px; padding-top: 20px"><img src="skins/ajax-loader.gif" alt="Cargando..." /></div>';
	
}

function enviarComentario(tipo_comentario){

	if($('nombre_comentario').value == ''){
		alert("Por favor ingresa tu nombre");
		return false;
	}
	if(!checkMail($('email_comentario').value)){
		alert("Por favor ingresa un email valido");
		return false;
	}
	if($('mensaje_comentario').value == ''){
		alert("Por favor ingresa tu comentario");
		return false;
	}
	
	var req = new Request({
		url: '/ajax.php?accion=saveComentario',
		method: 'post',
		data:
		{
			tipo: tipo_comentario,
			id_tipo: $('id_tipo_comentario').value,
			nombre: $('nombre_comentario').value,
			email: $('email_comentario').value,
			comentario: $('mensaje_comentario').value
		},
		onSuccess: function(txt){
			$('div_comentarios').innerHTML = txt;
		},
		onFailure: function(){
			$('div_comentarios').set('text', 'Ocurrio un error.');
		}
	}).send();
	
}

function obtenerAgenda(fecha, pagina){
	var req = new Request({
		url: '/ajax.php?accion=obtenerAgenda',
		method: 'post',
		data:
		{
			fecha: fecha,
			pagina: pagina
		},
		onSuccess: function(txt){
			$('container-datos-agenda').innerHTML = txt;
		},
		onFailure: function(){
			$('container-datos-agenda').set('text', 'Ocurrio un error.');
		}
	}).send();
	//$('container-datos-agenda').innerHTML = '<div style="text-align:center"><img src="skins/ajax-loader.gif" alt="Cargando..." /></div>';
}

function dateChanged(calendar) {
 // Beware that this function is called even if the end-user only
 // changed the month/year. In order to determine if a date was
 // clicked you can use the dateClicked property of the calendar:
 if (calendar.dateClicked) {
   var y = calendar.date.getFullYear();
   var m = calendar.date.getMonth();     // integer, 0..11
   var d = calendar.date.getDate();      // integer, 1..31
   m++; // El mes va de 0 a 11
   if(m < 10){
	   m = '0' + m;
   }
   if(d < 10){
	   d = '0' + d;
   }
	obtenerAgenda(y+"-"+m+"-"+d, 1);
 }
}
  
function switchVideo(id){
	var divs = document.getElementsByTagName('div');
	for (var i = 0; i < divs.length; i++){
		if(divs[i].id.substring(0, 22) == "div_reproductor_video_"){ 
			$(divs[i].id).style.display = 'none';
		}
	}	
	$('div_reproductor_video_'+id).style.display = 'inline';
}

function switchImagen(id){
	var divs = document.getElementsByTagName('div');
	for (var i = 0; i < divs.length; i++){
		if(divs[i].id.substring(0, 21) == "div_galeria_imagenes_"){ 
			$(divs[i].id).style.display = 'none';
		}
	}	
	$('div_galeria_imagenes_'+id).style.display = 'inline';
}

function submirRegistroNewsletter(){
	if(!checkMail($('email_newsletter').value)){
		alert("Por favor ingresa un email valido");
	}else{
		if($('nombre_newsletter') != null){
			nombre = $('nombre_newsletter').value;
		}else{
			nombre = '';	
		}
		var req = new Request({
			url: 'ajax.php?accion=registrarNewsletter',
			method: 'post',
			data:
			{
				nombre: nombre,
				email: $('email_newsletter').value
			},
			onSuccess: function(txt){
				$('registro_newsletter').innerHTML = '<br /><br /><h3><b>'+txt+'</b></h3>';
			},
			onFailure: function(){
				$('registro_newsletter').set('text', 'Ocurrio un error.');
			}
		}).send();
		$('registro_newsletter').innerHTML = '<div style="padding-left: 70px; padding-top: 20px"><img src="skins/ajax-loader.gif" alt="Cargando..." /></div>';
	}
}

function submirRegistroRecomendados(){
	if(!checkMail($('email_recomendado').value) || !checkMail($('amigo_email_recomendado').value) ){
		alert("Por favor ingresa un email valido");
	}else{
		if($('nombre_recomendado') != null){
			nombre = $('nombre_recomendado').value;
		}else{
			nombre = '';	
		}
		if($('amigo_nombre_recomendado') != null){
			amigo = $('amigo_nombre_recomendado').value;
		}else{
			amigo = '';	
		}
		var req = new Request({
			url: 'ajax.php?accion=registrarRecomendado',
			method: 'post',
			data:
			{
				nombre: nombre,
				amigo:	amigo,
				email :	$('email_recomendado').value,
				emailamigo: $('amigo_email_recomendado').value
			},
			onSuccess: function(txt){
				$('Recomenda').innerHTML = '<br /><br /><h3><b>'+txt+'</b></h3>';
			},
			onFailure: function(){
				$('Recomenda').set('text', 'Ocurrio un error.');
			}
		}).send();
		$('Recomenda').innerHTML = '<div style="padding-left: 70px; padding-top: 20px"><img src="skins/ajax-loader.gif" alt="Cargando..." /></div>';
	}
}




function checkMail(x){
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(x)){
		return true;
	}else{
		return false;
	}
}

function agregarFavoritos() {
	title = "Martin Sabbatella"
	url = "http://www.enmoronlohicimos.com";
	if (window.sidebar) // firefox
		alert("Para agregar el sitio a favoritos presione control + d");
	else if(window.opera && window.print){ // opera
		var elem = document.createElement('a');
		elem.setAttribute('href',url);
		elem.setAttribute('title',title);
		elem.setAttribute('rel','sidebar');
		elem.click();
	}
	else if(document.all)// ie
		window.external.AddFavorite(url, title);
}

