  var bcExp;
  var adModule;
  var refreshInterval;
  
  function onTemplateLoaded(experienceID) {
    //grab needed modules
    bcExp = brightcove.getExperience(experienceID);
    adModule = bcExp.getModule(APIModules.ADVERTISING);
    //ad start and end eventlisteners
    adModule.addEventListener("adStart", onAdStart);
    adModule.addEventListener("adComplete", onAdComplete);
    //dynamically set ad tag
    if(typeof aTitle != "undefined") {
      setAdTag("http://ad.doubleclick.net/pfadx/tc.lesaffaires/bc-lib;sz=2000x2000;!c=oou;!c=ooi;pos=pre;atitle=" + aTitle + ";ss=" + subSection + ";");
    } else {
      setAdTag("http://ad.doubleclick.net/pfadx/tc.lesaffaires/bc-lib;sz=2000x2000;!c=oou;!c=ooi;pos=pre;");
    }
}
  
  function onAdStart(evt) {
    document.getElementById("gpt_unit_/companion.bb/_0").style.width = "auto";
    document.getElementById("gpt_unit_/companion.bb/_0").style.height = "auto";
    if(typeof refreshInterval != "undefined") {
      clearInterval(refreshInterval);
    }
  }
  
  function onAdComplete(evt) {
    refreshInterval = setInterval(refreshCompanions, 30000);
  }
  
  function refreshCompanions() {
    var ord = Math.random()*10000000000000000;
    var refreshBB = document.getElementById("/companion.bb/_0_iframe").src.replace(/(.*)(dc_seed=)([0-9]+)(;)(.*)/,"$1$5");
    var removeOrd = refreshBB.search("ord=");
    refreshBB = refreshBB.substring(0, removeOrd);
    document.getElementById("/companion.bb/_0_iframe").src = refreshBB + "ord=" + ord + "?";
  }

  function setAdTag(adServerURL) {
    var adPolicy = new Object();
    //dynamically set adpolicy and ad tag
    adPolicy.adServerURL = adServerURL;
    adPolicy.prerollAds = true;
    adPolicy.midrollAds = false;
    adPolicy.postrollAds = true;
    adModule.setAdPolicy(adPolicy);
  }
  
  //companion ads code
  //adslot1
  var adSlot1 = googletag.defineSlot('/companion.bb/', [[300, 250], [300, 600], [160,600]]);
  adSlot1.set("ad_type", "flash");
  adSlot1.addService(googletag.companionAds());
  // Enable companion ads service
  googletag.enableServices();
