/*
Developer: Brandon Aaskov
Description: Handles Brightcove Ads

Version: 3.0, Updated: Marvin Li
Ad loading features in class.  To change target div, use adDisplay.setTargetDiv().
XML parsing retrieves all attributes and child nodes, instead of presets.  This prevents parser from hanging.
*/

//----------------------------------------------------- AdDisplay Class --------------------------------------------------
function AdDisplay() {
    this._targetDiv = "expandedBanner"
    this.adPriority = 0;
    this.enableDebug = false;
}
AdDisplay.prototype._targetDiv;
AdDisplay.prototype.setTargetDiv = function(targetDiv) {
    this._targetDiv = targetDiv;
}

//--- PLAYS VIDEO AD ---
AdDisplay.prototype.playVideoAd = function(adHash) {
    // construct a video object
    var adObj = new Object();
    adObj.type = "videoAd";

    var properties = new Array("videoURL", "videoClickURL", "duration");
    for (var i=0; i<properties.length; i++) {
        var val = adHash[properties[i]];
        if (val != null && val != "") {
            adObj[properties[i]] = val;
        }
    }
    var arrProperties = new Array("trackStartURLs", "trackMidURLs", "trackEndURLs");
    for (var i=0; i<arrProperties.length; i++) {
        var val = adHash[arrProperties[i]];
        if (val != null && val != "") {
            adObj[arrProperties[i]] = val.split(",");
        }
    }

    // load into brightcove
    callFlash("playAd", adObj);    
}

//--- RENDERS EXTERNAL BANNER---
AdDisplay.prototype.loadExternalBanner = function(bannerURL, clickURL, width, height, priority) {
    if (priority >= this.adPriority) {
        this.adPriority = priority;
        
        var expandedBanner = document.getElementById(this._targetDiv);
        
        // INSERTS FLASH MOVIE (ends with "SWF")
        if(bannerURL.substr(-3, 3).toLowerCase() == "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="' + width + '" height="' + height + '" id="expandedBanner" align="middle">';
            objectTag += '<param name="allowScriptAccess" value="always" />';
            objectTag += '<param name="movie" value="' + bannerURL + '" />';
            objectTag += '<param name="quality" value="high" />';
            objectTag += '<param name="bgcolor" value="#ffffff" />';
            objectTag += '<param name="FlashVars" value="clickTag=' + clickURL + '" />';
            objectTag += '<embed src="' + bannerURL + '" quality="high" bgcolor="#ffffff" width="' + width + '" height="' + height + '" name="expandedBanner" align="middle" allowScriptAccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" FlashVars="clickTag=' + clickURL + '" />';
            objectTag += '</object>';
            expandedBanner.innerHTML = objectTag; //writes out the object tag we just built to the expandedBanner div
        }
        else if (bannerURL.substr(0, 39) == "http://ad.doubleclick.net/adi/umg.inrd/")
        {
            expandedBanner.innerHTML = '<iframe height="' + height + '" width="' + width + '" scrolling="no" src="' + bannerURL + '"  frameborder="0"></iframe>';
        }
        // INSERTS IMAGE
        else
        {
            expandedBanner.innerHTML = "<a href='" + clickURL + "' target='_blank' ><img src='" + bannerURL + "' /></a>"; //writes out the regular anchor/tag to the expandedBanner div
        }
    }
}

AdDisplay.prototype.resumeVideo = function() {
    callFlash("endExternalAd");
}

//--- FOR DEBUGGING ---
AdDisplay.prototype.debugSource = function(str, targetDiv) {
    if (this.enableDebug) {
        var div = document.getElementById(targetDiv);
        var text = document.createTextNode(str);
        div.appendChild(text);
        div.appendChild(document.createElement("br"));
    }
}

//------------------------------------------------------------------------------------------------------------------------

// INSTANTIATE AdDisplay CLASS
var adDisplay = new AdDisplay();

//------------------------------------------------------- Entry Point ----------------------------------------------------
// playAd() is called when BC receives Ad XML
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 || adString.indexOf("<A ") != -1 || adString.substr(0, 7).toLowerCase() == "<script") 
    {
        adDisplay.resumeVideo();
        return;
    }

    //--- GRAB DATA ---
    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 adHash = new Object(); //sets up our ad object for collecting parameters
    adHash.type = adXML.firstChild.nodeName;
       
    // Grab attributes from ad XML
    for(var i = 0; i < adXML.firstChild.attributes.length; i++)
    {
        //POSSIBLE ATTRIBUTES:
        //duration, trackStartURLs, trackMidURLs, trackEndURLs
        var currentNode = adXML.firstChild.attributes[i];
        if (currentNode.firstChild != null) {
            adHash[currentNode.nodeName] = currentNode.firstChild.nodeValue;
        }
    }

    // Grab children node values from ad XML
    for(var i = 0; i < adXML.firstChild.childNodes.length; i++)
    {
        //POSSIBLE NODES:
        //videoURL/videoClickURL
        //expandedBannerURL/expandedBannerClickURL
        //collapsedBannerURL/collapsedBannerClickURL
        //rectangleURL/rectangleClickURL
        var currentNode = adXML.firstChild.childNodes[i];
        if (currentNode.firstChild != null) {
            adHash[currentNode.nodeName] = currentNode.firstChild.nodeValue;
        }
    }

    //adDisplay.debugSource(adHash.type, "sourcediv");
    //--- DISPLAY ADS ---
    if (adHash.type == "Rectangle300x250") {
        adDisplay.loadExternalBanner(adHash.rectangleURL, adHash.rectangleURL, 300, 250, 0); 
        adDisplay.resumeVideo(); // Rectangle300x250 has no video
    }
    else if (adHash.type == "SynchedBanner728x90") {
        adDisplay.loadExternalBanner(adHash.expandedBannerURL, adHash.expandedBannerClickURL, 728, 90, 1); 
        adDisplay.playVideoAd(adHash);
    }
    else if (adHash.type == "SynchedBanner468x60") {
        //SynchedBanner468x60 are actually prerolls w/ 300x250 companions, so use:
        adDisplay.loadExternalBanner(adHash.expandedBannerURL, adHash.expandedBannerClickURL, 300, 250, 1);
        adDisplay.playVideoAd(adHash);
    }
    else if (adHash.type = "videoAd") {
        adDisplay.playVideoAd(adHash);
    }
    else{ 
        adDisplay.resumeVideo();
    }   
}
//------------------------------------------------------------------------------------------------------------------------