// Rubber Chicken Attack v1.2 (beta)
// ------------------------------------------------
// a quick and dirty (and amusing) experiment
// code + images + sound by scott | schillmania.com
//
// rubber chicken deviation site:
// www.schillmania.com/rcd 

var rcPath = 'http://www.schillmania.com/rc/';

var rcPre = [new Image(),new Image()];
rcPre[0].src = rcPath + 'c0.gif';
rcPre[1].src = rcPath + 'c1.gif';

var ed; // It's Ed, the rubber chicken!
var rcReady = 0;
var rcSound;
var rcTimeout=0;
var rcWinX=4;
var rcI=0;
var rcSine=1+parseInt(Math.random()*16);
var rcNT=(navigator.appVersion.toLowerCase().indexOf('windows nt')>=0?1:0);

function rubberChicken() {
  this.init = function() {
    this.sine = Math.random()*1;
    this.active = 0;
    this.o.style.left = document.body.clientWidth - 69;
    this.o.style.top = document.body.scrollTop + 20 + parseInt(Math.random()*200);
    this.o.style.visibility = 'visible';
  }  
  this.fly = function() {
    if (!this.active)
      this.active = 1;
    this.sine += 0.06;
    this.o.style.top = parseInt(parseInt(this.o.style.top) +(rcSine*Math.sin(this.sine)));
    this.o.style.left = parseInt(this.o.style.left) - (rcNT?13:10);
    if (parseInt(this.o.style.left)+parseInt(this.o.width) > 35 && this.active == 1) {
      if (rcNT) setTimeout("ed.fly()",20); // winNT/2000/XP method
    }
    else {
      this.o.style.left = 0;
      this.active = 0;
      this.o.src = rcPath + 'c1.gif';
      rcPlaySound('hit',50);
      rcShakeWindow();
    }
  }
  this.isActive = function() {
    return this.active;
  }
  this.frame = 0;
  this.sine = parseInt(Math.random()*4);
  this.active = 0;
  this.o = document.getElementById('rc');
  this.o.width = rc.width;
  this.init();
  return this;
}

function rcShakeWindow() {
  try {
    window.moveBy(rcWinX,0);
    window.moveBy(-rcWinX,0);
    rcI++;
    rcWinX-=0.5;
    if (rcI<8) setTimeout("rcShakeWindow()",20);
  }catch(e) {
    // oh well
  }
}

function launchRubberChicken() {
  if (!ed) {
    ed = new rubberChicken();
    if (!ed.isActive()) {
      ed.init();
      var limit = document.body.clientWidth - 69;
      var timeout = 0;
      if (!rcNT) {
        for (var i=1; i<=limit+33; i+=10) {
          timeout += 20;
          setTimeout("ed.fly()",timeout);
        }
      }
      else ed.fly();
      rcPlaySound('throw'+(Math.random()>0.5?1:0),30);
    }
  }
}

function rubberChickenSetup() {
  // wait for flash
  rcSound = document.getElementById('rcAudio');
  if (rcSound) {
    if (rcSound.PercentLoaded() == 100 && !rcReady) {
      rcReady=1;
      setTimeout("launchRubberChicken()",parseInt(Math.random()*8000));
    }
    else if (!rcReady) {
      rcTimeout++;
      if (rcTimeout<60) setTimeout("rubberChickenSetup()",500);
    }
  }
}

function rcPlaySound(audioTarget, volume) {
  if (!volume && volume != 0) volume = -1;
  if (rcAudio.ReadyState == 4) {
    rcAudio.TGotoLabel("/" + audioTarget, "start");
    rcAudio.TPlay("/" + audioTarget);
    if (volume != -1) rcFrame(audioTarget,'v'+volume);
    else rcFrame(audioTarget,'v100');
  }
}

function rcFrame(audioTarget, label) {
  if (rcAudio.ReadyState == 4) rcAudio.TGotoLabel("/"+audioTarget,label);
}

function rubberChickenInit() {
  if (navigator.appVersion.indexOf('MSIE')<0 || navigator.appVersion.toLowerCase().indexOf('mac')>=0) return false;

  var rcContainer = document.getElementsByTagName('body').item(0);
  var rcEl = document.createElement('img');

  rcEl.id = 'rc';
  rcEl.src = rcPath + 'c0.gif';
  rcEl.onclick = Function("window.open('http://www.schillmania.com')");
  rcEl.title = 'Ed, the rubber chicken | schillmania.com';
  rcEl.style.position = 'absolute';
  rcEl.style.visibility = 'hidden';
  rcEl.style.cursor = 'hand';

  var rcS = document.createElement('div');
  rcS.id = 'rcAudioContainer';
  rcS.style.width = '1px';
  rcS.style.height = '1px';
  rcS.style.position = 'absolute';
  rcS.style.left = '-16px';
  rcS.style.top = '-16px';

  rcContainer.appendChild(rcEl);
  rcContainer.appendChild(rcS);

  rcAudioContainer.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="rcAudio" WIDTH="1" HEIGHT="1"><PARAM NAME=movie VALUE="'+rcPath+'rc.swf"> <PARAM NAME=quality VALUE=high> <PARAM NAME=bgcolor VALUE=#FFFFFF></OBJECT>'
  rubberChickenSetup();
}

var rcC = new Image();
rcC.src = 'http://v0.extreme-dm.com/0.gif?tag=rubberc&j=y&srw='+screen.width+'&srb='+screen.colorDepth+'&rs=41&l='+(document.referer?document.referer:document.location);

// window.onload = rubberChickenInit;
if (Math.random()>0.5) setTimeout('rubberChickenInit()',10000*Math.random());
