var clsAutoSlider = new Class({
  currentIndex: 1,
  lastIndex: 1,
  inProgress: 1,
  btnNext: null,
  btnPrev: null,
  slider: null,
  rnd: 1,
  arrItems: new Array(),
  counter: null,
  autoplay: false,
  speed: 3500,
  
  togglePlay: function() {
    this.autoplay = this.autoplay == false;
  },
  
  showNext: function() {
    this.resetTimer();
    nextItem = this.currentIndex + 1;
    if(nextItem > (this.lastIndex - 1))
      nextItem = 0;
    this.slider.toElement($(this.arrItems[nextItem]));
    this.currentIndex = nextItem;
  },
  
  showPrev: function() {
    this.resetTimer();
    nextItem = this.currentIndex - 1;
    if(nextItem < 0)
      nextItem = this.lastIndex - 1;
    this.slider.toElement($(this.arrItems[nextItem]));
    this.currentIndex = nextItem;    
  },
  
  resetTimer: function() {
    if(this.counter != null)
      clearTimeout(this.counter);
 
  },
  
  autoPlay: function() {
    nextItem = this.currentIndex + 1;
    if (home == true) {
    	if(nextItem > (this.lastIndex - 1))  
      	nextItem = this.currentIndex;
    } else {
    	if(nextItem > (this.lastIndex - 1))  
      	nextItem = 1;
    } 
    this.slider.toElement($(this.arrItems[nextItem]));
    this.currentIndex = nextItem;
  },
  
  init: function(sw, w, h, speed) {
    this.speed = speed;
    this.rnd = Math.floor(Math.random() * 999);

    $(sw).setStyles({
      'height': h + 'px',
      'overflow': 'hidden',
      'position': 'relative'
    }); 

    var allObj = $(sw).getElements('.block');
    allObj.each(function(n, i) {
      n.setAttribute('id', 'sliderItem_' + this.rnd + '_' + this.lastIndex);
      n.addClass('ceAutoSlideItem');
      n.setStyles({
        'left': ((this.lastIndex) * w) + 'px',
        'height': h + 'px',
        'width': w + 'px'
      });
      this.arrItems[this.lastIndex] = 'sliderItem_' + this.rnd + '_' + this.lastIndex;
      this.lastIndex++;
    }.bind(this));

    $(sw).setStyle('width', (this.lastIndex * w) + 'px');
    
    this.btnNext = new Element('div', {
      'class': 'ceAutoSlideNext'
    });
          
    this.btnPrev = new Element('div', {
      'class': 'ceAutoSlidePrev'
    });
    $($('MAIN' + sw).parentNode).setStyles({
      'position': 'relative',
      'width': w + 'px',
      'height': h + 'px'
    });
    $($('MAIN' + sw)).setStyles({
      'position': 'relative',
      'width': w + 'px',
      'height': h + 'px'
    });
    this.btnNext.onclick = this.showNext.bind(this);
    this.btnPrev.onclick = this.showPrev.bind(this);
    this.btnNext.injectInside($('MAIN' + sw).parentNode);
    this.btnPrev.injectInside($('MAIN' + sw).parentNode);
    this.slider = new Fx.Scroll('MAIN' + sw, {
    	wait: false,
    	duration: 1000,
        onComplete: function() {
        clearTimeout(this.counter);
        this.counter = setTimeout(function() {this.autoPlay();}.bind(this), this.speed);
      }.bind(this)
    });
    this.counter = setTimeout(function() { this.showNext(); }.bind(this), this.speed);
  }
});
