<?php
header("Content-type: text/javascript; charset=utf-8");
$rep = $_REQUEST['rep'];
include_once dirname(__FILE__).'/'.$rep.'/config.php';
include_once 'common.php';

if (is_array($config['metadata']) && array_key_exists("metadata",$_GET) && array_key_exists($_GET['metadata'], $config['metadata'])) {
    $media_title = $config['metadata'][$_GET['metadata']]['display'];
} else {
    $media_title = $config['title'];
}

?>
(function(_containerId, _iframeUrl, _mediaUrl, _platformUrl, _mediaTitle, _platformTitle, _width, _height) {
    
    var _messages = {
            en: {
                watch: "Watch",
                on: "on"
            },
            fr: {
                watch: "Regarder",
                on: "sur"
            }
        },
        _lang = ( navigator.language ? navigator.language : ( navigator.userLanguage ? navigator.userLanguage : "en" ) ).substr(0,2).toLowerCase();
    _lang = (_messages[_lang] ? _lang : "en");
    
    var _container = document.getElementById(_containerId),
        _frame = document.createElement("iframe");
    
    _frame.width = _width;
    _frame.height = _height;
    _frame.frameBorder = 0;
    _frame.src = _iframeUrl;
    
    _container.appendChild(_frame);
    
    var _p = document.createElement("p");
    _p.innerHTML = _messages[_lang].watch
        + ' <a href="'
        + _mediaUrl
        + '" target="_blank">'
        + _mediaTitle
        + '</a> '
        + _messages[_lang].on
        + ' <a href="'
        + _platformUrl
        + '" target="_blank">'
        + _platformTitle
        + '</a>';
    _container.appendChild(_p);
    
    var _blocked = false,
        _updater = function() {
            _blocked = true;
            window.setTimeout(function() {
                _blocked = false;
            }, 1000);
            _frame.contentWindow.postMessage(document.location.hash, "*");
        };
    
    window.onhashchange = _updater;
    
    window.addEventListener('message', function(_e) {
        if (/^#/.test(_e.data) && !_blocked) {
            document.location.hash = _e.data;
        }
    });
    
    window.setTimeout(_updater, 2000);
    
})(
    'metadataplayer_embed',
    '<?php echo(URL_ROOT.$rep); ?>/player_embed.php<?php echo(array_key_exists("metadata",$_GET)?"?metadata=".$_GET["metadata"]:""); ?>',
    '<?php echo(URL_ROOT.$rep); ?>/polemicaltimeline.php<?php echo(array_key_exists("metadata",$_GET)?"#metadata=".$_GET["metadata"]:""); ?>',
    '<?php echo URL_ROOT; ?>',
    "<?php echo str_replace('"','\\"', $media_title); ?>",
    'Polemic Tweet',
    650,
    600
);