/**
 * @author Brandon Aaskov, modified by Michael Bosworth to use mootools
 * 
 * ? Brightcove Inc 2007.  This document and its contents are intended for use solely in connection with 
 * custom services being provided by Brightcove and cannot be used for any other reason or in any other 
 * context without Brightcove?s prior written permission.
 */

var bc_playlist = [];

function init()
{
	createLibraryAPICall({
		method: "find_playlists_for_player_id",
		id: "1336737867",
		pageNumber: 0, 
		callback: "buildPlaylist"
	});
}

function createLibraryAPICall()
{
	var url = new Object();
	var args = $H(arguments[0]);
	
	args.keys().each(function(pKey, pIndex)
	{
		if(pKey == "method") url.method = args.values()[pIndex];
		if(pKey == "callback") url.callback = args.values()[pIndex];
		if(pKey == "pageNumber") url.pageNumber = args.values()[pIndex];
		if(pKey == "fields") url.fields = args.values()[pIndex];
		if(pKey == "id") url.id = args.values()[pIndex];
		if(pKey == "videoIds") url.videoIds = args.values()[pIndex];
	});
	
	var apiCall = "http://api.brightcove.com/services/library?command=" + url.method + "&callback=" + url.callback + "&player_id=" + url.id + "&page_number=" + url.pageNumber + "&token=" + apiToken + "";
	
	var scriptElem = document.createElement('script'); 
    scriptElem.setAttribute('src', apiCall); 
    scriptElem.setAttribute('type','text/javascript'); 
    document.getElementsByTagName('head')[0].appendChild(scriptElem);
}

function buildPlaylist(pItem)
{
	var lineups = pItem.items;
	lineups.each(function(lineup)
	{
		var time = 0;
		var bodyZones = [];
		var equipment = [];
		
		lineup.videos.each(function(video)
		{
			var timeTag = false;
			video.tags.each(function(value)
			{
				var tag = value.split("=");
				if (tag[0] == "target" && tag[1]) bodyZones.push(tag[1]);
				if (tag[0] == "equipment" && tag[1]) equipment.push(tag[1]);
				if (tag[0] == "time" && tag[1]) 
				{
					timeTag = true;
					time += tag[1] * 1000; //time in milliseconds
				}
			});
			if(!timeTag) time += video.length;
		});
		
		bodyZones = bodyZones.uniq(); //remove duplicates
		equipment = equipment.uniq(); //remove duplicates
		
		//build a playlist item based on the video object we got back
		bc_playlist.push(new bc_LineupItem(
		{
			videoIds: lineup.videoIds,
			displayName: lineup.name,
			time: time,
			thumbnail: lineup.thumbnailURL, 
			bodyZones: bodyZones,
			equipment: equipment,
			selected: false
		}));
	});
	drawPlaylist(bc_playlist);
}

function getIndexFromId(pElementId)
{
	var index;
	bc_playlist.each(function(pItem, pIndex)
	{
		if(pItem.getId() == pElementId) index = pIndex;
	});
	
	return index;
}

function drawPlaylist(pPlaylist)
{
	var htmlString = "";
	pPlaylist.each(function(pItem)
	{
		var name = (pItem.getName().toLowerCase().indexOf("fitness magazine: ") == -1) ? pItem.getName() : pItem.getName().substr(18);
		htmlString += "<li>\n";
		htmlString += "\t<img src='"+pItem.getThumbnail()+"' onclick='javascript:saveWorkout(\""+pItem.getId()+"\");' />\n";
		htmlString += "\t<h2>"+name+"</h2>\n";
		htmlString += "\t<span class='length'>"+Math.round(pItem.getTime()/1000/60)+" Minutes</span>\n";
		htmlString += "\t<span class='hr'></span>";
		htmlString += "</li>\n";
	});
	$("workouts").innerHTML = htmlString;
	
	$("workoutsContainer").style.visibility = "visible";
}

function saveWorkout(pElementId)
{
	var index = getIndexFromId(pElementId);
	
	bc_playlist.each(function(pItem)
	{
		if (pItem.getId() == pElementId) 
		{
			var time;
			if (Math.round(pItem.getTime()/1000/60) > 45) time = 60;
			if (Math.round(pItem.getTime()/1000/60) <= 45) time = 45;
			if (Math.round(pItem.getTime()/1000/60) <= 30) time = 30;	
			if (Math.round(pItem.getTime()/1000/60) <= 20) time = 20;
			if (Math.round(pItem.getTime()/1000/60) <= 15) time = 15;
			
			var cookieData = "videoIds=" + pItem.videoIds.join(",") + "&bodyZones="+pItem.getBodyZones()+"&equipment="+pItem.getEquipment()+"&time=" +time+"";
			var expiryDate = new Date();
			expiryDate.setTime(expiryDate.getTime() + (365 * 24 * 60 * 60 * 1000)); //1 year from now
			setCookie(
			{
				name: "bcFitnessMagazine",
				data: cookieData,
				expires: expiryDate
			});
		}
	});
	
	window.location = "/app/buildaworkout/member/watchworkout.jsp";
}