var RotateImages = function() {return {
  options: {
    listOfImages: "#panel__rotating_images .container",
//    subtitle: "#rotate-images .subtitle",
    subtitle: null,
//    counter: "#rotate-images .counter",
    counter: null,
    interval: 8000
  },
  currentImage: 0,
  start: function(base, firstCall) {
    //Set the opacity of all images to 0
    $('ul.ticker_window li').css({opacity: 0.0});
    
    //Get the first image and display it (gets set to full opacity)
    $('ul.ticker_window li:first').css({opacity: 1.0});
    
    if (base.options.subtitle) {
      //Display Text description of first image
      $(base.options.subtitle + ' .text').html($('ul.ticker_window li:first span').text());
    }

    if (base.options.counter) {
      // Set Image number to 01 for first image
      $(base.options.counter + ' span').html("01 ");   
    }

    //Call the rotator function to run the slideshow, 6000 = change to next image after 6 seconds
    base.currentImage = setInterval(firstCall, base.options.interval);

    return base;
  },
  
  left: function(base) {
    //Get the first image
    var current = ($('ul.ticker_window li.show') ? 
      $('ul.ticker_window li.show') : $('ul.ticker_window li:first'));

    //Get next image, when it reaches the end, rotate it back to the first image
    var prev = ((current.prev().length) ? ((current.prev().hasClass('show')) ? 
      $('ul.ticker_window  li:last') :current.prev()) : $('ul.ticker_window  li:last')); 

    return base.rotate(base, current, prev);
  },
  
  right: function(base) { 
    //Get the first image
    var current = ($('ul.ticker_window  li.show') ? 
      $('ul.ticker_window  li.show') : $('ul.ticker_window  li:first'));

    //Get next image, when it reaches the end, rotate it back to the first image
    var next = ((current.next().length) ? ((current.next().hasClass('show')) ? 
      $('ul.ticker_window  li:first') :current.next()) : $('ul.ticker_window li:first')); 

    return base.rotate(base, current, next);
  },

  rotate: function(base, current, next) {
    //Set the fade in effect for the next image, the show class has higher z-index
    next.css({opacity: 0.0})
      .addClass('show').stop()
      .animate({opacity: 1.0}, 2000);
  
    //Hide the current image
    current.stop().animate({opacity: 0.0}, 2000)
      .removeClass('show');

    if (base.options.subtitle) {
      //Display Text description of the image
      $(base.options.subtitle + ' .text').html($('ul.ticker_window li.show span').text());
    }

    if (base.options.counter) {
      var index = $("ul.ticker_window li").index(next);
      // Update Image number in "state" div
      $(base.options.counter + ' span').html(index+1);

      if(index < 10){
        $(base.options.counter + ' span').prepend("0");
      }
    }

     return base;
    }
}};

