window.addEvent('domready', function () {
	
	if ($('form-login')) form_login();
	if ($('form-registro')) form_registro();
	if ($('titulares-noticias')) fade_noticias();
	
	$('home').addEvent('click', function () { location.href = base_url; });
	
	crear_menu();
	
});

// Envio por ajax formulario login
var intentos = 0;
function form_login ()
{
    $('form-login').addEvent('submit', function (e) {
        new Event(e).stop();
        var request = new Request({
                url: base_url + 'log-in/',
                method: 'post',
                data: {
                    usuario: $('usuario').get('value'),
                    pass: $('pass').get('value')
                },
                onSuccess: function(response) {
                    response = JSON.decode(response);
                    intentos++;
                    if (intentos >= 3 && response.status != 'ok') {
                        if (confirm('Has olvidado la contraseña? Pulsa aceptar para recuperarla.')) window.location.href = base_url + "usuarios/recordar-clave/"
                    } else {
                        alert(response.mensaje);
                        if (response.status == 'ok') window.location.reload();
                    }
                }
        }).send();
    });	
}

// Validación formulario registro
function form_registro ()
{
    $('form-registro').addEvent('submit', function (e) {
        new Event(e).stop();
        if ($('nombre').get('value') == '') { alert('Debe introducir su nombre.'); return false; }
        if ($('apellidos').get('value') == '') { alert('Debe introducir sus apellidos.'); return false; }
        if ($('email').get('value') == '') { alert('Debe introducir su correo electrónico.'); return false; }
        if ($('dni').get('value') == '') { alert('Introducir el DNI no es obligatorio, pero si es requisito para poder participar en los concursos.'); }
        if ($('login').get('value') == '') { alert('Debe introducir un nombre de usuario.'); return false; }
        if ($('password').get('value') == '') { alert('Debe introducir una contraseña.'); return false; }
        if ($('r_password').get('value') == '') { alert('Debe repetir la contraseña introducida.'); return false; }
        if ($('password').get('value') != $('r_password').get('value')) { alert('Las contraseñas introducidas no coinciden.'); return false; }
        if (!$('acepto').checked) { alert('Debes aceptar los términos del contrato.'); return false; }
        this.submit();
    });		
}

// Fade titulares noticias
activeImage = 'img1'; 

function fade_noticias ()
{
	// Activamos la imagen por defecto  
	$(activeImage).set('opacity', 1);
	$('img2').set('opacity', 0);
	$('img3').set('opacity', 0);
	$('img4').set('opacity', 0);
        
        // Cambiamos la opacidad del letrero
        $('titular').set('opacity', 0.75);
        //$('titulo-tit').set('opacity', 1);
        
	// Preparamos los enlaces
	$$('a.noticia').each(function (el) {
		el.addEvent('mouseenter', function (e) {
			new Event(e).stop();
			
			var imagen = this.get('rel');
			$(activeImage).fade('out');
			$(imagen).fade('in');
			activeImage = imagen;

                        $('titulo-tit').set('html', $('titulo-tit').retrieve(imagen));
                        $('texto-tit').set('html', $('texto-tit').retrieve(imagen));
                        
                        $('a-noticia').set('href', this.get('href'));
                        $('a-noticia').set('title', this.get('title'));
		});
	});
}

// Menu
function crear_menu () {
	$('menu').getElements('li.menu').each( function( elem ){
		var list = elem.getElement('ul.links');
		var myFx1 = new Fx.Slide(list).hide();
		elem.addEvents({
			'mouseenter' : function(){
				myFx1.cancel();
				myFx1.slideIn();
			},
			'mouseleave' : function(){
				myFx1.cancel();
				myFx1.slideOut();
			}
		});
	})
};

// Resultado porra
function pronostico (el)
{
    var ele = $(el.id);
    var val = ele.get('value');
    val = val.replace(/\D/, '');
    ele.set('value', val);
    if (val != '') {
        new Request.JSON({
                url: base_url + 'pronostico-porra/',
                onSuccess: function (response)
                {
                    if (response.status == 1) {
                        alert(response.error);
                        ele.set('value', response.valor)
                    }
                }
        }).post({'campo': ele.get('id'), 'valor': val});
    }
}
