/*-------------------------------------------------------------------------
*
* Outbound from Flex (Properties)  (HTML Specific)
*This is an example of usage of the Unicorn Media SDK in communication with the
*Unicorn Media Player API, and is being provided for your use on an
*“as     //is” basis and without warranty implied or otherwise.
*Use of this software and example is at your own risk and it may not function properly if
*modified    //or altered in such a manner as it ceases to interface properly with the Unicorn Media System.
*If you are having difficulty with the function or
*implementation of this software, please contact support@unicornmedia.com.
*Updates the specified HTML element with the given value.
*
*---------------------------------------------------------------------------*/

function updateHTML(elmId, value) 
{
    if (elmId == "aEvents")
    {
        document.getElementById(elmId).value = document.getElementById(elmId).value + "\n" + value;
    }
    else
    {
        document.getElementById(elmId).value = value;
    }
    	
}

/**
 * Updates all static HTML elements to reflect Player information at this functions call time.
 */

function updateytplayerInfo() 
{ 
    updateHTML("PlayheadTime", GetPlayheadTime('flashobject')); 
    updateHTML("MediaItemTitle", GetMediaItemTitle('flashobject')); 
    updateHTML("MediaItemGuid", GetMediaItemGuid('flashobject')); 
    updateHTML("MediaItemDuration", GetMediaItemDuration('flashobject')); 
    updateHTML("MediaItemEntityName", GetMediaItemEntityName('flashobject')); 
    updateHTML("MediaItemRating", GetMediaItemRating('flashobject')); 
    updateHTML("MediaItemType", GetMediaItemType('flashobject')); 
    updateHTML("MediaItemKeywords", GetMediaItemKeywords('flashobject')); 
    updateHTML("MediaItemThumbnailURL", GetMediaItemThumbnailURL('flashobject'));
    //updateHTML("EntityThumbnailURL", GetEntityThumbnailURL('flashobject'));				//Revision 2 
    //updateHTML("PlaylistThumbnailURL", GetPlaylistThumbnailURL('flashobject'));   //Revision 2 
    //updateHTML("PlaylistName", GetPlaylistName('flashobject'));										//Revision 2 
    updateHTML("MediaItemDescription",GetMediaItemDescription('flashobject')); 
    updateHTML("MuteState", GetMuteState('flashobject')); 
    updateHTML("PlayerVolumeState", GetPlayerVolumeState('flashobject')); 
    updateHTML("PlayerState", GetPlayerState('flashobject'));
    updateHTML("MediaItemAdBreaks", GetMediaItemAdBreaks('flashobject'));
    updateHTML("PlayerEmbedCode", GetPlayerEmbedCode('flashobject'));
    updateHTML("AdMediaURL", GetAdMediaURL('flashobject'));
    updateHTML("AdMediaClickURL", GetAdMediaClickURL('flashobject'));
    updateHTML("CompanionAdArray", GetAdCompanionAds('flashobject'));
    updateHTML("queueIndexVal", GetQueueIndex('flashobject'));
		updateHTML("mediaItemProtocolVal", GetMediaItemProtocol('flashobject'));
}

/**
 * Sends the current playhead time on update (Number)
 *
 * @param i
 */
function UMOExt_JS_PlayheadTime(i)
{
    updateHTML("aPlayheadTime", i);
}

/**
 * Sends the current mediaitem title on update (String)
 *
 * @param i
 */
function UMOExt_JS_MediaItemTitle(i)
{
    updateHTML("aMediaItemTitle", i);
}
/**
 * Sends the current mediaitem guid on update (String)
 *
 * @param i
 */
function UMOExt_JS_MediaItemGuid(i)
{
    updateHTML("aMediaItemGuid", i);
}
/**
 * Sends the current mediaitem duration on update (Number)
 *
 * @param i
 */
 
function UMOExt_JS_MediaItemDuration(i)
{
    updateHTML("aMediaItemDuration", i);
}

/**
 * Sends the current mediaitem entity name (String)
 *
 * @param i
 */
function UMOExt_JS_MediaItemEntityName(i)
{
    updateHTML("aMediaItemEntityName", i);
}

/**
 * Sends the current mediaitem type on update (String)
 *
 * @param i
 */
function UMOExt_JS_MediaItemType(i)
{
    updateHTML("aMediaItemType", i);
}

/**
 * Sends the current mediaitem keywords on update (String)
 *
 * @param i
 */
function UMOExt_JS_MediaItemKeywords(i)
{
    updateHTML("aMediaItemKeywords", i);
}

/**
 * Sends the current mediaitem description on update (String)
 *
 * @param i
 */
 
function UMOExt_JS_MediaItemDescription(i)
{
    updateHTML("aMediaItemDescription", i);
}

/**
 * Sends the current medaiitem rating on update (String)
 *
 * @param i
 */
 
function UMOExt_JS_MediaItemAdBreaks(i)
{
    var adArray = i;
    var adList = adArray.length + " ads: (";
    for (var i = 0; i < adArray.length; i++)
    {
        adList = adList + " " + i + ": " + adArray[i].Duration + adArray[i].Type + "@" + adArray[i].SecondsIn + " ";
    }
    adList = adList + ")";
		
    updateHTML("aMediaItemAdBreaks", adList);
}
/**
 * Sends the current media item ad positions (Array)
 *
 * @param i
 */
 
function UMOExt_JS_MediaItemRating(i)
{
    updateHTML("aMediaItemRating", i);
}

/**
 * Sends the current mediaitem thumbnail URL (String)
 *
 * @param i
 */
 
function UMOExt_JS_MediaItemThumbnailURL(i)
{
    updateHTML("aMediaItemThumbnailURL", i);
}

/**
 * Sends the original embed code for the player instance
 *
 * @param i
 */
 
function UMOExt_JS_PlayerEmbedCode(i)
{
    updateHTML("aPlayerEmbedCode", i);
}

/**
 * Sends the current Video Ad URL
 *
 * @param i
 */

function UMOExt_JS_AdMediaURL(i)
{
    updateHTML("aAdMediaURL", i);
}
	
/**
 * Sends the current Video Ad Click URL
 *
 * @param i
 */
function UMOExt_JS_AdMediaClickURL(i)
{
    updateHTML("aAdMediaClickURL", i);
}

/**
 * Sends the current Companion Ad Array
 *
 * @param i
 */
 
function UMOExt_JS_AdCompanionAds(i)
{
    updateHTML("aCompanionAdArray", i.toString());
}

/**
 * Sends the results of the Async Channel Search
 *
 * @param i
 */
function UMOExt_JS_AsyncChannelSearchResults(i)
{
	updateHTML("asyncChannelSearchResult", i);
}

/**
 * Sends the results of the Async Share URL
 *
 * @param i
 */
function UMOExt_JS_AsyncShareURLResults(i)
{
	updateHTML("asyncShareURLResult", i);
}

/**
 * Sends the current media item's protocol
 *
 * @param i
 */
function UMOExt_JS_MediaItemProtocol(i)
{
	updateHTML("aMediaItemProtocolEvent", i);
}

/**
 * Sends the current bytes loaded of the progressive player stream.
 *
 * @param i
 */
function UMOExt_JS_PlayerBytesLoaded(i)
{
	updateHTML("aplayerBytesLoadedEvent", i);
}

/**
 * Sends the total amount of bytes the progressive player stream.
 *
 * @param i
 */
function UMOExt_JS_PlayerBytesTotal(i)
{
	updateHTML("aplayerBytesTotalEvent", i);
}

/**
 * Sends the current bytes loaded of the progressive ad stream.
 *
 * @param i
 */
function UMOExt_JS_VideoBytesLoaded(i)
{
	updateHTML("aAdPlayerBytesLoadedEvent", i);
}

/**
 * Sends the total amount of bytes the progressive ad stream.
 *
 * @param i
 */
function UMOExt_JS_VideoBytesTotal(i)
{
	updateHTML("aAdPlayerBytesTotalEvent", i);
}

/**
 * Sends the current Queue Index.
 *
 * @param i
 */
function UMOExt_JS_QueueIndex(i)
{
	updateHTML("aQueueIndexEvent", i);
}

/**
 * Sends the Queue Complete Event
 *
 * @param i
 */
function UMOExt_JS_QueueComplete(i)
{
	updateHTML("aQueueCompleteEvent", i);
}

/**
 * Sends the Queue Item Complete Event.
 *
 * @param i
 */
function UMOExt_JS_QueueItemComplete(i)
{
	updateHTML("aQueueItemCompleteEvent", i);
}

/**
 * Sends the event that an Ad Cuepoint was hit.
 *
 * @param i
 */
function UMOExt_JS_AdCuePoint(i)
{
	updateHTML("aAdCuepoint", i);
}

/**
 * Sends the Video AD play event.
 *
 * @param i
 */
function UMOExt_JS_VideoAdPlayStart(i)
{
	updateHTML("aAdItemPlayStartEvent", i);
}

/**
 * Sends the Video AD play complete event.
 *
 * @param i
 */
function UMOExt_JS_VideoAdPlayComplete(i)
{
	updateHTML("aAdItemPlayComplete", i);
}

/**
 * Sends the Video Ads play event.
 *
 * @param i
 */
function UMOExt_JS_VideoAdsStart(i)
{
	updateHTML("aAdsPlayStartEvent", i);
}

/**
 * Sends the Video Ads play complete event.
 *
 * @param i
 */
function UMOExt_JS_VideoAdsComplete(i)
{
	updateHTML("aAdsPlayComplete", i);
}

	
//////////  Revision 2 Items
//			// Sends the current Playlist Name.
//			function UMOExt_JS_PlaylistName(i)
//			{
//				updateHTML("aPlaylistName", i);
//			}
//			
//			// Sends the current Playlist Thumbnail URL
//			function UMOExt_JS_PlaylistThumbnailURL(i)
//			{
//				updateHTML("aPlaylistThumbnailURL", i);
//			}
//			
//			// Sends the current Media Items Category
//			function UMOExt_JS_MediaItemCategories(i)
//			{
//				updateHTML("aMediaItemCategories", i);
//			}
//			
//			// Sends the current Entity Thumbnail URL
//			function UMOExt_JS_EntityThumbnailURL(i)
//			{
//				updateHTML("aEntityThumbnailURL", i);
//				}


//////////////////////////////////
// Outbound from Flex (States)
//////////////////////////////////

/**
 * Sends the current volume of the player (Number, 0 - 10)
 *
 * @param i
 */
 
function UMOExt_JS_PlayerVolumeState(i)
{
    updateHTML("aPlayerVolumeState", i);
}

/**
 * Sends the current mute state of the player (uint, 0 = mute on, 1 = mute off)
 *
 * @param i
 */
 
function UMOExt_JS_MuteState(i)
{
    updateHTML("aMuteState", i);
}

/**
 * Sends the current player state, corresponding to a subset of fl.video.VideoState values (String)
 *
 * @param i
 */

function UMOExt_JS_PlayerState(i)
{
    updateHTML("aPlayerState", i);
}

function UMOExt_JS_QueueState(i)
{
	updateHTML("aQueueStateEvent", i);
}

//////////////////////////////////
// Outbound from Flex (Events)
//////////////////////////////////
 	
/**
 * Called from Flex when Player is completly loaded and ready
 *
 */

function UMOExt_JS_PlayerReady()
{
    updateHTML("aEvents", "Player Ready");
	
		
    thisMovie("flashobject").UMIExt_JS_AddEventListener("UMOExt_Evt_MediaItemPlayStart", "handle_UMOExt_Evt_MediaItemPlayStart");

    thisMovie("flashobject").UMIExt_JS_AddEventListener("UMOExt_Evt_MediaItemPlayComplete", "handle_UMOExt_Evt_MediaItemPlayComplete");
	
	thisMovie("flashobject").UMIExt_JS_AddEventListener("UMOExt_Evt_PlayerState", "handle_UMOExt_Evt_PlayerState");
}
	
/**
 * Called from the Loader, fires before it actually loads the player SWF
 *
 */
function UMOExt_JS_PlayerLoaded()
{
    updateHTML("aPlayerLoadedEvent", "Player Loaded");
}
	
/**
 * This is called from our Event Listener when the Player Ready event is received
 *
 * @param s
 */
 
function handle_UMOExt_Evt_PlayerReady(s)
{
    updateHTML("aEvents", "UMOExt_Evt_PlayerReady: " + s);
}
	
/**
 * This is called from our Event Listener when the Play Start event is received
 *
 * @param s
 */
function handle_UMOExt_Evt_MediaItemPlayStart(s)
{
    updateHTML("aMediaItemPlayStartEvent", "Player Started");
}

/**
 * This is called from our Event Listener when the Play Complete event is received
 *
 * @param s
 */
 
function handle_UMOExt_Evt_MediaItemPlayComplete(s)
{
    updateHTML("aMediaItemPlayComplete", "Player Completed");
}

/**
 * This is called from our Event Listener when the Player State changes
 *
 * @param s
 */
 
function handle_UMOExt_Evt_PlayerState(s)
{
    updateHTML("aEvents", "handle_UMOExt_Evt_PlayerState: " + s);
	if(s=="videoplayer_playing"){
		clicky.video('play', GetPlayheadTime('flashobject'), 'http://once.unicornmedia.com/now/od/auto/2bc2bf67-d196-43a8-aeea-147bf705317b/206d3038-6036-4a46-9e2e-b2cf4f3936e9/', 'Unicorn Once' );
		
	}
	if(s=="videoplayer_paused"){
		clicky.video('pause', GetPlayheadTime('flashobject'), 'http://once.unicornmedia.com/now/od/auto/2bc2bf67-d196-43a8-aeea-147bf705317b/206d3038-6036-4a46-9e2e-b2cf4f3936e9/', 'Unicorn Once' );
		
	}
	
	if(s=="videoplayer_seeking"){
		clicky.video('seek', GetPlayheadTime('flashobject'), 'http://once.unicornmedia.com/now/od/auto/2bc2bf67-d196-43a8-aeea-147bf705317b/206d3038-6036-4a46-9e2e-b2cf4f3936e9/', 'Unicorn Once' );
		
	}
	if(s=="videoplayer_stopped"){
		clicky.video('end', GetPlayheadTime('flashobject'), 'http://once.unicornmedia.com/now/od/auto/2bc2bf67-d196-43a8-aeea-147bf705317b/206d3038-6036-4a46-9e2e-b2cf4f3936e9/', 'Unicorn Once' );
		$('#umvideo').load('includes/video_media_end.html #loadcontent');
	}
}
	
/**
 * Adds an event listener for a specific event to execute an arbitrary javascript method
 *
 * @param movie
 */
function AddAllEventListeners(movie)
{
	
	
    updateHTML("aEvents", "Adding event listeners...");

    updateHTML("aEvents", "  UMOExt_Evt_PlayerReady");
    thisMovie("flashobject").UMIExt_JS_AddEventListener("UMOExt_Evt_PlayerReady", "handle_UMOExt_Evt_PlayerReady");
		
    updateHTML("aEvents", "  UMOExt_Evt_PlayerLoaded");
    thisMovie("flashobject").UMIExt_JS_AddEventListener("UMOExt_Evt_PlayerLoaded", "UMOExt_Evt_PlayerLoaded");

    updateHTML("aEvents", "  UMOExt_Evt_PlayerState");
    thisMovie("flashobject").UMIExt_JS_AddEventListener("UMOExt_Evt_PlayerState", "handle_UMOExt_Evt_PlayerState");

    updateHTML("aEvents", "Done.\n");
}
