
// JavaScript Document
//
// vxClientInitialize -- Function that is called from the vxFlashPlayer once it has been loaded.  
//
// At this point, we can send or listen to any events we're interested in
//

function vxClientInitialize()
{
	var vxFP = vxGetPlayer();
	
	vxFP.addEventListener("eChannelChanged", "onChannelChanged");
	vxFP.addEventListener("ePlayChannel", "onChannelPlayed");
	vxFP.addEventListener("eVideoClipBegin", "onVideoClipBegin");
	vxFP.addEventListener("eApplicationInitialized", "onAppInit");
	vxFP.addEventListener("eVideoClipComplete", "onVideoClipComplete");
	vxFP.addEventListener("eAuthenticationNeeded", "onAuthenticationNeeded");
	vxFP.addEventListener("eAddedToCustomPlaylist", "onAddedToCustomPlaylist");
	vxFP.addEventListener("eRemovedFromCustomPlaylist", "onRemovedFromCustomPlaylist");
}

function onAppInit(oEvent)
{
	vxTrace("VXJSAPI: onAppInit:");
}

function onChannelChanged(oEvent)
{
	if( oEvent.channelName != undefined )
		vxTrace("* Channel changed to " + oEvent.channelName);
}
function onChannelPlayed(oEvent)
{
		
}
function onVideoClipBegin(oEvent)
{
	vxTrace("* Video Clip Begin: \n   clipId: " + oEvent.clip.clipId + "\n   title: " + oEvent.clip.title + "\n   desciption: " + oEvent.clip.description);
//	var altId = str.substr(oEvent.clip.clipId.indexOf('_')+1, oEvent.clip.clipId.length)
//	alert('clip beginning: '+ oEvent.clip.title +'; '+ oEvent.clip.clipId);
//	alert('alt here'); 
//	changePlayerChannelClip(oEvent.clip.channel, altId);

//	changeBodyEpisode(oEvent.clip.channel, oEvent.clip.clipId);
}

function onVideoClipComplete(oEvent)
{
	vxTrace("* Video Clip Complete");
}

function onAuthenticationNeeded(oEvent)
{
	vxTrace("* Video Clip is not Authenticated: This is channel: " + oEvent.channelName + ", AuthSchemaId: " + oEvent.authSchemaId);
}

function onAddedToCustomPlaylist(oEvent)
{
	vxTrace("* onAddedToCustomPlaylist: Added Playlist Item: clipId = " + oEvent.clipId);
}

function onRemovedFromCustomPlaylist(oEvent)
{
	vxTrace("* onRemovedFromCustomPlaylist: Removed Playlist Item: clipId = " + oEvent.clipId);
}

// Tab chaning, searching etc.
function searchAndSwitch()
{
	// Get the player.
	var vxFP = vxGetPlayer();
	vxFP.addEventListener("eSearchComplete", "onSearchComplete");

	// Perform the search
	var searchEvent			= new Object();
	searchEvent.searchTerm	= "cats";
	vxDispatch("eSearchExecute", searchEvent);
}

function onSearchComplete(oEvent)
{	
	// Play the channel if the term matches the term we searched on.
	if (oEvent.searchTerm == "cats")
	{
		var playEvent			= new Object();
		playEvent.channelName	= "SearchChannel";
		playEvent.clipIndex		= 0;
		vxDispatch("ePlayChannel", playEvent);
	}
}

function playCustomChannel()
{	
	// Change the tab.
	var tabChangeEvent		= new Object();
	tabChangeEvent.eventid	= "Playlist";
	vxDispatch("eMenuClick", tabChangeEvent);
	
	// Play the channel.
	var playEvent			= new Object();
	playEvent.channelName	= "CustomChannel";
	playEvent.clipIndex		= 0;
	vxDispatch("ePlayChannel", playEvent);
	
	
}

function vxTrace(msg)
{
	var output = document.getElementById('vxOutput');
	if (output)
	{
		output.value += "\n"+msg;
		output.scrollTop = output.scrollHeight;
	}	
}

function vxDispatch(sEvent, oParams)
{
	//alert(sEvent + " called - " + oParams.clipId + ", " + oParams.channelName);
	vxGetPlayer().dispatchEvent(sEvent, oParams);
}

