

	//initialize global variables  
	var bug = new Object;  //create an empy object to store the object to me moved (makes it global)
	var theTimer 
	var xspeed = 0; //starting speed of bug
	var yspeed = 0;
	var delayCounter = 0; //used to delay busg movement
	var debugvar = "";
	function moveBug() {
	
	//prevent bug from going to fast
	  if(xspeed>=10||xspeed<=-10){
	  	xspeed=0;
	  }
	  if(yspeed>=10||yspeed<=-10){
	  	yspeed=0;
	  }
	  delayCounter +=1;
	  if(delayCounter>100){ //wait before flying
	   //influence bug to stay on screen
	   debugvar = "bug should be flying randomly around";
		  if(parseInt(bug.style.left)>=800){
			xspeed += Math.floor(Math.random()*3)-2;
		  } else if(parseInt(bug.style.left)<=0){
			xspeed += Math.floor(Math.random()*3);
		  } else {
			xspeed += Math.floor(Math.random()*3)-1;
		  }
		  if(parseInt(bug.style.bottom)>=600){
			yspeed += Math.floor(Math.random()*3)-2;
		  } else if(parseInt(bug.style.bottom)<=0){
			yspeed += Math.floor(Math.random()*3);
		  } else {
			yspeed += Math.floor(Math.random()*3)-1;
		  }
		}else if(delayCounter>150) {
			debugvar = "bug should be shaking a bit";
		  //keep bug in same position 
			xspeed = Math.floor(Math.random()*3)-1;
			yspeed = Math.floor(Math.random()*3)-1;
	    } else { 
		 	//do nothing
			xspeed = xspeed;
			yspeed = yspeed;
			debugvar = "bug should not be moving - only wings flapping";
		}
	  //actually update position
	  bug.style.left = parseInt(bug.style.left) + xspeed + "px";  
	  bug.style.bottom = parseInt(bug.style.bottom) + yspeed + "px";  
	if (document.getElementById("debug")) { 
	document.getElementById("debug").innerHTML = 
	  "left = " + parseInt(bug.style.left) + 
	  " xspeed = " + xspeed + 
	 " bottom = " + parseInt(bug.style.bottom) + 
	  " yspeed = " + yspeed + 
	  "  " + debugvar;
	}
	}
	
	function getBug(){
		if (document.getElementById("bug")) { //if bug doesn't exsist don't bother
			bug = document.getElementById("bug"); // get the object to me moved (store in global)
			bug.style.left = '250px'; // set its initial position have to do this through js for movement to work
			bug.style.bottom = '345px'; // set its initial position have to do this through js for movement to work
			theTimer = setInterval("moveBug()", 20);  //call moveMe again and again and again every 20ms
		}
	}
	

	window.onload = getBug;

