(function($){
  
var ns = "DoctorSlider";

window[ns] = function () {
  this.root = $('#doctor-slider');
  if ( ! this.root.length ) return;
  
  this.roller = $('#doctor-roller');
  this.item   = this.root.find('li');
  this.left   = this.root.find('.roll-left');
  this.right  = this.root.find('.roll-right');
  
  this.init();
  this.observe();
};

window[ns].prototype = {
  init: function () {
    this.size  = this.item.length;
    this.unit  = 5;
    this.itemWidth = 176;
    this.index = 0;
    
    this.hasleft = false;
    if ( this.size <= this.unit ) this.hasright = false;
    else this.hasright = true;
    
    this.updateControl();
  },
  
  observe: function () {
    this.left .click( $.proxy( this.goleft , this ) );
    this.right.click( $.proxy( this.goright, this ) );
  },
  
  updateControl: function () {
    var methodleft = this.hasleft ? 'enable' : 'disable';
    this[methodleft]('left');

    var methodright = this.hasright ? 'enable' : 'disable';
    this[methodright]('right');
  },
  
  disable: function ( type ) {
    this[type].css({ opacity: .2, cursor: 'default' });
  },
  
  enable: function ( type ) {
    this[type].css({ opacity: 1, cursor: 'pointer' });
  },
  
  goleft: function () {

    if ( this.index - this.unit <= 0 ) {
      this.index = 0;
      
      this.hasleft = false;
      if ( this.unit >= this.size ) this.hasright = false;
      else this.hasright = true;
      
    } else {
      this.index -= this.unit;
      this.hasright = true;
    }
    this.go( this.index );
  },
  
  goright: function () {
    
    if ( this.index + this.unit >= this.size - this.unit ) {
      this.index = this.size - this.unit;

      this.hasright = false;
      if ( this.index == 0 ) this.hasleft = false;
      else this.hasleft = true;
      
    } else {
      this.index += this.unit;
      this.hasleft = true;
    }
    this.go( this.index );
  },
  
  go: function ( index ) {
    var left = -index * this.itemWidth;
    this.roller.animate({ left: left });
    this.updateControl();
  }
};
  
})(jQuery)
