var spotlight_autoscroll = true;
var spotlights = [];
var current_spotlight = 0;
var next_spotlight = 1;
var spotlight_page = 1;

function togglePickSelection(pick) {
  $('promo-picks').descendants().each(function(e) {      
    if (e.tagName == 'LI') {
      e.removeClassName('active');
    }
  });
  $(pick).addClassName('active');
}
function toggleStaticPickSelection() {
  $('promo-box-summary-1').hide();
  $('promo-box-summary-2').hide();
  $('promo-box-summary-3').hide();
  $('promo-box-summary-4').hide();
}
function nextSpotlight(id) {
  //$('promo-box-summary').hide();
  //getSpotlight(id);
  showSpotlight(id);
}
function getSpotlight(id) {
  new Ajax.Updater('promo-box-summary', '/home/get_promo_summary/'+id, {asynchronous:true, evalScripts:true, method:'get', onComplete:function(request){togglePickSelection('promo-'+id);Effect.Appear('promo-box-summary', { delay: 0.5 })}});
}
function getSpotlightPage(city, page) {
  spotlight_autoscroll=false;
  new Effect.Fade('promo-box-summary');
  $('promo-picks').update('<li>&nbsp;</li><li>&nbsp;</li><li>&nbsp;</li><li><i>loading spotlights...</i></li>');
  new Ajax.Updater('promo-box-section', '/home/update_promo_box?name='+encodeURI(city)+'&pick_page='+page, {asynchronous:true, evalScripts:true, method:'get'});
}
function showSpotlight(id) {
  new Effect.Fade('promo-box-summary', { afterFinish: function() { 
    $$('div.spotlight-detail').each(function(d) {
      d.hide();
    });
    $('spotlight-'+id).show();
    togglePickSelection('promo-'+id);
    new Effect.Appear('promo-box-summary', { delay: 0.5 });
    }
  });
}

Event.observe(window, "load", function() {  
  new PeriodicalExecuter(function(pe) {
    if ((spotlights.length < 2) && (spotlight_page == 1)){
      spotlight_autoscroll = false;
    }
  
    if (spotlight_autoscroll) {
      var random_on = false;
      
      if (random_on) {
        next_spotlight = Math.floor(Math.random()*spotlights.length);
        if (current_spotlight != next_spotlight) {
          current_spotlight = next_spotlight;
          getSpotlight(spotlights[current_spotlight]);
        }
      } else {
        if (current_spotlight < spotlights.length-1) {
          current_spotlight += 1;
          nextSpotlight(spotlights[current_spotlight]);
        } else {
          current_spotlight = 0;
          get_next_spotlights();
        }
      }
      
    } else {
      pe.stop();
    }
  }, 7);
      
});