// vim: set et ts=4 sw=4 sts=4 ft=javascript :
var bcExp;
var modVP;
var modExp;
var modCon;
 
// called when template loads, this function stores a reference to the player and modules.
function onTemplateLoaded(experienceID) {

    bcExp = brightcove.getExperience(experienceID);
 
    modVP = bcExp.getModule(APIModules.VIDEO_PLAYER);
	modAd = bcExp.getModule(APIModules.ADVERTISING);
	
	// Set a callback function for the externalAd event 
    modAd.addEventListener(BCAdvertisingEvent.EXTERNAL_AD, onExternalAd);
    // Set a callback function for the Ad Complete event
    modAd.addEventListener(BCAdvertisingEvent.AD_COMPLETE, onAdComplete);
    // Set a callback function for the Ad Pause event
    modAd.addEventListener(BCAdvertisingEvent.AD_PAUSE, hideBannerAd);
    // Set a callback function for the Ad Resume event
    modAd.addEventListener(BCAdvertisingEvent.AD_RESUME, showBannerAd);
	
    //Enable External Ads
    modAd.enableExternalAds(true);
	
}

// Hide the companion ad when the page loads
$(document).ready(function() {
	hideBannerAd();
});

function onExternalAd(evt) {
    //Create a videoAd object
    var videoAd = getVideoAd(evt.ad);
    //Create a bannerAd object
    var bannerAd = getExpandedBanner(videoAd);
   
    // Render a video Ad 
    modAd.showAd(videoAd); 
    
    // Add an external  banner using JS
    var externalBanner = document.getElementById("companionAd");
    var externalBannerHTML = '';
    if (bannerAd && bannerAd.expandedBannerClickURL && bannerAd.expandedBannerURL) {
        externalBannerHTML = "<a href='" + bannerAd.expandedBannerClickURL + "' target='_blank' ><img src='" + bannerAd.expandedBannerURL + "' /></a>"; 
    } else if (bannerAd && bannerAd.expandedBannerHTML) {
        externalBannerHTML = unescape(bannerAd.expandedBannerHTML);
    }
    //writes out the regular anchor/tag to the externalBanner div
    if (externalBannerHTML) {
        externalBanner.innerHTML = externalBannerHTML;	
        showBannerAd();
    }
}

function onAdComplete(evt) {
	hideBannerAd();
	modAd.resumeAfterExternalAd(); 
}

function showBannerAd() {
    // Remove the banner on Ad Complete    
    $("#companionAd").show();
}

function hideBannerAd() {
    // Remove the banner on Ad Complete    
    $("#companionAd").hide();
}

// You would usually make a call to your ad server here. 
// In this example, we are harcoding the attributes of a video ad
function getVideoAd(adString){
    
	if (window.ActiveXObject) {

		//parses the XML for IE browsers
		var adXML = new ActiveXObject("Microsoft.XMLDOM");
		adXML.async = false;
		adXML.loadXML(adString); //parses the XML for IE browsers

	} else if (window.XMLHttpRequest) {
		var adXML = (new DOMParser()).parseFromString(adString, "text/xml"); //parses the XML for Mozilla browsers
	}

    var videoAd = new Object();

	var nodeItems = adXML.firstChild.childNodes.length; //the number of items in the XML
	var currentNode = adXML.firstChild.firstChild; //sets the first node in the XML as the current node

	//for dynamically setting the ad type: we're actually manually overriding this later, but this is a good example of how this is done should someone need it
	if (adXML.firstChild.nodeName == "videoAd") videoAd.type = "videoAd";
	if (adXML.firstChild.nodeName == "SynchedBanner728x90") videoAd.type = "synchedBanner";
	if (adXML.firstChild.nodeName == "SynchedBanner468x60") videoAd.type = "synchedBanner";

//get the root node attributes
	if(adXML.firstChild.getAttribute("duration")) {
		videoAd.duration = adXML.firstChild.getAttribute("duration");
	}
	if(adXML.firstChild.getAttribute("trackStartURLs")) {
		videoAd.trackStartURLs = adXML.firstChild.getAttribute("trackStartURLs").split(",");
	}
	if(adXML.firstChild.getAttribute("trackMidURLs")) {
		videoAd.trackMidURLs = adXML.firstChild.getAttribute("trackMidURLs").split(",");
	}
	if(adXML.firstChild.getAttribute("trackEndURLs")) {
		videoAd.trackEndURLs = adXML.firstChild.getAttribute("trackEndURLs").split(",");
	}
	if(adXML.firstChild.getAttribute("trackPointURLs")) {
		videoAd.trackPointURLs = adXML.firstChild.getAttribute("trackPointURLs").split(",");
	}
	if(adXML.firstChild.getAttribute("trackPointTime")) {
		videoAd.trackPointTime = adXML.firstChild.getAttribute("trackPointTime");
	}

	for(var i = 0; i < nodeItems; i++)
	{
		//checks to see if the current nodes are in our Rich Media Templates and assigns them if they exist
		if(currentNode.nodeName == "videoURL" && currentNode.firstChild) videoAd.videoURL = currentNode.firstChild.nodeValue; 
		if(currentNode.nodeName == "videoClickURL" && currentNode.firstChild) videoAd.videoClickURL = currentNode.firstChild.nodeValue;
		if(currentNode.nodeName == "expandedBannerURL" && currentNode.firstChild)	videoAd.expandedBannerURL = currentNode.firstChild.nodeValue;
		if(currentNode.nodeName == "expandedBannerClickURL" && currentNode.firstChild) videoAd.expandedBannerClickURL = currentNode.firstChild.nodeValue;
		if(currentNode.nodeName == "collapsedBannerURL" && currentNode.firstChild) videoAd.collapsedBannerURL = currentNode.firstChild.nodeValue;
		if(currentNode.nodeName == "collapsedBannerClickURL" && currentNode.firstChild) videoAd.collapsedBannerCickURL = currentNode.firstChild.nodeValue;
		if(currentNode.nodeName == "adXML" && currentNode.firstChild) videoAd.adXML = escape(currentNode.firstChild.nodeValue);
		
		currentNode = currentNode.nextSibling; //move to the next node for the next pass of the loop
	}

	videoAd.type = "videoAd";

    return videoAd;
}    

// You would usually make a call to your ad server here. 
// In this example, we are harcoding the creative URLs

function getExpandedBanner(videoAd){
    var expandedAd = new Object(); 
    
    expandedAd.expandedBannerClickURL = videoAd.expandedBannerClickURL;
    expandedAd.expandedBannerURL = videoAd.expandedBannerURL;
    expandedAd.expandedBannerHTML = videoAd.expandedBannerHTML;
            
    return expandedAd;
}

