/**
 * @author Lisa Marie Smith (Brightcove)
 * @version 1.0
 * @projectDescription 
 */

var _companionAds = new Object();

/**
 * Build Ad Object, Display on Page
 * @pXML is XML returned from the ad server
 * @pOption is 'all' or 'ads'. 'all' means 728x90 ad, 300x250 ad, and 170x30 logo. 'ads' means minus the logo
 */
function ExternalAd(pXML,pOption)
{	
	/**
	 * Parses the XML from the ad server and builds the ad object from it.
	 * @param {Object} pXML The XML returned from the ad server.
	 */
	this.buildAd = function(pXML)
	{	
		if (pXML.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(pXML);
		}
		else if (window.XMLHttpRequest)
		{	
			var adXML = (new DOMParser()).parseFromString(pXML, "text/xml"); //parses the XML for Mozilla browsers
		}
		
		var ad = new Object();
		ad.type = "videoAd";
		
		var nodeItems = adXML.firstChild.childNodes.length;
		var currentNode = adXML.firstChild.firstChild;
		
		//get the root node attributes
		ad.duration = (adXML.firstChild.getAttribute("duration") !== "") ? Number(adXML.firstChild.getAttribute("duration")) : 15;
		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(",");
		if(adXML.firstChild.getAttribute("trackPointURLs") && (adXML.firstChild.getAttribute("trackPointURLs") !== "")) ad.trackPointURLs = adXML.firstChild.getAttribute("trackPointURLs").split(",");
		ad.trackPointTime = (adXML.firstChild.getAttribute("trackPointTime") && (adXML.firstChild.getAttribute("trackPointTime") !== "")) ? Number(adXML.firstChild.getAttribute("trackPointTime")) : 0;
		
		for(var i = 0; i < nodeItems; i++)
		{
			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 == "banner_728x90URL" && currentNode.firstChild) _companionAds.banner_728x90URL = currentNode.firstChild.nodeValue;
			if(currentNode.nodeName == "banner_728x90ClickURL" && currentNode.firstChild) _companionAds.banner_728x90ClickURL = currentNode.firstChild.nodeValue;
			if(currentNode.nodeName == "banner_300x250URL" && currentNode.firstChild) _companionAds.banner_300x250URL = currentNode.firstChild.nodeValue;
			if(currentNode.nodeName == "banner_300x250ClickURL" && currentNode.firstChild) _companionAds.banner_300x250ClickURL = currentNode.firstChild.nodeValue;
			if(currentNode.nodeName == "banner_160x600URL" && currentNode.firstChild) _companionAds.banner_160x600URL = currentNode.firstChild.nodeValue;
			if(currentNode.nodeName == "banner_160x600ClickURL" && currentNode.firstChild) _companionAds.banner_160x600ClickURL = currentNode.firstChild.nodeValue;
			if(currentNode.nodeName == "banner_170x30URL" && currentNode.firstChild) _companionAds.banner_170x30URL = currentNode.firstChild.nodeValue;
			if(currentNode.nodeName == "banner_170x30ClickURL" && currentNode.firstChild) _companionAds.banner_170x30ClickURL = currentNode.firstChild.nodeValue;
			
			currentNode = currentNode.nextSibling;
		}
		return ad;
	}
	
	this.createSwf = function(pURL, pClickThrough, pId, pWidth, pHeight, pName)
	{
		var objectTag = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,32,18" width="' + pWidth + '" height="' + pHeight + '" id="'+pId+'" align="middle">\n';
		objectTag += '\t<param name="allowScriptAccess" value="always" />\n';
		objectTag += '\t<param name="movie" value="' + pURL + '" />\n';
		objectTag += '\t<param name="quality" value="high" />\n';
		objectTag += '\t<param name="bgcolor" value="#ffffff" />\n';
		objectTag += '\t<param name="wmode" value="transparent" />\n'; 
		objectTag += '\t<param name="FlashVars" value="clickTag=' + pClickThrough + '" />\n';
		objectTag += '\t<embed src="' + pURL + '" quality="high" bgcolor="#ffffff" width="' + pWidth + '" height="' + pHeight + '" name="' + pName + '" align="middle" allowScriptAccess="always" wmode="transparent" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" FlashVars="clickTag='+pClickThrough+'" />\n';
		objectTag += '</object>\n';
		
		if(document.getElementById(pId)) document.getElementById(pId).innerHTML = objectTag;
	}
	
	this.createImage = function(pURL, pClickThrough, pId)
	{
		if(document.getElementById(pId)) document.getElementById(pId).innerHTML = "<a href='" + pClickThrough + "' target='_blank' ><img src='" + pURL + "' border='0' /></a>\n";
	}

	this.createBanner = function(pAd, pOption)
	{
		var aryOption=new Array();
		aryOption=pOption.split(',');
		
		for (x in aryOption)
		{
			if (aryOption[x]=='banner_300x250') {
				(pAd.banner_300x250URL.indexOf('.swf') !== -1) ? this.createSwf(pAd.banner_300x250URL, pAd.banner_300x250ClickURL, 'banner_300x250', '300','250','banner_300x250') : this.createImage(pAd.banner_300x250URL, pAd.banner_300x250ClickURL, 'banner_300x250');		
			}
			if (aryOption[x]=='banner_728x90') {
				(pAd.banner_728x90URL.indexOf('.swf') !== -1) ? this.createSwf(pAd.banner_728x90URL, pAd.banner_728x90ClickURL, 'banner_728x90', '728','90','banner_728x90') : this.createImage(pAd.banner_728x90URL, pAd.banner_728x90ClickURL, 'banner_728x90');
			}
			if (aryOption[x]=='banner_160x600') {
				(pAd.banner_160x600URL.indexOf('.swf') !== -1) ? this.createSwf(pAd.banner_160x600URL, pAd.banner_160x600ClickURL, 'banner_160x600', '160','600','banner_160x600') : this.createImage(pAd.banner_160x600URL, pAd.banner_160x600ClickURL, 'banner_160x600');
			}
			if (aryOption[x]=='banner_170x30') {
				(pAd.banner_170x30URL.indexOf('.swf') !== -1) ? this.createSwf(pAd.banner_170x30URL, pAd.banner_170x30ClickURL, 'banner_170x30', '170','30','banner_170x30') : this.createImage(pAd.banner_170x30URL, pAd.banner_170x30ClickURL, 'banner_170x30');
			}		
		}
	}
	
	var ad = this.buildAd(pXML);
	this.createBanner(_companionAds,pOption);
	//callFlash("playAd", ad); MB - 4/07/09 Removed, player plays videoAd directly
	
}
