 // The web service call
 var ENtoken = "psA-C9iX8jxWaTv4TlruDuG2rPMKBHxdLZp0mEnyggQ9QZkr_4eOUA..";
 var FRtoken = "S6uUxcu-t63ICm7r86YmO57KgzbXcvpxruO6t2suEHpLSe3pp6a17Q..";
 var req;
 var confPlaylist = new Array();
 
 function initBCPlaylist(language, playlistID, nbItems, style, URLredirect) {
  // set variable
  confPlaylist.push(new Array(playlistID,nbItems,style,URLredirect));
  
  // Build request with playlist Id and token
  req = "http://api.brightcove.com/services/library?";
  req += "command=find_playlist_by_id&playlist_id=" + playlistID;
  //Choose the the right Token to use FR or EN
  if (language == "FR"){
	req += "&token=" + encodeURIComponent(FRtoken); // tokens need to be URL-encoded
  }
  else {
	req += "&token=" + encodeURIComponent(ENtoken); // tokens need to be URL-encoded
  }
  req += "&video_fields=id,name,shortDescription,thumbnailURL&callback=response";
  
  // Create a new request object
  bObj = new JSONscriptRequest(req); 
  // Build the dynamic script tag
  bObj.buildScriptTag(); 
  // Add the script tag to the page
  bObj.addScriptTag();
 }
 
 // Define the callback function
 // writes out the HTML for each title item in the list
 function response(jsonData) {
  var items = jsonData["videos"];
  var tDivName = "titleList_" + jsonData["id"];
  var tDiv = document.getElementById(tDivName);

  var i=0;
  var done = false;
  while (i<confPlaylist.length && !done) {
	if (confPlaylist[i][0] == jsonData["id"]){
		switch(confPlaylist[i][2]){
			case "58" :  buildHTML58(items,tDiv,confPlaylist[i][1],confPlaylist[i][3],confPlaylist[i][0]);
				break;
			case "59" :  buildHTML59(items,tDiv,confPlaylist[i][1],confPlaylist[i][3],confPlaylist[i][0]);
				break;
			case "62" :  buildHTML62(items,tDiv,confPlaylist[i][1],confPlaylist[i][3],confPlaylist[i][0]);
			break;
		}
		done = true;
	}
	i++;
  }  
 }
 
 function buildHTML59(items,tDiv,nbItems,rURL,pID){
	var i=0;
	var str = "";
	
	str += '<div class="CR_LStyle59" style="width: 100%">';
	str += '<div class="Font_size_A Font_color_A"></div>';
	
	while (i<items.length && i<nbItems) {
		str += '<div class="row59">';
			str += '<div>';
				str += '<div class="ImageBlock">';
					str += '<div style="z-index:1">';
						str += '<a href="#bcplayer" onClick="playTitleFromList(' + items[i].id + ', \'' + rURL + '\',' + pID + ')"><img src="' + items[i].thumbnailURL + '" width="92" height="69"/></a>';
					str += '</div>';
				str += '</div>';
				str += '<div class="TextBlock">';
					str += '<a href="#bcplayer" onClick="playTitleFromList(' + items[i].id + ', \'' + rURL + '\',' + pID + ')"><span class="Font_color_B Font_size_B">' + items[i].name + '</span></a>';
					str += '<br/>';
					str += '<span class="Font_size_A Font_color_A">' + items[i].shortDescription + '</span>';
				str += '</div>';
			str += '</div>';
		str += '</div>';
		str += '<div style="clear:both"></div>';
		str += '<div class="HLine_1px Border_color_A"><!-- . --></div>';
		i++;
	}
	
	str += '</div>';
	tDiv.innerHTML = str;
 }
 
 function buildHTML58(items,tDiv,nbItems,rURL,pID){
 	var i=0;
	var str = "";
	
	str += '<div class="CR_Style58" style="width: 100%">';
	str += '<div class="Font_size_A Font_color_A"></div>';
	
	while (i<items.length && i<nbItems) {
		str += '<div class="row58_bottom">';
		j=i;
		while (j<(i+2) && (j)<items.length && (j)<nbItems){
			str += '<div style="overflow:hidden;width:49%" class="content58">';
				str += '<div style="z-index:1">';
					str += '<a href="#bcplayer" onClick="playTitleFromList(' + items[j].id + ', \'' + rURL + '\',' + pID + ')"><img src="' + items[j].thumbnailURL + '" width="92" height="69"/></a>';
				str += '</div>';
				str += '<div class="textblock58" style="clear:both;">';
					str += '<a href="#bcplayer" onClick="playTitleFromList(' + items[j].id + ', \'' + rURL + '\',' + pID + ')"><span class="Font_color_B Font_size_B">' + items[j].name + '</span></a>';
					str += '<br/>';
					str += '<span class="Font_size_A Font_color_A">' + items[j].shortDescription + '</span>';
				str += '</div>';
			str += '</div>';
			j++;
		}
		str += '</div>';
		str += '<div style="clear:both"></div>';
		i+=2;
	}
	
	str += '</div>';
	tDiv.innerHTML = str;
 }
 
 function buildHTML62(items,tDiv,nbItems,rURL,pID){
 	var i=0;
	var j=0;
	var str = "";
	
	str += '<div class="CR_Style62" style="width: 100%">';
	str += '<div class="Font_size_A Font_color_A"></div>';
	
	while (i<items.length && i<nbItems) {
		str += '<div class="row62_bottom">';
		j=i;
		while (j<(i+4) && (j)<items.length && (j)<nbItems){
			str += '<div class="content4in" style="overflow:hidden;width: 24%;">';
				str += '<div class="imageblock62">';
					str += '<div style="z-index:1">';
						str += '<a href="#bcplayer" onClick="playTitleFromList(' + items[j].id + ', \'' + rURL + '\',' + pID + ')"><img src="' + items[j].thumbnailURL + '" width="92" height="69"/></a>';
					str += '</div>';
				str += '</div>';
				str += '<div class="textblock62">';
					str += '<a href="#bcplayer" onClick="playTitleFromList(' + items[j].id + ', \'' + rURL + '\',' + pID + ')"><span class="Font_color_B Font_size_B">' + items[j].name + '</span></a>';
					str += '<br/>';
				str += '</div>';
			str += '</div>';
			j++;
		}
		str += '</div>';
		str += '<div style="clear:both"></div>';
		i+=4;
	}
	
	str += '</div>';
	tDiv.innerHTML = str;
 }
 
 // time is stored in milliseconds; we need to convert to a mm:ss display format
 /*
 function formatTime(time) {
  
  var t_secs = Math.round(time/1000);
  var mins = Math.floor(t_secs/60);
  var secs = t_secs - (mins*60);
  return mins + ":" + secs;
 }
*/
  
 // handles click event from list items 
 function playTitleFromList(id, rURL, pID) {
  if (rURL == "") { 
	rURL = "http://" + document.location.hostname + document.location.pathname;
	document.location.href = rURL + "?bclid=" + pID + "&bctid=" + id;
	//content.getVideoAsynch(id); 
  }
  else { 
	document.location.href = rURL + "?bclid=" + pID + "&bctid=" + id; 
  }
 }
 
 function onVideoLoad(event) {
  video.loadVideo(event.video.id);
 }