//################################
//##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.
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)
function GetPlayheadTime(movie) {
    return thisMovie(movie).UMIExt_JS_GetPlayheadTime();
}

// Gets the current mediaitem title (String)
function GetMediaItemTitle(movie) {
    return thisMovie(movie).UMIExt_JS_GetMediaItemTitle();
}

// Gets the current mediaitem guid (String)
function GetMediaItemGuid(movie) {
    return thisMovie(movie).UMIExt_JS_GetMediaItemGuid();
}

// Gets the current mediaitem duration (Number)
function GetMediaItemDuration(movie) {
    return thisMovie(movie).UMIExt_JS_GetMediaItemDuration();
}

// Gets the current mediaitem entity name (String)
function GetMediaItemEntityName(movie) {
    return thisMovie(movie).UMIExt_JS_GetMediaItemEntityName();
}

// Gets the current mediaitem type (String) 	
function GetMediaItemType(movie) {
    return thisMovie(movie).UMIExt_JS_GetMediaItemType();
}

// Gets the current mediaitem keywords (String)
function GetMediaItemKeywords(movie) {
    return thisMovie(movie).UMIExt_JS_GetMediaItemKeywords();
}

// Gets the current mediaitem description (String)
function GetMediaItemDescription(movie) {
    return thisMovie(movie).UMIExt_JS_GetMediaItemDescription();
}

// Gets the current volume of the player (Number, 0 - 10)
function GetPlayerVolumeState(movie) {
    return thisMovie(movie).UMIExt_JS_GetPlayerVolumeState();
}

// Gets the current media item ad positions (Array)
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)
function GetMediaItemRating(movie) {
    return thisMovie(movie).UMIExt_JS_GetMediaItemRating();
}

// Gets the current mediaitem thumbnail URL (String)
function GetMediaItemThumbnailURL(movie) {
    return thisMovie(movie).UMIExt_JS_GetMediaItemThumbnailURL();
}

// Gets the current mute state of the player (uint, 0 = mute on, 1 = mute off)
function GetMuteState(movie) {
    return thisMovie(movie).UMIExt_JS_GetMuteState();
}

// Gets the original embed code for the player instance
function GetPlayerEmbedCode(movie) {
    return thisMovie(movie).UMIExt_JS_GetPlayerEmbedCode();
}

// Gets the current Video Ad URL
function GetAdMediaURL(movie) {
    return thisMovie(movie).UMIExt_JS_GetAdMediaURL();
}

// Gets the current Video Ad Click URL
function GetAdMediaClickURL(movie) {
    return thisMovie(movie).UMIExt_JS_GetAdMediaClickURL();
}

// Gets the current Companion Ad Object (ClickThrough)
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)
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)
function GetPlayerState(movie) {
    return thisMovie(movie).UMIExt_JS_GetPlayerState();
}

// Gets the Entity Thumbnail URL
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
function SetPlayerMuteOff(movie) {
    thisMovie(movie).UMIExt_JS_SetPlayerMuteOff();
}

// Sets mute on
function SetPlayerMuteOn(movie) {
    thisMovie(movie).UMIExt_JS_SetPlayerMuteOn();
}

// Toggles mute
function ToggleMute(movie) {
    thisMovie(movie).UMIExt_JS_ToggleMute();
}

// Sets player volume (Number, 0 - 10) 	
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
function RemoveStopScreen(movie, defaultStart) {
    thisMovie(movie).UMIExt_JS_RemoveStopScreen(defaultStart);
}

// Pauses the current mediaitem
function Pause(movie) {
    thisMovie(movie).UMIExt_JS_Pause();
}

// Resumes play of the current mediaitem
function Resume(movie) {
    thisMovie(movie).UMIExt_JS_Resume();
}

// Seeks to specified position in seconds
function Seek(movie, position) {
    thisMovie(movie).UMIExt_JS_Seek(position);
}

// Plays the current mediaitem from position 0
function PlayCurrentItemFromStart(movie) {
    thisMovie(movie).UMIExt_JS_PlayCurrentItemFromStart();
}

// Plays the current mediaitem from specified position
function PlayCurrentItemFromPosition(movie, position) {
    thisMovie(movie).UMIExt_JS_PlayCurrentItemFromPosition(position);
}

// Adds a mediaitem to the queue
function LoadMediaItemInQueue(movie, miGuid) {
    thisMovie(movie).UMIExt_JS_LoadMediaItemInQueue(miGuid);
}

// Adds a mediaitem to the queue and plays
function LoadMediaItemInQueueAndPlay(movie, miGuid) {
    thisMovie(movie).UMIExt_JS_LoadMediaItemInQueueAndPlay(miGuid);
}

// Adds a mediaitem to the queue and plays from specified 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
function ClearQueue(movie) {
    thisMovie(movie).UMIExt_JS_ClearQueue();
}

// Plays the current queue from position 0 (first item)  
function PlayCurrentQueueFromStart(movie) {
    thisMovie(movie).UMIExt_JS_PlayCurrentQueueFromStart();
}

// Plays the current queue from specified position   
function PlayCurrentQueueFromItemPosition(movie, position) {
    thisMovie(movie).UMIExt_JS_PlayCurrentQueueFromItemPosition(position);
}

function Set_Cookie(name, value, expires, path, domain, secure) {
    // set time, it's in milliseconds
    var today = new Date();
    today.setTime(today.getTime());

    /*
    if the expires variable is set, make the correct
    expires time, the current script below will set
    it for x number of days, to make it for hours,
    delete * 24, for minutes, delete * 60 * 24
    */
    if (expires) {
        expires = expires * 1000 * 60 * 60 * 24;
    }
    var expires_date = new Date(today.getTime() + (expires));

    document.cookie = name + "=" + escape(value) +
((expires) ? ";expires=" + expires_date.toGMTString() : "") +
((path) ? ";path=" + path : "") +
((domain) ? ";domain=" + domain : "") +
((secure) ? ";secure" : "");
}
function Get_Cookie(check_name) {
    // first we'll split this cookie up into name/value pairs
    // note: document.cookie only returns name=value, not the other components
    var a_all_cookies = document.cookie.split(';');
    var a_temp_cookie = '';
    var cookie_name = '';
    var cookie_value = '';
    var b_cookie_found = false; // set boolean t/f default f

    for (i = 0; i < a_all_cookies.length; i++) {
        // now we'll split apart each name=value pair
        a_temp_cookie = a_all_cookies[i].split('=');


        // and trim left/right whitespace while we're at it
        cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

        // if the extracted name matches passed check_name
        if (cookie_name == check_name) {
            b_cookie_found = true;
            // we need to handle case where cookie has no value but exists (no = sign, that is):
            if (a_temp_cookie.length > 1) {
                cookie_value = unescape(a_temp_cookie[1].replace(/^\s+|\s+$/g, ''));
            }
            // note that in cases where cookie is initialized but no value, null is returned
            return cookie_value;
            break;
        }
        a_temp_cookie = null;
        cookie_name = '';
    }
    if (!b_cookie_found) {
        return null;
    }
}
function Delete_Cookie(name, path, domain) {
    if (Get_Cookie(name)) document.cookie = name + "=" +
((path) ? ";path=" + path : "") +
((domain) ? ";domain=" + domain : "") +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

