bug.objects = new Array();
bug.z_moveCoords = new Array([0,-5],[1,-5],[2,-5],[3,-5],[4,-4],[5,-3],[5,-2],[5,-1],[5,0],[5,1],[5,2],[5,3],[4,4],[3,5],[2,5],[1,5],[0,5],[-1,5],[-2,5],[-3,5],[-4,4],[-5,3],[-5,2],[-5,1],[-5,0],[-5,-1],[-5,-2],[-5,-3],[-4,-4],[-3,-5],[-2,-5],[-1,-5]);
bug.numDirections = 32;var bugcount=0;
function bug(width, height, imageDir, isKillable){this.z_id = bug.objects.length;bug.objects[this.z_id] = this;
this.z_width = width;this.z_height = height;this.z_imageDir = imageDir;this.z_isKillable = isKillable;
this.z_image;this.z_moveDelay = 70;this.z_direction = 0;this.z_isActive;this.z_intervalId;
this.z_turnLeftChance;this.z_turnRightChance;this.z_turningBuffer = 0;this.z_currentTurn = 0;this.setTurnChance(15, 15);this.z_preloadImages();}
bug.prototype.generate = function(){var self = this;this.z_image = document.createElement("img");this.z_image.style.position = "absolute";this.z_image.style.zIndex = 100;this.start_x=400;this.start_y=200;this.z_image.style.left = this.start_x;this.z_image.style.top = this.start_y;
this.z_image.onclick = function(){self.z_onclick()};this.z_updateImage();this.z_setVisibility(false);document.body.appendChild(this.z_image);this.enable();bugcount++;}
bug.prototype.z_setVisibility = function(isVisible){this.z_image.style.visibility = isVisible ? "visible" : "hidden";}
bug.prototype.z_updateImage = function(){this.z_image.src = this.z_imageDir + this.z_direction + ".gif";}
bug.prototype.z_onclick = function(){if (this.z_isKillable){this.disable();this.z_image.src = this.z_imageDir + "splat.gif";this.z_isKillable=false;}}
bug.prototype.enable = function(){this.z_setVisibility(true);this.z_intervalId = setInterval("bug.objects[" + this.z_id + "].z_move()", this.z_moveDelay);}
bug.prototype.disable = function(){clearInterval(this.z_intervalId);bugcount--;}
bug.prototype.setTurnChance = function(leftChance, rightChance){this.z_turnLeftChance = leftChance;this.z_turnRightChance = rightChance;}
bug.prototype.z_move = function(){var newDirection = 0;
this.z_turningBuffer--;if (this.z_turningBuffer > 0){newDirection = this.z_currentTurn;}else{
if (Math.random() * 100 < this.z_turnLeftChance){newDirection--;}
if (Math.random() * 100 < this.z_turnRightChance){newDirection++;}}
if (newDirection != 0){if (this.z_turningBuffer < 0){this.z_turningBuffer = 3;this.z_currentTurn = newDirection;}this.z_direction += newDirection;
if (this.z_direction >= bug.numDirections){this.z_direction = -(bug.numDirections - this.z_direction);}else if (this.z_direction < 0){this.z_direction = (bug.numDirections + this.z_direction);}}this.z_updateImage();
this.z_image.style.left = parseInt(this.z_image.style.left) + bug.z_moveCoords[this.z_direction][0];this.z_image.style.top = parseInt(this.z_image.style.top) + bug.z_moveCoords[this.z_direction][1];
var main_dir=Math.atan2(parseInt(this.z_image.style.left)-this.start_x,parseInt(this.z_image.style.top)-this.start_y);var point_dir=Math.atan2(bug.z_moveCoords[this.z_direction][0],bug.z_moveCoords[this.z_direction][1]);var dist=Math.sqrt(Math.pow(parseInt(this.z_image.style.left)-this.start_x,2)+Math.pow(parseInt(this.z_image.style.top)-this.start_y,2));
var clockwise=false;if(main_dir != point_dir){if(main_dir>=0 && point_dir>=0 || main_dir<0 && point_dir<0 ){
if(main_dir>point_dir) clockwise=true;} else if(main_dir-point_dir<Math.PI && main_dir-point_dir>0 || main_dir+Math.PI*2-point_dir<Math.PI && main_dir+Math.PI*2-point_dir>0){clockwise=true;}
if(clockwise){this.setTurnChance(15, parseInt(15+dist/7));} else{this.setTurnChance(parseInt(15+dist/7), 15);}}
if (Math.random() * (10000*bugcount) < 7){newbug = new bug(44, 44, this.z_imageDir, true);newbug.generate()}}
bug.prototype.z_preloadImages = function(){var images = new Array();for (var i = 0; i < bug.numDirections; i++) {images[i] = new Image();images[i].src = this.z_imageDir + i + ".gif";}
	images[images.length] = new Image();images[images.length - 1].src = this.z_imageDir + "splat.gif";}
