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

function getCurrentEmbedCode() {
    var resCode = null;
    var embedCodeList = [
    <?php foreach ($embed_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();
    embedCodeList.some(function(timeEntry) {
        if(currentTime >= timeEntry[0] && currentTime <= timeEntry[1]) {
            resCode = timeEntry[2];
            return true;
        }
    });
    return resCode ? resCode : '';
}

setInterval(function() {
    if(getCurrentEmbedCode() !== startEmbedCode) {
        window.location.reload(true);
    }
}, 60000);
