var Galeria = new Class({
    Implements: [Options, Events],
    options: {
        slides: [],
        startIndex: 0,
        wrap: true,
        autoplay:true,
        contador:false,
        onShow: $empty
    },
    initialize: function(options){
        this.setOptions(options);
        this.autoPlay=this.options.autoplay;
        this.addSlides(this.options.slides);
        if(this.slides.length) this.showSlide(this.options.startIndex);
        if(this.autoPlay)this.timer = setInterval(this.cycleForward.bind(this), 8000);
    },
    slides: [],
    addSlides: function(slides){
        $$(slides).each(function(slide){
            this.slides.include($(slide));
            slide.setStyle('display','none');
           //slide.addEvent('click', this.cycleForward.bind(this));
        }, this);
    },
    addSlide: function(slide){
        this.addSlides($splat($(slide)));
    },
    cycleForward: function(){
        if($chk(this.now) && this.now < this.slides.length-1) this.showSlide(this.now+1);
        else if ((this.now) && this.options.wrap) this.showSlide(0);
        else if(!$defined(this.now)) this.showSlide(this.options.startIndex);
    },
    cycleBack: function(){
        if(this.now > 0) this.showSlide(this.now-1);
        else if(this.options.wrap) this.showSlide(this.slides.length-1);
    },
    showSlide: function(iToShow){
        if (this.fading) return;
        var now = this.now;        
        var currentSlide = this.slides[now];
        var slide = this.slides[iToShow];
        var fadeIn = function (s){
            this.fading = true;
            s.setStyles({
                display:'block',
                visibility: 'visible',
                opacity: 0
            });
            s.get('tween').start('opacity', 1).chain(function(){
                this.fading = false;
                this.fireEvent('onShow', [slide, iToShow]);
            }.bind(this));
        }.bind(this);
        if(slide) { 
            if($chk(now) && now != iToShow){
                this.fading = true;
                currentSlide.get('tween').start('opacity', 0).chain(function(){
                    currentSlide.setStyle('display', 'none');
                    fadeIn(slide);
                }.bind(this));
            } else fadeIn(slide);
            this.now = iToShow;
             if($("contador"))$("contador").innerHTML=this.now+1+"/"+this.slides.length;
        }
    },
    play_stop:function(){
        if(!this.autoPlay){
            this.timer = setInterval(this.cycleForward.bind(this), 8000);
            estado="pausa";
        }else{
            $clear(this.timer); //Belay that order!
            estado="play";
        }
        $('boton_play-pausa').set('class',estado);
        this.autoPlay = !this.autoPlay;
    }
});

 
var NewsSlide = new Class({
    Extends: Galeria,
    Implements:Options,
    options: {
        news: [],
        container: false
    },
    initialize: function(options){
        this.setOptions(options);
        this.parent(options);
        this.container = $(this.options.container);
        if(!this.container) return;
        this.botones(); 
        this.showSlide(this.options.startIndex);
   
    },
    botones: function(){
           var prev=new Element('span', {
                id:'prev'
            }).inject(this.container);
           var next=new Element('span', {
                id:'next'
            }).inject(this.container);
            next.addClass('boton');
            prev.addClass('boton');
            if(!this.options.contador){
                var play=new Element('span',{
                  id:'boton_play-pausa' 
               }).inject(this.container);
               play.addClass('pausa');
               play.addEvent('click', this.play_stop.bind(this)); 
            }else{
                var contador=new Element('span',{
                  id:'contador' 
               }).inject(this.container);
               contador.addClass('boton');
               pos=this.options.startIndex+1;
               contador.innerHTML=pos+"/"+this.options.slides.length;
            }
            next.addEvent('click', this.cycleForward.bind(this)); 
            prev.addEvent('click', this.cycleBack.bind(this));
             

    }
    
});

