/**/

// Uses the brightcove 2 api; migration to brightcove 3 has not begun yet

var _videoInformation = {
    videoIdsArray : [],
    videoSrcArray : []
}

// onTemplateLoaded - brightcove event
// when the player is ready for API interaction, think "$(document).ready(fn);" 
function onTemplateLoaded(message) {
    // fires whenever a video loads
  
  callFlash("addEventListener", "mediaLoad", "onMediaLoad"); 
    //fires when content loads for the first time
  callFlash("addEventListener", "contentLoad", "onContentLoad");
  callFlash("addEventListener", "mediaComplete", "onMediaComplete");

}

// returns the lineups attached to the player
function getPlayerLineups_Result(infoObj) {
  _videoInformation.videoIdsArray = infoObj[0].videoIds; //in this case we only have one lineup
}

//updates the title and blurb with the video that is currently playing
function getCurrentTitle_Result(infoObj) {
    document.getElementById("bcTitle").firstChild.nodeValue=infoObj.displayName;
    document.getElementById("bcDesc").firstChild.nodeValue=infoObj.shortDescription;

}

// loads video metadata for videos that aren't playing without loading their flv
function getTitleById_Result(infoObj) {
    _videoInformation.videoSrcArray.push({
        src: infoObj.thumbnailURL,
        alt: infoObj.displayName
    });
}

// when a new video starts, makes the api call to update the title and desc. 
function onMediaLoad() {
  callFlash("getCurrentTitle");
}

function onContentLoad() {
   callFlash("getCurrentTitle");
   callFlash("getPlayerLineups");

}

var currentVideo = 0;
function onMediaComplete() {
	if (_videoInformation.videoIdsArray.length <= currentVideo) {
		currentVideo=-1;
	}
	currentVideo++;
	loadVideos(currentVideo);
}

// loadVideos
// num - int, the index of the id you wish to load into the player
function loadVideos(num){
  currentVideo=num;
  callFlash("loadTitleById", _videoInformation.videoIdsArray[num]);
}



// formatPlayerString
// textString - string, the text to trim
// numChars - int, the target number of characters trim the textString to
// addEllipse - bool, true to add an ellipse, false to trim only
// adjust text length to ensure that the right amount of text is supplied
function formatPlayerString(textString, numChars, addEllipse){
    if(textString.length <= numChars){
        return textString;
    }
    else{
        var endOfString = textString.substring(numChars-15);
        var firstBoundaryIndex = endOfString.search(/[\s]/);
        var newEnding = (firstBoundaryIndex >= 0) ? endOfString.substr(0, firstBoundaryIndex) : "";
        return textString.substr(0, numChars-15) + newEnding + ((addEllipse) ? "..." : "");
    }
}
