
/**  Usage

    window.addEvent('domready', function() {
        window.slider = new Slider("#slider", {'width': 716});
    });

    Demo:  http://localhost:8080/brandSlider.zml

**/

var Slider = new Class({
Implements: Options,
options: {
  'width': 400,
  'max': 4,
  'start-index': 1
},

initialize: function(element, options) {
  this.setOptions(options);
  this.slider = document.getElement(element);

  if(!this.slider) 
    return false;

  this.slideWrap = this.slider.getElement('.wrap');
  this._setEvents();
  this.slider.getElements('.section')[1].addClass('current');
  this.index = 0;
  this.options['max'] = this.slider.getElements('.section').length;
  var self = this;
  this.slideWrap.set('tween', {'onComplete': function() { self.lock = false; }});
  this.lock = false;
  this._render();
  this.slider.getElements('.prev, .next').setStyle('display', 'block');
},

_getCurrent: function() {
  return this.slider.getElement('.current');
},

_setEvents: function() {
  var self = this;
  this.slider.addEvent('click', function(e) {
      var target = $(e.target);
      if (target.hasClass('next')) {
          e.preventDefault();
          self._next();
      } else if (target.hasClass('prev')) {
          e.preventDefault();
          self._prev();
      }
      target.blur();
  });
},

_next: function() {
  if (this.lock)
      return;
  if (this.index+1 < this.options['max']) {
      this.index++;
      this.lock = true;
      this._render();
  }
},

_prev: function() {
  if (this.lock)
      return;
  if (this.index > 0) {    
    this.lock = true;
      this.index--;
      this._render();
  }
},

_render: function() {
  var currentLeft = this.slideWrap.getStyle('left');
  var newLeft = this.options['width'] * this.index * -1;
  this.slideWrap.tween('left', currentLeft, newLeft);
  this._render_prev_next();
},

_render_prev_next: function() {
  var prev = this.slider.getElements('.prev');
  prev.set('tween', {'duration': 'short'});
  var next = this.slider.getElements('.next');
  next.set('tween', {'duration': 'short'});

  this.index == 0 ? prev.fade('out') : prev.fade('in');
  this.index == this.options['max']-1 ? next.fade('out') : next.fade('in');
}
});
