/*======================================================================================
' Unicorn Media video functions
======================================================================================*/
var UMPlayersOnPage = 1;                //number of players on the page (max 2)
var UMPlayersReady = 0;
var UMNewsPlayer = ''                   //id of the news player
var UMNewsGUID = '';
var UMPlaylistPlayer = ''               //id of the playlist player
var UMPlaylistGUID = '';
var UMPlaylistDefaultGUID = '';         //set to the 1st item in the playlist
var UMPlaylistItems;
var UMReq;

//fires when each Unicorn player is ready; added playerCount so script proceeds when ALL players are ready
function UMOExt_JS_PlayerReady() {
  UMPlayersReady++;
  if (UMPlayersReady == UMPlayersOnPage) {  //all players on the page are ready	      
	if (UMNewsPlayer.length > 0) {
	  //add event handler to the news player when player state changes
	  thisMovie(UMNewsPlayer).UMIExt_JS_AddEventListener('UMOExt_Evt_PlayerState', 'handle_UMOExt_Evt_PlayerState');
	  if (UMNewsGUID.length > 0) {
	    //start the video in the news player if one is requested before player is ready
	    RemoveStopScreen(UMNewsPlayer, true);
        LoadMediaItemInQueueAndPlay(UMNewsPlayer, UMNewsGUID);
    } }    
    if (UMPlaylistPlayer.length > 0) {
      if (UMPlaylistGUID.length > 0) {
        //start the video in the playlist player if one is requested before player is ready
        RemoveStopScreen(UMPlaylistPlayer, true);
        LoadMediaItemInQueueAndPlay(UMPlaylistPlayer, UMPlaylistGUID);
      }
      else {
        if (UMPlaylistDefaultGUID.length > 0) {          
          //load the default playlist video but don't start play
          LoadMediaItemInQueue(UMPlaylistPlayer, UMPlaylistDefaultGUID);
          UMPlaylistGUID = UMPlaylistDefaultGUID;
          UMPlaylistDefaultGUID = '';
} } } } }

//handles event when the player state changes;  will pause the video if the UMGUID is not set (video box is closed before playback starts)
function handle_UMOExt_Evt_PlayerState(s) {     
  if (UMGUID == '' && s == 'videoplayer_playing') Pause(UMNewsPlayer);
}

//stop the news player video 	  
function stopUMNewsVideo() {
  //UMNewsGUID = '';
  if (UMPlayersReady == UMPlayersOnPage && GetPlayerState(UMNewsPlayer) == 'videoplayer_playing') Pause(UMNewsPlayer);
}

//play a video in the news player
function playUMNewsVideo(guid) {
  var sameVideo = false;
  if(UMNewsGUID == guid)
  {
    sameVideo = true;
  }

  if (guid) UMNewsGUID = guid;
  if (UMPlayersReady == UMPlayersOnPage) {      //if player is not ready, the video will begin when PlayerReady event is fired
    if(sameVideo && GetPlayerState(UMNewsPlayer) == 'videoplayer_paused')
    {
        Resume(UMNewsPlayer);
    }
    else
    {
        RemoveStopScreen(UMNewsPlayer, true);
        LoadMediaItemInQueueAndPlay(UMNewsPlayer, UMNewsGUID);
    }
} }

//play a video in the playlist player
function playUMPlaylistVideo(guid) {
  if (guid) UMPlaylistGUID = guid;
  if (UMPlayersReady == UMPlayersOnPage) {      //if player is not ready, the video will begin when PlayerReady event is fired
    RemoveStopScreen(UMPlaylistPlayer, true);
    LoadMediaItemInQueueAndPlay(UMPlaylistPlayer, UMPlaylistGUID);
} }

//gets node value from UMPlaylistItems
function getUMPlaylistNode(tagName, node) {    
  var currentNode = UMPlaylistItems[node].getElementsByTagName(tagName);
  if(currentNode.length > 0) {
    if(currentNode[0].firstChild)
      return currentNode[0].firstChild.nodeValue;
    else return '';
  }
  else return '';
}

//gets attribute value from UMPlaylistItems
function getUMPlaylistAttribute(tagName, attrName, node) {    
  var currentNode = UMPlaylistItems[node].getElementsByTagName(tagName);
  if(currentNode.length > 0) {
    if(currentNode[0].getAttribute(attrName))
      return currentNode[0].getAttribute(attrName);
    else return '';
  }
  else return '';
}

function getUMRSS(guid) { 
  if (window.XMLHttpRequest) { UMReq = new XMLHttpRequest(); }
  else if (window.ActiveXObject) { UMReq = new ActiveXObject("Microsoft.XMLHTTP"); }
  if (UMReq != null) {
    UMReq.open("GET", "/cms40/site/unicorn/GetUMRSS.aspx?guid=" + guid + "&nocache=" + new Date().getTime(), true);
    UMReq.onreadystatechange = handleUMPlaylist;     //callback function to draw the list, must be defined on the site
    UMReq.send(null);
    return true;
  }
  else {    
    //alert('Your browser does not support the requirements for this feature.')
    return false;
} }
