var stopMainPreviewAnimation = false;

$(document).ready(function(){
  $(".MainPagePreviews").innerfade({
    animationtype: "fade",
    speed: "slow",
    timeout: 5000,
    type: "random",
    onBeforeChange: function (){
      return stopMainPreviewAnimation;
    }
  });

  $("a.ArrowBack").click(priorMainPagePreview);
  $("a.ArrowNext").click(nextMainPagePreview);
});

function nextMainPagePreview() {
  stopMainPreviewAnimation = true;

  var lastMainPreview = $(".MainPagePreviews div:last-child");
  var currentPreview = $(".MainPagePreviews > div:visible");

  currentPreview.hide();
  if (currentPreview.attr("id") == lastMainPreview.attr("id")) {
    currentPreview.parent().children(":first-child").show();
  } else {
    currentPreview.next().show();
  }
}

function priorMainPagePreview() {
  stopMainPreviewAnimation = true;

  var firstMainPreview = $(".MainPagePreviews div:first-child");
  var currentPreview = $(".MainPagePreviews > div:visible");

  currentPreview.hide();
  if (currentPreview.attr("id") == firstMainPreview.attr("id")) {
    currentPreview.parent().children(":last-child").show();
  } else {
    currentPreview.prev().show();
  }
}
