﻿function updateHTML(elmId, value) {
    document.getElementById(elmId).innerHTML = value;
}
function setytplayerState(newState) {
    updateHTML("playerstate", newState);
}
function onYouTubePlayerReady(PlayerId) {
    FLAytplayer = document.getElementById(PlayerId);
    setInterval("updateytplayerInfo('" + PlayerId + "')", 250);
    FLAytplayer.addEventListener("onStateChange", "onStateChange_" + PlayerId);
    //FLAytplayer.addEventListener("onError", "onPlayerError");
}
/*
function onPlayerError(errorCode) {
alert("An error occurred: " + errorCode);
}
*/
function Mute_Unmute(PlayerId) {
    FLAytplayer = document.getElementById(PlayerId);
    MuteControl = document.getElementById(PlayerId + "Mutecotrol");
    MuteImage = document.getElementById("PlayerId");
    if (FLAytplayer.isMuted()) {
        FLAytplayer.unMute();
        MuteControl.className = "icon mute";
    }
    else {
        FLAytplayer.mute();
        MuteControl.className = "icon unmute";
    }
}
function updateytplayerInfo(PlayerId) {
    updateHTML("videoduration" + PlayerId, splitTime(getDuration(PlayerId)));
    updateHTML("videocurrent" + PlayerId, splitTime(getCurrentTime(PlayerId)));
    updateHTML("DownloadPercent" + PlayerId, GetPercentageDownloaded(PlayerId));
    if (getPlayerState(PlayerId) == 0)
        if (document.getElementById("videoreplay" + PlayerId).value != "True") {
        Playbutton = document.getElementById(PlayerId + "Play");
        GotoFirstFrame(PlayerId);
        Playbutton.className = "icon play";
        var coverlayer = document.getElementById(PlayerId + "CoverLayer");
        coverlayer.style.visibility = "visible";
        coverlayer.style.display = "block";
    }
    else {
        Playbutton = document.getElementById(PlayerId + "Play");
        FLAytplayer = document.getElementById(PlayerId);
        GotoFirstFrame(PlayerId);
        FLAytplayer.playVideo();
        Playbutton.className = "icon pause";
    }
}
function GetPercentageDownloaded(PlayerId) {
    FLAytplayer = document.getElementById(PlayerId);
    var all = FLAytplayer.getVideoBytesTotal();
    var partBytes = FLAytplayer.getVideoBytesLoaded();
    var percent = getPercent2(all, partBytes);
    return "[" + Math.round(percent) + "% Loaded]";
}
function getPercent2(all, partBytes) {
    return (all > 0) ? (100 / all) * partBytes : 0;
}
function updateBytesbar(PlayerId) {
    FLAytplayer = document.getElementById(PlayerId);
    var all = FLAytplayer.getVideoBytesTotal();
    var partBytes = FLAytplayer.getVideoBytesLoaded();
    var percentBytes = getPercent2(all, partBytes);
    var bytesbarWidth = 100;
    document.getElementById('bytesbarIndicator').style.width = percentBytes * (bytesbarWidth / 100) + "%";
}
function loadNewVideo(id, startSeconds, PlayerId) {
    FLAytplayer = document.getElementById(PlayerId);
    PlayerIcon = document.getElementById(PlayerId + "PlayLayer");
    PlayControls = document.getElementById(PlayerId + "PlayControls");
    try {
        if (FLAytplayer) {
            PlayControls.style.display = 'block';
            PlayControls.style.visibility = 'visible';
            FLAytplayer.loadVideoById(id, parseInt(startSeconds));
            ChangeIcon(PlayerId);
            PlayerIcon.style.display = 'none'; PlayerIcon.style.visibility = 'hidden';
            if (FLAytplayer.isMuted()) {
                FLAytplayer.unMute();
            }
        }
    }
    catch (err) {
        showErrorMessage(PlayerId);
    }

}
function cueNewVideo(id, startSeconds, PlayerId) {
    FLAytplayer = document.getElementById(PlayerId);
    if (FLAytplayer) {
        FLAytplayer.cueVideoById(id, startSeconds);
    }
}
function play(PlayerId) {
    FLAytplayer = document.getElementById(PlayerId);
    Playbutton = document.getElementById(PlayerId + "Play");
    if (FLAytplayer) {
        if (Playbutton.className == "icon pause") {
            FLAytplayer.pauseVideo();
            Playbutton.className = "icon play"
        }
        else if (Playbutton.className = "icon play") {
            FLAytplayer.playVideo();
            Playbutton.className = "icon pause";
        }
    }
}
function ChangeIcon(PlayerId) {
    FLAytplayer = document.getElementById(PlayerId);
    Playbutton = document.getElementById(PlayerId + "Play");
    if (FLAytplayer) {
        if (Playbutton.className == "icon pause") {
        }
        else if (Playbutton.className = "icon play") {
            Playbutton.className = "icon pause";
        }
    }
}
function pause(PlayerId) {
    FLAytplayer = document.getElementById(PlayerId);
    if (FLAytplayer) {
        FLAytplayer.pauseVideo();
    }
}
function stop(PlayerId) {
    FLAytplayer = document.getElementById(PlayerId);
    if (FLAytplayer) {
        FLAytplayer.stopVideo();
        FLAytplayer
    }
}
function getPlayerState(PlayerId) {
    FLAytplayer = document.getElementById(PlayerId);
    if (FLAytplayer) {
        return FLAytplayer.getPlayerState();
    }
}
function seekTo(seconds, PlayerId) {
    FLAytplayer = document.getElementById(PlayerId);
    if (FLAytplayer) {
        FLAytplayer.seekTo(seconds, true);
    }
}
function getBytesLoaded(PlayerId) {
    FLAytplayer = document.getElementById(PlayerId);
    if (FLAytplayer) {
        return FLAytplayer.getVideoBytesLoaded();
    }
}
function getBytesTotal(PlayerId) {
    FLAytplayer = document.getElementById(PlayerId);
    if (FLAytplayer) {
        return FLAytplayer.getVideoBytesTotal();
    }
}
function fwd(PlayerId) {
    FLAytplayer = document.getElementById(PlayerId);
    if (FLAytplayer) {
        var fwdSeconds = getCurrentTime(PlayerId) + 10;
        FLAytplayer.seekTo(fwdSeconds);
        RemoveLayer1(PlayerId + "CoverLayer");
    }
}
function rwd(PlayerId) {
    FLAytplayer = document.getElementById(PlayerId);
    if (FLAytplayer) {
        var rwdSeconds = getCurrentTime(PlayerId) - 10;
        FLAytplayer.seekTo(rwdSeconds);
        RemoveLayer1(PlayerId + "CoverLayer");
    }
}
function GotoFirstFrame(PlayerId) {
    FLAytplayer = document.getElementById(PlayerId);
    if (FLAytplayer) {
        var rwdSeconds = 0;
        FLAytplayer.seekTo(rwdSeconds, true);
    }
}
function getCurrentTime(PlayerId) {
    FLAytplayer = document.getElementById(PlayerId);
    if (FLAytplayer) {
        return FLAytplayer.getCurrentTime();
    }
}
function getDuration(PlayerId) {
    FLAytplayer = document.getElementById(PlayerId);
    if (FLAytplayer) {
        return FLAytplayer.getDuration();
    }
}
function getStartBytes(PlayerId) {
    FLAytplayer = document.getElementById(PlayerId);
    if (FLAytplayer) {
        return FLAytplayer.getVideoStartBytes();
    }
}
function mute(PlayerId) {
    FLAytplayer = document.getElementById(PlayerId);
    if (FLAytplayer) {
        FLAytplayer.mute();
    }
}
function unMute(PlayerId) {
    FLAytplayer = document.getElementById(PlayerId);
    if (FLAytplayer) {
        FLAytplayer.unMute();
    }
}
function getEmbedCode(PlayerId) {
    FLAytplayer = document.getElementById(PlayerId);
}
function getVideoUrl(PlayerId) {
    FLAytplayer = document.getElementById(PlayerId);
}
function setVolume(newVolume, PlayerId) {
    FLAytplayer = document.getElementById(PlayerId);
    if (FLAytplayer) {
        FLAytplayer.setVolume(newVolume);
    }
}
function getVolume(PlayerId) {
    FLAytplayer = document.getElementById(PlayerId);
    if (FLAytplayer) {
        return FLAytplayer.getVolume();
    }
}
function clearVideo(PlayerId) {
    FLAytplayer = document.getElementById(PlayerId);
    if (FLAytplayer) {
        FLAytplayer.clearVideo();
    }
}
function ShowPlayLayer1(videodiv, lname) {
    var l = document.getElementById(lname);
    var v = document.getElementById(videodiv);
    l.style.width = "50px";
    l.style.height = "50px";
    return;
}
function ShowPlayLayer(videodiv, lname) {
    var l = document.getElementById(lname);
    var x = findPosX1(videodiv);
    var y = findPosY1(videodiv);
    alert(videodiv + "  " + x.toString() + "   " + y.toString());
    l.style.left = x + "px";
    l.style.top = y + "px";
    l.style.visibility = "visible";
    return;
}
function hideLayer1(lname) {
    var l = document.getElementById(lname);
    l.style.visibility = "hidden";
    return;
}
function RemoveLayer1(lname) {
    var l = document.getElementById(lname);
    l.style.visibility = "hidden";
    l.style.display = "none";
    return;
}
function splitTime(a) {
    var tm = new Date(a * 1000)
    var hours = Math.round(tm.getUTCHours());
    var minutes = Math.round(tm.getUTCMinutes());
    var seconds = Math.round(tm.getUTCSeconds());
    if (hours > 0) {
        timeStr = "" + hours;
        timeStr += ((minutes < 10) ? ":0" : ":") + minutes;
        timeStr += ((seconds < 10) ? ":0" : ":") + seconds;
    }
    else {
        timeStr = "";
        timeStr = ((minutes < 10) ? "0" : ":") + minutes;
        timeStr += ((seconds < 10) ? ":0" : ":") + seconds;
    }
    return timeStr
}
function showErrorMessage(PlayerId) {
    var coverlayer = document.getElementById(PlayerId + "CoverLayer");
    coverlayer.style.visibility = "hidden";
    //coverlayer.style.display = "block";
    var PlayLayer = document.getElementById(PlayerId + "PlayLayer");
    PlayLayer.style.visibility = "hidden";
    var PlayControls = document.getElementById(PlayerId + "PlayControls");
    PlayControls.style.visibility = "hidden";
    var ErrorLayer = document.getElementById(PlayerId + "ErrorLayer");
    ErrorLayer.style.visibility = "visible";
    var Captions = document.getElementById(PlayerId + "Captions");
    Captions.style.visibility = "hidden";
}