﻿/* this requires the following google library to be included before this file
*  <script src="http://www.google.com/jsapi" type="text/javascript"></script>
*  
*  The youtube video id is set in a global variable called youtubevideoid, this
*  variable must be set in the Sitecore item field called "Head Tag Includes".
*  $ytvideoid$ is the replacer token that is replaced with the video id value
*  stored on the item
*
*  Example: <script type="text/javascript"> var youtubevideoid = "$ytvideoid$"</script>
*/
google.load("swfobject", "2.1");
/*
* Chromeless player has no controls.
*/

// Update a particular HTML element with a new value
function updateHTML(elmId, value) {
    document.getElementById(elmId).innerHTML = value;
}

// This function is called when an error is thrown by the player
function onPlayerError(errorCode) {
alert("An error occured of type:" + errorCode);
}

// This function is called when the player changes state
function onPlayerStateChange(newState) {
    if (newState == 1) {
        $j("#playBtn").hide();
        $j("#pauseBtn").show();
    }
    if (newState == 0) {
        $j("#pauseBtn").hide();
        $j("#playBtn").show();
    }
}
      
// Allow the user to set the volume from 0-100
function setVideoVolume() {
    var volume = parseInt(document.getElementById("volumeSetting").value);
    if(isNaN(volume) || volume < 0 || volume > 100) {
        alert("Please enter a valid volume between 0 and 100.");
    }
    else if(ytplayer){
        ytplayer.setVolume(volume);
    }
}

function playVideo() {
    if (ytplayer) {
        ytplayer.playVideo();
        $j("#playBtn").hide();
        $j("#pauseBtn").show();
    }
}

function pauseVideo() {
    if (ytplayer) {
        ytplayer.pauseVideo();
        $j("#pauseBtn").hide();
        $j("#playBtn").show();
        
    }
}

function muteVideo() {
    if(ytplayer) {
        ytplayer.mute();
        $j("#muteBtn").hide();
        $j("#volumeBtn").show();
    }
}

function unMuteVideo() {
    if(ytplayer) {
        ytplayer.unMute();
        $j("#volumeBtn").hide();
        $j("#muteBtn").show();
    }
}
      
// This function is automatically called by the player once it loads
function onYouTubePlayerReady(playerId) {
    ytplayer = document.getElementById("ytPlayer");
    // This causes the updatePlayerInfo function to be called every 250ms to
    // get fresh data from the player
    //setInterval(updatePlayerInfo, 250);
    ytplayer.addEventListener("onStateChange", "onPlayerStateChange");
    ytplayer.addEventListener("onError", "onPlayerError");
    //Load an initial video into the player
    ytplayer.cueVideoById(youtubevideoid);
}

// The "main method" of this sample. Called when someone clicks "Run".
function loadPlayer() {
// Lets Flash from another domain call JavaScript
var params = { allowScriptAccess: "always", wmode: "transparent" };
// The element id of the Flash embed
var atts = { id: "ytPlayer" };
// All of the magic handled by SWFObject (http://code.google.com/p/swfobject/)
swfobject.embedSWF("http://www.youtube.com/apiplayer?" +
                   "&enablejsapi=1&playerapiid=player1", 
                   "videoDiv", "324", "200", "8", null, null, params, atts);
}

function _run() {
    loadPlayer();
}
google.setOnLoadCallback(_run);
