$j(document).ready(function() {
  $j('.best-sellers').attainProductSlider({
    display       : 5,
    slide         : 5,
    speed         : 1000
  });
  
  //ob.initialise();
});



var ob = {
  
  hover : function (e, sender) {
    ob.images.each(function () {
      if ( this != sender[0] ) {
        $j(this).stop(true, true).fadeTo('fast', 0.6);
      } else {
        $j(this).stop(true, true).fadeTo(0, 1);
      }
    });
  },
  
  unhover : function (e, sender) {
    ob.images.stop(true, true).fadeTo('fast', 1);
  },
  
  initialise : function () {
    ob.list = $j('.products');
    ob.images = $j('.best-sellers').find('.item');
    ob.list.mouseleave( function () { ob.unhover() } );
    ob.images.mouseenter( function (e) { ob.hover( e, $j(this) ); } );
  }
};

(function($) {
  
  var _default = {
    display       : 5,
    slide         : 1,
    btn_previous  : 'previous',
    btn_next      : 'next',
    show_buttons  : true,
    speed         : 300
  };
  
  var AttainSlider = function(options, sender){
    
    var container = $(sender);
    var productHolder = container.find('#products-holder');
    var containerItems = productHolder.children();
    var containerItemsWidth = $(containerItems[0]).innerWidth();
    var containerWidth = containerItems.length * containerItemsWidth;
    
    var _itemsLength = containerWidth;
    var pos = 0;
    
    productHolder.css('width', _itemsLength);
    
    // Merge options with _default
    _default = $.extend( _default, options );
    
    // Add the buttons
    if(_default.show_buttons){
      container.append( "<a href=\'javascript:void(0);\' class=\'btn btn-previous\'>" + _default.btn_previous + "</a><a href=\'javascript:void(0);\' class=\'btn btn-next\'>"+_default.btn_next+"</a>" );
    }
     
    container.find('.btn-previous').click(function(){
      if(pos >= _default.slide){
        pos = pos - _default.slide;
        animateSlide(pos);
      }
    });
    
    container.find('.btn-next').click(function(){
      if(pos < (containerItems.length - _default.display)){
        pos = pos + _default.slide;
        animateSlide(pos);
      }
    });
    
    
    var animateSlide = function (number) {
      var currentOffest = number * containerItemsWidth;
      productHolder.animate({ left: '-'+currentOffest }, _default.speed);
    }
    
  };
  
  $.fn.attainProductSlider = function(options) {
    
    this.each(function(){
      var slider = new AttainSlider(options, this);
    });
    
  };
  
})(jQuery);
