var originX = new Array(130, 229);
var originY = new Array(88, 88);
var radius = 10;
var pupilSize=30;

document.write('<div id="speak" class="overlay" style="white-space:nowrap;position:absolute;Z-INDEX:2;top:0px;left:0px;visibility:hidden">');
document.write('</div>');

document.write('<div id="teeth" style="position:absolute;Z-INDEX:1;top:0px;left:0px;visibility:hidden">');
document.write('<img src="teeth.gif">');
document.write('</div>');

document.write('<div id="blank0" style="position:absolute;Z-INDEX:1;top:0px;left:0px;visibility:hidden">');
document.write('<img src="blank.gif">');
document.write('</div>');

document.write('<div id="blank1" style="position:absolute;Z-INDEX:1;top:0px;left:0px;visibility:hidden">');
document.write('<img src="blank.gif">');
document.write('</div>');

document.write('<div id="pupil0" style="position:absolute;Z-INDEX:2;top:0px;left:0px;visibility:hidden">');
document.write('<img src="left.gif">');
document.write('</div>');

document.write('<div id="pupil1" style="position:absolute;Z-INDEX:2;top:0px;left:0px;visibility:hidden">');
document.write('<img src="right.gif">');
document.write('</div>');

function getAbsPos(oId, tl)
{
  var val = 0;
  for( var obj = oId;
       obj.nodeType != 9;
       obj = obj.parentNode )
  {
    if(obj.nodeName != "TR")
    {
      val += parseInt( ( tl == 'top' ) ? obj.offsetTop : obj.offsetLeft );
    }
  }
  return val;
}

function drawIt(mouseX, mouseY)
{
  var skull = document.getElementById("skull");
  var skullX = getAbsPos(skull, 'left');
  var skullY = getAbsPos(skull, 'top');

  for( var idx = 0; idx < 2; idx++)
  {
    var orgX = originX[idx] + skullX;
    var orgY = originY[idx] + skullY;
    var angle = 0;
    var pupil = document.getElementById("pupil" + idx);
    if(mouseX != orgX)
    {
      angle = Math.atan((mouseY - orgY)/(mouseX - orgX));
    }
    var adjust = (mouseX - orgX < 0 ? -1 : 1);
    pupil.style.left = orgX + adjust * radius * Math.cos(angle);
    pupil.style.top = orgY + adjust * radius * Math.sin(angle) - pupilSize;
    var blank = document.getElementById("blank" + idx);
    blank.style.left = originX[idx] + skullX -2;
    blank.style.top = originY[idx] + skullY - pupilSize;
  }

  var speak = document.getElementById("speak");
  var teeth = document.getElementById("teeth");
  speak.style.left = 180 + skullX;
  speak.style.top = 182 + skullY;
  teeth.style.left = 150 + skullX;
  teeth.style.top = 175 + skullY;
}

function mouseMove(evnt)
{
  var mouseX = evnt ? evnt.clientX : event.clientX;
  var mouseY = evnt ? evnt.clientY : event.clientY;
  drawIt(mouseX, mouseY);
}

var sayings = new Array(
"Hello",
"Are you still here?",
"Have you looked at the <a class='overlay' href='links.htm'>Links</a>?",
"Got any pictures for the <a class='overlay' href='gallery.htm'>gallery</a>?",
"Find out about <a class='overlay' href='opportunities.htm'>Events &amp; Courses</a>",
"What are we doing <a class='overlay' href='programme.htm'>this week</a>?",
"There is a <a class='overlay' href='http://www2.scouts.org.uk/explorers/'>National Explorer Scout Website</a>",
"Take a look at the <a class='overlay' target='_new' href='http://http://www.kisc.ch/index.php?uid=243&search=webcam'>Kandersteg Webcam</a>",
"Think of a number",
"Ging Gang Gooly Gooly Gooly Gooly",
"Haila! Haila Shaila",
"Shallywally Shallywally Shallywally Shallywally",
"Umpa Umpa Umpa Umpa",
"Campfire's Burning, Campfire's Burning",
"I like bananas, monkey nuts and grapes",
"That's why they call me TARZAN OF THE APES"
);

function MoveMouthSoon()
{
  var delay = 1000 * Math.random();
  setTimeout('MoveMouth()',delay);
}

function MoveMouth()
{
  var teeth = document.getElementById("teeth");
  if(teeth.style.visibility == "visible")
  {
    teeth.style.visibility = "hidden";
    var speak = document.getElementById("speak");
    if(speak.style.visibility == "visible")
    {
      MoveMouthSoon();
    }
  }
  else
  {
    teeth.style.visibility = "visible";
    MoveMouthSoon();
  }
}

function SayItSoon(duration)
{
  var delay = duration * 1000 * Math.random() +1000;
  setTimeout('SayIt()',delay);
}

function SayIt()
{
  var speak = document.getElementById("speak");
  if(speak.style.visibility == "visible")
  {
    speak.style.visibility = "hidden";
  }
  else
  {
    var select = Math.floor(Math.random() * sayings.length);
    speak.innerHTML = "<p class='overlay'>" + sayings[select] + "</p>";
    speak.style.visibility = "visible";
    MoveMouthSoon()
  }

  SayItSoon(15);
}

function shuffle()
{
  var shuffle = document.getElementById("shuffle");
  var tdref = new Array();
  for(var r=0; r < shuffle.rows.length; r++)
  {
    for(var c=0; c < shuffle.rows[r].cells.length; c++)
    {
      if(shuffle.rows[r].cells[c].id != "noshuffle")
      {
        tdref[tdref.length] = shuffle.rows[r].cells[c].innerHTML;
      }
    }
  }

  var select = Math.floor(Math.random() * 12);
  var h = 0;
  for( var r=0; r < shuffle.rows.length; r++)
  {
    for( var c=0; c < shuffle.rows[r].cells.length; c++)
    {
      if(shuffle.rows[r].cells[c].id != "noshuffle")
      {
        shuffle.rows[r].cells[c].innerHTML = tdref[(h + select) % tdref.length];
        h++;
      }
    }
  }
}

function Load()
{
  shuffle();
  drawIt(0, 0);
  for( var idx = 0; idx < 2; idx++)
  {
    var pupil = document.getElementById("pupil" + idx);
    pupil.style.visibility = "visible";
    var blank = document.getElementById("blank" + idx);
    blank.style.visibility = "visible";
  }
  document.onmousemove = mouseMove;

  SayItSoon(20);
}

window.onload = Load;

