jQuery(function($) {
  
  var TRANSITION_TIME = 8000;
  
  function spotlight() {
    
    var self = this;
    
    var spots = this.find('.tarot_div_spot');

    var cards = this.find('.spot-cards');    

    var icons = this.find('.spot_nav li');
    
    var indicator = $(document.createElement('div')).addClass('arrow');
    
    var nav = this.find('.spot_nav');

    function activate(li) {
      
      var index = li.prevAll().size();
      
      icons.filter('.active').animate({
        opacity: .4
      }, 400).removeClass('active');
      
      spots.filter('.active').removeClass('active');
      
      spots.eq(index).addClass('active');
      
      li.addClass('active');
      
      cards.attr('href', spots.eq(index).find('a').attr('href'));

      var slideTo = li.position().left + li.width()*.5;
      
      li.stop().animate({
        opacity: 1
      }, 400);
      
      indicator.stop().animate({
        left: slideTo
      }, 250, 'swing');
    }
    
    if (!self.is('.mini')) {
      nav.append(indicator);
      activate(icons.eq(0));
    }
   
    cards.attr('href', spots.eq(0).find('a').attr('href'));

    var hovered = false;
    
    this.find('.spot_nav li a')
      .mouseover(function() {
      
        var li = $(this).parent();
        
        if (li.is('.active')) return;
        
        activate(li);
        
        hovered = true;
      });
      
    this.find('.spot_nav')
      .mouseout(function() {
        
        hovered = false;
      });
   
    icons.click(function() { return false; });
 
    var interval = setInterval(function() {
      
      if (hovered) return;
      
      var next = icons.filter('.active').next();
      
      if (!next.size()) {
        next = icons.eq(0);
      }
      
      activate(next);
      
    }, TRANSITION_TIME);
    
    //load rest of images.
    spots.find('.image').each(function() {
      $(this).find('img').attr('src', $(this).attr('data-image'));
    });
  };
  
  $('.spotlight-v4').each(function() { spotlight.call($(this))} );
});

