//Copyright note: you are allowed to present the game at your site for free; but only with this text in it. This program is created by juergen mueller-luetken at 2005 - 06 - 11, email: kontakt@p-six.de. Please send me a note, if you use the program or if you find any bugs. Thanks a lot for honoring my work.
var version = "Version 1.5";
var ready = "Ready?"; // "Feddisch";
var attention = "Attention!"; // "Achtung!";
var over = "Game over"; // "Vaschissn";
var again = "Again?"; // "Noch ma?";

var ro = 0;
var rl = 0;

var theman;
var cman_name = "theman";//name
var cman_fig = "cman"//div_id
var cman_start_r = 23;
var cman_start_s = 14;
var cman_start_richt = 1;
var cman_start_top = 314+ro;//334;
var cman_start_left = 170+rl;
var cmanstop = false;
//cman_start_left wird in der index-datei erzeugt
var cman_img_src = "p";//image_src
var cman_img_name = "pac";//image_name
var pacbilder = new Array("","p0","ptk","ptkk","pix");

var geister = new Array();
var geist_name = new Array("geister[0]","geister[1]","geister[2]","geister[3]");//name
var geist_fig = new Array("geist0","geist1","geist2","geist3");//div_id
var geist_start_r = new Array(11,14,14,14);
var geist_start_s = new Array(13,14,12,16);
var geist_start_richt = new Array(1,3,4,4);
var geist_start_top = new Array(170+ro,206+ro,206+ro,206+ro);//(190,226,226,226);
var geist_start_left = new Array(170+rl,170+rl,148+rl,192+rl);
var geist_home_r = new Array(1,1,29,29);
var geist_home_s = new Array(23,3,23,3);
var geist_img_src = new Array("g0","g1","g2","g3");//image_src
var geist_img_name = new Array("gh0","gh1","gh2","gh3");//image_name
var geist_jagdradius_t = new Array(360,360,180,180);
var geist_jagdradius_l = new Array(320,160,320,160);
var geist_startups = new Array(0,0,8,16);
var geist_indahouse = new Array(false,true,true,true);

var geist_todesanzeige = new Array("ghost0","ghost1","ghost2","ghost3");
var geist_anz = new Array();//um die todesanzeigen zu objektivieren
var symbol_anzeige = "symb";
var richtungen = new Array(1,2,3,4)//(1,2,3,4);//links,rechts,unten,oben
var fr_punkte = new Array(0,100,300,500,500,700,700,1000,1000,2000,2000,3000,3000,5000);
var fr_bilder = new Array(0,1,2,3,3,4,4,5,5,6,6,7,7,8);
var bluetime = new Array(7,7,6.5,5.5,5.5,4.5,4,4,5,4,4,4,3.5,3,3.5,3.5,3,2,1.5,1.5,3,2,2,2,2,2,2,1,1.5,1.5,1.5,1,1,1,1,0,0,0,0.5,0.1);
var blinktime = new Array(5,4,3,3,3,2,3,2.5,1.5,2,2,1.5,2,2,2,1,1,1,1.5,1.5,1.5,1,1,1,1,0.5,0.2,0.2,1,1,0.01,0.005,0.002,0,0.004,0.2,0.1,0.03,1,0.05);
var restpillen = 20;
var more_restpillen = 3;
var bl = 12;
var km = 50;
var km_plus = 0;
var gl_km = Math.floor(km*3.6);//180 geschwindigkeit der geister im gefängnis und im tunnel
var gsch_km = Math.floor(km*3.2);//160 geschwindigkeit der erschreckten geister
var gt_km = Math.floor(km*0.5);// geschwindigkeit der geisteraugen auf dem weg nach hause
var schritt = 6;
var whole_top = 200+ro;
var whole_bottom = 212+ro;
var whole_udmiddle = 206+ro;
var whole_lrmiddle = 170+rl;
var rechts = 343+rl;
var links = -5+rl;
var max_score = "3333360";
var cookieName = "HIGHSCORE";
var pacbild,cieman,sy,sy_anz,sy_in;

var BLN,END_BLN,INT,SYMB,STRB,SCAT,SCAT_OFF;//variablen für die timeouts beim erschrecken (BLN,END_BLN) sowie beim Interrupt nach dem Fressen eines Geistes (INT); SYMB ist der Timeout für das Verschwinden des Symbols, STRB die variable für den tod des cman


var punkte=ppp=maxP=gefressen=millisek=sek1=sek2=howoften_scatter=maul=scatter_zeit=tot=lz=today_score=high_score = 0;
var game_on=cmanstop=symbol_da=scatter=powerpill=blinkend=neu_richt=nextlevel=sound=sicherung = false;
var zeit;
var new_life = 10000;
var leben = 2;
var level = 1;
var siren_speedchange = 7;
var siren_sound = 9;
var g0_km = false;
var start_time = 4700;
var sterbezeit = 800;
var howlong_scatter = 8000;
var zI = 10;
var sounds = new Array("start","isst","isstsymb","geisterschreckt","isstgeist","geistzruck","cmanstirbt","extracman","levelneu","siren_soft1","siren_soft2","siren_soft3","siren_soft4","siren_soft5");
var S0,S1,S2,S3,S4,S5,S6,S7,S8,S9;

var restbild = new Array("pix","p","p1","p2","p3","p4","p12","p22","p32","p42","g11","g111","g12","g122","g13","g133","g14","g144","g21","g211","g22","g222","g23","g233","g24","g244","g31","g311","g32","g322","g33","g333","g34","g344","gsch","gt1","gt2","gt3","gt4","gbl","fr1","fr2","fr3","fr4","fr5","fr6","fr7","fr8","k","pt1","pt2","pt3","pt4");
var restbild_b = new Array();

for(var bx=0;bx<restbild.length;bx++)
{
 restbild_b[bx] = new Image();
 restbild_b[bx].src = "bcm/"+restbild[bx]+".gif";
}

// 0 = wand
// 1 = pille
// 2 = knoten mit pille
// 3 = powerpille
// 4 = knoten mit powerpille
// 5 = ohne pille
// 6 = knoten ohne pille
// 7 = gefngniszelle und tunnel

var muster = new Array(31);
   muster[0] = new Array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
   muster[1] = new Array(0,2,1,1,1,1,2,1,1,1,1,1,2,0,0,2,1,1,1,1,1,2,1,1,1,1,2,0);
   muster[2] = new Array(0,1,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0);
   muster[3] = new Array(0,3,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,3,0);
   muster[4] = new Array(0,1,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0);
   muster[5] = new Array(0,2,1,1,1,1,2,1,1,2,1,1,2,1,1,2,1,1,2,1,1,2,1,1,1,1,2,0);
   muster[6] = new Array(0,1,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,1,0);
   muster[7] = new Array(0,1,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,1,0);
   muster[8] = new Array(0,2,1,1,1,1,2,0,0,2,1,1,2,0,0,2,1,1,2,0,0,2,1,1,1,1,2,0);
   muster[9] = new Array(0,0,0,0,0,0,1,0,0,0,0,0,5,0,0,5,0,0,0,0,0,1,0,0,0,0,0,0);
  muster[10] = new Array(0,0,0,0,0,0,1,0,0,0,0,0,5,0,0,5,0,0,0,0,0,1,0,0,0,0,0,0);
  muster[11] = new Array(0,0,0,0,0,0,1,0,0,6,5,5,6,5,5,6,5,5,6,0,0,1,0,0,0,0,0,0);
  muster[12] = new Array(0,0,0,0,0,0,1,0,0,5,0,0,0,7,7,0,0,0,5,0,0,1,0,0,0,0,0,0);
  muster[13] = new Array(0,0,0,0,0,0,1,0,0,5,0,7,7,7,7,7,7,0,5,0,0,1,0,0,0,0,0,0);
  muster[14] = new Array(7,7,7,7,7,7,2,5,5,6,0,7,7,7,7,7,7,0,6,5,5,2,7,7,7,7,7,7);
  muster[15] = new Array(0,0,0,0,0,0,1,0,0,5,0,7,7,7,7,7,7,0,5,0,0,1,0,0,0,0,0,0);
  muster[16] = new Array(0,0,0,0,0,0,1,0,0,5,0,0,0,0,0,0,0,0,5,0,0,1,0,0,0,0,0,0);
  muster[17] = new Array(0,0,0,0,0,0,1,0,0,6,5,5,5,5,5,5,5,5,6,0,0,1,0,0,0,0,0,0);
  muster[18] = new Array(0,0,0,0,0,0,1,0,0,5,0,0,0,0,0,0,0,0,5,0,0,1,0,0,0,0,0,0);
  muster[19] = new Array(0,0,0,0,0,0,1,0,0,5,0,0,0,0,0,0,0,0,5,0,0,1,0,0,0,0,0,0);
  muster[20] = new Array(0,2,1,1,1,1,2,1,1,2,1,1,2,0,0,2,1,1,2,1,1,2,1,1,1,1,2,0);
  muster[21] = new Array(0,1,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0);
  muster[22] = new Array(0,1,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0);
  muster[23] = new Array(0,4,1,2,0,0,2,1,1,2,1,1,2,1,1,2,1,1,2,1,1,2,0,0,2,1,4,0);
  muster[24] = new Array(0,0,0,1,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,1,0,0,0);
  muster[25] = new Array(0,0,0,1,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,1,0,0,0);
  muster[26] = new Array(0,2,1,2,1,1,2,0,0,2,1,1,2,0,0,2,1,1,2,0,0,2,1,1,2,1,2,0);
  muster[27] = new Array(0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0);
  muster[28] = new Array(0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0);
  muster[29] = new Array(0,2,1,1,1,1,1,1,1,1,1,1,2,1,1,2,1,1,1,1,1,1,1,1,1,1,2,0);
  muster[30] = new Array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);

  var lauffeld = new Array();

  function make_cman(Name)
  {
   this.Lay   = document.getElementById(Name).style;
   for_everybody(this);
   this.name = cman_name;
   this.oben  = cman_start_top;
   this.links = cman_start_left;
   this.r = cman_start_r;
   this.s = cman_start_s;
   this.image_name = cman_img_name;
   this.image_src = cman_img_src;
   this.on = false;
   this.clear_richt(cman_start_richt);
   this.is_pill = is_pill;
   this.cman_whattodo = cman_whattodo;
   this.cman_go = cman_go;
   this.move_mouth = move_mouth;
   this.cman_stop = cman_stop;
   this.eat_pill = eat_pill;
   this.sterben = sterben;
   this.funktion = ".cman_whattodo()";
   this.timeout;
  }

  function make_geister(Nr)
  {
   this.Lay   = document.getElementById(geist_fig[Nr]).style;
   for_everybody(this);
   this.name = "geister["+Nr+"]";
   this.oben  = geist_start_top[Nr];
   this.links = geist_start_left[Nr];
   this.r = geist_start_r[Nr];
   this.s = geist_start_s[Nr];
   this.image_name = geist_img_name[Nr];
   this.image_src = geist_img_src[Nr];
   this.clear_richt(geist_start_richt[Nr]);
   this.ups = geist_startups[Nr];
   this.indahouse = geist_indahouse[Nr];
   this.ghost_img = 1;
   this.zaehler = 0;
   this.nr = Nr;
   this.on = false;
   this.jagt = false;
   this.eaten = false;
   this.scared = false;
   this.scatter = true;
   this.regenerated = false;
   this.richtungswechsel = false;
   this.ghost_whattodo = ghost_whattodo;
   this.ghost_goout = ghost_goout;
   this.ghost_goin = ghost_goin;
   this.upordown = upordown;
   this.ghost_go = ghost_go;
   this.ghost_direction = ghost_direction;
   this.choose = choose;
   this.hinundweg = hinundweg;
   this.best_way = best_way;
   this.wedeln = wedeln;
   this.blinke = blinke;
   this.ghost_goin = ghost_goin;
   this.eingang = eingang;
   this.velocity = velocity;
   this.collision = collision;
   this.treffer = treffer;
   this.ghost_eaten = ghost_eaten;
   this.funktion = ".ghost_whattodo()";
   this.destination = destination;
   this.clear_destination = clear_destination;
   this.surprising = surprising;
   this.timeout;
  }

  function make_todesanzeigen(n)
  {
   this.lage = document.getElementById(geist_todesanzeige[n]).style;
   this.sicht_zeit = sicht_zeit;
  }

  function for_everybody(obj)
  {
   obj.km = km;
   obj.next_step = next_step;
   obj.wheretogo = wheretogo;
   obj.change_side = change_side;
   obj.move = move;
   obj.change_data = change_data;
   obj.clear_richt = clear_richt;
   obj.no_richt;
   obj.richt;
  }

  function objektivieren()
  {
   theman = new make_cman(cman_fig);
   for(ggg=0;ggg<geist_fig.length;ggg++)
   {
    geister[ggg] = new make_geister(ggg);
    geist_anz[ggg] = new make_todesanzeigen(ggg);
   }
  }

  function adjust_speed(x)
  {
   var test_km = km + x;
   if(test_km >= 10)
   {
    km = km + x;
    km_plus += x;
    gl_km = Math.floor(km*3.6);
    gsch_km = Math.floor(km*3.2);
    gt_km = Math.floor(km*0.5);
    if(km_plus < 0)
     document.getElementById("pacspeed").innerHTML = "Speed:&nbsp;+"+(km_plus*(-1));
    else
     document.getElementById("pacspeed").innerHTML = "Speed:&nbsp;"+(km_plus*(-1));
   }
  }

  //Funktionen, die von allen Figuren genutzt werden

  function next_step(r)// meldet die ziffer im nchsten feld des musters in laufrichtung
  {
   var m;
   switch(r)
   {
    case 1://"l":
    m = muster[this.r][this.s-1];
    break;

    case 2://"r":
    m = muster[this.r][this.s+1];
    break;

    case 3://"u":
    m = muster[this.r+1][this.s];
    break;

    case 4://"o":
    m = muster[this.r-1][this.s];
    break;
   }
   return m;
  }


  function change_side()// versetzt die figuren von einer seite zur anderen im tunnel
  {
   if(this.richt == 2)
   {
    if((this.s+1) == 28)
    {
     this.links = links;
     this.s = -1;
    }
   }
   if(this.richt == 1)
   {
    if((this.s-1) == -1)
    {
     this.links = rechts;
     this.s = 28;
    }
   }
  }

  function wheretogo()
  {
   var m = muster[this.r][this.s];
   if(m == 2 || m == 4 || m == 6)
    return true;
   else
    return false;
  }

  function move(d,m)
  {
   var zeit,str;
   if(m > 0)
   {
    switch(this.richt)
    {
     case 1:
     if(m < schritt)
     {
      this.links -= m;
      m = 0;
     }
     else
     {
      m -= schritt
      this.links -= schritt;
     }
     this.Lay.left = this.links + "px";
     break;

     case 2:
     if(m < schritt)
     {
      this.links += m;
      m = 0;
     }
     else
     {
      m -= schritt
      this.links += schritt;
     }
     this.Lay.left = this.links + "px";
     break;

     case 3:
     if(m < schritt)
     {
      this.oben += m;
      m = 0;
     }
     else
     {
      m -= schritt
      this.oben += schritt;
     }
     this.Lay.top = this.oben + "px";
     break;

     case 4:
     if(m < schritt)
     {
      this.oben -= m;
      m = 0;
     }
     else
     {
      m -= schritt
      this.oben -= schritt;
     }
     this.Lay.top = this.oben + "px";
     break;
    }
    if(this.name != "theman")
     this.wedeln();
    str = this.name+".move("+this.name+","+m+")";
    this.timeout = setTimeout(str,this.km);
    if(m <= 0)
     this.on = false;
   }
   else
   {
    if(nextlevel)
     change_level();
    if(game_on && !this.on)
    {
     if(this.name != "theman")
      this.wedeln();
     eval(this.name+this.funktion);
    }
   }
  }

  function change_data()
  {
   switch(this.richt)
   {
    case 1:
    this.s -= 1;
    break;

    case 2:
    this.s += 1;
    break;

    case 3:
    this.r += 1;
    break;

    case 4:
    this.r -= 1;
    break;
   }
  }

  function clear_richt(rt)
  {
   switch(rt)
   {
    case 1:
     this.richt = 1;
     this.no_richt = 2;
     break;
    case 2:
     this.richt = 2;
     this.no_richt = 1;
     break;
    case 3:
     this.richt = 3;
     this.no_richt = 4;
     break;
    case 4:
     this.richt = 4;
     this.no_richt = 3;
     break;
   }
  }

  //Funktionen, die nur von Pacman genutzt oder aufgerufen werden

  function is_pill(r)// meldet die ziffer im nächsten feld des lauffeldes in laufrichtung; pille oder nicht)
  {
   var reihe,m;
   switch(r)
   {
    case 1://"l":
    reihe = this.s-1;
    if(reihe < 0)
     reihe = 27;
    m = lauffeld[this.r][reihe];
    break;

    case 2://"r":
    reihe = this.s+1;
    if(reihe > 27)
     reihe = 0;
    m = lauffeld[this.r][reihe];
    break;

    case 3://"u":
    m = lauffeld[this.r+1][this.s];
    break;

    case 4://"o":
    m = lauffeld[this.r-1][this.s];
    break;
   }
   return m;
  }

  function cman_whattodo()// diese Funktion wird ständig aufgerufen und ist der starter für pacman
  {
   if(game_on && !this.on)
   {
    this.on = true;
    if((this.wheretogo() == true && this.next_step(neu_richt) != 0)||(neu_richt == this.no_richt))
    {
     this.clear_richt(neu_richt);
     neu_richt = false;
    }
    if(this.next_step(this.richt) != 0)
    {
     if(this.r == 14)
      this.change_side();
     this.cman_go();
    }
    else
     this.cman_stop();
   }
  }

  function cman_go()
  {
   var p = this.is_pill(this.richt);
   if(p == 3)
   {
   if(S1 != null)
    S1.src = "";
    sound = false;
    this.km -= 15;
   }
   else if(p == 1 && sound == false)
   {
   	if(S1 != null)
    S1.src = "ton/"+sounds[1]+".au";
    sound = true;
   }
   this.move(this.name,bl);
   this.change_data();
   this.eat_pill();
   this.km = km;
   if(this.r == 17 && this.s == 13 && symbol_da)
    eat_symbol();
  }


  function move_mouth()
  {
   if(game_on)
   {
    switch(maul)
    {
     case 0:
      pacbild.src = "bcm/p"+this.richt+"2.gif";
      break;

     case 1:
      pacbild.src = "bcm/p0.gif";
      break;

     case 2:
      pacbild.src = "bcm/p"+this.richt+"2.gif";
      break;

     case 3:
      pacbild.src = "bcm/p"+this.richt+".gif";
      break;
    }
    maul += 1;
    if(maul > 3)
     maul = 0;
    setTimeout("theman.move_mouth()",80);
   }
  }

  function cman_stop()
  {
   this.on = false;
   cmanstop = true;
   if(S1 != null)
   	S1.src = "";
  }

  function score(x)
  {
   punkte += x;
   document.getElementById("points").innerHTML = punkte;
   if(punkte > today_score)
   {
    today_score = punkte;
    document.getElementById("p_sc").innerHTML = today_score;
   }
   if(punkte >= new_life)
   {
    lz++;
    if(lz < 2)
     new_life += 20000;
    else
     new_life += 30000;
    if(leben < 2)
    {
     leben++;
     if(S7 != null)
     	S7.src = "ton/"+sounds[7]+".au";
     document.images["p"+leben].src = "bcm/p1.gif";
    }
   }
  }

  function eat_pill()
  {
   var x = lauffeld[this.r][this.s];
   if(x == 1)
   {
    document.images["r"+this.r+"s"+this.s].src = "bcm/pix.gif";
    lauffeld[this.r][this.s] = 3;
    count(10);
   }
   else if(x == 2)
   {
    set_powerpillmode();
    document.images["r"+this.r+"s"+this.s].src = "bcm/pix.gif";
    lauffeld[this.r][this.s] = 3;
    count(50);
   }
  }

  function count(x)
  {
   score(x);
   ppp += 1;
   if(ppp == 70 || ppp == 170)
    show_symbol();
   if((maxP - ppp) == Math.pow(2,siren_speedchange))
   {
    if(siren_speedchange > 3)
     siren_speedchange -= 1;
    siren_sound += 1;
    if(S9 != null)
    	S9.src = "ton/"+sounds[siren_sound]+".au";
   }
   if((maxP - ppp) == restpillen)
    g0_km = 50;
   if(ppp == maxP)
    nextlevel = true;
  }

  function eat_symbol()
  {
  	if(S2 != null)
   		S2.src = "ton/"+sounds[2]+".au";
   hide_symbol();
   show_symbolanz();
   if(level < fr_punkte.length)
   {
    score(fr_punkte[level]);
    sy_in.innerHTML = fr_punkte[level];
   }
   else
   {
    score(fr_punkte[fr_punkte.length-1]);
    sy_in.innerHTML = fr_punkte[fr_punkte.length-1];
   }
  }

  function sterben()
  {
   if(tot == 0)
    pacbild.src = "bcm/pt"+this.richt+".gif";
   else
    pacbild.src = "bcm/"+pacbilder[tot]+".gif";
   if(tot == 1)
    sterbezeit = 350;
   tot++;
   if(tot < 5)
   {
	clearTimeout(STRB);
	STRB = setTimeout("sterben()",sterbezeit);
   }
   else
   {
    if(leben > 0)
    {
     leben--;
     end_sounds();
     setTimeout("next_life()",1500);
    }
    else
    {
     game_on = true;
     end_sounds();
     hide_all();
     if(today_score > high_score)
      set_highscore();
     setTimeout("game_over()",1500);
    }
    clearTimeout(STRB);
   }
  }

  //Funktionen, die nur die geister nutzen oder betreffen

  function ghost_whattodo()
  {
   if(game_on && !this.on)
   {
    this.on = true;
    if(this.indahouse)
    {
     this.clear_richt(this.no_richt);
     this.velocity();
     this.ghost_goout();
    }
    else if(this.eaten && this.eingang() == true)
    {
     this.velocity();
     this.ghost_goin();
    }
    else
    {
     if(this.richtungswechsel)
     {
      this.richtungswechsel = false;
      this.clear_richt(this.no_richt);
     }
     if(this.wheretogo() == true)
      this.ghost_direction();
     if(this.r == 14)
      this.change_side();
     this.velocity();
     this.ghost_go();
    }
   }
  }

  function ghost_go()
  {
   this.change_data();
   this.move(this.name,bl);
  }

  function eingang()
  {
   if(((this.r == 11 && this.richt == 2 && this.s == 13)||(this.r == 11 && this.richt == 1 && this.s == 14)) || (this.r == 15 && this.s == 12))
    return true;
  }

  function ghost_goout()
  {
   if(this.ups > 1)
   {
    if(this.richt == 4)
    {
     this.ups -= 1;
     this.move(this.name,this.upordown());
    }
    else
    {
     this.move(this.name,this.upordown());
    }
   }
   else if(this.ups == 1)
   {
    this.ups -= 1;
    var ud = eval(this.oben - whole_udmiddle);
    if(ud < 0)
    {
     this.clear_richt(3);
     ud = ud * (-1);
    }
    else
    {
     this.clear_richt(4);
    }
    this.move(this.name,ud);
   }
   else if(this.ups == 0)
   {
    this.ups -= 1;
    var lr = eval(this.links - whole_lrmiddle);
    if(lr < 0)
    {
     this.clear_richt(2);
     lr = lr * (-1);
    }
    else
     this.clear_richt(1);
    if((this.nr == 0 || this.nr == 1) && lr <= 0)
     this.on = false;
    this.move(this.name,lr);
   }
   else if(this.ups == -1)
   {
    this.ups -= 1;
    this.clear_richt(4);
    this.move(this.name,eval(this.oben-(170+ro)));
   }
   else
   {
    this.clear_richt(1);
    this.r = 11;
    this.s = 13;
    this.indahouse = false;
    this.move(this.name,5);
   }
  }

  function upordown()
  {
   var d=0;
   switch(this.richt)
   {
    case 3:
     d = eval(whole_bottom - this.oben);
     break;
    case 4:
     d = eval(this.oben - whole_top);
     break;
   }
   return d;
  }

  function ghost_goin()
  {
   if(this.oben != whole_bottom)
   {
    this.r = 15;
    this.s = 12;
    this.links = whole_lrmiddle;
    this.Lay.left = this.links + "px";
    this.clear_richt(3);
    this.move(this.name,eval(whole_bottom-this.oben));
   }
   if(this.oben == whole_bottom)
   {
    var lm;
    lm = eval(this.links - geist_start_left[this.nr]);
    if(lm < 0)
    {
     this.clear_richt(2);
     lm = lm * (-1);
    }
    else
     this.clear_richt(1);
    this.ups = 0;
    if(S5 != null)
    	S5.src = "";
    if(powerpill)
    {
    	if(S3 != null)
     		S3.src = "ton/"+sounds[3]+".au";
    }
    this.indahouse = true;
    this.regenerated = true;
    this.scared = false;
    this.eaten = false;
    if((this.nr == 0 || this.nr == 1)&& lm <= 0)
     this.on = false;
    this.move(this.name,lm);
   }
  }

  function ghost_direction()
  {
   var richt_array = new Array();
   var y,z = 0;
   for(var i=0;i<richtungen.length;i++)
   {
    if(richtungen[i] != this.no_richt)
    {
     if(this.next_step(richtungen[i]) != 0)
     {
      richt_array[z] = richtungen[i];
      z++;
     }
    }
   }
   if(richt_array.length == 1)
   {
    this.clear_richt(richt_array[0]);
   }
   else
   {
    if(this.jagt)
    {
     this.clear_richt(this.best_way(richt_array));
    }
    else if(this.surprising() == true)
     this.clear_richt(richt_array[0]);
    else if(this.destination(richt_array) == false)
     this.clear_richt(this.choose(richt_array));
    else
     this.clear_destination(this.destination(richt_array));
   }
   this.zaehler += 1;
   if(this.zaehler == 21)
    this.zaehler = 0;
  }

  function destination(r_a)
  {
   var jagd_array = false;
   if(this.eaten)
    jagd_array = this.hinundweg(r_a,11,13);
   else if(this.scared)
    jagd_array = this.hinundweg(r_a,theman.r,theman.s);
   else if(!powerpill && this.scatter)
    jagd_array = this.hinundweg(r_a,geist_home_r[this.nr],geist_home_s[this.nr]);
   return jagd_array;
  }

  function clear_destination(j_a)
  {
   if(j_a.length == 1)
    this.clear_richt(j_a[0]);
   else if(j_a.length > 1)
    this.clear_richt(this.choose(j_a));
  }

  function choose(ra)
  {
   var rr;
   if(this.scared && !this.eaten)
   {
    for(var l=0;l<ra.length;l++)
    {
     if(ra[l] != this.richt)
      rr = ra[l];
    }
   }
   else
   {
    if(this.nr == 1 || this.nr == 3)
     rr = ra[ra.length-1];
    else
     rr = ra[0];
   }
   return rr;
  }

  function surprising()
  {
   var m = false;
   switch(this.nr)
   {
    case 0:
     if(this.zaehler == 0 || this.zaehler == 7 || this.zaehler == 8 || this.zaehler == 13 || this.zaehler == 18)
      m = true;
     break;

    case 1:
     if(this.zaehler == 0 || this.zaehler == 5 || this.zaehler == 8 || this.zaehler == 16 || this.zaehler == 20)
      m = true;
     break;

    case 2:
     if(this.zaehler == 0 || this.zaehler == 6 || this.zaehler == 10 || this.zaehler == 12 || this.zaehler == 19)
      m = true;
     break;

    case 3:
     if(this.zaehler == 0 || this.zaehler == 10 || this.zaehler == 11 || this.zaehler == 14 || this.zaehler == 18)
      m = true;
     break;
   }
   return m;
  }

  function best_way(ra)
  {
   var l,o,m,bestway,x=1000;
   for(var r=0;r<ra.length;r++)
   {
    switch(ra[r])
    {
     case 1:
      l = Math.abs(eval((this.links - 55) - (theman.links - 10)));
      o = Math.abs(this.oben - theman.oben);
      break;
     case 2:
      l = Math.abs(eval((this.links + 35) - (theman.links - 10)));
      o = Math.abs(this.oben - theman.oben);
      break;
     case 3:
      l = Math.abs(this.links - theman.links);
      o = Math.abs(eval((this.oben + 35) - (theman.oben - 10)));
      break;
     case 4:
      l = Math.abs(this.links - theman.links);
      o = Math.abs(eval((this.oben - 55) - (theman.oben - 10)));
      break;
    }
    m = Math.pow(l,2) + Math.pow(o,2);
    m = Math.floor(Math.sqrt(m));
    if(this.nr == 0 || this.nr == 3)
    {
     if(m < x)
     {
      x = m;
      bestway = ra[r];
     }
    }
    else
    {
     if(m <= x)
     {
      x = m;
      bestway = ra[r];
     }
    }
   }
   return bestway;
  }

  function hinundweg(r_a,zielr,ziels)
  {
   var r,s,z = 0;
   var jagd_array = new Array();
   if(this.scared && !this.eaten)
   {
    if(this.r > zielr)
     r = 3;
    else if(this.r < zielr)
     r = 4;
    else if(this.r == zielr)
     r = "";
    if(this.s > ziels)
     s = 2;
    else if(this.s < ziels)
     s = 1;
    else if(this.s == ziels)
     s = "";
   }
   else
   {
    if(this.r > zielr)
     r = 4;
    else if(this.r < zielr)
     r = 3;
    else if(this.r == zielr)
     r = "";
    if(this.s > ziels)
     s = 1;
    else if(this.s < ziels)
     s = 2;
    else if(this.s == ziels)
     s = "";
   }
   for(i=0;i<r_a.length;i++)
   {
    if((r == r_a[i])||(s == r_a[i]))
    {
     jagd_array[z] = r_a[i];
     z++;
    }
   }
   if(jagd_array.length)
    return jagd_array;
   else
    return false;
  }

  function wedele()
  {
   for(var zb=0;zb<geister.length;zb++)
   {
    var obj = geister[zb];
    if((powerpill && obj.regenerated && !obj.eaten) || (!powerpill && !obj.eaten))
    {
     if(obj.ghost_img == 1)
     {
      document.images[obj.image_name].src = "bcm/"+obj.image_src+obj.richt+".gif";
     }
     else
     {
      document.images[obj.image_name].src = "bcm/"+obj.image_src+obj.richt+obj.richt+".gif";
     }
     obj.ghost_img = 3 - obj.ghost_img;
    }
   }
   WED = setTimeout("wedele()",100);
  }

  function wedeln()
  {
   if(game_on)
   {
    if((powerpill && this.regenerated && !this.eaten) || (!powerpill && !this.eaten))
    {
     if(this.ghost_img == 1)
     {
      document.images[this.image_name].src = "bcm/"+this.image_src+this.richt+".gif";
     }
     else
     {
      document.images[this.image_name].src = "bcm/"+this.image_src+this.richt+this.richt+".gif";
     }
     this.ghost_img = 3 - this.ghost_img;
    }
   }
  }

  function blinke()
  {
   if(blinkend)
   {
    for(var zb=0;zb<geister.length;zb++)
    {
     var obj = geister[zb];
     if(!obj.eaten && !obj.regenerated)
     {
      if(obj.ghost_img == 1)
      {
       document.images[obj.image_name].src = "bcm/gsch.gif";
      }
      else
      {
       document.images[obj.image_name].src = "bcm/gbl.gif";
      }
      obj.ghost_img = 3 - obj.ghost_img;
     }
    }
    setTimeout("blinke()",100);
   }
  }

  function velocity()
  {
   if(!this.scared && !this.eaten && !this.indahouse)
   {
    this.km = km;
    if(this.r == 14)
     if(this.next_step(this.richt) == 7)
      this.km = gl_km;
   }
   else
   {
    if(this.eaten)
    {
     this.km = gt_km;
    }
    else if(this.indahouse)
     this.km = gl_km;
    else if(g0_km && this.nr == 0 && !this.scared)
     this.km = g0_km;
    else
     this.km = gsch_km;
   }
  }

  function collision()
  {
   var ab_t,ab_l;
   ab_t = (this.oben-10)-(theman.oben-10);
   if(ab_t < 0)
    ab_t = ab_t*(-1);
   ab_l = (this.links-10)-(theman.links-10);
   if(ab_l < 0)
    ab_l = ab_l*(-1);
   if(!this.scared && !this.eaten && !this.scatter && !this.indahouse && !sicherung)
   {
    if(ab_t < geist_jagdradius_t[this.nr] && ab_l < geist_jagdradius_l[this.nr])
     this.jagt = true;
    else
     this.jagt = false;
   }
   else
    this.jagt = false;
   if(ab_t < 10 && ab_l < 10 && !this.eaten)
   {
    this.treffer();
   }
  }

  function treffer()
  {
   if(this.scared)
   {
    this.eaten = true;
    this.ghost_eaten();
   }
   else
   {
    if(game_on)
    {
     game_on = false;
     hide_ghosts();
     end_sounds();
     if(S6 != null)
     	S6.src = "ton/"+sounds[6]+".au";
     theman.sterben();
    }
   }
  }

  function ghost_eaten()
  {

   document.images[this.image_name].src = "bcm/gt"+this.richt+".gif";
   this.regenerated = true;
   if(gefressen == 0)
    gefressen++;
   else
    gefressen = gefressen + gefressen;
   score(200*gefressen);
   document.getElementById("zp"+this.nr).innerHTML = (200*gefressen);
   geist_anz[this.nr].lage.top = this.oben + "px";
   geist_anz[this.nr].lage.left = this.links + "px";
   geist_anz[this.nr].lage.visibility = "visible";
   var str = "geist_anz["+this.nr+"].sicht_zeit()";
   setTimeout(str,1000);
   if(S3 != null)
   	S3.src = "";
   if(S4 != null)
   	S4.src = "ton/"+sounds[4]+".au";
   if(S5 != null)
   	S5.src = "ton/"+sounds[5]+".au";
   game_on = false;
   if(INT)
    clearTimeout(INT)
   interrupt();
  }

  //Funktionen, die die anzeigen betreffen

  function sicht_zeit()
  {
   this.lage.visibility = "hidden";
  }

  function hide_anzeigen()
  {
   for(var t=0;t<geist_anz.length;t++)
    geist_anz[t].lage.visibility = "hidden";
   sy.style.visibility = "hidden";
  }

  function set_symbol()
  {
   document.getElementById("lev").innerHTML = level;
   if(level <= fr_punkte.length)
   {
    document.images["sss"].src = "bcm/fr"+fr_bilder[level]+".gif";
    document.images["sym"].src = "bcm/fr"+fr_bilder[level]+".gif";
    document.images["ff"+level].src = "bcm/fr"+fr_bilder[level]+".gif";
   }
   else
   {
    document.images["sss"].src = "bcm/fr8.gif";
    document.images["sym"].src = "bcm/fr8.gif";
   }
  }

  function show_symbol()
  {
   sy.style.visibility = "visible";
   symbol_da = true;
   SYMB = setTimeout("hide_symbol()",10000);
  }

  function hide_symbol()
  {
   sy.style.visibility = "hidden";
   symbol_da = false;
  }

  function show_symbolanz()
  {
   sy_anz.style.visibility = "visible";
   setTimeout("hide_symbolanz()",500);
  }

  function hide_symbolanz()
  {
   sy_anz.style.visibility = "hidden";
  }

  // Funktionen, die allgemeinen charakter haben, d.h. die nicht speziell pacman oder die geister betreffen

  function set_powerpillmode()
  {
   if(!powerpill)
   {
    scatmode_interrupt();
    if(SCAT)
     clearTimeout(SCAT);
    if(SCAT_OFF)
     clearTimeout(SCAT_OFF);
   }
   blinkend = false;
   powerpill = true;
   if(BLN)
    clearTimeout(BLN);
   if(END_BLN)
    clearTimeout(END_BLN);
   prepare_blinken();
   for(var i=0;i<geister.length;i++)
   {
    var obj = geister[i];
    if(!obj.eaten)
    {
     obj.scared = true;
     obj.regenerated = false;
     obj.richtungswechsel = true;
     document.images[obj.image_name].src = "bcm/gsch.gif";
    }
   }
   gefressen = 0;
   if(S3 != null)
   S3.src = "ton/"+sounds[3]+".au";
  }

  function prepare_blinken()
  {
   var time = 0;
   if(level < bluetime.length)
    time = (bluetime[level-1]*1000);
   else
    time = (bluetime[bluetime.length-1]*1000);
   BLN = setTimeout("set_blinken()",time);
  }

  function set_blinken()
  {
   blinkend = true;
   blinke();
   var time = 0;
   if(level < blinktime.length)
    time = (blinktime[level-1]*1000);
   else
    time = (blinktime[blinktime.length-1]*1000);
   END_BLN = setTimeout("end_blinken()",time);
  }

  function end_blinken()
  {
   blinkend = false;
   powerpill = false;
   if(S3 != null)
   S3.src = "";
   if(S4 != null)
   S4.src = "";
   if(S5 != null)
   S5.src = "";
   for(var i=0;i<geister.length;i++)
   {
    var obj = geister[i];
    obj.scared = false;
    obj.regenerated = false;
   }
   get_sek1();
   if(scatter)
    SCAT_OFF = setTimeout("scatter_off()",howlong_scatter);
   else
    SCAT = setTimeout("scatter_on()",howlong_scatter);
  }

  function change_level()
  {
   nextlevel = false;
   game_on = false;
   end_sounds();
   level += 1;
   if(level < 256)
   {
    document.getElementById("ans").innerHTML = "Level "+level;
    document.getElementById("ansage").style.visibility = "visible";
    setTimeout("next_level()",150);
   }
   else
   {
    document.getElementById("ans").innerHTML = "The End";
    document.getElementById("ansage").style.visibility = "visible";
   }
  }

  function scatter_on()
  {
   scatter = true;
   if(howoften_scatter < 4)
   {
    howoften_scatter += 1;
    if(howoften_scatter == 2)
     howlong_scatter = 5000;
    SCAT_OFF = setTimeout("scatter_off()",howlong_scatter);
    get_sek1();
    for(var i=0;i<geister.length;i++)
    {
     var obj = geister[i];
     obj.scatter = true;
     obj.richtungswechsel = true;
    }
   }
  }

  function get_sek1()
  {
   zeit = new Date();
   sek1 = zeit.getSeconds();
  }

  function scatmode_interrupt()
  {
   zeit = new Date();
   sek2 = zeit.getSeconds();
   millisek = zeit.getMilliseconds();
   if(sek2 < sek1)
     sek2 += 60;
   var verg_zeit = eval(((sek2 - sek1) * 1000) + millisek);
   howlong_scatter = howlong_scatter - verg_zeit;
  }

  function scatter_off()
  {
   if(howoften_scatter < 3)
   {
    scatter = false;
    howlong_scatter = 20000;
    SCAT = setTimeout("scatter_on()",howlong_scatter);
    get_sek1();
    for(var i=0;i<geister.length;i++)
    {
     var obj = geister[i];
     obj.scatter = false;
     obj.richtungswechsel = true;
    }
   }
  }

  function test_collision()
  {
   if(game_on)
   {
    for(var c=0;c<geister.length;c++)
     geister[c].collision();
    setTimeout("test_collision()",1);
   }
  }

  function interrupt()
  {
   game_on = false;
   maul = 0;
   cieman.style.visibility = "hidden";
   INT = setTimeout("interrupt_off()",500);
  }

  function interrupt_off()
  {
   cieman.style.visibility = "visible";
   anschalten();
  }

  function get_highscore()
  {
   var Wert = "";
   if(document.cookie && document.cookie.indexOf('HIGHSCORE') != -1)
   {
    var Wertstart = document.cookie.indexOf(cookieName+"=") + 10;
    var Wertende = document.cookie.indexOf(";");
    if (Wertende == -1)
     Wertende = document.cookie.length;
    high_score = document.cookie.substring(Wertstart,Wertende);
   }
  }

  function set_highscore()
  {
   var Verfallszeit = 1000*60*60*24*365;
   var jetzt = new Date();
   var Auszeit = new Date(jetzt.getTime() + Verfallszeit);
   document.cookie = cookieName+"="+today_score+"; expires="+Auszeit.toGMTString()+";";
  }

 // Funktionen, die den Start ermöglichen

  function write_lab()
  {
   document.write('<div style="position:absolute;visibility:visible; top:'+(ro+30)+'px; left:'+rl+'px; width:360px; height:396px;">');
   document.write('<table style="background-image:url(bcm/hg.gif); background-repeat:no-repeat;background-position: top center;width:360px;height:396px;background-color:#000000" border="0" cellspacing="0" cellpadding="0">');
   document.write('<tr>');
   document.write('<td align="center" valign="center">');
   document.write('<table style="width:312px;height:372px;" border="0" cellspacing="0" cellpadding="0">');
   var nam;
   for(i=0;i<muster.length;i++)
   {
    document.write('<tr>');
    lauffeld[i] = new Array();
    for(j=0;j<muster[i].length;j++)
    {
     nam = "r"+i+"s"+j;
     switch(muster[i][j])
     {
      case 0:
       document.write('<td><img name="'+nam+'" width="12" height="12" src="bcm/pix.gif"></td>');
       lauffeld[i][j] = 0;
       break;

      case 1:
       document.write('<td><img name="'+nam+'" width="12" height="12" src="bcm/k.gif"></td>');
       lauffeld[i][j] = 1;
       maxP++;
       break;

      case 2:
       document.write('<td><img name="'+nam+'" width="12" height="12" src="bcm/k.gif"></td>');
       lauffeld[i][j] = 1;
       maxP++;
       break;

      case 3:
       document.write('<td><img name="'+nam+'" width="12" height="12" src="bcm/g.gif"></td>');
       lauffeld[i][j] = 2;
       maxP++;
       break;

      case 4:
       document.write('<td><img name="'+nam+'" width="12" height="12" src="bcm/g.gif"></td>');
       lauffeld[i][j] = 2;
       maxP++;
       break;

      case 5:
       document.write('<td><img name="'+nam+'" width="12" height="12" src="bcm/pix.gif"></td>');
       lauffeld[i][j] = 3;
       break;

      case 6:
       document.write('<td><img name="'+nam+'" width="12" height="12" src="bcm/pix.gif"></td>');
       lauffeld[i][j] = 3;
       break;

      case 7:
       document.write('<td><img name="'+nam+'" width="12" height="12" src="bcm/pix.gif"></td>');
       lauffeld[i][j] = 0;
       break;
     }
    }
    document.write('</tr>');
   }
   document.write('</table></td></tr></table></div>');
  }

  function write_cman()
  {
   document.write('<div id="'+cman_fig+'" style="position:absolute;visibility:hidden; z-index:'+zI+'; top:'+cman_start_top+'px; left:'+cman_start_left+'px; width:20px; height:20px;"><table style="width:20px;height:20px;" border="0" cellspacing="0" cellpadding="0">');
   document.write('<tr>');
   document.write('<td align="center" valign="center"><img name="'+cman_img_name+'" width="20" height="20" src="bcm/p0.gif"></td>');
   document.write('</tr>');
   document.write('</table></div>');
  }

  function write_ghosts()
  {
   for(i=0;i<geist_fig.length;i++)
   {
    document.write('<div id="'+geist_fig[i]+'" style="position:absolute;visibility:hidden; z-index:'+zI+'; top:'+geist_start_top[i]+'px; left:'+geist_start_left[i]+'px; width:20px; height:20px;"><table style="width:20px;height:20px;" border="0" cellspacing="0" cellpadding="0">');
    document.write('<tr>');
    document.write('<td align="center" valign="center"><img name="'+geist_img_name[i]+'" width="20" height="20" src="bcm/'+geist_img_src[i]+geist_start_richt[i]+'.gif"></td>');
    document.write('</tr>');
    document.write('</table></div>');
    zI -= 1;
   }
  }

  function write_todesanzeigen()
  {
   for(op=0;op<geist_todesanzeige.length;op++)
   {
    document.write('<div id="'+geist_todesanzeige[op]+'" style="position:absolute; visibility:hidden; z-index:5; top:0px; left:0px; width:20px; height:20px; background-color:#000000"><table width="20" height="20" border="0" cellpadding="0" cellspacing="0"><tr><td id="zp'+op+'" valign="center" class="wortwk">0000</td></tr></table></div>');
   }
  }

  function write_anzeigen()
  {
   document.write('<div id="symb_anz" style="position:absolute; visibility:hidden; z-index:8; top:'+(ro+242)+'px; left:'+(rl+170)+'px; width:20px; height:20px; background-color:#000000"><table width="20" height="20" border="0" cellpadding="0" cellspacing="0"><tr><td id="symb_in" valign="center" class="wortwk">0000</td></tr></table></div>');
   document.write('<div id="level_anz" style="position:absolute; visibility:visible; z-index:5; top:'+(ro+238)+'px; left:'+(rl+17)+'px; width:50px; height:30px; background-color:#000000"><table width="50" height="25" border="0" cellpadding="0" cellspacing="0"><tr><td id="lev" class="wortw">Level</td></tr></table></div>');
   document.write('<div id="symbol" style="position:absolute; visibility:visible; z-index:1; top:'+(ro+172)+'px; left:'+(rl+307)+'px; width:20px; height:20px; background-color:#000000"><table width="20" height="20" border="0" cellpadding="0" cellspacing="0"><tr><td><img name="sss" border="0" src="bcm/fr1.gif"></td></tr></table></div>');
   document.write('<div id="punkt_anz" style="position:absolute; visibility:visible; z-index:5; top:'+(ro+238)+'px; left:'+(rl+295)+'px; width:50px; height:30px; background-color:#000000"><table width="50" height="25" border="0" cellpadding="0" cellspacing="0"><tr><td id="points" class="wortw">Points</td></tr></table></div>');
   document.write('<div id="ansage" style="position:absolute; visibility:visible; z-index:15; top:'+(ro+242)+'px; left:'+(rl+130)+'px; width:100px; height:20px; background-color:#000000"><table width="100" height="20" border="0" cellpadding="0" cellspacing="0"><tr><td width="100" height="20" id="ans" class="wortw">' + version + '</td></tr></table></div>');
   document.write('<div id="leben_anz" style="position:absolute; visibility:visible; z-index:5; top:'+(ro+168)+'px; left:'+(rl+17)+'px; width:50px; height:25px; background-color:#000000"><table width="50" height="25" border="0" cellpadding="0" cellspacing="0"><tr><td><img name="p1" border="0" src="bcm/p1.gif"></td><td><img name="p2" src="bcm/p1.gif"></td></tr></table></div>');
   document.write('<div id="symb" style="position:absolute; visibility:hidden; z-index:1; top:'+(ro+242)+'px; left:'+(rl+170)+'px; width:20px; height:20px; background-color:#000000"><table width="20" height="20" border="0" cellpadding="0" cellspacing="0"><tr><td><img name="sym" border="0" src="bcm/fr1.gif"></td></tr></table></div>');
   document.write('<div id="scores" style="position:absolute; visibility:visible; z-index:1; top:'+ro+'px; left:'+rl+'px; width:360px; height:30px; background-color:#000000"><table style="width:360px; height:30px; background-color:#000000;" border="0" cellpadding="0" cellspacing="0" border="0"><tr><td id="max_sc" style="background-image:url(bcm/hg_max.gif);  background-repeat:no-repeat; background-position: center center; width:100px; height:30px; background-color:#000000;" class="wortw">Best Ever</td><td id="high_sc" style="background-image:url(bcm/hg_max.gif); background-repeat:no-repeat;background-position: center center; width:100px; height:30px; background-color:#000000;" class="wortw">Your Best</td><td id="p_sc" style="background-image:url(bcm/hg_max.gif); background-repeat:no-repeat; background-position: center center; width:100px; height:30px;background-color:#000000;" class="wortw">Best Now</td><td id="neu" style="background-image:url(bcm/hg_neu.gif);  background-repeat:no-repeat; background-position: center center; width:60px; height:30px; background-color:#000000;" align="center"><a if(document.all)onFocus="this.blur()" class="www" href="javascript:los()">Start</a></td></tr></table></div>');
   document.write('<div id="all_symbols" style="position:absolute; visibility:visible; z-index:1; top:'+(ro+426)+'px; left:'+rl+'px; width:360px; height:30px; background-color:#000000"><table style="width:360px;height:30px;background-color:#000000;" cellpadding="0" cellspacing="0"><tr><td style="background-image:url(bcm/hg_symbols2.gif); background-repeat:no-repeat;background-position: center center;width:360px;height:30px;background-color:#000000;"><table width="360" height="30" cellpadding="0" cellspacing="0" border="0"><tr><td style="width:27px"><img name="ff13" src="bcm/pix.gif"></td><td style="width:27px"><img name="ff12" src="bcm/pix.gif"></td><td style="width:28px"><img name="ff11" src="bcm/pix.gif"></td><td style="width:28px"><img name="ff10" src="bcm/pix.gif"></td><td style="width:28px"><img name="ff9" src="bcm/pix.gif"></td><td style="width:28px"><img name="ff8" src="bcm/pix.gif"></td><td style="width:28px"><img name="ff7" src="bcm/pix.gif"></td><td style="width:28px"><img name="ff6" src="bcm/pix.gif"></td><td style="width:28px"><img name="ff5" src="bcm/pix.gif"></td><td style="width:28px"><img name="ff4" src="bcm/pix.gif"></td><td style="width:28px"><img name="ff3" src="bcm/pix.gif"></td><td style="width:27px"><img name="ff2" src="bcm/pix.gif"></td><td style="width:27px"><img name="ff1" src="bcm/fr1.gif"></td></tr></table></td></tr></table></div>');
document.write('<div id="ansage" style="position:absolute; visibility:visible; z-index:15; top:'+(ro+456)+'px; left:'+rl+'px; width:360px; height:20px; background-color:#000000"><table width="360" height="20" cellpadding="0" cellspacing="0" border="0"><tr><td width="20" height="20" align="center"><a class="speed" onclick="adjust_speed(5);return false" href="#"><img src="bcm/minus.gif" width="20" height="20" alt="minus"></a></td><td id="pacspeed" width="290" height="20" class="wortw">Speed:&nbsp;50</td><td width="20" height="20" align="center"><a class="speed" onclick="adjust_speed(-5);return false" href="#"><img src="bcm/plus.gif" width="20" height="20" alt=""></a></td></tr></table></div>');
  }

  function build_game()
  {
   write_lab();
   write_cman();
   zI = 5;
   write_ghosts();
   write_todesanzeigen();
   write_anzeigen();
  }

  function initiate()
  {
   build_game();
   pacbild = document.images["pac"];
   cieman = document.getElementById("cman")
   sy = document.getElementById("symb");
   sy_anz = document.getElementById("symb_anz");
   sy_in = document.getElementById("symb_in");
   objektivieren();
   S0 = eval(document.getElementById(sounds[0]));
   S1= eval(document.getElementById(sounds[1]));
   S2 = eval(document.getElementById(sounds[2]));
   S3 = eval(document.getElementById(sounds[3]));
   S4 = eval(document.getElementById(sounds[4]));
   S5 = eval(document.getElementById(sounds[5]));
   S6 = eval(document.getElementById(sounds[6]));
   S7 = eval(document.getElementById(sounds[7]));
   S8 = eval(document.getElementById(sounds[8]));
   S9 = eval(document.getElementById("siren_soft"));
  }


 // Funktionen, die dem neustart des spieles dienen

  function reset_cman()
  {
   theman.oben  = cman_start_top;
   theman.links = cman_start_left;
   theman.Lay.top = theman.oben + "px";
   theman.Lay.left = theman.links + "px";
   theman.r = cman_start_r;
   theman.s = cman_start_s;
   theman.clear_richt(cman_start_richt);
   clearTimeout(theman.timeout)
   theman.on = false;
   pacbild.src = "bcm/p0.gif";
  }

  function reset_ghosts()
  {
   for(var g=0;g<geister.length;g++)
   {
    var obj = geister[g];
    obj.oben  = geist_start_top[g];
    obj.links = geist_start_left[g];
    obj.Lay.top = obj.oben + "px";
    obj.Lay.left = obj.links + "px";
    obj.r = geist_start_r[g];
    obj.s = geist_start_s[g];
    obj.clear_richt(geist_start_richt[g]);
    obj.indahouse = geist_indahouse[g];
    document.images[obj.image_name].src = "bcm/"+obj.image_src+obj.richt+".gif";
    obj.ghost_img = 1;
    obj.zaehler = 0;
    clearTimeout(obj.timeout);
    obj.on = false;
    obj.jagt = false;
    obj.eaten = false;
    obj.scared = false;
    obj.scatter = true;
    obj.regenerated = false;
    obj.richtungswechsel = false;
   }
  }

  function reset_lab()
  {
   for(var i=0;i<muster.length;i++)
   {
    for(var j=0;j<muster[i].length;j++)
    {
     nam = "r"+i+"s"+j;
     switch(muster[i][j])
     {
      case 0:
       document.images[nam].src = "bcm/pix.gif";
       lauffeld[i][j] = 0;
       break;

      case 1:
       document.images[nam].src = "bcm/k.gif";
       lauffeld[i][j] = 1;
       maxP++;
       break;

      case 2:
       document.images[nam].src = "bcm/k.gif";
       lauffeld[i][j] = 1;
       maxP++;
       break;

       case 3:
        document.images[nam].src = "bcm/g.gif";
        lauffeld[i][j] = 2;
        maxP++;
        break;

       case 4:
        document.images[nam].src = "bcm/g.gif";
        lauffeld[i][j] = 2;
        maxP++;
        break;

       case 5:
        document.images[nam].src = "bcm/pix.gif";
        lauffeld[i][j] = 3;
        break;

       case 6:
        document.images[nam].src = "bcm/pix.gif";
        lauffeld[i][j] = 3;
        break;

       case 7:
        document.images[nam].src = "bcm/pix.gif";
        lauffeld[i][j] = 0;
        break;
      }
     }
    }
   }

  function reset_symbols()
  {
   for(var i=1;i<=level;i++)
    document.images["ff"+i].src = "bcm/pix.gif";
  }

  function reset_game()
  {
   tot=howoften_scatter=gefressen=maul=millisek=sek1=sek2 = 0;
   sterbezeit = 800;
   powerpill=blinkend=scatter=symbol_da=cmanstop=neu_richt=g0_km=nextlevel=sound = false;
   howlong_scatter = 8000;
   if(SCAT)
    clearTimeout(SCAT);
   if(SCAT_OFF)
    clearTimeout(SCAT_OFF);
   if(STRB)
    clearTimeout(STRB);
   if(INT)
    clearTimeout(INT);
   if(SYMB)
    clearTimeout(SYMB);
   if(BLN)
    clearTimeout(BLN);
   if(END_BLN)
    clearTimeout(END_BLN);
   reset_cman();
   reset_ghosts();
  }

  function show_all()
  {
   for(var g=0;g<geister.length;g++)
    geister[g].Lay.visibility = "visible";
   theman.Lay.visibility = "visible";
  }

  function hide_ghosts()
  {
   for(var i=0;i<geister.length;i++)
    geister[i].Lay.visibility = "hidden";
  }

  function hide_all()
  {
   hide_anzeigen();
   hide_ghosts();
   hide_symbol();
   cieman.style.visibility = "hidden";
  }

  function end_sounds()
  {
  	if(S1 != null)
  	{
	   S1.src = "";
	   S3.src = "";
	   S5.src = "";
	   S9.src = "";
	  }
  }

  function anschalten()
  {
   game_on = true;
   test_collision();
   theman.move_mouth();
   if(!theman.on)
    theman.cman_whattodo();
   for(var z=0;z<geister.length;z++)
   {
    if(!geister[z].on)
     geister[z].ghost_whattodo();
   }
  }

  function next_life()
  {
   reset_game();
   hide_symbol();
   for(var g=0;g<geister.length;g++)
   {
    if(g > 0)
     geister[g].ups = geist_startups[g]+8;
   }
   document.images["p"+(leben+1)].src = "bcm/pix.gif";
   setTimeout("prepare()",500);
  }

  function next_level()
  {
   hide_all();
   if(S8 != null)
   	S8.src = "ton/"+sounds[8]+".au";
   maxP=ppp = 0;
   siren_speedchange = 7;
   siren_sound = 9;
   reset_game();
   set_symbol();
   reset_lab();
   for(var g=0;g<geister.length;g++)
   {
    if(g <= level)
     geister[g].ups = 0;
    else
     geister[g].ups = geist_startups[g];
   }
   setTimeout("prepare()",2500);
  }

  function set_nextlevel()
  {
   setTimeout("next_level()",50);
  }

  function next_game()
  {
   reset_symbols();
   level = 1;
   leben = 2;
   new_life = 10000;
   start_time = 4700;
   siren_speedchange = 7;
   siren_sound = 9;
   punkte=maxP=ppp=lz = 0;
   document.images["p1"].src = "bcm/p1.gif";
   document.images["p2"].src = "bcm/p1.gif";
   hide_anzeigen();
   hide_symbol();
   reset_game();
   set_symbol();
   reset_lab();
   document.getElementById("points").innerHTML = punkte;
   for(var g=0;g<geister.length;g++)
   {
    if(g <= level)
     geister[g].ups = 0;
    else
     geister[g].ups = geist_startups[g];
   }
   setTimeout("prepare()",10);
  }

  function game_over()
  {
   document.getElementById("ans").innerHTML = over;
   document.getElementById("ansage").style.visibility = "visible";
   setTimeout("weiter()",5000);
  }

  function weiter()
  {
   document.getElementById("ans").innerHTML = '<a class="www" href="javascript:next_game()">' + again + '</a>';
  }

  function prepare()
  {
   document.getElementById("ans").innerHTML = ready;
   document.getElementById("ansage").style.visibility = "visible";
   if(level == 1 && ppp == 0)
   {
   	if(S1 != null)
    	S0.src = "ton/"+sounds[0]+".au";
   }
   else
    start_time = 2000;
   theman.links += 5;
   geister[0].links -= 5;
   document.getElementById("lev").innerHTML = level;
   wedele();
   setTimeout("show_all()",1000);
   setTimeout("starten()",start_time);
  }

  function starten()
  {
   scatter = true;
   get_sek1();
   SCAT_OFF = setTimeout("scatter_off()",howlong_scatter);
   clearTimeout(WED);
   anschalten();
   document.getElementById("ansage").style.visibility = "hidden";
   if(S9 != null)
   	S9.src = "ton/"+sounds[siren_sound]+".au";
  }

  function los()
  {
   if(!game_on)
   {
    document.getElementById("ans").innerHTML = attention;
    document.getElementById("ansage").style.visibility = "visible";
    get_highscore();
    document.getElementById("max_sc").innerHTML = max_score;
    document.getElementById("high_sc").innerHTML = high_score;
    setTimeout("prepare()",1000);
   }
   else
   {
    game_on = false;
    setTimeout("next_game()",100);
   }
  }

 //DIE TASTENSTEUERUNG (von dem hbschen pacfisch-spiel der seite 'http://www.gamecraft.de/paccie/index.htm' 


  var aktiv=false

  function init_keydown()
  {
  /*
   if (document.captureEvents)
   {
    document.captureEvents(Event.KEYDOWN);
    document.captureEvents(Event.KEYUP);
   }
   */
   if(document.attachEvent)
   {
   	document.attachEvent("onkeydown", keyDown)
   	document.attachEvent("onkeyup", keyUp)
   }
   else
   {
   	document.addEventListener("keydown", keyDown, true);
   	document.addEventListener("keyup", keyUp, true);
   }
   aktiv=true;
  }

  function keyUp()
  {
   neu_richt = false;
  }

  function keyDown(geschehe)
  {
  
   var nr;
   var key = geschehe || window.event;
   if (document.layers)
    nr=key.which;
   else if (document.all)
    nr=key.keyCode;
   else if (document.getElementById)
   {
    if (key.which>0)
     nr=key.which;
    else if (key.keyCode>0)
     nr=key.keyCode;
    else nr=-1;
   }
   else nr=-1;
   if (aktiv)
    bewe(nr);
  }

  function bewe(c)
  {
   aktiv=false;
   if (c>96)
    c=c-32;
   else if (c>90)
    c=c-56;
   if ((c==52) || (c==37) || (c==74))
   {
    neu_richt = 1;
   }
   else if ((c==54) || (c==39) || (c==76))
   {
    neu_richt = 2;
   }
   else if ((c==50) || (c==40) || (c==75))
   {
    neu_richt = 3;
   }
   else if ((c==56) || (c==38) || (c==73))
   {
    neu_richt = 4;
   }
   if(cmanstop)
   {
    cmanstop = false;
    sound = false;
    theman.cman_whattodo();
   }
   aktiv=true;
  }