function toggleembed(){
    var d = document.getElementById('embedsect');
    if (d.style.display == 'none'){
        d.style.display = 'block';
    }else{
        d.style.display = 'none';
    }
}

// From https://stackoverflow.com/a/1173319        
function selectText(containerid) {
    if (document.selection) { // IE
        var range = document.body.createTextRange();
        range.moveToElementText(document.getElementById(containerid));
        range.select();
    } else if (window.getSelection) {
        var range = document.createRange();
        range.selectNode(document.getElementById(containerid));
        window.getSelection().removeAllRanges();
        window.getSelection().addRange(range);
    }
}        

// https://snippets.bentasker.co.uk/page-1708042214-Place-AJAX-GET-request-and-trigger-callback-function-with-result-Javascript.html   
function fetchPage(url,callback,errcallback){
    var xmlhttp;
    if (window.XMLHttpRequest){
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }else{
        // code for IE6, IE5 (why am I still supporting these? Die... Die.... Die....
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange=function(){
        if (xmlhttp.readyState==4){
    if(xmlhttp.status==200){
            callback(xmlhttp.responseText)
        }else{
            errcallback(xmlhttp.responseText)
        }

} }

    xmlhttp.open("GET",url,true);
    xmlhttp.send();
}


function writeDetails(response){
    var j = JSON.parse(response);
    
    var t = document.createTextNode(j['title']);
    var h = document.getElementById('title');
    h.innerHTML = '';
    h.appendChild(t);

    var d = document.createTextNode(j['description']);
    document.getElementById('description').appendChild(d);
}


// Get the video path from the request URL
const urlParams = new URLSearchParams(window.location.search);
vidurl = urlParams.get('url');

// Don't bother trying to load the player if no URL was given
if (vidurl){
    dirname = vidurl.match(/.*\//);

    // Trigger a XMLHTTP request to see if the video has an info manifest
    fetchPage('/'+dirname+'details.json',writeDetails,function(e){});


    document.addEventListener("DOMContentLoaded", function(event) { 
        d = document.getElementById('embedsecturl');
        // It's basically userinput, so escape it
        t = document.createTextNode(vidurl);
        d.innerHTML = '';
        d.appendChild(t);
        
        h = document.getElementById('title');
        if (h.innerHTML == ''){
            var t2 = t.cloneNode(true);
            h.appendChild(t2);
        }
    });        
}