 /*
 Bugzap! by ssjx (c)2008
 http://www.ssjx.co.uk
 */
 
        var bug,turret,score,shot,welcome,over;
        
        var currentscore=0,topscore=0;
        
        var speed = 8;
        var timer;
        
     
        var turretleft = 320;
        var bugx = 8;
        var bugy = 32; //32;
        var xdir = 1;
        
        var shota=0;
        var shotx =0;
        var shoty =0;
        var tx=0;
        
        var gamestate=0;
        
        function init()
	{
		//instantiate HTML object instance vars
		bug = document.getElementById('bug');
		turret = document.getElementById('turret');
		score = document.getElementById('score');
		shot = document.getElementById('shot');
		welcome = document.getElementById('welcome');
		over = document.getElementById('over');
		
		//register key listener with document object
		document.onkeydown = keyListener;
		document.onkeyup = keyup;
		
		//start the game loop 
		start();
        }
        
        function keyup(e)
	{
		if(!e){
		//for IE
		e = window.event;
		}
		
		
		if(e.keyCode==37 || e.keyCode==39)
		{
		tx=0;
		}
	
        }
        
        function keyListener(e){
            if(!e){
                //for IE
                e = window.event;
            }
            if(e.keyCode==37 && turretleft > 0){
                // keyCode 37 is left arrow
                tx=-8;
            }
            if(e.keyCode==39 && turretleft < 608){
                // keyCode 39 is right arrow
                tx=8;
            }
            
            if(e.keyCode== 90) //Z=90 //space=32
			{  
            	switch (gamestate)
            	{
            	case 0:
            		// if we are displaying title screen	  
			//reset everything
			currentscore=0
			 turretleft = 320;
			 turret.style.left = turretleft + 'px';
			 
		         bugx = 8;
		         bugy = 32; //32;
		         xdir = 1;
		        
			 shota=0;
			shot.style.display = 'none';
		
			render();
		
			//remove the box
			welcome.style.display = 'none';
			gamestate=1;
			
		break;
	
	   case 1:
	   	// we are playing the game

	   	
	   	
	            if(shota==0)
		    {
	                shota= 1; //shot active
	                shotx=turretleft;
			shoty=432;
			
	            	shot.style.top = shoty+'px';
	            	shot.style.left = shotx+'px';
	                shot.style.display = 'block';
	            }
            break;
            
            case 2:
            	//displaying game over
			over.style.display = 'none';
			welcome.style.display = 'block';
			
			if (currentscore>topscore)
			{
			topscore=currentscore;
			}
			
			score.innerHTML = 'High Score: ' + topscore;
			
			gamestate=0;
			
            break;
            
            }
            }
            //FYI - keyCode 38 is up arrow, keyCode 40 is down arrow
        }
        
        function start(){
            //game loop
            switch (gamestate)
            {
	    case 0:
	    	welcome.style.display = 'block';
	    break;
	    
	    case 1:
	    
	    	if (tx>0 && turretleft < 608)
	    	{
	    	turretleft = turretleft+tx;
                turret.style.left = turretleft + 'px'; 
	    	}
	    	
	    	if (tx<0 && turretleft >0)
	    	{
		turretleft = turretleft+tx;
                turret.style.left = turretleft + 'px'; 
		}
		    
		    
		detectCollisions();
		render();
		
		//end conditions
		if(bugy==432)
		{
		gamestate=2;
		}
	    break;
	    
	    case 2:
	    
		over.innerHTML="<h2>Game over!</h2>";
		
		over.innerHTML+="<p style='font-size:14pt;'>You scored "+currentscore+"</p>"; 
		
		if (currentscore>topscore)
		{
		over.innerHTML+="<p style='color:cyan; font-size:14pt;'>NEW HIGH SCORE!</p>";
		}
	
		over.innerHTML+="<p style='color:yellow; font-size:14pt;'><b>PRESS Z TO CONTINUE!</b></p>";
		
		over.style.display = 'block';

	    break;
	   	    
	    }

            
	    	timer = setTimeout('start()',40);
            
        }
        
        function detectCollisions()
	{
		if(bugx <= 0 || bugx >= 608)
		{
		xdir =xdir* -1;
		bugy+=16;
		}
        }
        
      
        function render()
	{
		moveshot();
		movebug();
		updateScore();
	}
        
        function moveshot()
        {
        	if (shota==1)
		{
			if (shoty<=16)
			{
				shota= 0; //shot active
			}
			
			//check for a hit
			for(i=0;i<32;i+=2)
			{
				if (shota==1)
				{
				if ((shotx+16)>bugx && (shotx+16)<(bugx+32) )
				{
					//these are inverted cos of upside down screen
					if ( (shoty+i)>bugy && (shoty+i)<(bugy+32) )
					{
					shota= 0; //shot active
					currentscore+=10;
					break;
					}		
				}
				}
			}
			
			if (shota==1)
			{
		       		//move the shot
		
				shot.style.left = shotx+'px';
				shot.style.top = shoty+'px';
				shoty-=speed;
				
			}
			
			if (shota==0)
			{
				shot.style.display = 'none';
			}
		
		}    
      
	}
        
        function movebug(){
            bugx += speed*xdir;
            bug.style.left = bugx+'px';
            bug.style.top = bugy+'px';
        }
        
        function updateScore(){
          //  currentScore += 5;
            score.innerHTML = 'Score: ' + currentscore ;
        }
        

