
if(!document.all)document.captureEvents(Event.KEYDOWN);
document.onkeydown = handleKeyDown;

function handleKeyDown(e) {

  var nKeyCode = document.all?window.event.keyCode:e.keyCode;
  //document.getElementById("lnk").innerHTML="keyCode = " + nKeyCode + "<br>";


  switch(nKeyCode)
  {
    case 27:
      clearTimeout(ttt1);
      //window.blur();
      break;

    case 38:
      if (nMarked >0) 
        nMarked--;
      else 
        nMarked=objs.length-1;
      objs[nMarked].focus();
      break;

    case 40:
      if (nMarked < objs.length-1) 
        nMarked++;
      else 
        nMarked=0;
      objs[nMarked].focus();
      break;

    default:
      break;
  }
}


function launchStream(idx) { self.location.href=(hrefs[idx]); }
function getRandom() { return Math.round( Math.random()*(objs.length-1) ); }

function timer()
{
  if (blocked>0) return;

  if(running)
  {
    blocked=1;
    objs[curr].style.background='#227';
    while(lastr==r || r==curr) r=getRandom();
    objs[lastr].style.background='#F00';
    dd=new Date();
    d1 = dd.getTime();
    launchStream(lastr); //////
    setst();
    curr=lastr;
    lastr=r;
    blocked=0;
  }
}

function setst()
{
  if(!running) return;
  dd=new Date();
  d2 = Math.round( (interval  - (dd.getTime() - d1 )) / 1000 );
  if(d2<2)   t1 = setTimeout("timer()", 800);
  document.getElementById('lnk').innerHTML=' next: '+objs[r].innerHTML+' in '+d2+' s.';
}

function starttimer()
{
  if (blocked>0) return;

  if(running)
  {
    blocked=1;
    running=false;
    if(t1)
      clearTimeout(t1);

    while(lastr==r || r==curr ) r=getRandom();
    dd=new Date();
    d2 = interval  - (dd.getTime()-d1) ;
    lastr=r;
    running=true;
    blocked=0;
    //t1 = setTimeout("timer()", 800 );
    setst();
  }
  else
  {
    stoptimer();
    interval=1000;
    curr=getRandom();
    r=curr;
    lastr=curr;
    running=false;
    var dly=300;
    do { if ( ! (dly=prompt('Enter delay in seconds (>=15)', dly)) ) return; } while(dly<3)
    interval=dly*1000;
    self.document.links[objs.length*2].innerHTML="click to skip next (reload to stop)";
    dd=new Date();
    d1 = dd.getTime();
    t1 = setTimeout("timer()", 800);
    t2=setInterval("setst()", 1000);
    running=true;
  }
}

function stoptimer()
{
  running=false;
  if(t1)
    clearTimeout(t1);
  if(t2)
    clearInterval(t2);
}

var ttti=1;
function ttt()
{
  var now;
  var m, s;

  now=new Date();
  s=now.getSeconds();
  m=now.getMinutes();

  if(now.getHours()<22 && now.getHours()>3) return;

  ttti+= (Math.ceil(Math.random()-.6)+s%5);
  ttti=ttti>59? 1 : ttti;

  var tr=ttti%Math.sqrt((ttti^s)*s%5)*8+1;
  var tg=Math.exp(ttti/20);
  var tb=Math.pow(ttti,Math.E)%11;

  tg= s<1? tg*2 : tg;
  tb= m==0? 40 : tb;

  tr=isNaN(tr)? 50 : Math.abs(tr);
  tg=isNaN(tg)? 90 : Math.abs(tg);
  tb=isNaN(tb)? 60 : Math.abs(tb);
  tr=parseInt(tr%240+16);
  tg=parseInt(tg%240+20);
  tb=parseInt(tb%240+4);

  objs[ttti%objs.length].style.background='rgb('+tr+','+tg+','+tb+')';
  objs[(s*3+ttti)%objs.length].style.border='1px solid rgb('+parseInt((15^ttti&3))+','+parseInt((2^ttti))+','+parseInt(s^7)+')';
  objs[(s*2+ttti+11)%objs.length].style.border='1px solid rgb('+parseInt(Math.exp(ttti/30)+s)+','+parseInt((7^ttti))+','+parseInt(ttti^3)+')';

  ttt1 = setTimeout("ttt()", 40+ttti%s);
}


function WorldClock(){
  if(firstcall>0) { firstcall--; self.focus(); }

  var now=new Date();
  var ofst=now.getTimezoneOffset()/60;
  var secs=now.getSeconds();
  var sec=-1.57+Math.PI*secs/30;
  var mins=now.getMinutes();
  var min=-1.57+Math.PI*mins/30;
  var hr=(isitlocal)?now.getHours():(now.getHours() + parseInt(ofst)) + parseInt(zone);
  var hrs=-1.575+Math.PI*hr/6+Math.PI*parseInt(now.getMinutes())/360;
  if (hr < 0) hr+=24;
  if (hr > 23) hr-=24;
  ampm = (hr > 11)?"PM":"AM";
  var statusampm = ampm.toLowerCase();

  var hr2 = hr;
  if (hr2 == 0) hr2=12;
  (hr2 < 13)?hr2:hr2 %= 12;
  if (hr2<10) hr2="0"+hr2

  var finaltime=hr2+':'+((mins < 10)?"0"+mins:mins)+':'+((secs < 10)?"0"+secs:secs)+' '+statusampm;

  if (document.all)
    worldclock.innerHTML=finaltime
  else if (document.getElementById)
    document.getElementById("worldclock").innerHTML=finaltime
  else if (document.layers){
    document.worldclockns.document.worldclockns2.document.write(finaltime)
    document.worldclockns.document.worldclockns2.document.close()
  }

  setTimeout('WorldClock()',1000);
}

function updateclock(z){
  zone=z.options[z.selectedIndex].value;
  isitlocal=(z.options[0].selected)?true:false;
}
window.onload=onLoad;
