  var mycookie;
  var editmode;
  var now = new Date();
  var guessAge = now.getTime();

  // on click:     0:key   1:type  2:stretch 3:url
  // in cookie:    0:key   1:name  2:age  (10x)   and then ONE flag for AUTO-PRESETS. 1=on, 0=off


  function loadChannels() {
    editmode = 0;
    mycookie = getCookie('channellist');
    if (!mycookie) mycookie = loadDefaults();

    var cookiechannels = new Array;
    cookiechannels = mycookie.split(',');
    if (!cookiechannels[30]) { cookiechannels[30] = 1; mycookie = mycookie + ",1"; }

    // 10 presets * 3 parameters per preset = 30, but starting at 0 = 29.  30 = auto-presets flag.
    for (x=0; x <= 29; x=x+3) {
      document.all['CH'+x/3].innerHTML = "<A HREF=\"ch.html\" QQQ=\"" + cookiechannels[x] + "\">" + cookiechannels[x+1] + "</A>";
    }

    playLink();
  }


  // "Automatic Editing" on or off.  Use value 1 to set on, value 0 to set off. Using no value toggles between on or off.
  function autoEdit(val) {
    var cookiechannels = new Array;
    cookiechannels = mycookie.split(',');
    cookiechannels[30] = val;
    mycookie = cookiechannels.join(',');
    document.cookie = "channellist=" + mycookie + "; expires=Monday, 04-Apr-2010 05:00:00 GMT";
    return true;
  }


  function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) end = dc.length;
    return unescape(dc.substring(begin + prefix.length, end));
  }


  function Switch(event) {
    var thing;
    if (window.ieVersion >= 5.5) {
      var a = event.srcElement;
      while (a && (a.tagName != "A")) { a = a.children[0]; }
      if (a) { thing = a.getAttribute("QQQ"); }
      if (thing) { document.channels.bSwitch(thing, 1); }
    }
    if (window.firefoxVersion >= 1.0) {
      var a = event.target;
      while (a && (a.tagName != "A")) { a = a.childNodes[0]; }
      if (a) { thing = a.getAttribute("QQQ"); }
      if (thing) { window.channels.bSwitch(thing, 1); }
    }
    return false;
  }


  function playLink() {
    var dochref = window.document.location.href;
    var piece = dochref.lastIndexOf("?") + 1;
    if (piece) {
      var play = dochref.substring(piece);
      piece = play.lastIndexOf("=") + 1;
      if (piece) {
        var command = play.substring(piece, 0);
        if (command == "PLAY=") {
          var channel = play.substring(piece);
          window.channels.bSwitch(channel ,0);
        }
      }
    }
  }


  function ShowInfo(event) {
    var thing;
    if (window.ieVersion >= 5.5) {
      var a = event.srcElement;
      if (a.href) { thing = a.getAttribute("QQQ"); }
    }
    if (window.firefoxVersion >= 1.0) {
      var a = event.target;
      if (a.href) { thing = a.getAttribute("QQQ"); }
    }
    if (!thing) return true;

    if (thing == "bookmark") {
      if (window.ieVersion >= 5.5) {
        window.external.AddFavorite('index.html','See.TV - Your World TV Portal!');
      } else {
        alert('Please press "Ctrl-D to add See.TV to your Bookmarks!');
      }
      return false;
    } else {
      document.all['PlayerID'].innerHTML = '<IFRAME NAME="info" SRC="' + thing + '.html" WIDTH=480 HEIGHT=424 FRAMEBORDER=0 BORDERCOLOR=#000000 MARGINHEIGHT=0 MARGINWIDTH=0 SCROLLING=NO HSPACE=0 VSPACE=0></IFRAME>';
      if (thing == "edit") {
        chanForm.chanInfo.value = '';
        if (editmode == 0) {
          for (x=0; x <= 9; x=x+1) {
            document.all['AR' + x].style.cursor = 'pointer';
            document.all['AR' + x].style.backgroundImage = 'URL(arrow.gif)';
            document.all['AR' + x].innerHTML = '<A HREF="ch.html" QQQ="AR' + x + '"></A>';
            editmode = 1;
          }
        autoEdit(0);  // set Auto Edit Cookie OFF
        }
      } else {
        if (editmode == 1) {
          chanForm.chanInfo.value = '';
          for (x=0; x <= 9; x=x+1) {
            document.all['AR' + x].style.cursor = '';
            document.all['AR' + x].style.backgroundImage = 'URL(arrowgone.gif)';
            document.all['AR' + x].innerHTML = '';
            editmode = 0;
          }
        }
      }
      return false;
    }
  }


  function fullScreen() {
    var WMP7, realplayer, mediaplayer;
    if (window.navigator.appName != "Netscape") { WMP7 = new ActiveXObject('WMPlayer.OCX'); }

    if (document.getElementById("mPlayer")) { mediaplayer = document.getElementById("mPlayer"); }
    if (document.getElementById("rPlayer")) { realplayer = document.getElementById("rPlayer"); }

    if (mediaplayer) {
      if (!mediaplayer.playState) { alert('Mozilla FireFox doesn\'t support an external full-screen link.\nPlease right-click in the video-window and choose Zoom, Full Screen.'); }
      if (WMP7) {
        if (mediaplayer.playState == 3) { mediaplayer.fullScreen = 'true'; } else {  alert('You can only switch to fullscreen while a channel is playing.'); }
      } else { mediaplayer.DisplaySize = 3; }
    }
    if (realplayer) {
      realplayer.SetFullScreen();
    }
    if ((!mediaplayer) && (!realplayer)) { alert('You can only switch to fullscreen while a channel is playing.'); }

    return false;
  }


  function clearScreen() {
    document.all['PlayerID'].innerHTML = '<OBJECT ID="mPlayer" width=480 height=424 classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,0,02,902" type="application/x-oleobject"><PARAM NAME="URL" VALUE="testpattern.gif"><PARAM NAME="AutoStart" VALUE="true"><PARAM name="uiMode" value="full"><PARAM NAME="AnimationAtStart" VALUE="True"><PARAM NAME="stretchToFit" VALUE="True"><EMBED type="application/x-mplayer2" pluginspage="http://www.microsoft.com/Windows/MediaPlayer/" SRC="testpattern.gif" name="MediaPlayer" width=480 height=424 ShowStatusbar="true" ShowControls="true" stretchToFit="true" AutoStart=1></EMBED></OBJECT>';
  }


