var Galeria = new Class({
    Implements: [Options, Events],
    options: {
        slides: [],
        startIndex: 0,
        wrap: true,
        onShow: $empty,
        items_to_show:1,
        autoPlay:true
    },
    initialize: function(options){
        this.setOptions(options);
        this.autoPlay=this.options.autoPlay;
        this.addSlides(this.options.slides);
        this.items_to_show=this.options.items_to_show;
        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(){
//    	alert(this.now.toInt()+this.items_to_show.toInt());
        if($chk(this.now) && (this.now.toInt()+this.items_to_show.toInt())< this.slides.length){
        	this.showSlide(this.now.toInt()+this.items_to_show.toInt());
        }
        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-this.items_to_show);
        else if(this.options.wrap) this.showSlide((this.slides.length/this.items_to_show).floor()*this.items_to_show);
    },
    showSlide: function(iToShow){
        if (this.fading) return;
        this.marca_boton($('pos_'+iToShow));
        var now = this.now;   
        var currentSlide = this.slides[now];
        var slide = this.slides[iToShow];
        var fadeIn = function (s){
            num=this.slides.length-this.now-this.items_to_show;
            items_to_show=(this.items_to_show<num || num<0)?this.items_to_show:this.slides.length-this.now-this.items_to_show; 
            for(i=0;i<this.items_to_show;i++){
                if(s.getNext()){
                    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));
                    s=s.getNext();
                }else{
                    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;
                for(i=0;i<this.items_to_show;i++){
                //    currentSlide.get('tween').start('opacity', 0).chain(function(){
                    if(currentSlide.getNext()){
                        currentSlide.get('tween').start('opacity', 0);
                        currentSlide.setStyle('display', 'none');
                        currentSlide=currentSlide.getNext();
                    }else{
                        currentSlide.get('tween').start('opacity', 0);
                        currentSlide.setStyle('display', 'none');
                    }
                 //   }.bind(this));                  
                    
                    
                }
                fadeIn(slide);
            } else fadeIn(slide);
            this.now = iToShow;
            //alert(this.now)
        }
    },
    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,
        carrousel_bots: false,
        selectores_bots: false
    },
    initialize: function(options){
        this.setOptions(options);
        this.parent(options);
        this.container = $(this.options.container);
        if(!this.container) return;
        this.showSlide(this.options.startIndex);
        if(this.options.carrousel_bots)this.botones();    
        if(this.options.selectores_bots)this.selectores();   
 
    },
    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');
            next.addEvent('click', this.cycleForward.bind(this)); 
            prev.addEvent('click', this.cycleBack.bind(this)); 

    },
    selectores:function(){
        els=$$("#slideNavButtons a");
        cant_el=els.length;
        i=0;
        while(i<cant_el){
            els[i].set('id','pos_'+i);
            i++;
        }
        $$("#slideNavButtons a").each(function(el){
            el.addEvent('click',function(){
                this.marca_boton(el);
                pos=el.id.replace('pos_', '');
                this.showSlide(pos);
            }.bind(this));
        }.bind(this));
    },
    marca_boton:function(el){
        if(el){
	        $$("#slideNavButtons a").each(function(el){
	            el.erase('class')
	        });
	        el.set('class','active');
        }
    }
    
});


