function receiveOoyalaEvent(playerId, eventName, eventParams)
{
	// Ooyala player object, call API setters/getters upon this object
	var player = document.getElementById(playerId);
	
			
	
	switch(eventName){
		case "apiReady": // wait for this event to be dispatched before making any API calls. 
		
			// load thumbnails :)
		    var videos = getInfoOfAllChannelItems(player);
		    var thumbnailHTML = generateThumbnailHTML(videos,playerId);
		    document.getElementById("wrapper").innerHTML = thumbnailHTML;

		    //for first video load 
            var video = player.getCurrentItemEmbedCode();
			$("#wrapper #belt li a img").removeClass();
			$("#wrapper #belt li a img#"+video).addClass("highlight");
    
			/**--------------------**
			 * Initialization code
			 *---------------------*/
			var nr_movies = $("#wrapper li").length;
			var vids_per_grp = 4;
			//assigned a value, but it is computed
			var nr_groups = 4;
			if ((nr_movies%vids_per_grp) > 0) {
				//there is a remainder
				nr_movies = nr_movies - (nr_movies%vids_per_grp);
				nr_groups = (nr_movies/vids_per_grp)+1;
			}
			else
				nr_groups = (nr_movies/vids_per_grp);
			
			//Create dots
			$("#dots").empty();
			
			var dots_string = "";
			i=0;
			for (i==0; i<nr_groups; i++) {
				dots_string += "<a href='#group"+(i+1)+"' rel='dot"+(i+1)+"'>"+(i+1)+"</a>";
			}
			$("#dots").html(dots_string);
			$("#dots a:first").addClass("first");
			$("#dots a:last").addClass("last");
			$("#dots a").eq(0).addClass("active");
			
			$('#infiniteCarousel').infiniteCarousel();
		break;
		
		case "currentItemEmbedCodeChanged":
			var videos = getInfoOfAllChannelItems(player);
			var nr_movies = $("#wrapper li:not(.empty):not(.cloned)").length;
			var vids_per_grp = 4;
			//assigned a value, but it is computed
			var nr_groups = 4;
			if ((nr_movies%vids_per_grp) > 0) {
				//there is a remainder
				nr_movies = nr_movies - (nr_movies%vids_per_grp);
				nr_groups = (nr_movies/vids_per_grp)+1;
			}
			else
				nr_groups = (nr_movies/vids_per_grp);
			
			
            $("#wrapper #belt li a img").removeClass();
			$("#wrapper #belt li a img#"+eventParams.embedCode).addClass("highlight");
			
			
			
        break;
	}
}
// returns an array of video objects; video objects have two parameters: embedCode, and thumbnailURL
function getInfoOfAllChannelItems(player)
{
  var thumbnailWidth = 113; // Set thumbnail width here - 4px padding added to get 121
  var thumbnailHeight = 67; // Set thumbnail height here - 4px padding added to get 75
  var channelItems = player.getLineup();
  var urls = [];
  var embedCodes = [];
  var titles = []; 
  var videos = [];

  for (var i = 0; i < channelItems.length; i++)
  {
    urls.push(player.getPromoFor(channelItems[i].embedCode, thumbnailWidth, thumbnailHeight));
	embedCodes.push(channelItems[i].embedCode);
	titles.push(channelItems[i].title);
	var video = new Object();
	video.embedCode = embedCodes[i];
	video.thumbnailURL = urls[i];
	video.title = titles[i];
	videos.push(video);
  }
  return videos;
}

//creates html for thumnail images in form <div1><img1></div1>...<divN><imgN><divN>
function generateThumbnailHTML(videos,playerId)
{
  var html = "";
  var changeVideo = "";
  html += "<ul id='belt'>";
  for (var i = 0; i < videos.length; i++)
  {
    changeembed = 'changeEmbed("' + playerId + '", "' + videos[i].embedCode + '")';
    html += "<li><a href='#' onclick='" + changeembed + ";return false' class='panel'>";
	html += "<img border='0' width='113px' height='67px' alt='" + videos[i].title + "' title='" + videos[i].title + "' id='" + videos[i].embedCode + "' src='" + videos[i].thumbnailURL + "' /></a></li>";
  }
  html += "</ul>";
  
  return html;
}

function changeEmbed(playerId,embedCode)
{
    var autoplay = true;
	var player = document.getElementById('ooyalaPlayer_6nefy_g8hnefm4');
    
    //change video
    player.changeCurrentItem(embedCode);
	
	$("#wrapper #belt li a img").removeClass();
	$("#wrapper #belt li a img#"+embedCode).addClass("highlight");
    
    //if autoplay is on, then automatically play video after switch
	if (autoplay) player.playMovie();
}

