/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

//YouTube API callback function
function onYouTubePlayerReady(playerId) {
  var ytplayer = document.getElementById("ytPlayer");
  ytplayer.addEventListener("onError", "onPlayerError");
}

videoSwitch = function(obejctID,videoID,width,height){
    var params = {allowScriptAccess: "always"};
        // 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/v/" + videoID + "?version=3&enablejsapi=1&playerapiid=player1", 
                       obejctID, width, height, "8", null, null, params, atts); 
}
videoSwitch.prototype.switchMovie = function(videoID,cue){
    if(videoSwitch.ytplayer){
        videoSwitch.ytplayer.stopVideo();
        videoSwitch.ytplayer.clearVideo();
        if(cue){
            videoSwitch.ytplayer.cueVideoById(videoID);
        }
        else{
            videoSwitch.ytplayer.loadVideoById(videoID);
        }
    }
}

videoSwitch.onPlayerError = function(errorCode) {
  alert("An error during video download. Error code:" + errorCode);
}

videoSwitch.onStateChange = function(state){
   // console.log(state); 
    if(state==5){
        videoSwitch.ytplayer.seekTo(0,false);
        videoSwitch.cuedTriggered = true;
    } 
    if(state==3 && videoSwitch.cuedTriggered){
        videoSwitch.cuedTriggered = false;
        videoSwitch.ytplayer.pauseVideo()
    }
}

//YouTube API callback function
function onYouTubePlayerReady(playerId) {
  videoSwitch.ytplayer = document.getElementById("ytPlayer");
  videoSwitch.ytplayer.addEventListener("onError", "videoSwitch.onPlayerError");
  videoSwitch.ytplayer.addEventListener("onStateChange", "videoSwitch.onStateChange");
}

function onPlayerError(errorCode) {
  alert("An error occured of type:" + errorCode);
}


