// 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();


    // set any dynamic data that Hoxton can not update by targeting a DOM element                   
    setDynamicNonDomData();



        // we set banner containers to display:block here so we can take size measurements,
        // ensure opacity:0 is set, then opacity:1 at start of main TL
        Creative.displayBanner();

        var _adWidth = container.offsetWidth;
        var _adHeight = container.offsetHeight;

        // add size class to container so we can do size specific animations on the TL:
        container.classList.add("size"+_adWidth+"x"+_adHeight);
    

    Creative.startAd();
}



/*
* Set any dynamic data that Hoxton can not update by targeting a DOM element
*/                               
function setDynamicNonDomData()
{
    // set frame timimgs
    setFrameTimes();

    // set general banner fade in and out speeds
    _fadeInSpeed = Number(_dynamicData.fadeInSpeed);
    _fadeOutSpeed = Number(_dynamicData.fadeOutSpeed);

     // create array of objects to define when to show cta's and logos
    _arrLogoFrames = getBoolArrayData(_arrLogoFrames, _dynamicData.logoFrames);
    _arrFrameObjs.push({eleRef:logoContainer, arrFrames:_arrLogoFrames, ifShowing:false});
    _arrCtaFrames = getBoolArrayData(_arrCtaFrames, _dynamicData.ctaFrames);
    _arrFrameObjs.push({eleRef:cta_container, arrFrames:_arrCtaFrames, ifShowing:false});
    _arrSpareImgFrames = getBoolArrayData(_arrSpareImgFrames, _dynamicData.spareImgFrames);
    _arrFrameObjs.push({eleRef:spareContainer, arrFrames:_arrSpareImgFrames, ifShowing:false});


    // update copy alignment (only really needed on 300x250 and 336x280)
    for(var j = 1; j <= _totalFrames; j++)
    {
        switch(_dynamicData["align_copy_frame0"+j+"a"])
        {
            case "top":
                getById("copy_frame0"+j+"a").classList.remove("copy-align-bottom");
                getById("copy_frame0"+j+"a").classList.add("copy-align-top");
                break;

            case "bottom":
                getById("copy_frame0"+j+"a").classList.remove("copy-align-top");
                getById("copy_frame0"+j+"a").classList.add("copy-align-bottom");
                break;

            default:
        }
    }

    // set exit url
    Creative.setExitURL( _dynamicData.exit_url );

    // set looping props
    var arrLooping = _dynamicData.loopingProps.split(",");
    if(arrLooping.length === 3)
    {
        _totalLoops = Number(arrLooping[0].trim());
        _endFrameDelay = Number(arrLooping[1].trim());
        if(arrLooping[2].trim() === "true")
        {
            _useReplayBtn = true;
        }
        else
        {
            _useReplayBtn = false;
        }
    }

}



/*
* Function sets the frame times for the banner. Frames with a time of 0 will be skipped.
* Any frame with no content will have its time set to 0 despite the value for the frame in the frameTime array
*/
function setFrameTimes() {

    var arrFrameWaits = _dynamicData.frameTimes.split(",");
    if(arrFrameWaits.length === _arrFrameWaits.length)
    {
        for(var j = 0; j < _arrFrameWaits.length; j++)
        {
            _arrFrameWaits[j] = Number(arrFrameWaits[j].trim());
        }
    }
}




/*
* function gets the contents of an numeric array
*/
function getNumArrayData(arrTarget, strSource)
{
    var arr = strSource.split(",");

    if(arr.length === arrTarget.length)
    {
        for(var i = 0 ; i < arr.length; i++)
        {
            arr[i] = Number(arr[i].trim());
        }

        arrTarget = arr;
    }

    return arrTarget;
}


/*
* function gets the contents of an boolean array
*/
function getBoolArrayData(arrTarget, strSource)
{
    var arr = strSource.split(",");

    if(arr.length === arrTarget.length)
    {
        for(var i = 0 ; i < arr.length; i++)
        {
            if(arr[i].trim() == "true")
            {
                arr[i] = true;
            }
            else
            {
                arr[i] = false;
            }
        }
        arrTarget = arr;
    }

    return arrTarget;
}

/*
* function checks if number
*/
function isNumeric(n)
{
    return !isNaN(parseFloat(n)) && isFinite(n);
}


