/*-------------------------------------------------------------------------
*
*Player Control Functions
*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.
*
*---------------------------------------------------------------------------*/

/**
 * Returns the handle of the movie to interact with.
 *
 * @param movieName
 */ 	
function thisMovie(movieName) 
{
    var winobj = undefined;
    var docobj = undefined;
                
    if (navigator.appName.indexOf("Microsoft") != -1)
    {
        winobj = window[movieName];
        if (winobj != undefined)
        {
            return winobj;
        }
        else
        {
            docobj = document[movieName];
            if (docobj != undefined)
                return docobj;
        }
                
    } 
    else
    {
        docobj = document[movieName];
        if (docobj != undefined)
            return docobj;
    }
                
    docobj = document.getElementById(movieName);
                
    return docobj;
}

    	 
//////////////////////////////////
// Inbound to Flex (Getters)
//////////////////////////////////

/**
 * Gets the current playhead time (Number)
 *
 * @param movie
 */
function GetPlayheadTime(movie)
{	
    return thisMovie(movie).UMIExt_JS_GetPlayheadTime();
}

/**
 * Gets the current mediaitem title (String)
 *
 * @param movie
 */
function GetMediaItemTitle(movie)
{
    return thisMovie(movie).UMIExt_JS_GetMediaItemTitle();
}

/**
 * Gets the current mediaitem guid (String)
 *
 * @param movie
 */
function GetMediaItemGuid(movie)
{
    return thisMovie(movie).UMIExt_JS_GetMediaItemGuid();
}

/**
 * Gets the current mediaitem duration (Number)
 *
 * @param movie
 */
function GetMediaItemDuration(movie)
{
    return thisMovie(movie).UMIExt_JS_GetMediaItemDuration();
}  

/**
 * Gets the current mediaitem entity name (String)
 *
 * @param movie
 */
function GetMediaItemEntityName(movie)
{
    return thisMovie(movie).UMIExt_JS_GetMediaItemEntityName();
}

/**
 * Gets the current mediaitem type (String)
 *
 * @param movie
 */
 	
function GetMediaItemType(movie)
{
    return thisMovie(movie).UMIExt_JS_GetMediaItemType();
}
	
/**
 * Gets the current mediaitem keywords (String)
 *
 * @param movie
 */

function GetMediaItemKeywords(movie)
{
    return thisMovie(movie).UMIExt_JS_GetMediaItemKeywords();
}  

/**
 * Gets the current mediaitem description (String)
 *
 * @param movie
 */
 
function GetMediaItemDescription(movie)
{
    return thisMovie(movie).UMIExt_JS_GetMediaItemDescription();
}

/**
 * Gets the current mediaitem protocol (String) (String)
 *
 * @param movie
 */
function GetMediaItemProtocol(movie)
{
  	return thisMovie(movie).UMIExt_JS_GetMediaItemProtocol();
}

/**
 * Gets the current volume of the player (Number, 0 - 10)
 *
 * @param movie
 */

function GetPlayerVolumeState(movie)
{
    return thisMovie(movie).UMIExt_JS_GetPlayerVolumeState();
}

/**
 *Gets the current volume of the player (Number, 0 - 10)
 *
 * @param movie
 */
function GetMediaItemAdBreaks(movie)
{
    var adArray = thisMovie(movie).UMIExt_JS_GetMediaItemAdBreaks();
    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 + ")";
		
    return adList;
}

/**
 *Gets the current mediaitem rating (String)
 *
 * @param movie
 */
function GetMediaItemRating(movie)
{
    return thisMovie(movie).UMIExt_JS_GetMediaItemRating();
}  

/**
 *Gets the current mediaitem thumbnail URL (String)
 *
 * @param movie
 */

function GetMediaItemThumbnailURL(movie)
{
    return thisMovie(movie).UMIExt_JS_GetMediaItemThumbnailURL();
}

/**
 *Gets the current mute state of the player (uint, 0 = mute on, 1 = mute off)
 *
 * @param movie
 */
function GetMuteState(movie)
{
    return thisMovie(movie).UMIExt_JS_GetMuteState();
}
/**
 *Gets the original embed code for the player instance
 *
 * @param movie
 */
function GetPlayerEmbedCode(movie)
{
    return thisMovie(movie).UMIExt_JS_GetPlayerEmbedCode();
}

/**
 * Gets the current Queue Index
 *
 * @param movie
 */
function GetQueueIndex(movie)
{
	return thisMovie(movie).UMIExt_JS_GetQueueIndex();
}
	

/*
 * Gets the current Video Ad URL
 *
 * @param movie
 */ 
function GetAdMediaURL(movie)
{
    return thisMovie(movie).UMIExt_JS_GetAdMediaURL();
}
	
/*
 * Gets the current Video Ad Click URL
 *
 * @param movie
 */
 
function GetAdMediaClickURL(movie)
{
    return thisMovie(movie).UMIExt_JS_GetAdMediaClickURL();
}

/*
 * Gets the current Companion Ad Object (ClickThrough)
 *
 * @param movie
 */

function GetAdCompanionAd(movie)
{
    var obj = thisMovie(movie).UMIExt_JS_GetAdCompanionAd();
    if (obj != null)
    {
        return "Object"
    }
    else
    {
        return ""
    }
}
	
/*
 * Gets the current Companion Ad Object Array (URL)
 *
 * @param movie
 */

function GetAdCompanionAds(movie)
{
    var obj = thisMovie(movie).UMIExt_JS_GetAdCompanionAds();
    if (obj != null)
    {
        return obj.toString()
    }
    else
    {
        return ""
    }
}

/*
 * Gets the current player state, corresponding to a subset of fl.video.VideoState values (String)
 *
 * @param movie
 */
 
function GetPlayerState(movie)
{
    return thisMovie(movie).UMIExt_JS_GetPlayerState();
}
	
/*
 * Gets the Entity Thumbnail URL
 *
 * @param movie
 */
 
function GetEntityThumbnailURL(movie)
{
    return thisMovie(movie).UMIExt_JS_GetEntityThumbnailURL();
}
	
////////  Revision 2 Items
//		// Gets the current Playlist Name
//		function GetPlaylistName(movie)
//		{
//		  	return thisMovie(movie).UMIExt_JS_GetPlaylistName();
//		}
//		
//		// Gets the current Playlist Thumbnail URL
//		function GetPlaylistThumbnailURL(movie)
//		{
//		  	return thisMovie(movie).UMIExt_JS_GetPlaylistThumbnailURL();
//		}
	

//////////////////////////////////
// Inbound to Flex (Setters)
//////////////////////////////////

/*
 * Sets mute off
 *
 * @param movie
 */

function SetPlayerMuteOff(movie)
{
    thisMovie(movie).UMIExt_JS_SetPlayerMuteOff();
}
	
/*
 * Sets mute on
 *
 * @param movie
 */
 
function SetPlayerMuteOn(movie)
{
    thisMovie(movie).UMIExt_JS_SetPlayerMuteOn();
}  
	
/*
 * Toggles mute
 *
 * @param movie
 */
 
function ToggleMute(movie)
{
    thisMovie(movie).UMIExt_JS_ToggleMute();
}  

/*
 * Sets player volume (Number, 0 - 10) 
 *
 * @param movie
 * @param volume
 */
 	
function SetPlayerVolume(movie, volume)
{
    thisMovie(movie).UMIExt_JS_SetPlayerVolume(volume);
}  

//////////////////////////////////
// Inbound to Flex (Actions)
//////////////////////////////////
 	
/*
 * Removes the stop-screen, optionally executes the default player start command
 *
 * @param movie
 * @param defaultStart
 */

function RemoveStopScreen(movie, defaultStart)
{
    thisMovie(movie).UMIExt_JS_RemoveStopScreen(defaultStart);
}

/*
 * Pauses the current mediaitem
 *
 * @param movie
 */
 
function Pause(movie)
{
    thisMovie(movie).UMIExt_JS_Pause();
}
	
/*
 * Resumes play of the current mediaitem
 *
 * @param movie
 */
 
function Resume(movie)
{
    thisMovie(movie).UMIExt_JS_Resume();
}
	
/*
 * Seeks to specified position in seconds
 *
 * @param movie
 * @param position
 */
 
function Seek(movie, position)
{
    thisMovie(movie).UMIExt_JS_Seek(position);
}

/*
 * Plays the current mediaitem from position 0
 *
 * @param movie
 */
 
function PlayCurrentItemFromStart(movie)
{
    thisMovie(movie).UMIExt_JS_PlayCurrentItemFromStart();
}

/*
 * Plays the current mediaitem from specified position
 *
 * @param movie
 * @param position
 */
 
function PlayCurrentItemFromPosition(movie, position)
{
    thisMovie(movie).UMIExt_JS_PlayCurrentItemFromPosition(position);
}

/*
 * Adds a mediaitem to the queue
 *
 * @param movie
 * @param miGuid
 */
 
function LoadMediaItemInQueue(movie, miGuid)
{
    thisMovie(movie).UMIExt_JS_LoadMediaItemInQueue(miGuid);
}
	
/*
 * Adds a mediaitem to the queue and plays
 *
 * @param movie
 * @param miGuid
 */
 
function LoadMediaItemInQueueAndPlay(movie, miGuid)
{
    thisMovie(movie).UMIExt_JS_LoadMediaItemInQueueAndPlay(miGuid);
}
	
/*
 * Adds a mediaitem to the queue and plays from specified position
 *
 * @param movie
 * @param miGuid
 * @param position
 */
 
function LoadMediaItemInQueueAndPlayFromPosition(movie, miGuid, position)
{
    thisMovie(movie).UMIExt_JS_LoadMediaItemInQueueAndPlayFromPosition(miGuid, position);
}
	
////////// Revision 2 Items
//			// Loads a Playlist into the queue
//			function LoadPlaylistInQueue(movie, plGUID)
//			{
//			  	thisMovie(movie).UMIExt_JS_LoadPlaylistInQueue(plGUID);
//			}    
//			
//			// Loads a Playlist into the queue and plays
//			function LoadPlaylistInQueueAndPlay(movie, plGUID)
//			{
//			  	thisMovie(movie).UMIExt_JS_LoadPlaylistInQueueAndPlay(movie, plGUID);
//			}    
//			
//			// Loads a Playlist into the queue and Plays from given item
//			function LoadPlaylistInQueueAndPlayFromItem(movie, plGUID)
//			{
//			  	thisMovie(movie).UMIExt_JS_LoadPlaylistInQueueAndPlayFromItem(movie, plGUID);
//			}    
//			
//			// Loads a Playlist into the queue and Plays from given item position
//			function LoadPlaylistInQueueAndPlayFromItemPosition(movie, plGUID)
//			{
//			  	thisMovie(movie).UMIExt_JS_LoadPlaylistInQueueAndPlayFromItemPosition(movie, plGUID);
//			} 
				
/*
 * Removes all items from the queue
 *
 * @param movie
 */
 
function ClearQueue(movie)
{
    thisMovie(movie).UMIExt_JS_ClearQueue();
}

/*
 * Plays the current queue from position 0 (first item)
 *
 * @param movie
 */
   
function PlayCurrentQueueFromStart(movie)
{
    thisMovie(movie).UMIExt_JS_PlayCurrentQueueFromStart();
}
	
/*
 * Plays the current queue from specified position
 *
 * @param movie
 * @param position
 */
    
function PlayCurrentQueueFromItemPosition(movie, position)
{
    thisMovie(movie).UMIExt_JS_PlayCurrentQueueFromItemPosition(position);
}

/*
 * Sets the Media Player size
 *
 * @param movie
 * @param width
 * @param height
 */
 
function SetPlayerSize(movie, width, height)
{
	thisMovie(movie).UMIExt_JS_SetPlayerSize(width, height);
}

/*
 * Starts the async search call based on the given criteria
 *
 * @param movie
 * @param criteria
 */
 
function AsyncChannelSearch(movie, criteria)
{
	thisMovie(movie).UMIExt_JS_AsyncChannelSearch(criteria);
	thisMovie(movie).UMIExt_JS_AddEventListener("UMOExt_Evt_AsyncChannelSearchResults", "handle_AsyncChannelSearchResults");
}

/*
 * Async Channel Search handler
 *
 * @param results
 */
 
function handle_AsyncChannelSearchResults(results)
{
	document.controls.asyncChannelSearchResult.value = results; 
}

/*
 * Starts the async call to get the given social network share URL.
 *
 * @param movie
 * @param socialNetwork
 */
 
function AsyncShareURL(movie, socialNetwork)
{
	thisMovie(movie).UMIExt_JS_AsyncShareURL(socialNetwork);
	thisMovie(movie).UMIExt_JS_AddEventListener("UMOExt_Evt_AsyncShareURLResults", "handle_AsyncShareURLResult");
}

/*
 * Async Share URL result handler
 *
 * @param result
 */
 
function handle_AsyncShareURLResult(result)
{
	document.controls.asyncShareURLResult.value = result; 
}

/*
 * Starts the async call to get the embed code of the current asset based on application behavior settings.
 *
 */
 
function AsyncEmbedCode(movie)
{
	thisMovie(movie).UMIExt_JS_AsyncEmbedCode();
	thisMovie(movie).UMIExt_JS_AddEventListener("UMOExt_Evt_AsyncEmbedCodeResults", "handle_AsyncEmbedCodeResult");
}

/*
 * Async Embed Code result handler
 *
 * @param result
 */
 
function handle_AsyncEmbedCodeResult(result)
{
	document.controls.asyncEmbedCodeResult.value = result; 
}

/*
 * Starts the async call to get the media link of the current asset based on application behavior settings.
 *
 */
 
function AsyncMediaLink(movie)
{
	thisMovie(movie).UMIExt_JS_AsyncMediaLink();
	thisMovie(movie).UMIExt_JS_AddEventListener("UMOExt_Evt_AsyncMediaLinkResults", "handle_AsyncMediaLinkResult");
}

/*
 * Async MediaLink result handler
 *
 * @param result
 */
 
function handle_AsyncMediaLinkResult(result)
{
	document.controls.asyncMediaLinkResult.value = result; 
}

/*
 * Starts the async call to get the categories of the current asset based on application behavior settings.
 *
 */
 
function AsyncCategories(movie)
{
	thisMovie(movie).UMIExt_JS_AsyncCategories();
	thisMovie(movie).UMIExt_JS_AddEventListener("UMOExt_Evt_AsyncCategoriesResults", "handle_AsyncCategoriesResult");
}

/*
 * Async Categories result handler
 *
 * @param result
 */
 
function handle_AsyncCategoriesResult(result)
{
	document.controls.asyncMediaLinkResult.value = result; 
}
