// this contains the code to place an external ad outside of the brightcove video player
// to enable you need a div with the id of expandedBanner that can only contain a space
// this ads a config variable to the brightcove config list
//   config["onLoad"] - the name of the function to run when the onTemplateLoaded function is called 

var debugFlg = 0;
var debugIE = 1;
if (debugFlg == 0 && (location.search.indexOf("debug=") > -1))
  debugFlg = location.search.substr(location.search.indexOf("debug=") + 6, 1);

//prints debuging messages
function logMsg(msgTxt, blockLabel) {
  //if firebug is installed then use the console
  if ((debugFlg > 0) && (typeof (console) != "undefined")) {
    if (blockLabel)
      console.group(blockLabel);

    console.log(msgTxt);

    if (blockLabel)
      console.groupEnd();
  }
  //else create a div and use that
  else if (debugFlg && debugIE) {
    if (!document.getElementById("msgLog")) {
      debug = document.createElement("div");
      debug.id = "msgLog";
      debug.style.clear = "both";
      debug.style.color = "black";
      debug.style.backgroundColor = "white";
      document.body.appendChild(debug);
    }

    if (blockLabel == null)
      document.getElementById("msgLog").innerHTML += msgTxt + "<br />";
    else {
      msgTxt = msgTxt.replace(/</g, "&lt;");
      msgTxt = msgTxt.replace(/>/g, "&gt;");
      document.getElementById("msgLog").innerHTML += blockLabel + "<div style='font-size: 10px; margin-left: 10px;'>" + msgTxt + "</div>";
    }
  }
}

// called when template loads, we use this to store a reference to     
// the player and modules and add event listeners for external Ads
//if a placement is present for external ads then define the external ad calls
if (typeof (onTemplateLoaded) == "undefined")
{
  onTemplateLoaded = function(experienceID) {
    logMsg("Player Template Loaded");

    var player = brightcove.getExperience(experienceID);
    var adModule = player.getModule(APIModules.ADVERTISING);
    var videoPlayer = player.getModule(APIModules.VIDEO_PLAYER);
    var videoExperience = player.getModule(APIModules.EXPERIENCE);

    var VideoAction = new Array;
    var AdPrefix = "AD:";

    //logs an action with the video stream
    function videoActionLog(event) {
      var rtnInfo = "";
      
      var VideoTitle =  VideoAccountName.replace(" ","").toUpperCase() + ":" + videoPlayer.getCurrentVideo().displayName;
      var VideoPlayer = videoExperience.getExperienceID();
      var VideoLength = videoPlayer.getCurrentVideo().length/1000;
      var VideoLocation = event.position;
      
      if (event.type.indexOf("ad") == 0) {
        VideoTitle = AdPrefix + VideoTitle;
        
        VideoLength = 0;
        if (event.type != "adComplete")
        {
          var currentAd = adModule.getCurrentAdProperties();
          if (typeof(currentAd) == "object")
            VideoLength = currentAd.duration;
        }
      }

      rtnInfo += "Title: " + VideoTitle + "\n";
      rtnInfo += "Player: " + VideoPlayer + "\n";
      rtnInfo += "Length: " + VideoLength + "\n";
      rtnInfo += "Location: " + VideoLocation;

      switch (true)
      {
        case (event.type.indexOf("Start") > -1 || event.type.indexOf("Resume") > -1):
          if (!VideoAction[VideoTitle] || VideoAction[VideoTitle] == "Complete")
          {
              logMsg(rtnInfo,"Video Start");
              _scTrackMediaStart(VideoTitle,VideoLength,VideoPlayer,"videoStart")
              VideoAction[VideoTitle] = "Play";
          }
          else if (VideoAction[VideoTitle] == "Stop")
          {
            logMsg(rtnInfo,"Video Resume");
            _scTrackMediaResume(VideoTitle,VideoLocation)
            VideoAction[VideoTitle] = "Play";
          }
        break;

        case (event.type.indexOf("Stop") > -1 || event.type.indexOf("Pause") > -1):
          if (VideoAction[VideoTitle] != "Stop")
          {
            logMsg(rtnInfo,"Video Pause");
            _scTrackMediaPause(VideoTitle,VideoLocation)
            VideoAction[VideoTitle] = "Stop";
          }
        break;

        case (event.type.indexOf("Complete") > -1):
          if (VideoAction[VideoTitle] != "Complete")
          {
            logMsg(rtnInfo,"Video Complete");
            _scTrackMediaComplete(VideoTitle,"videoComplete")
            VideoAction[VideoTitle] = "Complete";
          }
        break;
      }
    }
    
    // Set a callback functions for the Video Events
    videoPlayer.addEventListener(BCVideoEvent.VIDEO_START,videoActionLog);
    videoPlayer.addEventListener(BCVideoEvent.VIDEO_STOP,videoActionLog);
    videoPlayer.addEventListener(BCVideoEvent.VIDEO_COMPLETE,videoActionLog);
    
    // Set a callback functions for the Ad Events
    adModule.addEventListener(BCAdvertisingEvent.AD_START, videoActionLog); 
    adModule.addEventListener(BCAdvertisingEvent.AD_PAUSE, videoActionLog); 
    adModule.addEventListener(BCAdvertisingEvent.AD_RESUME, videoActionLog); 
    adModule.addEventListener(BCAdvertisingEvent.AD_COMPLETE, videoActionLog); 

    //ads the external ad handelers in to the page
    if (document.getElementById("expandedBanner")) 
    {
      //call that handels an external ad
      function onExternalAd(event) {
        logMsg("External Ad Event");
      
        logMsg(event.ad, "Ad code");
      
        //code to create the ad object here, sets the ad object to false if invalid
        var AdObject = ExternalAd(event.ad);

        if (typeof(AdObject) == "object") 
        {
          logMsg("Type: " + AdObject.type + "\nVideoURL: " + AdObject.videoURL + "\nVideoClickURL: " + AdObject.videoClickURL + "\nDuration: " + AdObject.duration,"Displaying Ad");
          AdDuration = AdObject.duration;
          adModule.showAd(AdObject);
        }
        else
        {
          logMsg("Invalid Ad Code Provided");
          adModule.resumeAfterExternalAd(); 
        }
      }
      
      //resumes playback after the ad completes
      function onExternalAdComplete(event)
      {
        logMsg("External Ad Completed");
        adModule.resumeAfterExternalAd(); 
      }

      // ads in the correct type of ad
      adModule.enableAdFormats(6);
      
      logMsg("Supported Ad Formats: "+ adModule.getEnabledAdFormats().toString() );

      // Set a callback function for the externalAd event 
      adModule.addEventListener(BCAdvertisingEvent.EXTERNAL_AD, onExternalAd);
      adModule.addEventListener(BCAdvertisingEvent.AD_COMPLETE, onExternalAdComplete); 

    }
    else
      logMsg("External Ads Disabled");


  }
}
else
  logMsg("onTemplateLoaded already Defined");
