// Ed the Rubber Chicken: Web Attack V1.0.1202
// -------------------------------------------
// A quick and dirty (and amusing) experiment
//
// Code, images and sound by Scott Schiller
// www.schillmania.com | www.scottschiller.com

var index = 0;
var i = 0;
var chicken = null;
var chickenHead = null;
var redThing = null;
var counter = 0;
var clientWidth = 0;
var clientHeight = 0;
var spinCounter = 0;
var winX=0;
var winY=0;
var pan = 0;
var vol = 100;
var cHeadArray = [-5,-4,-3,-2,-1,0];
for (i=0; i<16; i++) {
  cHeadArray[5+i]=i*2;
}
var slide = [1,1,2,3,4,5,5,4,3,2,1,1];
var slide2 = [1,2,3,4,5,6,7,8,9,10,9,8,7,6,5,4,3,2,1];
var imgs = [];
var throwSound = '';
var moveDist = 0;
var delay = navigator.appVersion.toLowerCase().indexOf('windows nt')>=0?20:2;
for (i=0; i<36; i++) {
  imgs[i] = new Image();
  imgs[i].src = 'http://www.schillmania.com/rcd_web/images/'+i+'.gif';
}
var rotateLimit=imgs.length-1;

for (j=1; j<4; j++) {
  imgs[++i] = new Image();
  imgs[i].src = 'http://www.schillmania.com/rcd_web/images/'+j+'_1.gif';
}

function pI(n) { return parseInt(n); }
function vis(o) { return (eval(o)?eval(o).style.visibility:''); }
function rnd(n) { return pI(Math.random()*n); }

function init() {
  document.getElementById('audioContainer').innerHTML = '<object classid = "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase = "http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0" id = "audio" width="1" height="1"><PARAM NAME=movie VALUE="http://www.schillmania.com/rcd_web/sound.swf"> <PARAM NAME=quality VALUE=high> <PARAM NAME=bgcolor VALUE=#FFFFFF>';
  chicken = document.getElementById('rc');
  chickenHead = document.getElementById('ch');
  redThing = document.getElementById('rt');
  // initialize properties
  chicken.direction=0;
  chicken.spinDirection=0;
  chicken.vX=0;
  chicken.vY=0;
  getClientProperties();
}

function getClientProperties()  {
  clientWidth = pI(document.body.clientWidth);
  clientHeight = pI(document.body.clientHeight);
  doPositioning()
}

function doPositioning() {
  redThing.style.top = clientHeight;
  chickenHead.style.top = pI(clientHeight)-(chickenHead.height-16);

  if (vis('redThing') == 'visible') {
    redThing.style.top = pI(redThing.style.top) - redThing.height;
  }
  else
    redThing.style.left=32;
  if (vis('chickenHead') != 'hidden' && (chickenBehaviorIndex == 0 || chickenBehaviorIndex >= 6)) {
    chickenHead.style.visibility = 'hidden';
    redThing.style.visibility = 'hidden';
  }
  else if (chickenBehaviorIndex>2) {
    chickenHead.style.top = pI(clientHeight)-(chickenHead.height-16);
    redThing.style.visibility = 'hidden';
  }
  else if (vis('chickenHead') != 'visible') {
    chickenHead.style.top = pI(clientHeight)-(chickenHead.height-16);
    chickenHead.style.left= 8;
  }
}
  
function rotate() {
  chicken.src = 'http://www.schillmania.com/rcd_web/images/'+index+'.gif';
  index+=chicken.spinDirection;
  if (index>=rotateLimit && chicken.spinDirection>0)
    index=0;
  else if (index<=0 && chicken.spinDirection<0)
    index=rotateLimit;
}

function oneOrMinusOne() {
  return (rnd(2)==1?1:-1);
}

function throwChicken(direction) {
  chicken.direction = rnd(3);
  if (!chicken.direction) chicken.direction=-1;
  chicken.spinDirection = oneOrMinusOne();
  chicken.vX = rnd(24)+32;

  chicken.vY = oneOrMinusOne()==-1?rnd(16)*-1:1;
  spinCounter = rnd(3);

  if (chicken.direction == -1) {
    chicken.vX *= -1;
    chicken.style.left=pI(clientWidth)+200;
  }
  else if (chicken.direction==1) chicken.style.left=-200;
  else chicken.style.left=rnd((clientWidth-200));
 
  if (chicken.direction != 2)
    chicken.style.top=rnd(clientHeight/2);
  else {
    chicken.style.top = clientHeight + 8
    chicken.vY = (rnd(8)+16)*-1;
  }

  chicken.style.visibility='visible'
  vol=100;

  throwSound='throw'+rnd(4);

  playFrame(throwSound,'v100');
  playFrame(throwSound,'p'+(chicken.direction==1?'_':'')+'100');
  playSound(throwSound);
  moveChicken();
}

function moveChicken() {
  var cLeft = pI(chicken.style.left);
  var cTop = pI(chicken.style.top);
  var cNewLeft = pI(cLeft+(chicken.vX));
  var cNewTop = pI(cTop+chicken.vY);

  if (cNewLeft<1 && chicken.vX<0) {
    chicken.style.left = 1;
    bounce(-1);
  }
  else if (cNewLeft >= (clientWidth-chicken.width) && chicken.vX>0) {
    chicken.style.left = pI(clientWidth-chicken.width);
    bounce(1);
  }
  else {
    chicken.style.left = cNewLeft;
  }
  if ((cNewTop >= clientHeight && chicken.vY<0) || cNewTop < clientHeight ) {
    chicken.style.top = cNewTop;
    counter++;
    if (counter>=spinCounter) {
      counter=0;
      rotate();
    }
  }
  else if (vis('chicken') != 'hidden') {
    chicken.style.visibility = 'hidden';
  }

  chicken.vX *= 0.99;
  if (chicken.vY < 1)
    chicken.vY += 0.3;
  else
    chicken.vY *= 1.05;

  // calculate position on screen, audio etc.
  pan = pI(Math.ceil(pI(chicken.style.left))/(clientWidth-200)*100);
  pan = -100 + (pan*2)
  if (pan<-100) pan = -100;
  if (pan>100) pan = 100;

  var tmpPan = String(Math.abs(pan));
  var tmp = '';

  for (i=0; i<tmpPan.length-1; i++) { // parse out flash label lower
    tmp = tmp + tmpPan.charAt(i);
  }  
  tmp = tmp + '0';

  playFrame(throwSound,'p'+(pan<0?'_':'')+tmp);
    
  if (vis('chicken') != 'hidden')
    setTimeout("moveChicken()",20);
  else {
    // play falling sound, drop volume for 1 second..
    fadeOutChicken();
    setTimeout("killChicken()",2000);
  }
}

function fadeOutChicken() {
  vol-=10;
  playFrame(throwSound,'v'+vol);
  if (vol >= 0) setTimeout("fadeOutChicken()",150);
}

function bounce(direction) {
  if (direction == -1) {
    playSound('bounce_left');
  }
  else {
    playSound('bounce_right');
  }
  chicken.vX *= -0.66;
  counter=rotateLimit; // so rotate is immediate after bounce
  chicken.spinDirection *= -1;
}

function killChicken() {
  i=0; // reset index for shakewindow()
  winX=5;
  winY=5;
  playSound('splat')
  shakeWindow();
  setTimeout("playSound('moan')",1500);
}

function shakeWindow() {
  window.moveBy(0,winX)
  window.moveBy(winX,0)
  window.moveBy(0,-winX)
  window.moveBy(-winX,0)
  i++;
  winX-=0.5;
  if (i<10) setTimeout("shakeWindow()",20);
}

function playSound(audioTarget) {
  if (audio) {
    if (audio.ReadyState == 4) {
      audio.TGotoLabel("/" + audioTarget, "start")
      audio.TPlay("/" + audioTarget)
    }
  }
}

function playFrame(audioTarget, label) {
  if (audio.ReadyState == 4) {
    audio.TGotoLabel("/"+audioTarget,label);
  }
}

var redThingFlag = 1;

function toggle() {
  if (vis('redThing') != 'visible')
    redThing.style.visibility = 'visible';
  if (redThing.style.pixelTop == clientHeight)
    redThingFlag = -1;
  else
    redThingFlag = 1;
  i = 0;
  toggleRedThing();
}

function toggleRedThing() {
  redThing.style.top = pI(redThing.style.top)+(slide[i]*redThingFlag);
  i++;
  if (i<slide.length)
    setTimeout("toggleRedThing()",20);
  else
    setTimeout("resumeChickenBehavior()",randomDelay());
}      

function initMove() {
  i=0;
  var dir = oneOrMinusOne();
  var dist= dir==1?clientWidth-redThing.style.pixelLeft-chickenHead.width:redThing.style.pixelLeft;
  moveDist= rnd(dist)* dir;
  if (vis('redThing') == 'hidden')
    redThing.style.visibility = 'visible';
  move();
}

function move() {
  redThing.style.left = pI(redThing.style.left)+pI(slide2[i]*0.01*moveDist)
  i++
  if (i<slide2.length) {
    setTimeout("move()",20);
    if (i%9==0) playRandomBGSound();
  }
  else {
    counter++;
    setTimeout("resumeChickenBehavior()",randomDelay());
  }
}

var chickenBehaviorLoops = 0;
var chickenMoveLoops = 0;
var chickenBehaviorIndex = 0;

function chickenInit() {
  if (!chickenBehaviorIndex) { // do not start if already active
    counter=0;
    chickenBehaviorLoops = rnd(5)+1;
    resumeChickenBehavior();  
  }
}

function resumeChickenBehavior() {
  if (chickenBehaviorIndex==0) {
    redThing.style.left = rnd(clientWidth-redThing.width);
    chickenBehaviorIndex++;
    toggle();
  }
  else if (chickenBehaviorIndex==1) {
    initMove();
    if (counter>=chickenBehaviorLoops) {
      counter=0;
      chickenBehaviorIndex++;
    }
  }
  else if (chickenBehaviorIndex==2) {
    chickenBehaviorIndex++;
    toggle();
  }
  else if (chickenBehaviorIndex==3) {
    playSound('hello'+rnd(6));
    chickenHead.style.left = redThing.style.left;
    chickenHead.style.top = clientHeight - (chickenHead.height-16)
    chickenHead.style.visibility = 'visible';
    chickenBehaviorIndex++;
    setTimeout("resumeChickenBehavior()",randomDelay()+1000);
  }
  else if (chickenBehaviorIndex==4) {
    doBlink();
    if (counter>=chickenBehaviorLoops) {
      counter=0;
      chickenBehaviorIndex++;
    }
  }
  else if (chickenBehaviorIndex==5) {
    index=0;
    buckOff();
  }
  else if (chickenBehaviorIndex==6) {
    throwChicken();
    chickenBehaviorIndex=0;
  }
}

function playRandomBGSound() {
  if (oneOrMinusOne()==1) {
    playSound('background'+rnd(3));
  }
}

function randomDelay() {
  return rnd(1000);
}

function doBlink() {
  var tmp=rnd(3)+1;
  chickenHead.src='http://www.schillmania.com/rcd_web/images/'+tmp+'_0.gif';
  counter++;
  if (counter<chickenBehaviorLoops+2)
    setTimeout("blink("+tmp+")",randomDelay());
  else
    setTimeout("resumeChickenBehavior()",randomDelay());
}
function blink(tmp) {
  chickenHead.src = 'http://www.schillmania.com/rcd_web/images/'+tmp+'_1.gif';
  playRandomBGSound();
  setTimeout("chickenHead.src='http://www.schillmania.com/rcd_web/images/"+tmp+(oneOrMinusOne()==1?'_1':'_0')+".gif';setTimeout('doBlink()',randomDelay())",rnd(250))
}

function buckOff() {
  chickenHead.style.top = pI(chickenHead.style.top) + cHeadArray[index];
  index++;
  if (index<cHeadArray.length)
    setTimeout("buckOff()",20);
  else {
    chickenBehaviorIndex++;
    setTimeout("resumeChickenBehavior()",randomDelay()+500);
  }
}

function playRandomChickenSound() {
  var sounds = Array('moan','splat','bounce_left','bounce_right','throw0','throw1','throw2','throw3','hello0','hello1','hello2','hello3','hello4','hello5','background0','background1','background2');
  var n = Math.floor(Math.random()*sounds.length);
  playSound(sounds[n]);
  if (sounds[n].indexOf('throw')>=0)
    playFrame(sounds[n],'v100');
}

window.onload = function() {init();chickenInit()}

