﻿function ShowSpot(spotIndex, clicked) {
  var spots = $jQuery(".SpotsSwitcher .SpotFrame");
  if (spotIndex < 0 || spotIndex > spots.length - 1)
    spotIndex = 0;

  var selectedSpot = $jQuery(".SpotsSwitcher .SpotFrame.Selected");
  _spotIndex = spotIndex;
  if (selectedSpot.length == 0)
    hideSpotCallback();
  else {

    if (!_autoSwitch) {
      selectedSpot.hide();
      hideSpotCallback();
    }
    else
      selectedSpot.fadeOut("fast", hideSpotCallback);
  }

  var trackingIndex = spotIndex + 1;
  if (clicked)
    trackClick("/spots/show/" + trackingIndex);
}

var _spotIndex = 0;
var _spotTimerID = null;
function hideSpotCallback() {
  $jQuery(".SpotsSwitcher .SpotLinks li a").removeClass("Selected")
  $jQuery(".SpotsSwitcher .SpotLinks li a").eq(_spotIndex).addClass("Selected")
  $jQuery(".SpotsSwitcher .SpotFrame").removeClass("Selected")
  $jQuery(".SpotsSwitcher .SpotFrame").eq(_spotIndex).addClass("Selected")

  var items = $jQuery(".SpotsSwitcher .SpotFrame.Selected");
  if (!_autoSwitch)
    items.show();
  else {
    items.fadeIn("slow");

    window.clearTimeout(_spotTimerID);
    _spotTimerID = window.setTimeout("ShowSpot(" + (_spotIndex + 1) + ", false)", 10000);
  }
}

if (typeof ($jQuery) != "undefined") {
  $jQuery(document).ready(function() {
    $jQuery(".SpotsSwitcher").removeClass("HideSpots");
    var spots = $jQuery(".SpotsSwitcher .SpotFrame");
    spots.hide();
    var randomNumber = 0;
    if (_randomSpots)
      randomNumber = Math.floor(spots.length * Math.random());
    ShowSpot(randomNumber, false);
  });
}
