/*
Developer: Brandon Aaskov (Brightcove)
*/

var bcHomepage;
var bcCategoryPage;
var homepageSWF = "homepage_thumbs"; //shorthand for ExternalInterface calls
var categorySWF = "category_thumbs"; //shorthand for ExternalInterface calls
var titleIDs = new Array();

//--------------------------------------------------------------------------------------------- ADDED 10/29/07
var url = window.location.toString(); //grabs the URL that's in the address bar

//in case the if statement (next bit) doesn't pass, we want to make sure the values in the player snippet are "null" and not "undefined"
var titleId = null; 
var lineupId = null;
var refId = null;
var titleIdInLineup = false;
var refIdInLineup = false;

if(url.indexOf("?") !== -1) 
{
	var urlArray = url.split("?"); //splits the URL on the ? into two different chunks - the end chunk has the values we care about
	var bcValues = urlArray[1].split("&"); //splits all of the extra appended values on the ampersand
	
	for(var i = 0; i < bcValues.length; i++)
	{
		if(bcValues[i].indexOf("bctid=") !== -1) //looks for bctid= in the URL string
		{
			titleId = bcValues[i].substr(6);
		}
		
		if(bcValues[i].indexOf("bclid=") !== -1) //looks for bclid= in the URL string
		{
			lineupId = bcValues[i].substr(6);
		}
		
		if(bcValues[i].indexOf("bcrid=") !== -1) //looks for bclid= in the URL string
		{
			refId = bcValues[i].substr(6);
		}
	}
}
//---------------------------------------------------------------------------------------------


function onTemplateLoaded()
{
	//set listeners for the player
	callFlash("addEventListener", "contentLoad", "onContentLoad");	
	callFlash("addEventListener", "mediaReady", "onMediaReady");
	callFlash("addEventListener", "titleLoad", "onTitleLoad");
	
	//get elements from the page
	bcHomepage = document.getElementById("bcHomepage");
	bcCategoryPage = document.getElementById("bcCategoryPage");
	bcArticlePage = document.getElementById("bcArticlePage");
	bcRelatedVideos = document.getElementById("bcRelatedVideos");
	bcPlayerThumbnails = document.getElementById("bcPlayerThumbnails");
	bcThumbnailTitle = document.getElementById("bcThumbnailTitle");
	bcTitleHeadline = document.getElementById("bcTitleHeadline");
	bcPaging = document.getElementById("bcPaging");
	bcDescription = document.getElementById("bcDescription");
	bcVideoHeader = document.getElementById("bcVideoHeader"); //the VIDEO header on the article page
}

function onContentLoad()
{
	if(bcHomepage || bcCategoryPage) callFlash("getFeaturedLineup"); //if it's the homepage player or category player...
	if(bcHomepage) thisMovie(homepageSWF).unhide(); //remove the covering piece
	
	if(bcArticlePage) onMediaReady(); //manual call
}

function onMediaReady()
{
	callFlash("getCurrentTitle");	
}

function getCurrentTitle_Result(titleDTO)
{
	//for the category and article pages
	if(bcTitleHeadline) bcTitleHeadline.innerHTML = titleDTO.displayName;
	if(bcDescription) bcDescription.innerHTML = titleDTO.shortDescription;
	if(bcVideoHeader) bcVideoHeader.style.visibility = "visible"; //show the VIDEO header on the article page
}

function getFeaturedLineup_Result(lineupDTO)
{
	titleIDS = new Array();
	titleIDs = lineupDTO.videoIds; //assign the array from the lineupDTO
	
	//build up a list of the title ids of the current (featured) lineup
	for(var i = 0; i < lineupDTO.videoIds.length; i++)
	{
		callFlash("getTitleById", lineupDTO.videoIds[i]); //get each titleDTO from the lineup
		
		if(titleId == lineupDTO.videoIds[i]) titleIdInLineup = true;
	}
	
	if(!titleIdInLineup)
	{
		callFlash("fetchTitleById", titleId);
	}
	
	if(!refIdInLineup)
	{
		callFlash("fetchTitleByReferenceId", refId);
	}
	
	if (bcRelatedVideos) bcRelatedVideos.innerHTML = "MORE VIDEOS (" + lineupDTO.videoIds.length + ")";
	if (bcRelatedVideos) bcRelatedVideos.style.color = "#000";
	onMediaReady();

	if (bcHomepage) thisMovie(homepageSWF).getPage("next"); //run the getPage function in the swf
	if (bcCategoryPage) thisMovie(categorySWF).buildThumbnails();
}

function getTitleById_Result(titleDTO)
{
	if(refId == titleDTO.referenceId) refIdInLineup = true; //if the refId'd title is in the lineup, mark it as true so that we don't fetch it
	
	//builds the arrays in the swf
	if (bcHomepage) thisMovie(homepageSWF).buildTitleIDs(Number(titleDTO.id));
	if (bcHomepage) thisMovie(homepageSWF).buildTitles(String(titleDTO.displayName));
	if (bcHomepage) thisMovie(homepageSWF).buildThumbArray(String(titleDTO.thumbnailURL));
	if (bcCategoryPage) thisMovie(categorySWF).buildTitleIDs(Number(titleDTO.id));
	if (bcCategoryPage) thisMovie(categorySWF).buildTitles(String(titleDTO.displayName));
	if (bcCategoryPage) thisMovie(categorySWF).buildThumbArray(String(titleDTO.thumbnailURL));
	if (bcCategoryPage) thisMovie(categorySWF).buildDescriptions(String(titleDTO.shortDescription));
}

function onTitleLoad(pTitleDTO)
{
	callFlash("loadTitleById", pTitleDTO.parameters.title.id);
}

function fetchTitleByReferenceId_Result(pTitleDTO)
{
	callFlash("loadTitleById", pTitleDTO.id);
}

//for the purposes of the external interface API with flash, since microsoft and mozilla have different references to the swf file
function thisMovie(movieName) 
{
    if (navigator.appName.indexOf("Microsoft") != -1) 
	{
        return window[movieName];
    }
    else 
	{
        return document[movieName];
    }
}