// JavaScript SpackMan
// Author: Thomas Ballard (thomas@esqsoft.com) and Andrew Ballard (andrew@esqsoft.com)
// Copyright 2002  E.S.Q. Software
// All Rights Reserved

/*
  Brief Introduction:
  My 7 year old Andrew was home sick; I was home due to employer ineptitude. 
  Andrew decided we should create a game kind of like a popular one from my childhood. 
  I introduced him to some game programming/development techniques, he
  drew up sprites on paper and we talked through how to tackle the different 
  components of the game we'd identified. We decided to build it as a web site game. (A concept I have been 
  working on for awhile.) We stayed up all night, well he made it through most of the night and caffine helped me 
  through the rest. After about 24 hours we had 90% of the project completed.  (Wow, I've seen people get paid 
  obscene money for taking weeks to create complete crap while we did a damn cool job, with 24 hours, 
  a temporarily unemployed developer and a 7 year old, oh yes, and just for the fun of it... "managers" take notice!) 
  For anyone keeping score... I hand coded everything from scratch, I deplore cut and paste hacks, go back to class!
  So, this exercize was a great break from the hell of post dream job life and an opportunity 
  for a seven year old to see that all adults aren't greedy and self serving.
*/

//**Start Encode**

//-- GLOBALS ----------------------------------------------------------------------------------------------
var activated=false;

var pman1=new Object(); mobj=pman1;
  mobj.x=0;
  mobj.xx=0;                    // horizontal accelerator
  mobj.y=0;
  mobj.yy=0;                    // vertical accelerator
  mobj.score=0;

  mobj.direction=4;             // 1=up, 2=right, 3=down, 4=left
  mobj.anim_cnt=0;
  mobj.filename=new Image(); mobj.filename.src='images/pman.gif';
  mobj.up=new Image(); mobj.up.src='images/pman_up.gif';
  mobj.down=new Image(); mobj.down.src='images/pman_down.gif';
  mobj.left=new Image(); mobj.left.src='images/pman_left.gif';
  mobj.right=new Image(); mobj.right.src='images/pman_right.gif';
  mobj.state  ='normal';        // normal=enemies can kill you, super=you kill enemies (super is temporary)
  mobj=null;                    // clean up
//-- end: pman object ----------------------------



var ghost1=new Object(); mobj=ghost1;
  mobj.x=0;
  mobj.xx=0;
  mobj.y=0;
  mobj.yy=0;

  mobj.direction=0;
  mobj.anim_cnt=0;
  mobj.filename=new Image(); mobj.filename.src='images/ghost1.gif';
  mobj.up=new Image(); mobj.up.src='images/ghost1_a.gif';
  mobj.down=new Image(); mobj.down.src='images/ghost1_a.gif';
  mobj.left=new Image(); mobj.left.src='images/ghost1_a.gif';
  mobj.right=new Image(); mobj.right.src='images/ghost1_a.gif';
  mobj.state  =5; // 1=happy, 2=mad, 3=scared, 4=dead, 5=circle, 6=escape (see notes.txt for explaination of emotional states)
  mobj.need_to_change_direction=false;
  mobj.time_to_change_emotion=10;
  mobj=null;
//-- end: ghost object ----------------------------



var ghost2=new Object(); mobj=ghost2;
  mobj.x=0;
  mobj.xx=0;
  mobj.y=0;
  mobj.yy=0;

  mobj.direction=1;
  mobj.anim_cnt=0;
  mobj.filename=new Image(); mobj.filename.src='images/ghost2.gif';
  mobj.up=new Image(); mobj.up.src='images/ghost2_a.gif';
  mobj.down=new Image(); mobj.down.src='images/ghost2_a.gif';
  mobj.left=new Image(); mobj.left.src='images/ghost2_a.gif';
  mobj.right=new Image(); mobj.right.src='images/ghost2_a.gif';
  mobj.state  =5; // 1=happy, 2=mad, 3=scared, 4=dead, 5=circle, 6=escape (see notes.txt for explaination of emotional states)
  mobj.need_to_change_direction=false;
  mobj.time_to_change_emotion=50;
  mobj=null;
//-- end: ghost object ----------------------------



var ghost3=new Object(); mobj=ghost3;
  mobj.x=0;
  mobj.xx=0;
  mobj.y=0;
  mobj.yy=0;

  mobj.direction=2;
  mobj.anim_cnt=0;
  mobj.filename=new Image(); mobj.filename.src='images/ghost3.gif';
  mobj.up=new Image(); mobj.up.src='images/ghost3_a.gif';
  mobj.down=new Image(); mobj.down.src='images/ghost3_a.gif';
  mobj.left=new Image(); mobj.left.src='images/ghost3_a.gif';
  mobj.right=new Image(); mobj.right.src='images/ghost3_a.gif';
  mobj.state  =5; // 1=happy, 2=mad, 3=scared, 4=dead, 5=circle, 6=escape (see notes.txt for explaination of emotional states)
  mobj.need_to_change_direction=false;
  mobj.time_to_change_emotion=50;
  mobj=null;
//-- end: ghost object ----------------------------



var ghost4=new Object(); mobj=ghost4;
  mobj.x=0;
  mobj.xx=0;
  mobj.y=0;
  mobj.yy=0;

  mobj.direction=3;
  mobj.anim_cnt=0;
  mobj.filename=new Image(); mobj.filename.src='images/ghost4.gif';
  mobj.up=new Image(); mobj.up.src='images/ghost4_a.gif';
  mobj.down=new Image(); mobj.down.src='images/ghost4_a.gif';
  mobj.left=new Image(); mobj.left.src='images/ghost4_a.gif';
  mobj.right=new Image(); mobj.right.src='images/ghost4_a.gif';
  mobj.state  =5; // 1=happy, 2=mad, 3=scared, 4=dead, 5=circle, 6=escape (see notes.txt for explaination of emotional states)
  mobj.need_to_change_direction=false;
  mobj.time_to_change_emotion=50;
  mobj=null;
//-- end: ghost object ----------------------------



var ghostpen=new Object(); mobj=ghostpen;
  mobj.x=0;                     // we need to know where the door to the ghost pen is for processing the ghost escape and dead states.
  mobj.y=0;
//-- end: ghost object ----------------------------



var game=new Object(); mobj=game;
  mobj.wavlist_location='sounds/';
  mobj.move_delay=100;          /* milliseconds between movement */
  mobj.move_timer=null;
  mobj.state='';
  mobj.anim_delay=100;          // milliseconds between animation frames
  mobj.anim_timer=null;
  mobj.update_timer=null;       // milliseconds between changes to the score/stats panel
  mobj.level=new Array();
  mobj.level[0]=1;
  mobj.dots_left=null;
  mobj.level[1]=new Array(
    '5AAAAAAAAAAAAAAAAAAAC',
    '6...................6',
    '6.5AF.5AF.4.9AC.9AC.6',
    '6Z6...6...6...6...6Z6',
    '6.1.9AE.9ABAF.7AF.1.6',
    '6...................6',
    '7AAC.4.9AF.9AF.4.5AAE',
    '   6.6.........6.6   ',
    '   6.6.5AAWAAC.6.6   ',
    '9AAE.1.6#   #6.1.7AAF',
    '    ...6     6...    ',
    '9AAC.4.6#   #6.4.5AAF',
    '   6.6.7AAAAAE.6.6   ',
    '   6.6.       .6.6   ',
    '5AAE.1.9AAAAAF.1.7AAC',
    '6.........@.........6',
    '6.9AA8AAF.4.9AA8AAF.6',
    '6....6....6....6....6',
    '6.9F.6.9F.6.9F.6.9F.6',
    '6Z...6....6....6...Z6',
    '6.9F.1.9F.6.9F.1.9F.6',
    '6.........6.........6',
    '7AAAAAAAAABAAAAAAAAAE'
  );
  mobj=null;
//-- end: game object ----------------------------



var board=new Object(); mobj=board;
  mobj.mwidth=25;  // cell width
  mobj.mheight=25; // cell height

  //PRECACHE THESE IMAGES!!! DONT RISK ABSOLUTELY PISSING OFF YOUR ISP AND WEB HOST!!!
  mobj.xA=new Image(); mobj.xA.src='images/A.gif';
  mobj.xB=new Image(); mobj.xB.src='images/B.gif';
  mobj.xC=new Image(); mobj.xC.src='images/C.gif';
  mobj.xE=new Image(); mobj.xE.src='images/E.gif';
  mobj.xF=new Image(); mobj.xF.src='images/F.gif';
  mobj.xW=new Image(); mobj.xW.src='images/W.gif';
  mobj.xX=new Image(); mobj.xX.src='images/X.gif';
  mobj.xY=new Image(); mobj.xY.src='images/Y.gif';
  mobj.xZ=new Image(); mobj.xZ.src='images/Z.gif';
  mobj.x0=new Image(); mobj.x0.src='images/0.gif';
  mobj.x1=new Image(); mobj.x1.src='images/1.gif';
  mobj.x4=new Image(); mobj.x4.src='images/4.gif';
  mobj.x5=new Image(); mobj.x5.src='images/5.gif';
  mobj.x6=new Image(); mobj.x6.src='images/6.gif';
  mobj.x7=new Image(); mobj.x7.src='images/7.gif';
  mobj.x8=new Image(); mobj.x8.src='images/8.gif';
  mobj.x9=new Image(); mobj.x9.src='images/9.gif';

  mobj.hcells=10;   // horizontal height of the grid in cells
  mobj.vcells=10;   // vertical height of the grid in cells
  mobj.hcells=game.level[game.level[0]][0].length;
  mobj.vcells=game.level[game.level[0]].length;
  mobj=null;
//-- end: board object ----------------------------



//window.ok_to_run=false;      // set this after validating the browser will support the game
var mascii=new Array(
'','','','','','','','','','','','','','','','',
'','','','','','','','','','','','','','','','',
'','','','','','','','','','','','','','','','',
'','','','','','','','','','','','','','','','','',
'A','B','C','D','E','F','G','H','I','J','K','L','M',
'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
'','','','','','',
'a','b','c','d','e','f','g','h','i','j','k','l','m',
'n','o','p','q','r','s','t','u','v','w','x','y','z'
);



// HANDLE LOADING EXTERNALLY CONFIGURABLE ITEMS

  var str3='<SC'+'RIPT LANGUAGE="JavaScript" SRC="'+game.wavlist_location+'sound.js"></SC'+'RIPT>';
  document.write(str3);






window.filterOn=false;
function apply_filter(){
  if(window.filterOn){
    document.getElementById('msgb').style.filter="";
    window.filterOn=false;
  }
  else{
    document.getElementById('msgb').style.filter="glow(color=330000,strength=7)";
  }
}




function populate_msgb(mval){
  document.getElementById('msgb').innerHTML=mval;
}



function draw_board(){
  var mstr=''; 
  var sstr=''; 
  var mimg_cnt=0; //enemy counter
  var bg1=new Array('nebula.jpg','galmoon.jpg','moon.jpg','saturn.jpg');
  var n=parseInt(Math.random()*bg1.length);
  mstr+='<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 STYLE="z-index:1;background-image:url(images/'+bg1[n]+');background-repeat:no-repeat;background-position:center center;">';
  for(var y=0; y<board.vcells; y++){
    mstr+='<TR>';
    for(var x=0; x<board.hcells; x++){
      mstr+='<TD ID="a_'+x+'_'+y+'" STYLE="left:'+(x*board.mwidth)+';top:'+(y*board.mheight)+';">';

      mimg=game.level[game.level[0]][y].charAt(x);
      if(mimg=='Z'){
        //store x and y of ghost pen door (should only have 1 pen)
        ghostpen.x=x;
        ghostpen.y=y;
      }
      if(mimg=='@'){
        pman1.x=x; 
        pman1.y=y;
        sstr='SRC="'+board.xX.src+'" ';
      }
      else if(mimg=='#'){
        eval('ghost'+(++mimg_cnt)+'.x=x');
        eval('ghost'+(mimg_cnt)+'.y=y');
        sstr='SRC="'+board.x0.src+'" ';
      }
      else if(mimg=='.') sstr='SRC="'+board.xY.src+'" ';
      else if(mimg==' ') sstr='SRC="'+board.x0.src+'" ';
      else{
        ttmp=eval('board.x'+mimg+'.src');
        sstr='SRC="'+ttmp+'" ';
      }
      mstr+='<IMG id="i_'+x+'_'+y+'" NAME="i_'+x+'_'+y+'" '+sstr+'WIDTH='+board.mwidth+' HEIGHT='+board.mheight+'>';
      mstr+='</TD>';
      if(mimg=='.') game.dots_left++;
    }
    mstr+='</TR>';
  }

  // INCLUDE CONTAINER(S) FOR COMMUNICATING WITH THE PLAYER (APPLE II AND PC GRAPHICS EAT YOUR HEARTS OUT)
  mstr+='<TR CLASS=td1 STYLE="top:314;z-index:10;"><TD COLSPAN='+board.hcells+' CLASS=msgb ID=msgb WIDTH=50% ALIGN=CENTER>Loading ...</TD></TR>';

  mstr+='</TABLE>';

  // INSTANTIATE BAD GUYS
  mstr+='<IMG NAME=pman1 SRC="pixel.gif" CLASS=td1 WIDTH='+board.mwidth+' HEIGHT='+board.mheight+' STYLE="z-index:8;">';
  mstr+='<IMG NAME=ghost1 SRC="pixel.gif" CLASS=td1 WIDTH='+board.mwidth+' HEIGHT='+board.mheight+' STYLE="z-index:9;">';
  mstr+='<IMG NAME=ghost2 SRC="pixel.gif" CLASS=td1 WIDTH='+board.mwidth+' HEIGHT='+board.mheight+' STYLE="z-index:9;">';
  mstr+='<IMG NAME=ghost3 SRC="pixel.gif" CLASS=td1 WIDTH='+board.mwidth+' HEIGHT='+board.mheight+' STYLE="z-index:9;">';
  mstr+='<IMG NAME=ghost4 SRC="pixel.gif" CLASS=td1 WIDTH='+board.mwidth+' HEIGHT='+board.mheight+' STYLE="z-index:9;">';
  document.getElementById('game_panel').innerHTML=mstr;
  //document.write(mstr);

  // POSITION BAD GUYS (RECONCILE THE OBJECTS WITH THE DATA LOCATIONS IN THE MAP)
  eval('document.pman1.style.top=document.getElementById(\'a_'+pman1.x+'_'+pman1.y+'\').style.top');
  eval('document.pman1.style.left=document.getElementById(\'a_'+pman1.x+'_'+pman1.y+'\').style.left');

  eval('document.ghost1.style.top=document.getElementById(\'a_'+ghost1.x+'_'+ghost1.y+'\').style.top');
  eval('document.ghost1.style.left=document.getElementById(\'a_'+ghost1.x+'_'+ghost1.y+'\').style.left');

  eval('document.ghost2.style.top=document.getElementById(\'a_'+ghost2.x+'_'+ghost2.y+'\').style.top');
  eval('document.ghost2.style.left=document.getElementById(\'a_'+ghost2.x+'_'+ghost2.y+'\').style.left');

  eval('document.ghost3.style.top=document.getElementById(\'a_'+ghost3.x+'_'+ghost3.y+'\').style.top');
  eval('document.ghost3.style.left=document.getElementById(\'a_'+ghost3.x+'_'+ghost3.y+'\').style.left');

  eval('document.ghost4.style.top=document.getElementById(\'a_'+ghost4.x+'_'+ghost4.y+'\').style.top');
  eval('document.ghost4.style.left=document.getElementById(\'a_'+ghost4.x+'_'+ghost4.y+'\').style.left');
}


function stopEvent(evt){
  if(evt.stopPropogation){
    evt.stopPropogation();
  }
  else{ 
    evt.cancelBubble = true;
  }
}


var malpha=new Array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
function handle_keypress(e){

keycode = e ? e.charCode : window.event.keyCode; 
if(!keycode) keycode = e.keyCode; /* for IE8 */
//if(!window.didit){
//  window.didit=1;
//  expose(e);
//}
//if(!confirm(keycode)){ 
//  stopEvent(event);
//  exit();
//}
  

//  if(!window.ok_to_run){ location.href='index.html'; return; }

  if(keycode>96) keycode-=32; // capitalize

  //alert(mascii[keycode]+']['+keycode);  //DEBUG
  //alert('[ '+document.AFORM.mup.value+' ][ '+mascii[keycode]+' ][ '+keycode+' ]');  //DEBUG

  if(game.state=='paused'){
    pause_game(0);
    return;
  }

  if(game.state=='playing'){
    if(mascii[keycode]==document.AFORM.mup.value)    pman1.direction=1;
    if(mascii[keycode]==document.AFORM.mright.value) pman1.direction=2;
    if(mascii[keycode]==document.AFORM.mdown.value)  pman1.direction=3;
    if(mascii[keycode]==document.AFORM.mleft.value)  pman1.direction=4;
  }

  return;
}



function checkit(mevent,mobj){
  document.AFORM.cancelBubble=true;

  mval = mevent ? mevent.keyCode : window.event.keyCode;
  if(!confirm(mval)) exit();

//  mval=mevent.keyCode;
  if(mval<97 && mval>124) return false; // Accept alpha keys only
  if(mval>96) mval-=32; // capitalize
  mval=mascii[mval];

  //var mstr=''; //DEBUG
  for(var i=0; i<document.AFORM.length; i++){
    if(document.AFORM.elements[i].value==mval){
      mevent=null;
      return false;
      //document.AFORM.elements[i].value='';
    }
    //mstr+='\n'+document.AFORM.elements[i].value; //DEBUG
  }
  //alert(mstr); //DEBUG
  mobj.value=mval;
  return false;
}



function start_game(){
//  if(!window.ok_to_run){ location.href='index.html'; return; }
  if(game.state!='ready') return;
  populate_msgb('');

  game.state='playing';
  game_loop();
  return false;
}



function pause_game(mval){
//  if(!window.ok_to_run){ location.href='index.html'; return; }
  if(game.state!='playing' && game.state!='paused') return;

  if(mval=='1'){
    populate_msgb('Paused');
    game.state='paused';
  }
  else{
    populate_msgb('');
    game.state='playing';
    game_loop();
  }

  return;
}



function game_loop(){
  if(game.state!='playing') return;

  //animate_pman('sync'); // run here if anim is synced with game_loop
  move_pman();
  eat_dots();
  process_ghosts();
  update_stats();

  if(game.move_timer){
    clearTimeout(game.move_timer);
    game.move_timer=null;
  }
  game.move_timer=setTimeout('game_loop()',game.move_delay);

  return;
}



function animate_pman(mmode){
//  if(!window.ok_to_run){ location.href='index.html'; return; }
  //if(game.state!='playing') return;

  items=new Array('pman1','ghost1','ghost2','ghost3','ghost4');
  for(var i=0; i<items.length; i++){
    mobj=eval(items[i]);
    dmobj=eval('document.'+items[i]);
    if(++mobj.anim_cnt>1) mobj.anim_cnt=0;
    tempfile=mobj.filename.src;
    if(mobj.anim_cnt==1){
      switch(mobj.direction){
        case 1: tempfile=mobj.up.src;
                break;
        case 2: tempfile=mobj.right.src;
                break;
        case 3: tempfile=mobj.down.src;
                break;
        case 4: tempfile=mobj.left.src;
                break;
      }
    }
    dmobj.src=tempfile;
  }




  if(mmode=='async'){ // if running asyncronously then we must initiate a return call to this function here
    if(game.anim_timer){
      clearTimeout(game.anim_timer);
      game.anim_timer=null;
    }
    game.anim_timer=setTimeout('animate_pman("async")',game.anim_delay);
  }
  return;
}



var mpath=null;
function eat_dots(){
  var test_cell=document.getElementById('i_'+pman1.x+'_'+pman1.y);
  if(!mpath){ 
    mpath=test_cell.src.substr(0,test_cell.src.indexOf('images/'));
  }

  if(test_cell.src.indexOf('Y.gif')!=-1){ // (big dot)
    pman1.score++;
    test_cell.src=mpath+'images/X.gif'; // (little dot)
    game.dots_left--;
    if(document.AFORM.dsound[0].checked) document.getElementById('msound').src=sound.eat_dot_file.src;
  }

  if(game.dots_left<=0){
    win_routine();
  }
}



function update_stats(){

  document.getElementById('mscore').innerHTML=pman1.score;

  if(game.update_timer){
    clearTimeout(game.update_timer);
    game.update_timer=null;
  }
  game.update_timer=setTimeout('update_stats()',500);
}



function move_pman(){
  var tempx=pman1.x;
  var tempy=pman1.y;

  sdir=pman1.direction;
  switch(sdir){
    case 1: tempy-=1;
            if(tempy<0) tempy=board.vcells-1;
            break;
    case 2: tempx+=1;
            if(tempx>=board.hcells) tempx=0;
            break;
    case 3: tempy+=1;
            if(tempy>=board.vcells) tempy=0;
            break;
    case 4: tempx-=1;
            if(tempx<0) tempx=board.hcells-1;
            break;
  }
  var test_cell=document.getElementById('i_'+tempx+'_'+tempy);
  var pass=false;
  switch(test_cell.src.charAt(test_cell.src.length-5)){
    case 'Y': pass=true; break;
    case 'X': pass=true; break;
    case '0': pass=true; break;
    case 'Z': pass=true; break;
  }
  if(!pass){
    tempx=pman1.x;
    tempy=pman1.y;
  }
  pman1.x=tempx; pman1.y=tempy;


  document.pman1.style.top=document.getElementById('a_'+pman1.x+'_'+pman1.y).style.top;
  document.pman1.style.left=document.getElementById('a_'+pman1.x+'_'+pman1.y).style.left;

  //if you want this move function to run indepently of a game loop then uncomment the timer and recurse below
  //if(game.move_timer){
  //  clearTimeout(game.move_timer);
  //  game.move_timer=null;
  //}
  //game.move_timer=setTimeout('move_pman()',game.move_delay);
}



function process_ghosts(){
  for(var i=1; i<5; i++){
    eval('mobj=ghost'+i);
    tname='ghost'+i;  // Need this for accessing container by name at end of this function

    var tempx=mobj.x;
    var tempy=mobj.y;
/*
    --mobj.time_to_change_emotion;
    if(mobj.time_to_change_emotion<1){
      mobj.time_to_change_emotion=50;

      switch(mobj.state){
        case 6: // remain in escape mode until the escape routing tells us to change
                mobj.state=6;
                break;
        case 5: // TRANSITION FROM CIRCLE to ESCAPE
                mobj.state=6;
                break;
        case 1: mobj.state=1;
                break;
        default:// Hmmm, still working this on out.
                var emotional_range=4;
                var n=parseInt(Math.random()*emotional_range); // pick an emotion randomly
                break;
      }
      
    }
*/
    //mobj.state  =1; // 1=happy, 2=mad, 3=scared, 4=dead, 5=circle, 6=escape (see notes.txt for explaination of emotional states)
    switch(mobj.state){
      case 6: //EMOTION: ESCAPE
        if(mobj.x<ghostpen.x) tempx=-1;
        else if(mobj.x>ghostpen.x) tempx=1;
        else if(mobj.y>ghostpen.y) tempy=-1;
        else mobj.state=1; //change from escape to happy
        break;
      case 5: //EMOTION: CIRCLE
        if(mobj.need_to_change_direction==true){
          mobj.need_to_change_direction=false;
          if((++mobj.direction)>3) mobj.direction=0;
        }

        switch(mobj.direction){
          case 1: //RIGHT
                  tempx+=1;
                  break;
          case 2: //DOWN
                  tempy+=1;
                  break;
          case 3: //LEFT
                  tempx-=1;
                  break;
          case 0: //UP
                  tempy-=1;
                  break;
          }
          break;
    }

    var test_cell=document.getElementById('i_'+tempx+'_'+tempy);
    var pass=false;
//alert(test_cell.src.charAt(test_cell.src.length-5));
    switch(test_cell.src.charAt(test_cell.src.length-5)){
      case 'Y': pass=true; break;
      case 'X': pass=true; break;
      case '0': pass=true; break;
      case 'Z': pass=true; break;
    }
    if(!pass){
      tempx=mobj.x;
      tempy=mobj.y;
      mobj.need_to_change_direction=true;
    }
    else{
      mobj.x=tempx; 
      mobj.y=tempy;
    }

    eval('document.'+tname+'.style.top=document.getElementById(\'a_'+mobj.x+'_'+mobj.y+'\').style.top');
    eval('document.'+tname+'.style.left=document.getElementById(\'a_'+mobj.x+'_'+mobj.y+'\').style.left');

  }
}



function win_routine(){
  if(pman1.anim_timer){
    clearTimeout(pman1.anim_timer);
    pman1.anim_timer=null;
  }
  if(pman1.move_timer){
    clearTimeout(pman1.move_timer);
    pman1.move_timer=null;
  }
  if(game.update_timer){
    clearTimeout(game.update_timer);
    game.update_timer=null;
  }

  populate_msgb('Winner!');
  game.state='win_level';
  return;
}



function draw_controls(){
  var mstr=''
  mstr+='<FORM STYLE="display:inline;" NAME="AFORM" onsubmit="return false;">'
  mstr+='<TABLE BORDER=0 WIDTH=100% CELLPADDING=0 CELLSPACING=0 BGCOLOR=000000>'
  mstr+='  <TR>'
  mstr+='    <TD>'
  mstr+='<TABLE BORDER=0 WIDTH=100% CELLPADDING=0 CELLSPACING=0>'
  mstr+='  <TR>'
  mstr+='    <TD WIDTH=1%><IMG SRC=images/credit_background_left.gif></TD>'
  mstr+='    <TD WIDTH=100% ALIGN=center BACKGROUND="images/credit_background.gif"><IMG NAME=game_by SRC=images/thomas_and_andrew.gif STYLE="position:relative;top:-1px;"></TD>'
  mstr+='    <TD WIDTH=1%><IMG SRC=images/credit_background_right.gif></TD>'
  mstr+='  </TR>'
  mstr+='</TABLE>'
  mstr+='     </TD>'
  mstr+='  </TR>'
  mstr+='  <TR>'
  mstr+='   <TD>'
  mstr+=''
  mstr+='<TABLE BORDER=0 WIDTH=100% CELLPADDING=0 CELLSPACING=0 BGCOLOR=333333>'
  mstr+='  <TR>'
  mstr+='    <TD WIDTH=1%><IMG SRC=pixel.gif WIDTH=15 HEIGHT=5></TD>'
  mstr+='    <TD WIDTH=1%><IMG SRC=pixel.gif WIDTH=100 HEIGHT=5></TD>'
  mstr+='    <TD WIDTH=1%><IMG SRC=pixel.gif WIDTH=15 HEIGHT=5></TD>'
  mstr+='    <TD WIDTH=1%><IMG SRC=pixel.gif WIDTH=15 HEIGHT=5></TD>'
  mstr+='    <TD WIDTH=1%><IMG SRC=pixel.gif WIDTH=15 HEIGHT=5></TD>'
  mstr+='    <TD WIDTH=1%><IMG SRC=pixel.gif WIDTH=15 HEIGHT=5></TD>'
  mstr+='    <TD WIDTH=100%><IMG SRC=pixel.gif WIDTH=15 HEIGHT=5></TD>'
  mstr+='    <TD WIDTH=1%><IMG SRC=pixel.gif WIDTH=50 HEIGHT=5></TD>'
  mstr+='    <TD WIDTH=1%><IMG SRC=pixel.gif WIDTH=50 HEIGHT=5></TD>'
  mstr+='    <TD ROWSPAN=4 BGCOLOR=000000><IMG SRC=images/spack_title.gif HEIGHT=50></TD>'
  mstr+='  </TR>'
  mstr+='  <TR>'
  mstr+='    <TD>&nbsp;</TD>'
  mstr+='    <TD><BUTTON ONCLICK="return start_game();" ONFOCUS="blur()" CLASS=btn ACCESSKEY=S><U>S</U>tart Game</BUTTON></TD>'
  mstr+='    <TD>&nbsp;</TD>'            
  mstr+='    <TD>&nbsp;</TD>'
  mstr+='    <TD><INPUT NAME=mup TYPE=text MAXLENGTH=1 VALUE=I CLASS=kbd_cnt STYLE="background-image:url(images/up_arrow.gif);" ONKEYPRESS="checkit(event,this)"></TD>'
  mstr+='    <TD>&nbsp;</TD>'
  mstr+='    <TD>&nbsp;</TD>'
  mstr+='    <TD CLASS=cscore>Score:</TD>'
  mstr+='    <TD ID=mscore CLASS=cscore>&nbsp;<TD>'
  mstr+='  </TR>'
  mstr+='  <TR>'
  mstr+='    <TD>&nbsp;</TD>'
  mstr+='    <TD><BUTTON ONCLICK="pause_game(1);" ONFOCUS="blur()" CLASS=btn ACCESSKEY=P><U>P</U>ause Game</BUTTON></TD>'
  mstr+='    <TD>&nbsp;</TD>'
  mstr+='    <TD><INPUT NAME=mleft TYPE=text MAXLENGTH=1 VALUE=J CLASS=kbd_cnt STYLE="background-image:url(images/left_arrow.gif);background-position:center center;background-repeat:no-repeat;" ONKEYPRESS="checkit(event,this)"></TD>'
  mstr+='    <TD><INPUT NAME=mdown TYPE=text MAXLENGTH=1 VALUE=K CLASS=kbd_cnt STYLE="background-image:url(images/down_arrow.gif);background-position:center center;background-repeat:no-repeat;" ONKEYPRESS="checkit(event,this)"></TD>'
  mstr+='    <TD><INPUT NAME=mright TYPE=text MAXLENGTH=1 VALUE=L CLASS=kbd_cnt STYLE="background-image:url(images/right_arrow.gif);background-position:center center;background-repeat:no-repeat;" ONKEYPRESS="checkit(event,this)"></TD>'
  mstr+='    <TD>&nbsp;</TD>'
  mstr+='    <TD CLASS=cscore>Sounds:</TD>'
  mstr+='    <TD CLASS=cscore NOWRAP><INPUT TYPE=radio NAME=dsound CHECKED STYLE="background-color:transparent;width:20px;">On&nbsp;<INPUT TYPE=radio NAME=dsound STYLE="background-color:transparent;width:20px;">Off</TD>'
  mstr+='  </TR>'
  mstr+='  <TR>'
  mstr+='    <TD WIDTH=1%><IMG SRC=pixel.gif WIDTH=15 HEIGHT=5></TD>'
  mstr+='    <TD WIDTH=1%><IMG SRC=pixel.gif WIDTH=100 HEIGHT=1></TD>'
  mstr+='    <TD WIDTH=1%><IMG SRC=pixel.gif WIDTH=15 HEIGHT=1></TD>'
  mstr+='    <TD WIDTH=1%><IMG SRC=pixel.gif WIDTH=15 HEIGHT=1></TD>'
  mstr+='    <TD WIDTH=1%><IMG SRC=pixel.gif WIDTH=15 HEIGHT=1></TD>'
  mstr+='    <TD WIDTH=1%><IMG SRC=pixel.gif WIDTH=15 HEIGHT=1></TD>'
  mstr+='    <TD WIDTH=100%><IMG SRC=pixel.gif WIDTH=15 HEIGHT=1></TD>'
  mstr+='    <TD WIDTH=1%><IMG SRC=pixel.gif WIDTH=50 HEIGHT=1></TD>'
  mstr+='    <TD WIDTH=1%><IMG SRC=pixel.gif WIDTH=50 HEIGHT=1></TD>'
  mstr+='  </TR>'
  mstr+='</TABLE></TD>'
  mstr+=' </TR>'
  mstr+='</TABLE>'
  mstr+='</FORM>'
  document.getElementById('control_panel').innerHTML=mstr;
  document.getElementById('control_panel').style.top=((game.level[game.level[0]].length+2)*board.mheight)+'px';
  //document.write(mstr);
}

