// Slideshow
var Fader = new Class({
    Implements: Options,
    options: {
	pause: 7000,
	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.curr = 0;
	this.next = 1;
	this.num_imgs = this.imgs.length - 1;
	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.num_imgs) this.stop();

	this.next = (this.curr == this.num_imgs) ? 0 : this.curr + 1;

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

	this.curr = this.next;
    }

});

// Alinear video y banners
var ajustado = false;
function alinear_twitter ()
{

    var posf = $('facebook').getPosition();
    var post = $('twitter').getPosition();

    if (post.y > posf.y)    { diff = post.y - posf.y; $('facebook').setStyle('margin-top', 50 + diff); }
    else	    	    { diff = posf.y - post.y; $('twitter').setStyle('margin-top', 50 + diff); }

    ajustado = true;
}

// Enlaces externos válidos XHTML 1.0
function enlaces_externos ()
{
    $$('a[rel=external]').each(function (el) {
	el.set('target', '_blank');
    });
}

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

    if ($('slideshow-arriba')) var fader1 = new Fader('slideshow-arriba', {'duration': 2000});
    if ($('slideshow-abajo')) var fader3 = new Fader('slideshow-abajo', {'duration': 2000});

    enlaces_externos();
});

window.addEvent('scroll', function (el) { 
    if ($('twitter') && !ajustado) alinear_twitter();
});
