var AGENDA = {};

AGENDA.calendario = function(mes, annio) {
	$.get("/backend/AGENDA.calendario.php", 
		{mes: mes, annio: annio, r: Math.random()}, 
		function(data) {	
			window.document.getElementById("AGENDA_calendario").innerHTML = data;
		}
	);
};

AGENDA.eventosPortada = function() {
	$("div[class=ZnotACT noticias_portada] div[class=noticia]").each(function (index) {
		if(index == 2 || index == 3) {
			$(this).addClass('noticia_evento50');
			$(this).find("div.not_foto").hide();
			$(this).find("div.not_foto peque.").show();
			$(this).find("div.lead-text").hide();
		}
	});
};	

AGENDA.cambioMes = function(nummes) {
	
	var annioSeleccionado = '';
	
	$("form#frmCalendario").find(".calendario_select").each(function(index) {
		if(index == 1) {
			if(this.selectedIndex >= 0) {
				annioSeleccionado = parseInt(this.value);
			}
		}
	});
	if(nummes < 10) nummes = '0' + nummes;
	if(nummes == 0) {
		AGENDA.calendario(12, parseInt(annioSeleccionado - 1));
	} else if(nummes == 13) {
		AGENDA.calendario('01', parseInt(annioSeleccionado + 1));
	} else {
		AGENDA.calendario(nummes, annioSeleccionado);
	}
};

AGENDA.enviarEvento = function() {
	var frm = document.forms['frmEvento'];
	frm.action = '/backend/AGENDA.enviar-evento.php';
	PRODUCTOS.envFormFichero(frm, 
		function(respuesta) {
			switch(respuesta) {
				
				case 'KO-1':
					alert("Ha ocurrido un error al intentar enviar el evento. Por favor, inténtalo más tarde.");
					break;
				case 'KO-2':
					alert("Ha ocurrido un error al intentar enviar la foto. Comprueba que la foto tiene formato jpg o gif y que no pesa más de 2MG.");
					break;
				case 'KO-3':
					alert("Tienes que estar logueado para enviar un evento.");
					break;
				default:
					alert("El evento se ha enviado correctamente. Gracias por tu participación!");
					//document.forms['frmEvento'].innerHTML = "El evento se ha enviado correctamente. Gracias por tu participación!";
					//var arr = respuesta.split('---');
					//window.document.location.href = arr[1];
					var arr = respuesta.split('---');
					if(arr[1] != null && arr[1] != 'undefined') {
						window.document.location.href = arr[1];
					} else {
						window.document.location.href = PRODUCTOS.edicion + '/agenda-eventos';
					}
					break;

			}
		}
	);
};

AGENDA.apuntarme = function (id_evento) {
	if(!PRODUCTOS.estaLogeado()) {
		$('#'+id_evento).slideDown('slow');
		return;
	}
	$.get(PRODUCTOS.edicion + "/backend/AGENDA.apuntarme-evento.php", 
		{id_evento: id_evento}, 
		function(data) {	
			alert(data);
			afrase = $('#num-usuarios-'+id_evento).html().split(' ');
			afrase[1]=++afrase[1];
			$('#num-usuarios-'+id_evento).html(afrase[0]+' '+afrase[1]+' '+afrase[2]);
		}
	);
};

AGENDA.enviarApuntar = function(id_evento) {
	
	var filter = /^[A-Za-z][A-Za-z0-9_.]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/;
	
	if ($('[name="nombre'+id_evento+'"]').val() == '') {
		alert('Por favor, introduce tu nombre');
		return;
	}
	if ($('[name="email'+id_evento+'"]').val() == '') {
		alert('Por favor, introduce tu email');
		return;
	}
	
	if (!filter.test($('[name="email'+id_evento+'"]').val())) {
        alert('El email introducido no es válido');
        return;
    }
	
	$.post('/backend/AGENDA.apuntarme-evento.php', 
		{
			nombre: $('[name="nombre'+id_evento+'"]').val(),
			id_evento: id_evento,
			email: $('[name="email'+id_evento+'"]').val(),
			localidad: $('[name="localidad'+id_evento+'"]').val(),
			telefono: $('[name="telefono'+id_evento+'"]').val(),
			url: document.location.href 
		},
		function(data) {
				
				alert(data);
				
				$(".art_herramientas a").removeClass("activo");
				$('#'+id_evento).slideUp('slow');
				$('[name="nombre'+id_evento+'"]').val('');
				$('[name="email'+id_evento+'"]').val('');
				$('[name="localidad'+id_evento+'"]').val('');
				$('[name="telefono'+id_evento+'"]').val('');
				afrase = $('#num-usuarios-'+id_evento).html().split(' ');
				afrase[1]=++afrase[1];
				$('#num-usuarios-'+id_evento).html(afrase[0]+' '+afrase[1]+' '+afrase[2]);
				
		}
	);
};

AGENDA.getNumUsuariosApuntados = function (id_evento) {
	$.get(PRODUCTOS.edicion + "/backend/AGENDA.num-usuarios-evento.php", 
		{id_evento: id_evento}, 
		function(data) {
			$("#num-usuarios-" + id_evento).html('- '+data);
		}
	);
};

AGENDA.cambiaCintillo = function(texto) {
	$("h2.encabezado_seccion").after(texto);
};
