<?php
include_once('./config.php');
header("Content-type: application/x-javascript");
?>
var startYoutubeCode = "<?php echo($youtube_code ? $youtube_code : '') ?>";

function getCurrentYoutubeCode() {
    var resCode = null;
    var youtubeCodeList = [
    <?php foreach ($youtube_code_list as $time_entry): ?>
        [<?php echo($time_entry[0]->getTimestamp() * 1000); ?>, <?php echo($time_entry[1]->getTimestamp() * 1000); ?>, "<?php echo($time_entry[2]);?>"],
    <?php endforeach; ?>
    ];
    var currentTime = Date.now();
    youtubeCodeList.some(function(timeEntry) {
        if(currentTime >= timeEntry[0] && currentTime <= timeEntry[1]) {
            resCode = timeEntry[2];
            return true;
        }
    });
    return resCode ? resCode : '';
}

setInterval(function() {
    if(getCurrentYoutubeCode() !== startYoutubeCode) {
        window.location.reload(true);
    }
}, 5000);
