// Enlaces externos
function fix_external ()
{
    $$('a[rel=external]').each(function (el) {
        el.set('target', '_blank');
    });
}

// Validación formulario boletín
function check_form_boletin ()
{

    if ($('nombre').get('value') == '') { $('nombre').addClass('error'); alert('Debe escribir su nombre.'); return false; }
    else { $('nombre').removeClass('error'); }

    if ($('apellidos').get('value') == '') { $('apellidos').addClass('error'); alert('Debe escribir sus apellidos.'); return false; }
    else { $('apellidos').removeClass('error'); }

    var email = $('email').get('value');
    if ( email == '' || !es_correo(email)) { $('email').addClass('error'); alert('Debe escribir su correo electrónico.'); return false; }
    else { $('email').removeClass('error'); }

    return true;

}

// Comprueba si es un correo válido
function es_correo(str) {

	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)


	if (str.indexOf(at)==-1 || str.indexOf(at)==0 ||
	str.indexOf(at)==lstr){
	   return false;
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 ||
	str.indexOf(dot)==lstr){
	    return false;
	}

	if (str.indexOf(at,(lat+1))!=-1){
	    return false;
	 }

	 if (str.substring(lat-1,lat)==dot ||
	 str.substring(lat+1,lat+2)==dot){
	    return false;
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
	    return false;
	 }

	 if (str.indexOf(" ")!=-1){
	    return false;
	 }

	 return true;
}

// Enlaces imágenes noticia
function cambiar_imagen (id)
{
    $$('img.slide').each(function (el) {
	el.setStyle('display', 'none');
    });

    $('img-'+id).setStyle('display', 'block');
}

// Iniciamos los enlaces para las imágenes en el detalle de las noticias
function enlaces_noticias ()
{
    var x = 0;
    $$('#nav-img a').each(function (el) {
	el.set('href', 'javascript:void(0)');
	el.addEvent('click', function () {
	    cambiar_imagen(el.get('html') - 1);
	});
	x++;
    });
}

// Slideshow
var Fader = new Class({
        Implements: Options,
        options: {
                pause: 5000,
                duration: 1000,
                loop: true,
                onComplete: Class.empty,
                onStart: Class.empty
        },
        initialize: function(container,options) {
                this.setOptions(options);
                this.container = $(container);
                this.imgs = this.container.getElements('img');
                this.imgs.setStyles({
                        'position':'absolute',
                        'top':0,
                        'left':0,
                        'opacity':0
                });
                this.imgs[0].setStyle('opacity',1);
                this.el = new Element('div',{'styles': {
                        'position':'relative'
            }});
            this.el.injectInside(this.container);
            this.el.adopt(this.imgs);
                this.next = 0;
                this.start();
        },
        start: function() {
                this.show();
                this.periodical =
this.show.bind(this).periodical(this.options.pause);
        },
        stop: function() {
                $clear(this.periodical);
        },
        show: function() {
                if (!this.options.loop && this.next==this.imgs.length-1)
		this.stop();
                this.next = (this.next==this.imgs.length-1)?0:this.next+1;
                var prev = (this.next==0)?this.imgs.length-1:this.next-1;

                this.imgs[this.next].fade('in');
                this.imgs[prev].fade('out');
        }

});

window.addEvent('domready', function () {

    fix_external();

    $('banner_rot').setStyle('display', 'block');
    var fader = new Fader('banner_rot');
    fader.start();

    if ($('slideshow')) { var fader1 = new Fader('slideshow'); }

    if ($('galeria-zona')) initImageZoom();
    if ($('nav-img')) enlaces_noticias();

});
