﻿var player = null;
function playerReady(thePlayer) {
    player = document.getElementById(thePlayer.id);
}
function loadVideo(id) {
    document.getElementById("ScrollBoxVideoItemBox" + currentVideoID).className = "ScrollBoxVideoItem";
    currentVideoID = id;
    document.getElementById("ScrollBoxVideoItemBox" + currentVideoID).className = "ScrollBoxVideoItem ScrollBoxVideoItem-Selected";
    document.getElementById("ScrollBoxVideoItemBox" + currentVideoID).focus();
    loadData('getvideo.aspx?id=' + currentVideoID);
}
function loadData(URL) {
    // Create the XML request 
    xmlReq = null;
    if (window.XMLHttpRequest) xmlReq = new XMLHttpRequest();
    else if (window.ActiveXObject) xmlReq = new ActiveXObject("Microsoft.XMLHTTP");
    if (xmlReq == null) return; // Failed to create the request

    // Anonymous function to handle changed request states
    xmlReq.onreadystatechange = function () {
        switch (xmlReq.readyState) {
            case 0: // Uninitialized
                break;
            case 1: // Loading
                break;
            case 2: // Loaded
                break;
            case 3: // Interactive
                break;
            case 4: // Done!
                if (player != null) {
                    player.sendEvent('LOAD', '/Data/Learning/Library/Videos/' + xmlReq.responseXML.getElementsByTagName('VideoFilename')[0].firstChild.data);
                    player.sendEvent('PLAY');

                    var title = xmlReq.responseXML.getElementsByTagName('Title')[0].firstChild;
                    var author = xmlReq.responseXML.getElementsByTagName('Author')[0].firstChild;
                    var createddate = xmlReq.responseXML.getElementsByTagName('DateCreated')[0].firstChild;
                    var description = xmlReq.responseXML.getElementsByTagName('Description')[0].firstChild;
                    var downloadlink = xmlReq.responseXML.getElementsByTagName('VideoFileNameForDL')[0].firstChild;

                    if (title != null) {
                        document.getElementById('description_box').innerHTML = "<b>" + title.data + "</b><br/>";
                    }

                    if (author != null) {
                        document.getElementById('description_box').innerHTML += "<b>" + author.data + "</b><br/>";
                    }

                    if (createddate != null) {
                        document.getElementById('description_box').innerHTML += "<b>" + createddate.data + "</b><br/>";
                    }

                    if (description != null) {
                        document.getElementById('description_box').innerHTML += "<b>" + description.data + "</b>";
                    }

                    document.getElementById('DLVideoToYourComputer').href = (downloadlink != null) ? "/Utilities/Download.aspx?file=/Data/Learning/Library/Videos/" + downloadlink.data : "";

                }
                break;
            default:
                break;
        }
    }

    // Make the request
    xmlReq.open('GET', URL, true);
    xmlReq.send(null);
}
function playNext() {
    loadVideo(document.getElementById('ScrollBoxVideoItem'+currentVideoID).value);
}

window.onload = function() {
    loadVideo(currentVideoID);
}