var bcInstance = null;
var adObj      = null;

function onTemplateLoaded (e)
{
    bcInstance = new Brightcove ();
    bcInstance.setup (e);    
};

var Brightcove = Class.create ();

Brightcove.prototype.initialize = function ()
{
    this.adStart   = false;
    this.nextVideo = false;
    this.exp       = null;
    this.cues      = null;
    this.adObj     = null;
    this.video     = null;
    this.player    = null;
    this.playlist  = null;
    this.adModule  = null;
    this.adPolicy  = null;
    this.postroll  = false;
this.videoAd = null;
};

Brightcove.prototype.noAd = function ()
{
    $("flashAd").src = "placeAd.php";
};

Brightcove.prototype.adReady = function ()
{
     var adXML = this.getXMLDoc (dlBcXML);

     this.adReqComplete (adXML);
};

Brightcove.prototype.setup = function (e)
{
    this.player   = brightcove.getExperience (e);

    this.exp      = this.player.getModule (APIModules.EXPERIENCE);
    this.cues     = this.player.getModule (APIModules.CUE_POINTS);
    this.video    = this.player.getModule (APIModules.VIDEO_PLAYER);
    this.adModule = this.player.getModule (APIModules.ADVERTISING);

    //this.adModule.enableExternalAds (true);
   
    this.adPolicy = this.video.experience.modules.advertising.getAdPolicy ();

    var that = Object.clone (this);

    this.exp.addEventListener      (BCExperienceEvent.TEMPLATE_READY, function (e) {that.onTemplateReady.call (that, e)});
    this.cues.addEventListener     (BCCuePointEvent.CUE,              function (e) {that.onCuePoint.call      (that, e)});
    this.video.addEventListener    (BCMediaEvent.PLAY,                function (e) {that.onMediaStart.call    (that, e)});
    this.video.addEventListener    (BCVideoEvent.VIDEO_COMPLETE,      function (e) {that.onVideoComplete.call (that, e)});
    this.video.addEventListener    (BCVideoEvent.VIDEO_START,         function (e) {that.onVideoStart.call    (that, e)});
    this.video.addEventListener    (BCMediaEvent.CHANGE,              function (e) {that.onMediaChange.call   (that, e)});
    this.video.addEventListener    (BCVideoEvent.VOLUME_CHANGE,       function (e) {that.onVolumeChange.call  (that, e)});
    //this.adModule.addEventListener (BCAdvertisingEvent.EXTERNAL_AD,   function (e) {that.onExternalAd.call    (that, e)});
    this.adModule.addEventListener (BCAdvertisingEvent.AD_COMPLETE,   function (e) {that.onAdComplete.call    (that, e)});
    this.adModule.addEventListener (BCAdvertisingEvent.AD_START,      function (e) {that.onAdStart.call       (that, e)});

    if (readCookie ("volume") == null) {
        this.video.setVolume (0.6);
    } else {
        this.video.setVolume (readCookie ("volume"));
    }
};

Brightcove.prototype.onMediaStart = function (e) { 
//alert ("media start");
}

Brightcove.prototype.onMediaChange = function (e)
{
    if (dlBcXML != undefined && dlBcXML != "") {
        this.adReqComplete (this.getXMLDoc (dlBcXML));
    } else {
        $("flashAd").src = "placeAd.php";
        this.postroll = false;

        this.video.play ();
    }
};

Brightcove.prototype.adReqComplete = function (adXML)
{
    var i = $("flashAd");
    i.setAttribute ("name", "myframe");

    var expandedBannerClickURLTag = adXML.getElementsByTagName ("expandedBannerClickURL") [0];
    var expandedBannerURLTag      = adXML.getElementsByTagName ("expandedBannerURL") [0];

    var clickURL = null;
    var banURL = null;

    if (expandedBannerClickURLTag.childNodes.length > 0) {
        clickURL = expandedBannerClickURLTag.firstChild.nodeValue;
    }

    if (expandedBannerURLTag.childNodes.length > 0) {
        banURL   = expandedBannerURLTag.firstChild.nodeValue;
    }

    var format   = adXML.getElementsByTagName ("format")                 [0].firstChild.nodeValue;

    if (format == "adTag") {
        i.setAttribute ("src", banURL);
    } else {

        i.setAttribute ("src", "placeAd.php?ts=" + new Date ().getTime () + "&clickURL=" + clickURL + "&banURL=" + banURL + "&format=" + format);
    }

    this.onExternalAd (adXML);
};

Brightcove.prototype.onCuePoint = function (e) {

 };

Brightcove.prototype.onAdStart = function (e)
{
//alert ("ad Start");
 //   this.video.stop ();
    var that     = Object.clone (this);
    this.adStart = true;
};

Brightcove.prototype.onAdComplete = function (e)
{
    if (this.postroll == true) {
        this.playNextVideo ();
    } else {
        this.video.play ();
    }
};

Brightcove.prototype.onTemplateReady = function (e) {
}

Brightcove.prototype.onVideoComplete = function (e)
{
    this.video.stop ();

    if (this.postroll == true) {
        this.adModule.showAd (adObj);
    } else {
        this.playNextVideo ();
    }
};

Brightcove.prototype.onVideoStart = function (e)
{
//alert ("video start");
//    this.video.stop ();
};

Brightcove.prototype.setRenditions = function (context)
{
    var renditions = context.renditions;
    var renditionIndex = -1;
    var size = 0;

    for (var i = 0; i < renditions.length; i ++) {
   
        if (renditions [i].size > size) {
            size = renditions [i].size;
            renditionIndex = i;
        }
    }

    return renditionIndex;
};

Brightcove.prototype.onVolumeChange = function ()
{
    setCookie ("volume", this.video.getVolume ());
};

Brightcove.prototype.debug = function (param)
{
    var str = "";

    for (i in param) {
        str += i + "=" + param [i] + "\n";
    }

    return (str);
};

Brightcove.prototype.playNextVideo = function ()
{
    if (getCookie ("nextVideo") != null) {

        if (getCookie ("nextVideo") == subtopic [2]) return;

        if (getCookie ("vidType") == "c") {
            window.location = "v" + getCookie ("nextVideo") + "-dlife.html?c=" + getCookie ("category");
        }

        if (getCookie ("vidType") == "t") {
            window.location = "v" + getCookie ("nextVideo") + "-dlife.html?t=" + getCookie ("tag");
        }
    }
};

Brightcove.prototype.getURLParam = function (name, realParam)
{
    var regexS = null;

    name = name.replace (/[\[]/, "\\\[").replace (/[\]]/, "\\\]");

    if (realParam == 1) {
        regexS = name + "([0-9]+)";
    } else {  
        regexS = name + "=([0-9]*)";
    }

    var regex = new RegExp (regexS);
    var results = regex.exec (window.location.href);

    if (results == null) {
        return "";
    } else {
        return results [1];
    }
};

Brightcove.prototype.getXMLDoc = function (x)
{
    var adXML;

    if (window.ActiveXObject) {

        adXML = new ActiveXObject ("Microsoft.XMLDOM");
        adXML.async = false;
        adXML.loadXML (x);

    } else {

        if (window.XMLHttpRequest) {
            adXML = (new DOMParser ()).parseFromString (x, "text/xml");  
        }
    }

    return adXML;
};

Brightcove.prototype.onExternalAd = function (adXML)
{
    var vid = (this.getURLParam ("v", 1));

    var videoAd = this.getVideoAd (adXML);

    adObj = videoAd;

    var bannerAd = this.getCollapsedBanner (adXML);

    if (adXML.getElementsByTagName ("position") [0].firstChild.nodeValue == "postroll") {

        this.postroll = true;
        this.video.play ();
        this.adModule.resumeAfterExternalAd ();

    } else {
        this.postroll = false;
//alert ("lets show it");
        this.adModule.showAd (videoAd);
    }
};

Brightcove.prototype.getVideoAd = function (adXML)
{
    var videoAd = new Object();
     
    videoAd.type = "videoAd";

    var node = adXML.getElementsByTagName ("SynchedBanner728x90") [0];

    videoAd.trackStartURLs = [node.getAttribute ("trackStartURLs")];
    videoAd.trackEndURLs   = [node.getAttribute ("trackEndURLs")];
    videoAd.trackMidURLs   = [node.getAttribute ("trackMidURLs")];
    videoAd.duration       = node.getAttribute ("duration");

    var ni = node.childNodes.length;
    var cn = node.firstChild;

    for (var i = 0; i < ni; i ++) {

        if (cn.nodeName == "videoURL"       && cn.firstChild) {videoAd.videoURL       = cn.firstChild.nodeValue;}
        if (cn.nodeName == "videoClickURL"  && cn.firstChild) {videoAd.videoClickURL  = cn.firstChild.nodeValue;}

        cn = cn.nextSibling;
    }

    videoAd.trackPointTime= "0";

    return videoAd;
};    

Brightcove.prototype.getCollapsedBanner = function (adXML)
{
    var collapsedAd = new Object ();
       
    var ni = adXML.firstChild.childNodes.length;
    var cn = adXML.firstChild.firstChild;

    for (var i = 0; i < ni; i ++) {

        if (cn.nodeName == "collapsedBannerURL"      && cn.firstChild) {collapsedAd.collapsedBannerURL      = cn.firstChild.nodeValue;}
        if (cn.nodeName == "collapsedBannerClickURL" && cn.firstChild) {collapsedAd.collapsedBannerClickURL = cn.firstChild.nodeValue;}
           
        cn = cn.nextSibling;
    }
               
    return collapsedAd;
};  

Brightcove.prototype.getExpandedBanner = function (adXML)
{
    var expandedAd = new Object ();

    var cn = adXML.firstChild.firstChild;
    var ni = adXML.firstChild.childNodes.length;

    for (var i = 0; i < ni; i ++) {

        if (cn.nodeName == "expandedBannerURL"      && cn.firstChild) {expandedAd.expandedBannerURL      = cn.firstChild.nodeValue;}
        if (cn.nodeName == "expandedBannerClickURL" && cn.firstChild) {expandedAd.expandedBannerClickURL = cn.firstChild.nodeValue;}

        cn = cn.nextSibling;
    }

    return expandedAd;
};

