<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>OctopusNet</title>
<link rel="stylesheet" type="text/css" href="/style.css">
<script type="text/javascript" src="/menu.js"></script>
<!--  Add included scripts here -->
<script type="text/javascript" src="/systeminfo.lua"></script>
<script type="text/javascript">Octoserve = false;</script>
<script type="text/javascript" src="/octoserve/serverinfo.js"></script>

<!-- Add page scripts here -->
<script type="text/javascript">

var VLCPluginValid = false;
var VLCPlugin;
var RTSPLink = false;
var UseAudioPids = true;

if( browserType == "Netscape" )
{
  if( browserPlatform == "ipad" )
  {
    RTSPLink = true;
  }
  else
  {
    for(var i = 0; i < navigator.plugins.length; i++ )
    {
      var t = navigator.plugins[i].name + " " + navigator.plugins[i].version;

      if( navigator.plugins[i].name == "VLC Web Plugin" )
      {
        VLCPlugin = navigator.plugins[i];
        if( browserPlatform == "win" )
        {
          if( navigator.plugins[i].version >= "2.0.6.0" ) VLCPluginValid = true;
          if( navigator.plugins[i].version >= "2.2.0.0" ) UseAudioPids = false;
        }
        else if( browserPlatform == "linux" )
        {
          VLCPluginValid = true;
        }

        break;
      }
    }
  }
}


var CurTracks = new Array();

var SelectedSource = 0;
var SelectedChannel = 0;
var SourceList = new Array();

function PlayEvent()
{
//  var vlc = document.getElementById("vlcplayer");
// var vlc = getVLC("vlcplayer");
  var vlc = vlcplayer;
  var options = new Array();
  var t = "rtsp://"+host+"/"+this.PlayRequest;
  vlc.playlist.clear();
  var id = vlc.playlist.add(t, this.PlayName, options);
  vlc.playlist.playItem(id);
  CurTracks = this.PlayTracks;

  document.getElementById("nowplaying").firstChild.nodeValue  = this.PlayName;
  document.getElementById("ac0").disabled  = CurTracks.length < 1;
  document.getElementById("ac1").disabled  = CurTracks.length < 2;
  document.getElementById("ac2").disabled  = CurTracks.length < 3;
  document.getElementById("ac3").disabled  = CurTracks.length < 4;
}

function SetAudioChannel(ac)
{
  var vlc = vlcplayer;
  if( ac < CurTracks.length ) vlc.audio.track = UseAudioPids ? CurTracks[ac] : ac + 1;
}

function MuteAudio()
{
  var vlc = vlcplayer;
  vlc.audio.mute = !vlc.audio.mute;
}


function AddRow(table,name,request,tracks)
{
  var row = document.createElement("tr");

    var col = document.createElement("td");
    var txt = document.createTextNode(name);
    col.appendChild(txt);
    row.appendChild(col);

    col = document.createElement("td");
    if( RTSPLink )
    {
      var t = "rtsp://"+host+"/"+request;
      var a = document.createElement("a");
      a.setAttribute("href",t);
      a.appendChild(document.createTextNode("Play"));
      col.appendChild(a);
    }
    else
    {
      var form = document.createElement("form");
      form.setAttribute("action", "");

      var field = document.createElement("input");
      field.setAttribute("type", "button");
      field.setAttribute("value", "Play");
      //field.setAttribute("onclick", "Play("+name+","+request+")");
      //field.onclick = "Play("+name+","+request+")";
      field.PlayName = name;
      field.PlayRequest = request;
      field.PlayTracks = tracks;
      field.addEventListener("click",PlayEvent);
      form.appendChild(field);

      col.appendChild(form);
    }
    row.appendChild(col);

  table.appendChild(row);
}

// function TitleCompare(a,b)
// {
//   if( a.Title.toUpperCase() < b.Title.toUpperCase() ) return -1;
//   if( a.Title.toUpperCase() > b.Title.toUpperCase() ) return 1;
//   return 0;
// }

var xmlhttp = new XMLHttpRequest();
var url = "/channellist.lua?select=json";

xmlhttp.onreadystatechange=function()
{
    if (xmlhttp.readyState == 4 )
    {
      if( xmlhttp.status == 200 )
         myFunction(xmlhttp.responseText);
    }
}

var hasCable = false;
var hasSat = false;
var hasTer = false;
var LastSourceName = "";
var LastChannelName = "";

function myFunction(response) {
   var ChannelList = JSON.parse(response);

   var LastSource = 0;
   var Index = 0;

   if(typeof(Storage) !== "undefined")
   {
      try
      {
         LastSourceName = localStorage.getItem("LastSourceName");
         LastChannelName = localStorage.getItem("LastChannelName");
         if( !LastSourceName ) LastSourceName = "";
         if( !LastChannelName ) LastChannelName = "";
      }
      catch(e)
      {
      }
   }

   document.Source.Select.length = 0;
   for(var i = 0; i < ChannelList.GroupList.length; i++)
   {
      var newChannelList = new Array();
      for(var j = 0; j < ChannelList.GroupList[i].ChannelList.length; j++ )
      {
         if( ( hasSat   && ChannelList.GroupList[i].ChannelList[j].Request.indexOf("dvbs") > 0 ) ||
             ( hasCable && ChannelList.GroupList[i].ChannelList[j].Request.indexOf("dvbc") > 0 ) ||
             ( hasTer   && ChannelList.GroupList[i].ChannelList[j].Request.indexOf("dvbt") > 0 ) )
         {
            newChannelList.push(ChannelList.GroupList[i].ChannelList[j]);
         }

      }
      if( newChannelList.length > 0 )
      {
         SourceList[Index] = new Object();
         SourceList[Index].ChannelList = newChannelList;
         SourceList[Index].Title = ChannelList.GroupList[i].Title;
         document.Source.Select.options[Index] = new Option(SourceList[Index].Title,Index, false, true);

         if( LastSourceName.length > 0 )
         {
            if( LastSourceName == SourceList[Index].Title ) LastSource = Index;
         }
         Index = Index + 1;
      }
   }
   // for(var i = 0; i < SourceList.length; i++ )
   // {
   //    SourceList[i].ChannelList.sort(TitleCompare);
   // }
   document.Source.Select.selectedIndex = LastSource;
   SetSource(LastSource);
}

function OnLoad()
{

  if( !RTSPLink )
  {
    document.getElementById("Play").style.display = "none";
  }
  if( VLCPluginValid || RTSPLink )
  {
    for(var i = 0; i < Octoserve.TunerList.length; i++ )
    {
      if( Octoserve.TunerList[i] )
      {
        if( Octoserve.TunerList[i].Desc.indexOf("C/T") >= 0 ) hasCable = true;
        if( Octoserve.TunerList[i].Desc.indexOf("C/C2") >= 0 ) hasCable = true;
        if( Octoserve.TunerList[i].Desc.indexOf("S/S2") >= 0 ) hasSat = true;
      }
    }
    // TODO: differentiate between dvbc and dvbt
    hasTer = hasCable;

    xmlhttp.open("GET", url, true);
    xmlhttp.send();

  }
  else
  {
    document.getElementById("channeltable").style.display = "none";
  }
}

function CreatePlugin()
{
  document.getElementById("ControlPlugin").style.display = "none";
  if( browserType == "Netscape" )
  {
    if( RTSPLink )
    {
        document.write('NOTE: Requires VLC App or similar with RTSP support');
        return;
    }
    else if( VLCPlugin && !VLCPluginValid )
    {
      document.write("Invalid VLC Plugin found: "+ VLCPlugin.version + "<br/>" + browserPlatform + "<br/>");
      document.write('Check for new Version on <a href="http://www.videolan.org">VLC Mediaplayer</a>');
      return;
    }

    if( VLCPluginValid )
    {
      document.getElementById("ControlPlugin").style.display = "block";
      document.write('<embed type="application/x-vlc-plugin" pluginspage="http://www.videolan.org" width="640" height="480" id="vlcplayer">');
      document.write('</embed>');
    }
    else
    {
      if ( browserPlatform == "win" || browserPlatform == "linux" )
      {
        document.write('Install <a href="http://www.videolan.org">VLC Mediaplayer</a> for in Browser TV');
      }
    }
  }
  else if (browserType == "MSIE" )
  {
      document.write('Install <a href="http://www.mozilla.org">Firefox</a> and <a href="http://www.videolan.org">VLC Mediaplayer</a>  for in Browser TV:&nbsp');
    return;
  }
}

function SetSource(SourceIndex)
{
  SelectedSource = SourceIndex;
  if( SourceList[SourceIndex].Title != LastSourceName )
  {
     LastSourceName = SourceList[SourceIndex].Title;
     LastChannelName = "";
     if(typeof(Storage) !== "undefined")
     {
        try
        {
            localStorage.setItem("LastSourceName",LastSourceName);
        }
        catch(e)
        {
        }
     }
  }

  document.Channel.Select.length = 0;
  var i = -1;
  for( j = 0; j < SourceList[SourceIndex].ChannelList.length; j++ )
  {
    document.Channel.Select.options[j] = new Option(SourceList[SourceIndex].ChannelList[j].Title,j, false, true);
    if( i < 0 )
    {
      if( LastChannelName.length > 0 )
      {
         if( SourceList[SourceIndex].ChannelList[j].Title == LastChannelName ) i = j;
      }
      else
      {
         if( SourceList[SourceIndex].ChannelList[j].Title.toUpperCase() >= "DAS ERSTE" ) i = j;
      }
    }


  }
  if( i < 0 ) i = 0;
  document.Channel.Select.selectedIndex = i;
  SetChannel(i);
}

function SetChannel(ChannelIndex)
{
  SelectedChannel = ChannelIndex;
  if( SourceList[SelectedSource].ChannelList[ChannelIndex].Title != LastChannelName )
  {
     LastChannelName = SourceList[SelectedSource].ChannelList[ChannelIndex].Title;
     if(typeof(Storage) !== "undefined")
     {
        try
        {
            localStorage.setItem("LastChannelName",LastChannelName);
        }
        catch(e)
        {
        }
     }
  }

  var a = document.getElementById("Play");
  if( RTSPLink )
  {
    var t = "rtsp://"+host+"/"+SourceList[SelectedSource].ChannelList[ChannelIndex].Request;
    a.href = t;
  }
  else
  {
    var t = "rtsp://"+host+"/"+SourceList[SelectedSource].ChannelList[ChannelIndex].Request;
    var vlc = vlcplayer;
    var options = new Array();
    vlc.playlist.clear();
    var id = vlc.playlist.add(t, SourceList[SelectedSource].ChannelList[ChannelIndex].Title, options);
    vlc.playlist.playItem(id);
    CurTracks = SourceList[SelectedSource].ChannelList[ChannelIndex].Tracks;

    document.getElementById("nowplaying").firstChild.nodeValue  = SourceList[SelectedSource].ChannelList[ChannelIndex].Title;
    document.getElementById("ac0").disabled  = CurTracks.length < 1;
    document.getElementById("ac1").disabled  = CurTracks.length < 2;
    document.getElementById("ac2").disabled  = CurTracks.length < 3;
    document.getElementById("ac3").disabled  = CurTracks.length < 4;
  }

}

</script>
</head>


<body onload="OnLoad()">


<table class="maintable" align="center">
  <colgroup>
    <col width="182px"/>
    <col width="728px"/>
  </colgroup>
  <tr><td class="maintd" colspan="2">
    <a href="http://www.digitaldevices.de"><img src="/BannerDD.jpg" alt="DD" width="910" height="130" /></a>
  </td></tr>
  <tr><td class="maintd" colspan="2">&nbsp;</td></tr>
  <tr>
    <td class="maintd"><script type="text/javascript">CreateMenu();</script></td>
    <td class="content">
      <div>
        <!-- Begin Content -->
        <table cellpadding="2px" align="center">
          <tr><td>
            <div id="ControlPlugin">
              <table width="100%">
                <tr>
                  <td><div align="left" id="nowplaying">&nbsp;</div></td>
                  <td><div align="right">
                    <form action="">
                      Audio:&nbsp;
                      <input id="ac0" type="Button" Value="1" onclick="SetAudioChannel(0)"/>
                      <input id="ac1" type="Button" Value="2" onclick="SetAudioChannel(1)"/>
                      <input id="ac2" type="Button" Value="3" onclick="SetAudioChannel(2)"/>
                      <input id="ac3" type="Button" Value="4" onclick="SetAudioChannel(3)"/>
                      <input type="Button" Value="Mute" onclick="MuteAudio()"/>
                    </form>
                  </div></td>
                </tr>
              </table>
            </div>
          </td></tr>


          <tr><td>
            <script type="text/javascript">CreatePlugin();</script></td>
          </td></tr>
          <tr><td>
            <div id="channeltable">
              <table cellpadding="2px" align="center">
                <tr>
                  <td>Source</td>
                  <td>
                    <form name="Source" action="">
                      <select name="Select" size="1" style="width: 300px" onchange="SetSource(document.Source.Select.selectedIndex)" >
                      </select>
                    </form>
                  </td>
                </tr>

                <tr>
                  <td>Channel</td>
                  <td>
                    <form name="Channel" action="">
                      <select name="Select" size="1"  style="width: 300px" onchange="SetChannel(document.Channel.Select.selectedIndex)">
                      </select>
                    </form>
                  </td>
                </tr>
              </table>
            </div>
          </td></tr>
          <tr><td>
            <div id="playlink" align="center" >
              <a id="Play" href="">Play</a>
            </div>
          </td></tr>
        </table>

        <!-- End Content -->
      </div>
    </td>
  </tr>
  <tr><td colspan="2">&nbsp;</td></tr>
</table>

</body>
</html>