var token = "AWj5XhFRCSH3fU_V_HLBKllS6wInB7xTKFo68Lw--ofrafGR1rpVyQ..";
var debug = true;
function debugtrace(text) {
	if (debug) alert(text);
}
function addScriptTag(id, url, callback) {
	var scriptTag = document.createElement("script");
   
   // Add script object attributes
   scriptTag.setAttribute("type", "text/javascript");
   scriptTag.setAttribute("charset", "utf-8");
   scriptTag.setAttribute("src", url + "&callback=" + callback);
   scriptTag.setAttribute("id", id);
	
	var head = document.getElementsByTagName("head").item(0);
	head.appendChild(scriptTag);
	
}

function getTopVideos() {
	addScriptTag("topVideos", "http://api.brightcove.com/services/library?command=find_all_videos&token="+token+"&video_fields=id,name,thumbnailURL,publishedDate,playsTrailingWeek&sort_by=plays_trailing_week", "response");
}

function getRecentVideos() {
	addScriptTag("recentVideos", "http://api.brightcove.com/services/library?command=find_all_videos&token="+token+"&video_fields=id,name,thumbnailURL,publishedDate,playsTrailingWeek&sort_by=PUBLISH_DATE&sort_order=DESC", "response");
}

function getVideoThumbs() {
	debugtrace("getVideoThumbs called ...");
	addScriptTag("videoThumbs", "http://api.brightcove.com/services/library?command=find_all_videos&token="+token+"&video_fields=id,name,thumbnailURL,videoStillURL,publishedDate&sort_by=PUBLISH_DATE&sort_order=DESC", "response2");
}

function getVideoId(vid, callback) {
	debugtrace("getVideoId called ...");
	addScriptTag("videoThumbs", "http://api.brightcove.com/services/library?command=find_video_by_id&video_id="+vid+"&token="+token+"&video_fields=id,name,thumbnailURL,videoStillURL,publishedDate,renditions,length&media_delivery=http", callback);
}

function getAllVideos(callback) {
	debugtrace("getAllVideos called with ..." + callback);
	addScriptTag("allVideos", "http://api.brightcove.com/services/library?command=find_all_videos&token="+token+"&video_fields=id,name,thumbnailURL,videoStillURL,publishedDate,length&sort_by=PUBLISH_DATE&sort_order=DESC", callback);
}


function response(jsonData) {
	var resp = document.getElementById("resp");
	for (var i=0; i<jsonData["items"].length; i++) {
		var title = jsonData["items"][i];
		var str = "";
		str += '<div class="title" onClick="playTitle(' + title.id + ')">';
		str += '<div class="thumb"><img src="' + title.thumbnailURL + '"/></div>';
		str += '<p class="displayName">' + title.name + '</p>';
		pd = new Date();
		pd.setTime(title.publishedDate);
		str += '<p class="views">viewed ' + title.playsTrailingWeek + ' times</p>';
		str += '<p class="views">published on  ' + pd.toDateString() + '</p>';
		str += '</div>';
		resp.innerHTML += str;
	}
}

function response2(jsonData) {
	debugtrace("Response2 called ...");
	for (var i=0; i<jsonData["items"].length; i++) {
		var title = jsonData["items"][i];
		// Chekc for Recent Video thumbs
		var thumbid = "VT" + title.id;
		var thumbname = "VT" + title.name;
		var thumb = document.getElementById(thumbid);
		debugtrace("Processing video = " + thumbid);
		if (thumb != null) {
			debugtrace("Setting background for " + thumbid);
			thumb.setAttribute("style", "background-image:url("+title.thumbnailURL+");");
			debugtrace("Background for " + thumbid + " set to " + title.thumbnailURL);
		}
		// Check for Related Video as well
		var thumbid = "VR" + title.id;
		var thumbname = "VR" + title.name;
		var thumb = document.getElementById(thumbid);
		debugtrace("Processing video = " + thumbid);
		if (thumb != null) {
			debugtrace("Setting background for " + thumbid);
			thumb.setAttribute("style", "background-image:url("+title.thumbnailURL+");");
			debugtrace("Background for " + thumbid + " set to " + title.thumbnailURL);
		}
	}
}

function playTitle(id) {
	window.open("js-sidebar-player.html?bctid=" + id,"Top_Videos_Sidebar","location=0,status=0,scrollbars=0,width=500,height=445"); 
}

// JavaScript Document
