// JS to set banners content to dynamic content loaded from DoubleClick

// create var to provide easy access to dynamic data
var _dynamicData = {};

//console.log("Hoxton ready!");
hoxton.timeline = Creative.tl;

// Define the function that should fire when the Ad Server is ready and assets are preloaded
hoxton.isInitialized = setDynamicContent;


/*
* Function sets any dynamic content
*/
function setDynamicContent()
{
    //console.log("setDynamicContent()");

    // for shorthand references to state object
    _dynamicData = hoxton.getState();

    Creative.setExitURL( _dynamicData.exit_url );

    loadVideo();
}

/*
* Function loads the video
*/                               
function loadVideo()
{
    _numFiles = 0;
    _numFilesLoadedData = 0;

    if(_dynamicData.video01.toLowerCase().indexOf("novideo")!=-1) {
        
        console.log("no video");

    } else {
        _numFiles++;

        video01.src = _dynamicData.video01;

        video01.load();
        video01.addEventListener("canplaythrough", videoLoadedDataHandler, false);

        // when switching between versions, the above "canplaythrough" event will not always fire because
        // it may have already been fired. So to ensure the videoLoaded function fires we also check the readyState:
        if(video01.readyState > 3) {
            videoLoadedDataHandler();
        }
    }

    if(_numFiles==0){
        _noVideo=true;
        // Start Ad - there is no video
        Creative.startAd();
        console.log("no video set")
    }
}

/*
* Function fired when video has loaded enough data to do duration calculations
*/                               
function videoLoadedDataHandler(e)
{
    // console.log(e.target.id);
    _numFilesLoadedData++;
    if(_numFilesLoadedData === _numFiles) // only once data for videos loaded
    {   
        _noVideo=false;

        // Start Ad now that video is loaded
        Creative.startAd();
        console.log("startAd - WITH VIDEO");
    }
}
