// You should define a function called "BCTemplateLoaded" in your code.
// This will be called when the Brightcove Player is loaded. You only need this
// function when using the Brightcove player.

var bcExp;
var bcPlayer;
var bcCuepoints;
var bcContent;

function onTemplateLoaded(experienceID) {
	bcExp = brightcove.getExperience(experienceID);
	bcPlayer = bcExp.getModule(APIModules.VIDEO_PLAYER);
	bcCuepoints = bcExp.getModule(APIModules.CUE_POINTS);
	bcContent = bcExp.getModule(APIModules.CONTENT);
	
	//bcPlayer.addEventListener("mediaChange", LRonMediaLoad);
	bcPlayer.addEventListener("mediaBegin", onMediaStart);
	
	bcCuepoints.addEventListener("cuePoint", LRonCuePoint);
	
	BCTemplateLoaded();
}

function LRonMediaLoad(evt) {
	var currentVideo = bcPlayer.getCurrentVideo();
	BCRemoveCuePoints();
	var cuepoints = LRCustomHandler.GetCuepointsForBCVideo();
	var added = bcCuepoints.addCuePoints(currentVideo.id, cuepoints);
}

function onMediaStart(evt) {
	var currentVideo = bcPlayer.getCurrentVideo();
	BCRemoveCuePoints();
	var cuepoints = LRCustomHandler.GetCuepointsForBCVideo();
	var added = bcCuepoints.addCuePoints(currentVideo.id, cuepoints);
}

function LRonCuePoint(evt) {
 	 LRCustomHandler.onVideoCuePoint(evt.cuePoint.name);
}

function BCRemoveCuePoints() {
	// remove cuepoints for the current title
	var currentVideo = bcPlayer.getCurrentVideo();
	bcCuepoints.clearCodeCuePoints(currentVideo.id);
}

function LRLoadVideo(videoName, brightcoveId, referenceId)  {
	//stop the current video from playing so we don't trigger cuepoints during the load process
	bcPlayer.stop();
	//set the new video name for accessing cuepoints
	LRCustomHandler.currentVideoName = videoName;
	//now load the new video
	if (brightcoveId != null) {
		bcPlayer.loadVideo(brightcoveId,"id");
	} else if (referenceId != null) {
		bcPlayer.loadVideo(referenceId,"referenceId");
	}
}