/*
Developer: Brandon Aaskov
Description: This code will enable the player to accept the 728x90 and 468x60 pods, and then when the ad comes back to the player, if it's something a single title player can't understand on its own natively, the XML will be passed to the playAd function. It's in this function that all the magic happens. Things are parsed, the ad object is sent to the player, and some code is built and written out to the expandedBanner div on the HTML page. That's the ad that is associated with the player, but lives outside the player.

This code could (and should) be developed further to check for scenarios such as when XML doesn't come back (such as a default ad that gets sent back when there's nothing to deliver).

Version: 2.0.0
*/

//------------------------------------------------------- BC API STUFF ------------------------------------------------
function onTemplateLoaded(message)
{
	callFlash("enableAdFormats", 14, 2); //this enables frmt=2 and frmt=14 (the 468x60 and 728x90 video pods, respectively, to be "enabled" for the player so that they're sent in the ad call. 
}
//---------------------------------------------------------------------------------------------------------------------

function playAd(adString, callback)
{
	/*
	 * Nearly every ad server sends back an anchor tag when there's no ad to deliver.
	 * If that happens in this case, pass control back to the player and end the function.
	 */
	if (adString.indexOf("<a ") !== -1) 
	{
		callFlash("endExternalAd");
		return;
	}

	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 ad = new Object(); //sets up our ad object for passing to the player later
	
	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") ad.type = "videoAd";
	if (adXML.firstChild.nodeName == "SynchedBanner728x90") ad.type = "synchedBanner";
	if (adXML.firstChild.nodeName == "SynchedBanner468x60") ad.type = "synchedBanner";
	*/
		
	//get the root node attributes
	if(adXML.firstChild.getAttribute("duration") !== "") ad.duration = adXML.firstChild.getAttribute("duration");
	if(adXML.firstChild.getAttribute("trackStartURLs") !== "") ad.trackStartURLs = adXML.firstChild.getAttribute("trackStartURLs").split(",");
	if(adXML.firstChild.getAttribute("trackMidURLs") !== "") ad.trackMidURLs = adXML.firstChild.getAttribute("trackMidURLs").split(",");
	if(adXML.firstChild.getAttribute("trackEndURLs") !== "") ad.trackEndURLs = adXML.firstChild.getAttribute("trackEndURLs").split(",");


	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) ad.videoURL = currentNode.firstChild.nodeValue; 
		if(currentNode.nodeName == "videoClickURL" && currentNode.firstChild) ad.videoClickURL = currentNode.firstChild.nodeValue;
		if(currentNode.nodeName == "expandedBannerURL" && currentNode.firstChild)	ad.expandedBannerURL = currentNode.firstChild.nodeValue;
		if(currentNode.nodeName == "expandedBannerClickURL" && currentNode.firstChild) ad.expandedBannerClickURL = currentNode.firstChild.nodeValue;
		if(currentNode.nodeName == "collapsedBannerURL" && currentNode.firstChild) ad.collapsedBannerURL = currentNode.firstChild.nodeValue;
		if(currentNode.nodeName == "collapsedBannerClickURL" && currentNode.firstChild) ad.collapsedBannerClickURL = currentNode.firstChild.nodeValue;
		
		currentNode = currentNode.nextSibling; //move to the next node for the next pass of the loop
	}
	
	ad.type = "videoAd"; //force it to be of type "video ad"
	callFlash("playAd", ad); //send the ad object to the player: in this case, it's just the video related pieces
	
	
	
	//------------------------------------------------------- EXPANDED BANNER --------------------------------------------------
	//renders the expanded banner outside of the player
	var expandedBanner = document.getElementById("expandedBanner");
	
	if(ad.expandedBannerURL.substr(-3, 3) == "swf") //if the expanded banner is a swf
	{
		var objectTag = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="300" height="250" id="expandedBanner" align="middle">';
		objectTag += '<param name="allowScriptAccess" value="always" />';
		objectTag += '<param name="movie" value="' + ad.expandedBannerURL + '" />';
		objectTag += '<param name="quality" value="high" />';
		objectTag += '<param name="bgcolor" value="#ffffff" />';
		objectTag += '<param name="FlashVars" value="clickTag=' + ad.expandedBannerClickURL + '" />';
		objectTag += '<embed src="' + ad.expandedBannerURL + '" quality="high" bgcolor="#ffffff" width="300" height="250" name="expandedBanner" align="middle" allowScriptAccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" FlashVars="clickTag=' + ad.expandedBannerClickURL + '" />';
		objectTag += '</object>';
		
		expandedBanner.innerHTML = objectTag; //writes out the object tag we just built to the expandedBanner div
	}
	else //if it's just an image
	{
		expandedBanner.innerHTML = "<a href='" + ad.expandedBannerClickURL + "' target='_blank' ><img src='" + ad.expandedBannerURL + "' /></a>"; //writes out the regular anchor/tag to the expandedBanner div
	}
	//-------------------------------------------------------------------------------------------------------------------------
}