/****************************************************************************\
 * cucco.js - A.K.A. "Sam the Cucco"                                        *
 * © 2005 David Johnson, tml@mudora.com                                     *
 * ZeldaBlog, http://www.zeldablog.com                                      *
 * Please do not use without explicit permission.  This copyright notice    *
 * should accompany this script at all times.  This file may be edited in   *
 * order to accomodate other layouts; all derived scripts, however, remain  *
 * the copyright of ZeldaBlog.                                              *
\****************************************************************************/

var IE = false;
var NS = false;
var FF = false;
var halfWidth = 0;
var timeoutID = null;

if(document.all) IE = true;
else if(document.layers) NS = true;
else FF = true;

var chicken = new Object();
chicken = {dir: 0, x: 0, y: 0};
var chickens = new Array;
for(a = 0; a < 5; a++) {
  chickens[a] = new Object();
  chickens[a] = {x: 125 * (a + 1), y: 0};
}
var hits = 0;
var it = 0;

function moveCucco() {
  if(chicken.x < halfWidth - 323) chicken.x = halfWidth - 323;
  else if(chicken.x > halfWidth + 308) chicken.x = halfWidth + 308;
  if(chicken.y < 66) chicken.y = 66;
  else if(chicken.y > 264) chicken.y = 264;

  if(IE) {
    cucco.style.left = chicken.x + 'px';
    cucco.style.top = chicken.y + 'px';
  } else if(NS) {
    document.cucco.left = chicken.x + 'px';
    document.cucco.top = chicken.y + 'px';
  } else if(FF) {
    document.getElementById("cucco").style.left = chicken.x + 'px';
    document.getElementById("cucco").style.top = chicken.y + 'px';
  }
}

function moveAttackCucco(a) {
  if(chickens[a].x < halfWidth - 323) chickens[a].x = halfWidth - 323;
  else if(chickens[a].x > halfWidth + 308) chickens[a].x = halfWidth + 308;
  if(chickens[a].y < 66) chickens[a].y = 66;
  else if(chickens[a].y > 264) chickens[a].y = 264;

  if(IE) {
    (eval('panic' + a)).style.left = chickens[a].x + 'px';
    (eval('panic' + a)).style.top = chickens[a].y + 'px';
  } else if(NS) {
    (eval('document.panic' + a)).left = chickens[a].x + 'px';
    (eval('document.panic' + a)).top = chickens[a].y + 'px';
  } else if(FF) {
    document.getElementById('panic' + a).style.left = chickens[a].x + 'px';
    document.getElementById('panic' + a).style.top = chickens[a].y + 'px';
  }
}

var flyL = new Image();
var flyR = new Image();
var landL = new Image();
var landR = new Image();
flyL.src = 'http://botu.northcastle.co.uk/Forums/Codes/ZB/cuccoul.gif';
flyR.src = 'http://botu.northcastle.co.uk/Forums/Codes/ZB//cuccour.gif';
landL.src = 'http://botu.northcastle.co.uk/Forums/Codes/ZB//cuccodl.gif';
landR.src = 'http://botu.northcastle.co.uk/Forums/Codes/ZB//cuccodr.gif';


function resizeWindow() {
  if(IE || NS) self.location.reload();
  else halfWidth = chicken.x = window.innerWidth / 2 - 19;
}

function initialiseCucco() {
  if(IE) window.onresize = resizeWindow;
  else if(FF) window.onresize = resizeWindow;
  else  {
    window.captureEvents(Event.RESIZE);
    window.onresize = resizeWindow;
  }

  chicken.dir = Math.random() * 2 * Math.PI;
  chicken.y = 210;
  if(IE) halfWidth = chicken.x = document.body.clientWidth / 2 - 19;
  else halfWidth = chicken.x = window.innerWidth / 2 - 19;
  moveCucco();
  timeoutID = setTimeout('beginCuccoMove()', 4000);
}

function beginCuccoMove() {
  it = 0;
  chicken.dir += Math.random() * 2 * Math.PI;
  if(chicken.dir > 2 * Math.PI) chicken.dir -= 2 * Math.PI;
  timeoutID = setTimeout('continueCuccoMove()', 0);
}

function continueCuccoMove() {
  it++;
  chicken.x += 5 * Math.sin(chicken.dir);
  chicken.y += 5 * Math.cos(chicken.dir);
  if(chicken.dir < Math.PI)
    document.images['chick'].src = eval((it % 2) ? 'landR.src' : 'flyR.src');
  else document.images['chick'].src = eval((it % 2) ? 'landL.src' : 'flyL.src');
  moveCucco();
  if(it == 13) timeoutID = setTimeout('beginCuccoMove()', 4000);
  else timeoutID = setTimeout('continueCuccoMove()', 100);
}

function grabMouse(e) {
  if(IE) {
    chicken.mousex = window.event.x - 16;
    chicken.mousey = window.event.y - 19;
  } else if(NS) {
    chicken.mousex = e.screenX - 16;
    chicken.mousey = e.screenY - 19;
  } else if(FF) {
    chicken.mousex = e.clientX - 16;
    chicken.mousey = e.clientY - 19;
  }
}

function panic() {
  clearTimeout(timeoutID);
  hits++;
  chicken.mousex = chicken.x;
  chicken.mousey = chicken.y;
  if(IE) document.onmousemove = grabMouse;
  else if(FF) window.onmousemove = grabMouse;
  else  {
    window.captureEvents(Event.MOUSEMOVE);
    window.onMouseMove = grabMouse;
  }
  timeoutID = setTimeout('panicMode()', 0);
}

function panicMode() {
  it = !it;
  chicken.dir = Math.atan2(chicken.x - chicken.mousex, chicken.y - chicken.mousey);
  if(chicken.x == chicken.mousex && chicken.y == chicken.mousey)
    chicken.dir = Math.random() * 2 * Math.PI;
  if(chicken.dir < 0) chicken.dir += 2 * Math.PI;
  if(chicken.dir < Math.PI)
    document.images['chick'].src = eval((it % 2) ? 'landR.src' : 'flyR.src');
  else document.images['chick'].src = eval((it % 2) ? 'landL.src' : 'flyL.src');
  chicken.x += 5 * Math.sin(chicken.dir);
  chicken.y += 5 * Math.cos(chicken.dir);

  moveCucco(chicken);
  if(hits >= 3) cuccoAttack();
  timeoutID = setTimeout('panicMode()', 50);
}

function cuccoAttack() {
  for(a = 0; a <= 4; a++) {
    var centerx = chicken.mousex + 20 * Math.round(.6 * (a - 2));
    var centery = chicken.mousey + 20 * Math.round(a % 2 * 2 - 1) * (a == 2 ? 0 : 1);
    var dir = Math.atan2(centerx - chickens[a].x, centery - chickens[a].y);
    if(chickens[a].x == chicken.mousex && chickens[a].y == chicken.mousey)
      dir = Math.random() * 2 * Math.PI;
    if(dir < 0) dir += 2 * Math.PI;
    if(dir < Math.PI)
      document.images['chick' + a].src = eval((it % 2) ? 'landR.src' : 'flyR.src');
    else document.images['chick' + a].src = eval((it % 2) ? 'landL.src' : 'flyL.src');
    chickens[a].x += 5 * Math.sin(dir);
    chickens[a].y += 5 * Math.cos(dir);
    moveAttackCucco(a);
  }
}