﻿
// classads.js

// Product class page rotating ads.

var classAdsTimer;
var classAdsCounter = -1;     // First increment will be 0, to start at the first image.
// classAdClassID, classAdFolder, classAdImgID are defined up in the page.

// Final arrays to rotate, combined class-specific and all-class ads.
var classAds = new Array();
var classAdLinks = new Array();

// Init. Merge class-specific and all-class arrays. Start display.
function ClassAdsInit()
{
  var classAds_One = null;
  var classAdLinks_One = null;

  // Class-specific ads.
  //  Test if ads for this class, exit early.
  //  ASSUME: Ads and Links are matched up.
  var hasAds = false;
  eval("hasAds = (typeof classAds_" + classAdClassID + " != 'undefined');");    // Technically should test null too, but we know it's not.
  if (!hasAds)
  {
    ClassAdHide();
    return;
  }
  
  eval("classAds_One = classAds_" + classAdClassID + ".slice(0)");
  eval("classAdLinks_One = classAdLinks_" + classAdClassID + ".slice(0)");

  classAds = classAds.concat(classAds_One);
  classAdLinks = classAdLinks.concat(classAdLinks_One);

  // Append ads for all classes.
  classAds = classAds.concat(classAds_All);
  classAdLinks = classAdLinks.concat(classAdLinks_All);

  // Start it.
  ClassAdDisplay();
}

// Display next ad, set timer to call again.
function ClassAdDisplay()
{
  // Set timer for next one.
  classAdsTimer = setTimeout("ClassAdDisplay()", 5000);

  // Increment, wrap, display.
  classAdsCounter++;
  classAdsCounter %= classAds.length;
  
  var el = document.getElementById(classAdImgID);
  if (el)
  {
    el.src = "/assets/marketing/promos/classads/images/" + classAdFolder + "/" + classAds[classAdsCounter];
  }
}

// Go to ad destination.
function ClassAdGo()
{
  var counter = classAdsCounter;      // Seems to snap a copy, in case it changes during the click.
  location = classAdLinks[counter];
}

// Hide the ad.
//  Displaying spacer still has the link.
function ClassAdHide()
{
  var el = document.getElementById("divClassAd");
  if (el)
  {
    el.style.display = "none";
  }
}

