﻿function onYouTubePlayerReady(playerId)
{
  var ytVideoID = $(".videoID").val();
  ytplayer = document.getElementById("flashVideoContainer");
  ytplayer.cueVideoById(ytVideoID, 0);
  $("#playController").click(function(){return handle_play()});
  $("#pauseController").click(function(){return handle_pause()});
  $("#restartController").click(function(){return handle_restart()});
  $(".videoController").click(function(){return handle_swapVideo(this)}); 
}

function handle_play()
{
    if(ytplayer)
        ytplayer.playVideo();
        
    return false;
}

function handle_pause()
{
    if(ytplayer)
        ytplayer.pauseVideo();
        
    return false;
}

function handle_restart()
{
    if(ytplayer)
    {
        ytplayer.seekTo(0, false);
        ytplayer.playVideo();
    }
    
    return false;
}
    
function handle_swapVideo(obj)
{
    var ytVideoID = $(obj).attr("rel");
    if(ytplayer && ytVideoID != "")
    {
        var videoTitle = $(".title_"+ytVideoID).text();
        $(".playingVideo").text(videoTitle);
        
        ytplayer.cueVideoById(ytVideoID, 0);
        ytplayer.playVideo();
    }
    
    return false;
}
var flashvars = {};
var params = { swliveconnect:"true", allowScriptAccess: "always" };
var attributes = { id:"flashVideoContainer", name:"flashVideoContainer" };
$.swfobject.embedSWF("http://www.youtube.com/apiplayer?enablejsapi=1&playerapiid=ytplayer", "videoplayer", "299", "230", "8", "expressInstall.swf", flashvars, params, attributes);   
