
// looping config vars
var _totalLoops     = 0;
var _endFrameDelay  = 4;

var container       = getById("container");
var loadingContent  = getById("loading_content");

// video
var video01         = getById("video01");
var _noVideo        = true;

// exit
var exitBtn         = getById("exit_btn");
var _exitURL        = "https://www.hogarth.com";

var isPlaying_video;


// Create and provide timeline to Hoxton
var Creative = {

    tl: gsap.timeline( { defaults:{ease:"none"} } ),


    setExitURL: function( strURL )
    {
        _exitURL = strURL;
    },

    onExit: function( e ) 
    {
        hoxton.exit( "Exit" , _exitURL );
    },

    onBannerStart: function() 
    {
        console.log("Creative.onBannerStart()");
    },

    onBannerComplete: function() 
    {
        console.log("Creative.onBannerComplete()");
    },

    jumpToEndFrame: function() 
    {
        Creative.tl.pause();
        Creative.tl.seek( "end", false);
    },

    checkIsBackup: function() 
    {
        return ( window.location.href.indexOf( 'hoxtonBackup' ) >= 0 ) ? true : false;
    },

    startAd: function() 
    {
        Creative.createButtons();
        Creative.displayBanner();
        Creative.setUpTimeline();

        Creative.init();
        Creative.checkIsBackup() ? Creative.jumpToEndFrame() : null;
    },

    createButtons: function()
    {
        exitBtn.addEventListener("click", Creative.onExit, false);
    },

    setUpTimeline: function()
    {
        Creative.tl.repeat(_totalLoops);
        Creative.tl.repeatDelay(_endFrameDelay);
        Creative.tl.eventCallback("onStart", Creative.onBannerStart);
        Creative.tl.eventCallback("onComplete", Creative.onBannerComplete);
    },

    displayBanner: function()
    {
        loadingContent.style.display = "none";
        container.style.display = "block";
    },

    init: function() 
    {
        Creative.tl
            .addLabel("reset", 0.001)
                .call( Creative.playVideo , ["video01"] , "reset")
                .to(".main-copy", {alpha:0}, "reset")
                .to(copy_01, {duration:1, alpha:1}, ">")
                .to(copy_01, {duration:0.5, alpha:0}, ">2")
                .to(copy_02, {duration:1, alpha:1}, ">")
            .addLabel("end", ">");
    },

    playVideo: function( videoId )
    {
        console.log("playVideo");
        
        var ele = getById(videoId);
        
        if(!_noVideo)
        {
            // show video:
            gsap.to(ele, 0, {alpha:1});

            // play
            isPlaying_video = ele.currentTime > 0 && !ele.paused && !ele.ended && ele.readyState > ele.HAVE_CURRENT_DATA;


            if(isPlaying_video) {
                ele.pause();
                ele.currentTime = 0;
                ele.play();
            } else {
                ele.play(); 
            }
            gsap.to("#novideo-text",0,{display:"none"})

        }
        else
        {
            gsap.to(video01,0,{alpha:0});
            gsap.to("#novideo-text",0,{display:"block"})
        }
    }

};

function getById( eleID ) 
{
    return document.getElementById(eleID);
}

