/***************************************************
Haas Outdoors Windows Media Player scripts
Author: Kerry Edwin Gerhard 2006-07
***************************************************/

// user resolution - defaults to medium
var videoRes = "Medium";
// user sessionid
var sessionId;
var playlistArray;
var userName = "Anonymous";
var lastMediaId = 0;
var lastMediaTitle;
var hwcPlayer, hwcPlayerXL, hwcPlayerXLV;
var nppPlayer;
var bResChanging = false;


function getPlayer()
{
  return document.thePlayer;
}

function showID(oId) {

  var o = document.getElementById(oId);
  if(o!=null)
    o.style.display = 'inline';
}

function hideID(oId) {
  var o = document.getElementById(oId);
  if(o)
    o.style.display = 'none';
}
function showPic(oId, imgUrl)
{
  var o = document.getElementById(oId);
  if((o!=undefined))
    o.src = imgUrl;

}
function getImageBase()
{
  var thisUrl = window.location.href;
  var thisPath = window.location.pathname;
  var pos = thisUrl.indexOf(thisPath);
  var baseUrl = thisUrl.substring(0, pos);
  if(thisUrl.toLowerCase().indexOf('/ep/')>-1)
    baseUrl = baseUrl + '/EP';
  return baseUrl;
}
function togglePlaylist()
{
  /*if(playlistArray.length<1)
    return;*/

  var o = document.getElementById('imgPLCollapsed');

  if(o!=null)
  {
    if(o.src.indexOf('button-collapsed-off-1.gif')>-1)
    {
      showID('tblPlaylist');
      o.src = getImageBase() + "/images/video/button-expanded-off-1.gif";
    }
    else if(o.src.indexOf('button-expanded-off-1.gif'))
    {
      hideID('tblPlaylist');
      o.src = getImageBase() + "/images/video/button-collapsed-off-1.gif";
    }
  }
}

function stopPlayer()
{
  if(document.thePlayer==undefined)
    return;
  if(document.thePlayer.controls!=undefined)
    document.thePlayer.controls.stop();
}
function playPausePlayer()
{
  if(document.thePlayer==null)
    return;
  if (document.thePlayer.playState == 3)
  {
    if(document.thePlayer.controls.isAvailable('pause'))
      document.thePlayer.controls.pause();
  }
  else
  {
    document.thePlayer.controls.play();
  }

}
function doFullscreen()
{
//  if(document.thePlayer==null)
//    return;
  if (document.thePlayer.playState == 3)
      document.thePlayer.fullScreen = "True";
}
function setPlaylistEvents()
{
  if((document.thePlayer!=null)
    &&(navigator.appName!='Microsoft Internet Explorer')
    &&((window.location.href.toLowerCase().indexOf('/member.aspx')>-1)
      ||(window.location.href.toLowerCase().indexOf('/video.aspx')>-1)))
    {
      document.thePlayer.playStateChange="playStateChange(NewState)";
    }

}
// This function is used for VOD, search and playlist
function renderPlayer(videoUrl, pWidth, pHeight)
{
    var playerDiv = document.getElementById('divWMP');

    if((document.thePlayer==null)||(navigator.appName!='Microsoft Internet Explorer')) {
        var tag;
        if(navigator.appName=='Microsoft Internet Explorer') {
            tag = '<object';
            tag += ' ID="thePlayer"';
            tag += ' width="' + pWidth + '"';
            tag += ' height="' + pHeight + '"';
            tag += ' classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6"';
            tag += ' codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701"';
            tag += ' standby="Loading Microsoft Windows Media Player components..."';
            tag += ' type="application/x-oleobject"';
            tag += '>   ';
            tag += '\r\n';
            tag += '<param name="URL" value="' + videoUrl + '" />';
            tag += '<param name="DisplaySize" value="0" />';
            tag += '<param name="AllowChangeDisplaySize" value="True" /> ';
            tag += '<param name="ShowControls" value="True" />';
            tag += '<param name="ShowStatusBar" value="False" />';
            tag += '\r\n';
            tag += '<param name="AutoStart" value="True" />';
            tag += '<param name="CurrentPosition" value="0.0"/>';
            tag += '<param name="stretchToFit" value="true"/>';
            tag += '<param name="BufferingTime" value="0"/>';
            tag += '<param name="windowlessVideo" value="false"/>';
            tag += '<embed type="application/x-mplayer2" src="' + videoUrl + '" name="thePlayer" width="' + pWidth + '" height="' + pHeight + '"';
            tag += 'ShowControls="1" ShowStatusBar="0" ShowDisplay="0" AutoStart="1"></embed>';
            tag += '</object>';
            tag += '\r\n';

            playerDiv.innerHTML=tag;
        }
        else
        {
            if ((navigator.userAgent.indexOf("Firefox") > -1) && (navigator.platform.indexOf("Win32") > -1)) {
                tag = '<object';
                tag += ' ID="thePlayer"';
                tag += ' width="' + pWidth + '"';
                tag += ' height="' + pHeight + '"';
                tag += ' codebase="http://port25.technet.com/videos/downloads/wmpfirefoxplugin.exe"';
                tag += ' standby="Loading Microsoft Windows Media Player components..."';
                tag += ' type="application/x-ms-wmp"';
                tag += '>   ';
                tag += '\r\n';
                tag += '<param name="URL" value="' + videoUrl + '" />';
                tag += '<param name="DisplaySize" value="0" />';
                tag += '<param name="AllowChangeDisplaySize" value="True" /> ';
                tag += '<param name="ShowControls" value="True" />';
                tag += '<param name="ShowStatusBar" value="False" />';
                tag += '\r\n';
                tag += '<param name="AutoStart" value="True" />';
                tag += '<param name="CurrentPosition" value="0.0"/>';
                tag += '<param name="stretchToFit" value="true"/>';
                tag += '<param name="BufferingTime" value="0"/>';
                tag += '<param name="windowlessVideo" value="false"/>';
                tag += '<embed type="application/x-mplayer2" src="' + videoUrl + '" name="thePlayer" width="' + pWidth + '" height="' + pHeight + '"';
                tag += 'PluginsPage="http://www.microsoft.com/Windows/Downloads/Contents/Products/MediaPlayer/" ShowControls="1" ShowStatusBar="0" ShowDisplay="0" AutoStart="1"></embed>';
                tag += '</object>';
                tag += '\r\n';
        
            }
            else
            {
                if(videoUrl.length > 0) {
                    tag = '<embed';
                    tag += '  name="thePlayer"';
                    tag += '  width="' + pWidth + '"';
                    tag += '  height="' + pHeight + '"';
                    tag += '  type="application/x-mplayer2"';
                    tag += '  pluginspage="http://www.microsoft.com/Windows/Downloads/Contents/Products/MediaPlayer/"';
                    tag += '  src="' + videoUrl + '"';
                    tag += '\r\n';
                    tag += '  AutoStart="1"';
                    tag += '  ShowStatusBar="0"';
                    tag += '  ShowControls="1"';
                    tag += '  ShowDisplay="0">';
                    tag += '</embed>';
                    tag += '\r\n';
                }
            }

            playerDiv.innerHTML=tag;  
            setPlaylistEvents();
        }
    }
    else
    {
        if(navigator.platform.indexOf("Win32") > -1)
            document.thePlayer.URL = videoUrl;
        else
        {
            // this is supposed to work for quicktime
            document.getElementById('thePlayer').SetURL(videoUrl);
        }
    }
}

// This is used for subscriber home page
function renderIPTV(videoUrl, pWidth, pHeight, bAutoStart, bSilent, bSubscriber)
{
    // overwrite width and height - new requirements
    var playerDiv = document.getElementById('divWMP');
    pWidth="100%";
    pHeight="100%";
    bSubscriber = true;
    
    if(document.thePlayer==undefined) {
        var tag;
        if(navigator.appName=='Microsoft Internet Explorer') {
            tag = '<object';
            tag += ' ID="thePlayer"';
            tag += ' width="' + pWidth + '"';
            tag += ' height="' + pHeight + '"';
            tag += ' classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6"';
            tag += ' codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701"';
            tag += ' standby="Loading Microsoft Windows Media Player components..."';
            tag += ' type="application/x-oleobject"';
            tag += '>   ';
            tag += '\r\n';
            tag += '<param name="URL" value="' + videoUrl + '" />';
            tag += '<param name="DisplaySize" value="0" />';
            tag += '\r\n';

            if(bSilent==true)
                tag += '<param name="Volume" value="-10000" />';
            else
                tag += '<param name="Volume" value="50" />';
                
            if(bAutoStart==true)
                tag += '<param name="AutoStart" value="True" />';
            else
                tag += '<param name="AutoStart" value="False" />';
                
            if(bSubscriber==true)
            {
                tag += '<param name="uiMode" value="full" />';
                tag += '<param name="enableContextMenu" value="True" />';
                tag += '<param name="AllowChangeDisplaySize" value="True" /> ';
            }
            else
            {
                tag += '<param name="uiMode" value="mini" />';
                tag += '<param name="enableContextMenu" value="False" />';
                tag += '<param name="AllowChangeDisplaySize" value="False" /> ';
            }
            
            tag += '\r\n';
            tag += '<param name="CurrentPosition" value="0.0"/>';
            tag += '<param name="stretchToFit" value="True"/>';
            tag += '<param name="BufferingTime" value="0"/>';
            tag += '<param name="windowlessVideo" value="false"/>';
            tag += '<embed type="application/x-mplayer2" src="' + videoUrl + '" name="thePlayer" width="' + pWidth + '" height="' + pHeight + '"';
            tag += 'ShowControls="1" ShowStatusBar="0" ShowDisplay="0" AutoStart="1"></embed>';
            tag += '</object>';
            tag += '\r\n';
        }
        else
        {
            if ((navigator.userAgent.indexOf("Firefox") > -1) &&(navigator.platform.indexOf("Win32") > -1)) {
                tag = '<object';
                tag += ' ID="thePlayer"';
                tag += ' width="' + pWidth + '"';
                tag += ' height="' + pHeight + '"';
                tag += ' standby="Loading Microsoft Windows Media Player components..."';
                tag += ' type="application/x-ms-wmp"';
                tag += '>   ';
                tag += '\r\n';
                tag += '<param name="URL" value="' + videoUrl + '" />';
                tag += '<param name="DisplaySize" value="0" />';
                tag += '\r\n';

                if(bSilent==true)
                    tag += '<param name="Volume" value="-10000" />';
                else
                    tag += '<param name="Volume" value="50" />';
                    
                if(bAutoStart==true)
                    tag += '<param name="AutoStart" value="True" />';
                else
                    tag += '<param name="AutoStart" value="False" />';
                    
                if(bSubscriber==true)
                {
                    tag += '<param name="uiMode" value="full" />';
                    tag += '<param name="enableContextMenu" value="True" />';
                    tag += '<param name="AllowChangeDisplaySize" value="True" /> ';
                }
                else
                {
                    tag += '<param name="uiMode" value="mini" />';
                    tag += '<param name="enableContextMenu" value="False" />';
                    tag += '<param name="AllowChangeDisplaySize" value="False" /> ';
                }
                
                tag += '\r\n';
                tag += '<param name="CurrentPosition" value="0.0"/>';
                tag += '<param name="stretchToFit" value="True"/>';
                tag += '<param name="BufferingTime" value="0"/>';
                tag += '<param name="windowlessVideo" value="false"/>';
                tag += '<embed type="application/x-mplayer2" src="' + videoUrl + '" name="thePlayer" width="' + pWidth + '" height="' + pHeight + '"';
                tag += 'ShowControls="1" ShowStatusBar="0" ShowDisplay="0" AutoStart="1"></embed>';
                tag += '</object>';
                tag += '\r\n';
            }
            else
            {
                tag = '<embed';
                tag += '  name="thePlayer"';
                tag += '  width="' + pWidth + '"';
                tag += '  height="' + pHeight + '"';
                tag += '  type="application/x-mplayer2"';
                tag += '\r\n';
                tag += '  pluginspage="http://www.microsoft.com/Windows/Downloads/Contents/Products/MediaPlayer/"';
                tag += '  src="' + videoUrl + '"';
                tag += '\r\n';
                
                if(bSilent==true)
                    tag += '  Volume="-10000"';
                else
                    tag += '  Volume="50"';
                    
                if(bAutoStart==true)
                    tag += '  AutoStart="1"';
                else
                    tag += '  AutoStart="0"';
                    
                if(bSubscriber==true)
                {
                    tag += '  AllowChangeDisplaySize="1"';
                    tag += '  enableContextMenu="1"';
                    tag += '  showControls="1"';
                    tag += '  uiMode="full"';
                }
                else
                {
                    tag += '  AllowChangeDisplaySize="0"';
                    tag += '  enableContextMenu="0"';
                    tag += '  showControls="0"';
                    tag += '  uiMode="mini"';
                }
                
                tag += '  AllowChangeDisplaySize="0"';
                tag += '  enableContextMenu="0"';
                tag += '  CurrentPosition="0.0"';
                tag += '  stretchToFit="1"';
                tag += '\r\n';
                tag += '  DisplaySize="0"';
                tag += '  BufferingTime="0">';
                tag += '</embed>';
                tag += '\r\n';
      
            }
        }
        
        playerDiv.innerHTML=tag;
    }
    else
    {
        document.thePlayer.URL = videoUrl;
    }
}

function renderMossyOakTV(videoUrl, pWidth, pHeight, bIsSubscriber)
{
    var playerDiv = document.getElementById('divWMP');
    playerDiv.style.width = pWidth;
    playerDiv.style.height = pHeight;

    if(document.thePlayer==undefined) {
        var tag;
        
        if(navigator.appName=='Microsoft Internet Explorer') {
            tag = '<object';
            tag += ' ID="thePlayer"';
            tag += ' width="' + pWidth + '"';
            tag += ' height="' + pHeight + '"';
            tag += ' classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6"';
            tag += ' codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701"';
            tag += ' standby="Loading Microsoft Windows Media Player components..."';
            tag += ' type="application/x-oleobject"';
            tag += '>   ';
            tag += '\r\n';
            tag += '<param name="URL" value="' + videoUrl + '" />';
            tag += '<param name="DisplaySize" value="0" />';
            tag += '<param name="AutoStart" value="True" />';
            tag += '<param name="Volume" value="50" />';
            tag += '<param name="stretchToFit" value="True" />';
            tag += '\r\n';
            
            if(bIsSubscriber)
            {
                tag += '<param name="uiMode" value="full" />';
                tag += '<param name="AllowChangeDisplaySize" value="True" /> ';
                tag += '<param name="enableContextMenu" value="True" />';
            }
            else
            {
                tag += '<param name="uiMode" value="mini" />';
                tag += '<param name="AllowChangeDisplaySize" value="False" /> ';
                tag += '<param name="enableContextMenu" value="False" />';
            }

            tag += '<embed type="application/x-mplayer2" src="' + videoUrl + '" name="thePlayer" width="' + pWidth + '" height="' + pHeight + '"';
            tag += 'ShowControls="1" ShowStatusBar="0" ShowDisplay="0" AutoStart="1"></embed>';
            tag += '</object>';
        }
        else
        {
            if ((navigator.userAgent.indexOf("Firefox") > -1) && (navigator.platform.indexOf("Win32") > -1)) {
                tag = '<object';
                tag += ' ID="thePlayer"';
                tag += ' width="' + pWidth + '"';
                tag += ' height="' + pHeight + '"';
                tag += ' standby="Loading Microsoft Windows Media Player components..."';
                tag += ' type="application/x-ms-wmp"';
                tag += '>   ';
                tag += '\r\n';
                tag += '<param name="URL" value="' + videoUrl + '" />';
                tag += '<param name="DisplaySize" value="0" />';
                tag += '<param name="AutoStart" value="True" />';
                tag += '<param name="Volume" value="50" />';
                tag += '<param name="stretchToFit" value="True" />';
                tag += '\r\n';
                
                if(bIsSubscriber)
                {
                    tag += '<param name="uiMode" value="full" />';
                    tag += '<param name="AllowChangeDisplaySize" value="True" /> ';
                    tag += '<param name="enableContextMenu" value="True" />';
                }
                else
                {
                    tag += '<param name="uiMode" value="mini" />';
                    tag += '<param name="AllowChangeDisplaySize" value="False" /> ';
                    tag += '<param name="enableContextMenu" value="False" />';
                }

                tag += '<embed type="application/x-mplayer2" src="' + videoUrl + '" name="thePlayer" width="' + pWidth + '" height="' + pHeight + '"';
                tag += 'ShowControls="1" ShowStatusBar="0" ShowDisplay="0" AutoStart="1"></embed>';
                tag += '</object>';
            }
            else
            {
                tag = '<embed';
                tag += '  name="thePlayer"';
                tag += '  width="' + pWidth + '"';
                tag += '  height="' + pHeight + '"';
                tag += '  type="application/x-mplayer2"';
                tag += '  pluginspage="http://www.microsoft.com/Windows/Downloads/Contents/Products/MediaPlayer/"';
                tag += '  src="' + videoUrl + '"';
                tag += '  AutoStart="1"';
                
                if(bIsSubscriber)
                {
                    tag += '  enableContextMenu="1"';
                    tag += '  showControls="1"';
                }
                else
                {
                    tag += '  enableContextMenu="0"';
                    tag += '  showControls="0"';
                    tag += '  AllowChangeDisplaySize="0">';
                }
                
                tag += '</embed>';
            }
        }
    
        playerDiv.innerHTML=tag;
    }
    else
    {
        document.thePlayer.width=pWidth;
        document.thePlayer.height=pHeight;
        document.thePlayer.URL = videoUrl;
    }
}

function playVideo(mediaId, userName)
{
  
  if( (mediaId==undefined) || (mediaId==null) || (mediaId=="") )
    return;

  bResChanging = true;
  var videoToken = videoService.RenderVideo(mediaId,videoRes,userName,renderPlaylist,ErrorHandler,TimeOutHandler);
  window.setTimeout("bResChanging = false", 500);


}
function renderPlaylist(videoToken)
{
  if(videoToken==null) return; //alert('Error retrieving playlist' + videoToken);
  renderPlayer(videoToken,'388','323');
  window.location.href="#mediaPlayer";

}

function displayCurrentMediaInfo(mediaInfo)
{
    var tbl = document.getElementById('tblCurrent');
    if(tbl==null) return;
    if(mediaInfo.length==0) return;

    var summary = mediaInfo[2];

    if(summary.length>240)
      summary = summary.substring(0,240) + '...';

    setInnerText(tbl.rows[0].cells[0],mediaInfo[0]); // Title
    
    if(mediaInfo[1]!='NA')
      setInnerText(tbl.rows[1].cells[0],mediaInfo[1]); // Hunter
    setInnerText(tbl.rows[2].cells[0],summary);
    if((mediaInfo[3]!='Hunting | 0 secs')&&(mediaInfo[3]!='Hunting | '))
      setInnerText(tbl.rows[3].cells[0],mediaInfo[3]); // Info

  return summary;
}
function displayPlaylistMediaInfo(mediaInfo)
{
  if(mediaInfo[0].indexOf("<error")==-1)
  {
    var summary = displayCurrentMediaInfo(mediaInfo);
    var plItem = new mediaPlaylist(mediaInfo[4], mediaInfo[0], mediaInfo[1], summary, mediaInfo[3]);
    if(playlistArray==undefined)
    {
      playlistArray = new Array();
      playlistArray.push(plItem);
    }
    else if(playlistArray.length==0)
    {
      playlistArray.push(plItem);
    }
    else if(playlistArray.length>0)
    {
      playlistArray[0] = plItem;
    }
  }
  displayPlaylistCount();
}

function displayPlaylistUpNextInfo(mediaInfo)
{
  if(mediaInfo[0].indexOf("<error")==-1)
  {
    var plItem = new mediaPlaylist(mediaInfo[4], // mediaid
                                    mediaInfo[0], //mediaTitle
                                    mediaInfo[1], // hunter
                                    mediaInfo[2], // summary
                                    mediaInfo[3]); // info

    if(mediaInfo[1].length > 90)
      mediaInfo[1] = mediaInfo[1].substring(0, 87) + "...";

    if(mediaInfo[2].length > 160)
      mediaInfo[2] = mediaInfo[2].substring(0, 157) + "...";

    if(playlistArray==undefined)
      playlistArray = new Array();
    playlistArray.push(plItem);
    showID('vodPlaylist');
    document.getElementById('imgPLCollapsed').src = getImageBase() + "/images/video/button-collapsed-off-1.gif";
    setInnerText(document.getElementById('upNextTitle'),mediaInfo[0]);
    setInnerText(document.getElementById('upNextMediaId'),mediaInfo[4]);

    displayPlaylistCount();
  }
}

function displayPlaylistItem(mediaInfo)
{
  if(mediaInfo[0].indexOf("<error")==-1)
  {
    var plItem = new mediaPlaylist(mediaInfo[4], // mediaid
                                    mediaInfo[0], //mediaTitle
                                    mediaInfo[1], // hunter
                                    mediaInfo[2], // summary
                                    mediaInfo[3]); // info

    if(playlistArray==undefined)
      playlistArray = new Array();
    playlistArray.push(plItem);
    addPlaylistItemToTable(plItem);
    displayPlaylistCount();
  }
}

function ErrorHandler(result)
{
   var msg=result.get_exceptionType() + "\r\n";
   msg += result.get_message() + "\r\n";
   msg += result.get_stackTrace();
   alert(msg);
}
function TimeOutHandler(result)
{
   alert("Timeout: " + result);
}
function showNowPlaying(mediaTitle, hunter, summary, info, mediaId)
{
  showID('mediaNowPlaying');
  var tbl = document.getElementById('tblCurrent');
  if(tbl==null)
    return;

  if(summary.length>80)
    summary = summary.substring(0,80) + '...';
  setInnerText(tbl.rows[0].cells[0],mediaTitle);
  setInnerText(tbl.rows[1].cells[0],hunter);
  setInnerText(tbl.rows[2].cells[0],summary);
  if((info != 'Hunting |  | 0 secs | ') && (info != '0 secs'))
      setInnerText(tbl.rows[3].cells[0],info);

  var plItem = new mediaPlaylist(mediaId, mediaTitle, hunter, summary, info);
  if(playlistArray==undefined)
    playlistArray = new Array();
  playlistArray.push(plItem);

}

function playShowVideo(mediaid)
{
  bResChanging = true;
  window.setTimeout("bResChanging = false", 500);
  showID('mediaNowPlaying');
  videoService.GetMediaInfo(mediaid,displayPlaylistMediaInfo,ErrorHandler,TimeOutHandler);
  playVideo(mediaid, userName);
}
function playShowVideoUser(mediaid, userName)
{
  //userName = user;
  bResChanging = true;
  window.setTimeout("bResChanging = false", 500);
  showID('mediaNowPlaying');
  videoService.GetMediaInfo(mediaid,displayPlaylistMediaInfo,ErrorHandler,TimeOutHandler);
  playVideo(mediaid, userName);
}


function unloadPlayer()
{
  var player = getPlayer();
  if(player!=undefined)
  {
    stopPlayer();
    player = null;
  }
  if(window.location.href.toLowerCase().indexOf('/mediaplayerxl.aspx')>-1)
  {
    try
    {
      if(((hwcPlayer==undefined)||(hwcPlayer==null))
        &&(bResChanging==false)
        &&((window.opener!=null)&&(window.opener.location.href.toLowerCase().indexOf('home.aspx')>-1)))
        window.opener.setTimeout('window.location.reload();', 200);
    }
    catch(e) {}
  }
}


function initializeMedia()
{
  document.body.onunload = unloadPlayer;
  if(window.location.href.toLowerCase().indexOf('/member.aspx')>-1)
  {
    hideID('vodResolutionControls');
  }



}
function removePlaylistRow(tdElement)
{
  //var idx = tdElement.parentElement.parentElement.rowIndex;
  var idx = tdElement.parentNode.parentNode.rowIndex;
  var tbl = document.getElementById('tblPlaylist');
  tbl.deleteRow(idx);
  tbl.deleteRow(idx-1);
  playlistArray.splice(idx, 1);
  displayPlaylistCount();
}
function removeUpNextItem()
{
  if(playlistArray.length>=1)
  {

    if(playlistArray.length>2)
    {
      var plItem = playlistArray[2];
      setInnerText(document.getElementById('upNextTitle'),plItem.title);
      setInnerText(document.getElementById('upNextMediaId'),plItem.mediaId);
      var tbl = document.getElementById('tblPlaylist');
      if(tbl.rows.length >= 2)
      {
        tbl.deleteRow(0);
        tbl.deleteRow(0);
      }
    }
    else
    {
      setInnerText(document.getElementById('upNextTitle'),"");
      setInnerText(document.getElementById('upNextMediaId'), "");
      hideID('vodPlaylist');
    }
    playlistArray.splice(1, 1);

  }
  displayPlaylistCount();
}
function rollupPlaylist()
{
  if( (playlistArray==undefined) || (document.getElementById('tblPlaylist')==undefined) )
    return;
  var tbl = document.getElementById('tblPlaylist');
  var cnt = playlistArray.length;
  if(cnt > 1)
  {

    var plItem = playlistArray.splice(1,1);
    plItem = plItem[0]; // took a while to figure this out
    if(plItem==undefined)
    {
      setInnerText(document.getElementById('upNextTitle'),"");
      setInnerText(document.getElementById('upNextMediaId'),"");
      hideID('vodPlaylist');
      return;
    }

    var tbl = document.getElementById("tblCurrent");
    if(tbl==undefined)
      return;

    setInnerText(tbl.rows[0].cells[0],plItem.title == undefined ? '' : plItem.title);
    setInnerText(tbl.rows[1].cells[0],plItem.hunter == undefined ? '' : plItem.hunter);
    setInnerText(tbl.rows[2].cells[0],plItem.summary == undefined ? '' : plItem.summary);
    if((plItem.info != undefined)
      &&(plItem.info != 'Hunting | 0 secs')
      &&(plItem.info != 'Hunting |  | 0 secs | '))
    {
      setInnerText(tbl.rows[3].cells[0],plItem.info);
    }
    else
      setInnerText(tbl.rows[3].cells[0],'');


    if(playlistArray.length>0)
    {

      if(playlistArray.length==1)
      {
        setInnerText(document.getElementById('upNextTitle'),"");
        setInnerText(document.getElementById('upNextMediaId'),"");
        hideID('vodPlaylist');
        displayPlaylistCount();
        return;
      }
      else if(playlistArray.length > 1)
      {
        plItem = playlistArray[1];
        setInnerText(document.getElementById('upNextTitle'),plItem.title);
        setInnerText(document.getElementById('upNextMediaId'),plItem.mediaId);

      }
    }

    if(document.getElementById('tblPlaylist').rows.length >= 2) // two rows per playlist item
    {
      document.getElementById('tblPlaylist').deleteRow(0);
      document.getElementById('tblPlaylist').deleteRow(0);
    }
  }
  else
  {
    playlistArray.pop();
    setInnerText(document.getElementById('upNextTitle'),"");
    setInnerText(document.getElementById('upNextMediaId'),"");
    hideID('vodPlaylist');
    var tbl1 = document.getElementById("tblCurrent");
    if(tbl1!=undefined)
    {
      setInnerText(tbl1.rows[0].cells[0],'Awaiting your selection');
      setInnerText(tbl1.rows[1].cells[0],'');
      setInnerText(tbl1.rows[2].cells[0],'');
      setInnerText(tbl1.rows[3].cells[0],'');
    }

  }
  displayPlaylistCount();
}

function playUpNext()
{

  if(playlistArray.length>1)
    playVideo(playlistArray[1].mediaId, userName);

  //var mediaId = document.getElementById('upNextMediaId').value;
  rollupPlaylist();
//  playVideo(mediaId, userName);

}
function playTableItem(mediaId, tdElement)
{
  removePlaylistRow(tdElement);
  playVideo(mediaId,userName);
  videoService.GetMediaInfo(mediaId,displayCurrentMediaInfo,ErrorHandler,TimeOutHandler);

}

function addPlaylistItemToTable(playlistItem)
{
    var tbl = document.getElementById('tblPlaylist');
    if(tbl==null)
    {
      alert("Error getting playlist table.");
      return;

    }
    var row = tbl.insertRow(-1);
    c = document.createElement("TD");
    c.innerHTML = "<span class=\"playlistTitle\" onclick=\"javascript:playTableItem(" + playlistItem.mediaId + ",this);\">" + playlistItem.title + "</span>";
    row.appendChild(c);
    row = tbl.insertRow(-1);
    c = document.createElement("TD");
    c.innerHTML = "<span class=\"fauxLink\" onclick=\"javascript:playTableItem(" + playlistItem.mediaId + ",this);\">Play</span> | <span class=\"fauxLink\" onclick=\"removePlaylistRow(this)\">Remove</span>";
    row.appendChild(c);

}

function addToPlaylist(mediaId)
{
  if(mediaId==undefined) return;

  if(playlistArray==undefined)
  {
    var p = getPlayer();

    if(p!=null)
    {
      if(p.currentMedia==null)
      {
        playShowVideo(mediaId);
      }
      else
      {
        videoService.GetMediaInfo(mediaId,displayPlaylistUpNextInfo,ErrorHandler,TimeOutHandler);
      }
    }
  }
  else
  {
    showID('vodPlaylist');


    document.getElementById('imgPLCollapsed').src = getImageBase() + "/images/video/button-expanded-off-1.gif";
    if(playlistArray.length==1)
    {
        document.getElementById('imgPLCollapsed').src = getImageBase() + "/images/video/button-collapsed-off-1.gif";
        videoService.GetMediaInfo(mediaId,displayPlaylistUpNextInfo,ErrorHandler,TimeOutHandler);
        hideID('playlist_items');

    }
    else if(playlistArray.length>1)
    {
      showID('playlist_items');
      document.getElementById('imgPLCollapsed').src = getImageBase() + "/images/video/button-expanded-off-1.gif";
      videoService.GetMediaInfo(mediaId,displayPlaylistItem,ErrorHandler,TimeOutHandler);
    }
  }

}
function displayPlaylistCount()
{
  if(playlistArray!=undefined)
  {
    var count = 0;
    if(playlistArray.length>0)
      count = playlistArray.length - 1;
    document.getElementById('divPlaylistCount').innerHTML = '<b>Playlist</b> (' + count + ')';
  }

}

/*** Playlist Class ***/

function mediaPlaylist(mediaId, mediaTitle, hunter, summary, info)
{

  if(mediaTitle==undefined) mediaTitle = "";
  if(hunter==undefined) hunter = "";
  if(summary==undefined) summary = "";
  if(info==undefined) info = "";
  this.mediaId = mediaId;
  this.title = mediaTitle;
  this.hunter = hunter;
  this.summary = summary;
  this.info = info;
}

/*** End Playlist Class ***/

function searchUrl(theUrl)
{
  if((theUrl==undefined)|| (theUrl.length==0))
    return null;

  var  v, n = theUrl.substring(1).split(/&/), i = n.length;
  var u  = new Array();

  while( i-- )
  {
    v = n[i].split( /=/ );
    u[v[0]] = v[1];
  }
  return u;
}
function showNativePlayer(theUrl)
{
  nppPlayer = window.open(theUrl,"nativeProxy","width=64,height=64,status=1");
}
function showChildPlayer(childPlayerUrl)
{
  if(childPlayerUrl!=undefined)
  {
    var newUrl = getImageBase() + "/media/mediaplayerxlm.aspx?filePath=" + childPlayerUrl;
    hwcPlayer = window.open(newUrl,"mediaPlayer","width=770px,height=650px,status=0");
  }
}
function showExternalPlayer(mode)
{
//  var ck = mGetCookie("userName");
//  if((ck==null)||(ck==""))
//  {
//    window.location.href =  getImageBase() + "/help/benefits.aspx";
//    return;
//  }

  stopPlayer();
  if(document.thePlayer != undefined)
  {
    document.thePlayer.enabled = false;
  }
  if(mode==0) // IPTV
  {
    var newUrl = getImageBase() + "/media/mediaplayerxl.aspx?IPTV=1";
    hwcPlayerXL = window.open(newUrl,"mediaPlayerXL","width=770px,height=650px,status=0,resizeable=0");
  }

}
function onLoadPlayer() /* not currently used */
{
  var parms = searchUrl(this.location.href);
  if(parms["filePath"]!=undefined)
  {
    if(document.thePlayer!=undefined)
      document.thePlayer.URL = parms["filePath"];

  }
  else if(parms["mediaid"]!=undefined)
  {
    playVideo(parms["mediaid"],userName);
  }

}

function setPlayerVolume()
{
  try
  {
  var p = getPlayer();
  p.Settings.Volume = 100;
  }
  catch(e){}
}

function restartPlaylist()
{
  if((playlistArray!=undefined)&&(playlistArray.length>0))
  {
    var plItem = playlistArray[0];
    //playVideo(plItem.mediaId, userName);
    var videoToken = videoService.RenderVideo(plItem.mediaId,videoRes,userName,renderPlaylist,ErrorHandler,TimeOutHandler);
  }
}

function setResolution(res)
{
  videoRes = res;
  if(!document.getElementById("imgLoRes"))
    return;
  switch(res)
  {
    case "Low":
      document.getElementById("imgLoRes").src = getImageBase() + "/images/video/low-off.gif";
      document.getElementById("imgMedRes").src = getImageBase() + "/images/video/medium-on.gif";
      document.getElementById("imgHiRes").src = getImageBase() + "/images/video/high-on.gif";
      break;
    case "Medium":
      document.getElementById("imgLoRes").src = getImageBase() + "/images/video/low-on.gif";
      document.getElementById("imgMedRes").src = getImageBase() + "/images/video/medium-off.gif";
      document.getElementById("imgHiRes").src = getImageBase() + "/images/video/high-on.gif";
    break;
    case "High":
      document.getElementById("imgLoRes").src = getImageBase() + "/images/video/low-on.gif";
      document.getElementById("imgMedRes").src = getImageBase() + "/images/video/medium-on.gif";
      document.getElementById("imgHiRes").src = getImageBase() + "/images/video/high-off.gif";
    break;
  }
}

function ResCallBackHandler(arg, context)
{
  bResChanging = true;
  restartPlaylist();
  window.setTimeout("bResChanging = false", 5000);
}

function ResCallBackErrorHandler(arg, context)
{
  alert("An error has occured setting your resolution.");
  window.location.href.reload(true);
}

function setResLow()
{
  setResolution("Low");
  setUserResolutionOnServer("Low");

}
function setResMedium()
{
  setResolution("Medium");
  setUserResolutionOnServer("Medium");

}
function setResHigh()
{
  setResolution("High");
  setUserResolutionOnServer("High");

}
function confirmFlag()
{
  return window.confirm('Are you sure you want to flag this video as inappropriate?');
}


function mGetCookie( cookieName ) {

var start = document.cookie.indexOf( cookieName + "=" );
var len = start + cookieName.length + 1;
if ( ( !start ) &&
( cookieName != document.cookie.substring( 0, cookieName.length ) ) )
{
return null;
}
if ( start == -1 ) return null;
var end = document.cookie.indexOf( "&", len );
if ( end == -1 ) end = document.cookie.length;
return unescape( document.cookie.substring( len, end ) );
}

function setInnerText(theNode, theText)
{
  if(navigator.appName=='Microsoft Internet Explorer')
    theNode.innerText = theText;
  else
    theNode.textContent = theText;
    
}

function openTVSchedule()
{
    var newUrl = getImageBase() + "/media/mossyschedule.aspx";
    hwcPlayerXL = window.open(newUrl,"tvsched","width=470px,height=400px,status=0,resizeable=0,toolbar=0,location=no,menubar=no");

}

function refreshNowPlaying()
{
  var p = getPlayer();
  
}

function confirmPublish()
{
  return window.confirm('Schedule records already exist for this week. Do you want to overwrite?');
}
