$j(document).ready(function() {
  var config = {delay: '5000', autostart: true}; //delay is 3 sec
  var object = $j('#slideshow-gallery');
  Slider.init(object); 
  Slider.start(config);
})

var Slider = {
  config : false,
  current : 1,
  play : false,
  first: 1,
  last: 6,
  intervalId : 0,
  imgPath : '/test3/img/gallery/',
  init: function(object) {    
  },

  start: function (config) {
    this.config = config;
    if (this.config.last != undefined) {
      this.last = this.config.last;
    }
    if (undefined == this.config.delay) {
      this.config.delay = 5000;
    }
    this.show(this.first);        
    if (this.config.autostart) {
      this.play();
    }
  },
  
  next: function() {
    this.show();
  },
  
  prev: function() {
    this.show(undefined, true)
  },
  
  stop: function() {
  },
  
  play: function() {    
    obj = this;
    this.intervalId = setInterval(function() {obj.show()}, this.config.delay);    
  },
  
  show: function(index, backplay) {
    if (index == undefined || !index) {
      if (backplay) {
        index = this.current - 1 < this.first ? this.last : this.current - 1;
      } else {
        index = this.current + 1 > this.last ? this.first : this.current + 1;
      }
    }
    
    if (index > this.last || index < this.first) {
      return false;
    }
    
    this.current = index;
    $j('#gallery-' + index).fadeIn(100, function() {
      $j('#gallery-' + index).delay(4700);
      $j('#gallery-' + index).fadeOut(100);
    });
    
  }
}
