﻿var ytplayer
function updateHTML(elmId, value) {
    document.getElementById(elmId).innerHTML = value;
}

function setytplayerState(newState) {
    //updateHTML("playerstate", newState);
    if (newState == 1) {
        updateHTML("actionButton", "Pause");
    } else {
        updateHTML("actionButton", "Play")
    }
    // self.status = newState;
    if (newState == 5) {
        play()
        unMute()
    }
}

function onYouTubePlayerReady(playerId) {
    ytplayer = document.getElementById("myytplayer");
    setInterval(updateytplayerInfo, 250);
    updateytplayerInfo();
    ytplayer.addEventListener("onStateChange", "onytplayerStateChange");
    ytplayer.addEventListener("onError", "onPlayerError");
    /*alert("HERE");
    if (!meadiaBuilt) {
        buildMedia();
    } */    
}
function onytplayerStateChange(newState) {
    setytplayerState(newState);    
}

function onPlayerError(errorCode) {
    if (errorCode != 100 && errorCode != 2) {
    alert("An error occured: " + errorCode);
    }
}

function updateytplayerInfo() {
    /*updateHTML("bytesloaded", getBytesLoaded());
    updateHTML("bytestotal", getBytesTotal());*/
    updateHTML("videoduration", getFormatedDuration());
    updateHTML("videotime", getFormatedCurrentTime());
    /*updateHTML("startbytes", getStartBytes());
    updateHTML("volume", getVolume());*/
    try {
        var elem = document.getElementById("amountLoaded");
        elem.style.width = Math.round(Math.round((getBytesLoaded() / getBytesTotal()) * 100) / 100 * 206) + "px"
        try {
            if (!isDragging) {
                var elem = document.getElementById("playPosition");
                elem.style.left = Math.round(Math.round((getCurrentTime() / getDuration()) * 100) / 100 * 206) + 61 + "px"
            }
        } catch (e) {
            var elem = document.getElementById("playPosition");
            elem.style.left = Math.round(Math.round((getCurrentTime() / getDuration()) * 100) / 100 * 206) + 61 + "px"
        }
        
    } catch (e) { }
}

// functions for the api calls
function loadNewVideo(id, startSeconds) {
    //alert(id + " " + ytplayer);
    if (ytplayer) {
        try {
            ytplayer.loadVideoById(id, parseInt(startSeconds));
        } catch (e) { }
    }
}
function reposition(pos) {
    var seconds = (pos - 61) / 206 * getDuration()
    //Math.round(Math.round((getCurrentTime() / getDuration()) * 100) / 100 * 206) + 61 + "px"
    seekTo(seconds)
}
function cueNewVideo(id, startSeconds) {
    if (ytplayer) {
        ytplayer.cueVideoById(id, startSeconds);
    }
}

function play() {
    if (ytplayer) {
        ytplayer.playVideo();
    }
}

function pause() {
    if (ytplayer) {
        ytplayer.pauseVideo();
    }
}
function playPause() {
    
    if (ytplayer) {
        if (getPlayerState() == 1) {
            pause()
        } else {
            play()
        }
    }
}
function stop() {
    if (ytplayer) {
        ytplayer.stopVideo();
    }
}

function getPlayerState() {
    if (ytplayer) {
        return ytplayer.getPlayerState();
    }
}

function seekTo(seconds) {
    if (ytplayer) {
        ytplayer.seekTo(seconds, true);
    }
}

function getBytesLoaded() {
    if (ytplayer) {
        return ytplayer.getVideoBytesLoaded();
    }
}

function getBytesTotal() {
    if (ytplayer) {
        return ytplayer.getVideoBytesTotal();
    }
}

function getCurrentTime() {
    if (ytplayer) {
        return ytplayer.getCurrentTime();
    }
}
function getFormatedCurrentTime() {
    if (ytplayer) {
        return timeSpan(Math.round(ytplayer.getCurrentTime()));
    }
}
/*function formatTime(val) {
    retval = ""
    if (val > 60) {
        retval= twoDigti(Math.floor(val/60)) & ":"
    } else {
        retval = "00:" + val;
    }

}*/
function getDuration() {
    if (ytplayer) {
        return ytplayer.getDuration();
    }
}
function getFormatedDuration() {
    if (ytplayer) {
        return timeSpan(Math.round(ytplayer.getDuration()));
    }
}
function getStartBytes() {
    if (ytplayer) {
        return ytplayer.getVideoStartBytes();
    }
}

function mute() {
    if (ytplayer) {
        ytplayer.mute();
    }
}

function unMute() {
    if (ytplayer) {
        ytplayer.unMute();
    }
}

function getEmbedCode() {
    alert(ytplayer.getVideoEmbedCode());
}

function getVideoUrl() {
    alert(ytplayer.getVideoUrl());
}

function setVolume(newVolume) {

    if (ytplayer) {
        ytplayer.setVolume(newVolume);
    }
}

function getVolume() {
    if (ytplayer) {
        return ytplayer.getVolume();
    }
}

function clearVideo() {
    if (ytplayer) {
        ytplayer.clearVideo();
        loadNewVideo(-1, 0);
    }
}

