
document.write('<script src="http://admin.brightcove.com/js/BrightcoveExperiences.js"><\/script>');
document.write('<script src="http://admin.brightcove.com/js/APIModules_all.js"><\/script>');




var mycarousel_itemList = [];



function mycarousel_itemLoadCallback(carousel, state)
{
    for (var i = carousel.first; i <= carousel.last; i++) {
        if (carousel.has(i)) {
            continue;
        }

        if (i > mycarousel_itemList.length) {
            break;
        }

        carousel.add(i, mycarousel_getItemHTML(mycarousel_itemList[i-1]));
    }
};

/**
 * Item html creation helper.
 */

function mycarousel_getItemHTML(item,blah)
{
    return '<div onClick="playTitleFromList(' + item.id + ')"><img src="' + item.pic + '" width="120" height="90" alt="' + item.pic + '" /><br>'+item.title+'</div>';
};

function PL_display()
{
 jQuery('#mycarousel').jcarousel({
        size: mycarousel_itemList.length,
        scroll: 5,
        itemLoadCallback: {onBeforeAnimation: mycarousel_itemLoadCallback}
    });
}
jQuery(document).ready(function() {

});










	// The web service call
	var token = "nzyAspihUpS75Nsw8CZRQhLj0Z0hc_z4VWa-EkdCbTU.";


	function initCall() {


	var req = "http://api.brightcove.com/services/library?";
	req += "command=find_videos_by_text&token=" + encodeURIComponent(token);  // tokens need to be URL-encoded
	req += "&text="+document.SampleForm.myText.value+"&fields=id,name,shortDescription,thumbnailURL,length&callback=response";
		// Create a new request object
		//bObj = new JSONscriptRequest(req);
		bObj = new JSONscriptRequest(req);
		// Build the dynamic script tag
		bObj.buildScriptTag();
		// Add the script tag to the page
		bObj.addScriptTag();
	}
	function getPL() {

	//find_all_playlists&fields=name,id&sort_by=publish_date&sort_order=ASC&get_item_count=true&token=[token]
	var req2 = "http://api.brightcove.com/services/library?";

	req2 += "command=find_playlist_by_id&token=" + encodeURIComponent(token);  // tokens need to be URL-encoded

	req2 += "&playlist_id="+pl+"&fields=name,id,shortDescription,thumbnailURL,videos&callback=getPL_response";
	//req2 += "&playlist_id=1785356614&callback=response";

		bObj2 = new JSONscriptRequest(req2);
		bObj2.buildScriptTag();
		bObj2.addScriptTag();
	}

	// Define the callback function
	// writes out the HTML for each title item in the list
	function getPL_response(jsonData) {


		document.getElementById('pl_title').innerHTML = "<h3><span class=\"ft_2\">"+jsonData.name+"</span></h3> ";
		var pDiv = document.getElementById("div_playlist");
		var i=0;
		pDiv.innerHTML ="";

		while (i<jsonData["videos"].length) {

			var str = "";


			str += '<div class="title" onClick="playTitleFromList(' + jsonData["videos"][i].id + ')">';
			str += '<div class="thumb"><img src="' + jsonData["videos"][i].thumbnailURL + '"/></div>';
			str += '<p class="displayName">' + jsonData["videos"][i].name + ' (' + formatTime(jsonData["videos"][i].length) + ')'+ '</p>';
			str += '<p class="desc">' + jsonData["videos"][i].shortDescription + '</p>';
			str += '</div>';
			//pDiv.innerHTML += str;
			mycarousel_itemList[i] = {pic: jsonData["videos"][i].thumbnailURL, title: jsonData["videos"][i].name, id: jsonData["videos"][i].id };
			//carousel.add(i, mycarousel_getItemHTML(jsonData["videos"][i].thumbnailURL))
			i++;
		}

		playTitleFromList(jsonData["videos"][3].id);
		PL_display();
	}

	// 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;

	}

	/* Player Interface */

	var player;
	var content;
	var video;
	var adModule;


	// called when template loads, we use this to store a reference to the player and modules
	// and add event listeners for the video load (when the user clicks on a video)
	function onTemplateLoaded(player) {
	//alert("loaded");
	showPlayer();
	getPL();
		player = brightcove.getExperience(player);
		video = player.getModule(APIModules.VIDEO_PLAYER);
		content = player.getModule(APIModules.CONTENT);
		content.addEventListener(BCContentEvent.VIDEO_LOAD, onVideoLoad);

		// Get a reference to the Ad Module API
    adModule = player.getModule(APIModules.ADVERTISING);
    //Enable External Ads
    adModule.enableExternalAds(true);
    // Set a callback function for the externalAd event
    adModule.addEventListener(BCAdvertisingEvent.EXTERNAL_AD, onExternalAd);
    // Set a callback function for the Ad Complete event
    adModule.addEventListener(BCAdvertisingEvent.AD_COMPLETE, onAdComplete);


	}

	function onExternalAd(evt) {
	alert("onExternalAd");
	alert("ad object is "+evt);
	adModule.showAd(adXML);
	    /* perform whatever ad actions are necessary with adXML(i.e adModule.showAd(adXML) ),
       when complete you can call resumeAfterExternalAd to pass control back to the
       player. In this example, resumeAfterExternalAd is called onAdComplete */
  }

  function onAdComplete(evt) {
  alert("onAdComplete")
    adModule.resumeAfterExternalAd();
  }


	// handles click event from list items
	function playTitleFromList(id) {
		content.getVideoAsynch(id);
	}

	function onVideoLoad(event) {
		//event.getcuePoints
		//alert("videoload: "+event.video.alpha);
		video.loadVideo(event.video.id);

		//callFlash("addEventListener","cuePoint","onCuePoint");
	}


function onMediaLoad(evt)
{
callFlash("addEventListener","cuePoint","onCuePoint");
}

function onCuePoint(cpObj) {
alert("onCuePoint");
var cP= cpObj.parameters.cuePoint;
var cpMetaInformation = cP.metadata;
alert("Inside cuePoint (cP.name) : " + cP.name + " -- (cpMetaInformation) : " + cpMetaInformation);
}