//JavaScript DHTML toy: Cucarachas

//Developed by Tim High, thigh@2pg.com, 12 Jun 2000

//Upgraded by Fabio de Castro, fcastro@2pg.com Some day in October 2000 *

//http://www.cucaracha.com.br/

//

//A group of cockroaches wander about the screen.

//When clicked on, they "squish" and come back to life up to 1 minute later.

//

//Requires the function call "initCucarachas()" in the onLoad parameter of the <BODY> tag.

//

//The xbdhtml.js script file must be loaded before this script.

//Put this in the header:

//<script language="JavaScript" src="xbdhtml.js"></script>

//

//* Powered by Jah _\|/_





//////CONFIGURATION/////////



var numCucarachas = parseInt(Math.random() * 4)+4; //Number of Cockroaches on screen

var w = 100; //Width of the Cucaracha images

var h = 100	; //Height of the Cucaracha images

var startOnLoad = false; //Should there be roaches on the screen from the

                         //moment the page loads?

var ressurrectTimeMin = 30; //Minimum number of seconds until roach ressurrects itself.

var ressurrectTimeMax = 60; //Maximum number of seconds until roach ressurrects itself.



//////END CONFIGURATION/////////





var offx, offy;

var Cucarachas = new Array();

var Layers = new Array();

offx = w/2;

offy = h/2;





var isNav4, isIE4;

if (parseInt(navigator.appVersion.charAt(0)) >= 4) {

  isNav4 = (navigator.appName == "Netscape") ? true : false;

  isIE4 = (navigator.appName.indexOf("Microsoft") != -1) ? true : false;

}



if (isNav4 || isIE4) {

  cuca1 = new Image();

  cuca1.src = "puerta/cuca001.gif";



  cuca2 = new Image();

  cuca2.src = "puerta/cuca002.gif";



  cuca3 = new Image();

  cuca3.src = "puerta/cuca003.gif";



  cuca4 = new Image();

  cuca4.src = "puerta/cuca004.gif";



  cuca5 = new Image();

  cuca5.src = "puerta/cuca005.gif";



  cuca6 = new Image();

  cuca6.src = "puerta/cuca006.gif";



  cuca7 = new Image();

  cuca7.src = "puerta/cuca007.gif";



  cuca8 = new Image();

  cuca8.src = "puerta/cuca008.gif";



  cucaDead0 = new Image();

  cucaDead0.src = "puerta/CucaDead001.gif";



  cucaDead1 = new Image();

  cucaDead1.src = "puerta/CucaDead002.gif";



  cucaDead2 = new Image();

  cucaDead2.src = "puerta/CucaDead003.gif";

}





function initCucarachas() {

  if (isNav4 || isIE4) {

    for (i=0; i < numCucarachas; i++) {

      Layers[i] = getElt("cucarachaLayer" + i);

      if(Layers[i].captureEvents) {

        Layers[i].captureEvents(Event.MOUSEDOWN);

        Layers[i].onmousedown = killCucaracha;

        document["cucarachaImg" + i].onmousedown = killCucaracha;

      }else {

        Layers[i].onmousedown = killCucaracha;

      }

      if (startOnLoad) {

        Cucarachas[i].ressurrect();

      }else {

        Cucarachas[i].clean();

      }

    }

  }

}



function Cucaracha(num) {

  this.num = num;

  this.timeout = null;

  this.walk = walkCucaracha;

  this.swapImg = swapCucaracha;

  this.kill = killCucaracha;

  this.clean = cleanCucaracha;

  this.ressurrect = ressurrectCucaracha;

  //width & height just don't seem to be working for Netscape.

  //Make sure to include the width & height tags for the image.

  writeElt('cucarachaLayer' + this.num,'<img name="cucarachaImg' + this.num + '" src="cuca4.gif" border="0" width="' + w + '" height="' + h + '">',

           0, 0, 10, w, h);

  Cucarachas[this.num] = this;

}





function walkCucaracha () {

  var moveX, moveY;

  var currLeft = getEltLeft(Layers[this.num]);

  var currTop = getEltTop(Layers[this.num]);



  var action = Math.floor(Math.random() * 100);

  if (action < 20) {

    this.dir = ((this.dir-2) % 8) + 1;

    if (this.dir == 0) this.dir = 8;

    this.swapImg(eval("cuca" + this.dir));

  }else if (action < 40) {

    this.dir = (this.dir % 8) + 1;

    this.swapImg(eval("cuca" + this.dir));

  }else {

    switch (this.dir) {

      case 1:

        moveX = 0;

        moveY = Math.floor(Math.random() * (-20)) - 1;

        if ((currTop + moveY) < 0) moveY = -currTop;

        break;

      case 2:

        moveX = Math.floor(Math.random() * (10)) + 1;

        moveY = Math.floor(Math.random() * (-10)) - 1;

        if ((currLeft + moveX) > getCurrentWinWidth()) moveX = getCurrentWinWidth() - currLeft;

        if ((currTop + moveY) < 0) moveY = -currTop;

        break;

      case 3:

        moveX = Math.floor(Math.random() * (20)) + 1;

        moveY = 0;

        if ((currLeft + moveX) > getCurrentWinWidth()) moveX = getCurrentWinWidth() - currLeft;

        break;

      case 4:

        moveX = Math.floor(Math.random() * (10)) + 1;

        moveY = Math.floor(Math.random() * (10)) + 1;

        if ((currLeft + moveX) > getCurrentWinWidth()) moveX = getCurrentWinWidth() - currLeft;

        if ((currTop + moveY) > getCurrentWinHeight()) moveY = getCurrentWinWidth() - currTop;

        break;

      case 5:

        moveX = 0;

        moveY = Math.floor(Math.random() * (20)) + 1;

        if ((currTop + moveY) > getCurrentWinHeight()) moveY = getCurrentWinWidth() - currTop;

        break;

      case 6:

        moveX = Math.floor(Math.random() * (-10)) - 1;

        moveY = Math.floor(Math.random() * (10)) + 1;

        if ((currLeft + moveX) < 0) moveX = -currLeft;

        if ((currTop + moveY) > getCurrentWinHeight()) moveY = getCurrentWinWidth() - currTop;

        break;

      case 7:

        moveX = Math.floor(Math.random() * (-20)) - 1;

        moveY = 0;

        if ((currLeft + moveX) < 0) moveX = -currLeft;

        break;

      case 8:

        moveX = Math.floor(Math.random() * (-10)) - 1;

        moveY = Math.floor(Math.random() * (-10)) - 1;

        if ((currLeft + moveX) < 0) moveX = -currLeft;

        if ((currTop + moveY) < 0) moveY = -currTop;

        break;

      default:

        moveX = 0;

        moveY = 0;

        break;

    }

    

    moveEltTo(Layers[this.num], currLeft + moveX, currTop + moveY);

  }



  this.timeout = setTimeout("Cucarachas[" + this.num + "].walk();", 100);

}



function killCucaracha(e) {

  var cuc = null;

  var lay = null;

  var x, y, diffX, diffY;

  if(isNav4) {

    x = e.pageX;

    y = e.pageY;

  } else {

    x = window.event.clientX;

    y = window.event.clientY;

  }

  for (i=0; i < numCucarachas; i++) {

    lay = Layers[i];

    cuc = Cucarachas[i];

    diffX = x - getEltLeft(lay);

    diffY = y - getEltTop(lay);

    if ((diffX >= 0) && (diffX < w) && (diffY >= 0) && (diffY < h)) {

      break;

    }

  }

  if (cuc != null) {

    clearTimeout(cuc.timeout);

    // C.V --> altera o preco da carga em baixo

    cuc.swapImg(eval('cucaDead' + parseInt(Math.random() * 3)));

    cuc.timeout = setTimeout("Cucarachas[" + cuc.num + "].clean();", 5000);

  }

}



//function killCucaracha() {

//  clearTimeout(this.timeout);

//  this.swapImg(cucaDead);

//  this.timeout = setTimeout("Cucarachas[" + this.num + "].clean();", 5000);

//}

//

//function makeKillCucaracha(killNum) {

//  funcOut = "function killCucaracha() {\n";

//  funcOut += "  clearTimeout(Cucarachas[" + killNum + "].timeout);\n";

//  funcOut += "  Cucarachas[" + killNum + "].swapImg(cucaDead);\n";

//  funcOut += "  Cucarachas[" + killNum + "].timeout = setTimeout('Cucarachas[' + Cucarachas[" + killNum + "].num + '].clean();', 5000);\n";

//  funcOut += "}";

//  return new Function(funcOut);

//}



function cleanCucaracha() {

  setEltZIndex(Layers[this.num], -10);

  setEltVisibility(Layers[this.num],'hidden');

  this.timeout = setTimeout("Cucarachas[" + this.num + "].ressurrect();", Math.floor(Math.random()*((ressurrectTimeMax-ressurrectTimeMin)*1000)) + ressurrectTimeMin);

}



function ressurrectCucaracha() {

  moveEltTo(Layers[this.num], Math.floor(Math.random()*getCurrentWinWidth()), Math.floor(Math.random()*getCurrentWinHeight()));

  this.dir = Math.floor(Math.random()*8) + 1;

  this.swapImg(eval("cuca" + this.dir));

  setEltVisibility(Layers[this.num], "visible");

  setEltZIndex(Layers[this.num], 10);

  this.timeout = setTimeout("Cucarachas[" + this.num + "].walk();", 500);

}



function swapCucaracha(img) {

  document["cucarachaImg" + this.num].src = img.src;

}





/////////GENERATE Cucarachas/////////////

if (isNav4 || isIE4) {

  for (i=0; i < numCucarachas; i++) {

    new Cucaracha(i);

  }

}