  var NewsSlide = Class.create({ 
    
    initialize: function(){
    	
    	if($('news-slides') == undefined) {
			return;
		}
		
		
    	
      
      this.current = 0;
      this.running = false;
      
      this.slides = $('news-slides').select('div');
      this.buttons = $('news-buttons').select('button');
      
      this.count = this.slides.length;
      
      var _this = this;
      
      this.buttons.each(function(el,i){
        if(i<_this.count) Event.observe($(el), 'click', function(event){  _this.slide(i); }); 
      });
      
      /* previous/next */
      Event.observe($('news-trigger-prev'), 'click', function(){  _this.previous(); }); 
      Event.observe($('news-trigger-next'), 'click', function(){  _this.next(); }); 
      
      //this.pe = new PeriodicalExecuter(this.slide.bind(this), 5);
      //this.timeout = window.setTimeout(function(){ slide(); }.bind(this), 5000);
      
      this.timeoutfnc = (function(){ this.slide(); }.bind(this));
      this.timeout = this.timeoutfnc.delay(5);
      
    },
    
    
    slide: function(show){
      
      //console.log(show);
      
      if(this.running || this.current==show) return; //one by one
      
      window.clearTimeout(this.timeout); 
      this.running = true;
      
      if(typeof show!='number') show = this.current+1;  //next one
      
      if(show>=this.count) show = 0;  //back to first
      if(show<0) show = this.count-1;  //back to last
      
      Effect.Fade(this.slides[this.current],{ 
            duration: 0.3,
            afterFinish: function(){
              Effect.Appear(this.slides[show],{
                duration: 0.5,
                afterFinish: function(){ 
                  this.running = false; 
                  this.current = show;
                  this.timeout = this.timeoutfnc.delay(5);
                }.bind(this) 
              });
           
           }.bind(this) 
      });
      
    },
    
    previous: function(){ this.slide(this.current-1); },
    
    next: function(){ this.slide(this.current+1); }
    
  });
  
  //document.observe("dom:loaded", function(){ new NewsSlide(); });
  Event.observe(window, 'load', function(){ new NewsSlide(); });
  
