﻿function slLoad(sender) {
    playerInstance.init();
}


playerManager = function() {
    this._silverlightControl = null;
    this._sPlayer = null;
    this._playerContainer = null;
}

playerManager.prototype = {
    init: function() {
        this._playerContainer = document.getElementById("chromePlayer");
        this._playerContainer.style.visibility = 'hidden';
        this._silverlightControl = document.getElementById("slControl");
        this._sPlayer = playerInstance._silverlightControl.Content.silverlightChromeManager;
        this._sPlayer.SetJSPlayerObject(playerInstance);
    },
    showPlayer: function() {
        if (this._playerContainer != null) {
            this._playerContainer.style.visibility = 'visible';
        }
    },
    hidePlayer: function() {
        if (this._playerContainer != null) {
            this._playerContainer.style.visibility = 'hidden';
        }
    },
    getPlayerVisibility: function() {
        if (this._playerContainer != null) {
            return this._playerContainer.style.visibility;
        }
    },
    loadNewVideo: function(id, startSeconds) {
        if (ytplayer) {
            ytplayer.loadVideoById(id, parseInt(startSeconds));
        }
    },
    play: function() {
        if (ytplayer) {
            ytplayer.playVideo();
        }
    },
    pause: function() {
        if (ytplayer) {
            ytplayer.pauseVideo();
        }
    },
    stop: function() {
        if (ytplayer) {
            ytplayer.stopVideo();
        }
    },
    playerStateChangedEventHandler: function(newState) {
        if (this._sPlayer != null) {
            this._sPlayer.OnPlayerStateChanged(newState);
        }
    },
    playerErrorEventHandler: function(errorCode) {
        if (this._sPlayer != null) {
            this._sPlayer.OnPlayerError(errorCode);
        }
    },

    cueNewVideo: function(id, startSeconds) {
        if (ytplayer) {
            ytplayer.cueVideoById(id, startSeconds);
        }
    },

    getPlayerState: function() {
        if (ytplayer) {
            return ytplayer.getPlayerState();
        }
    },

    seekTo: function(seconds) {
        if (ytplayer) {
            ytplayer.seekTo(seconds, true);
        }
    },

    getBytesLoaded: function() {
        if (ytplayer) {
            return ytplayer.getVideoBytesLoaded();
        }
    },

    getBytesTotal: function() {
        if (ytplayer) {
            return ytplayer.getVideoBytesTotal();
        }
    },

    getCurrentTime: function() {
        if (ytplayer) {
            return ytplayer.getCurrentTime();
        }
    },

    getDuration: function() {
        if (ytplayer) {
            return ytplayer.getDuration();
        }
    },

    getStartBytes: function() {
        if (ytplayer) {
            return ytplayer.getVideoStartBytes();
        }
    },

    mute: function() {
        if (ytplayer) {
            ytplayer.mute();
        }
    },

    unMute: function() {
        if (ytplayer) {
            ytplayer.unMute();
        }
    },
    getIsMuted: function() {
        if (ytplayer) {
            return ytplayer.isMuted();
        }
    },
    getEmbedCode: function() {
        if (ytplayer) {
            return ytplayer.getVideoEmbedCode();
        }
    },

    getVideoUrl: function() {
        if (ytplayer) {
            return ytplayer.getVideoUrl();
        }
    },

    setVolume: function(newVolume) {
        if (ytplayer) {
            ytplayer.setVolume(newVolume);
        }
    },

    getVolume: function() {
        if (ytplayer) {
            return ytplayer.getVolume();
        }
    },

    clearVideo: function() {
        if (ytplayer) {
            ytplayer.clearVideo();
        }
    }
}

var playerInstance = new playerManager();



function onYouTubePlayerReady(playerId) {
    ytplayer = document.getElementById("myytplayer");
    ytplayer.addEventListener("onStateChange", "onytplayerStateChange");
    ytplayer.addEventListener("onError", "onPlayerError");
}

function onytplayerStateChange(newState) {
    if (playerInstance != null)
        playerInstance.playerStateChangedEventHandler(newState);
}

function onPlayerError(errorCode) {
    if (playerInstance != null)
        playerInstance.playerErrorEventHandler(errorCode);
}

