/*********************************************************
 *  Feuerwerk-Objekt                                     *
 *  Version: 1.00                                        *
 *                                                       *
 *  von Roland Steffen                                   *
 *  e-Mail: rolandsteffen@gmx.de                         *
 *  Homepage: http://www.rolandsteffen.de                *
 *  12. März 2003                                        *
 *                                                       *
 *  Copyright:                                           *
 *  http://www.rolandsteffen.de/copy.html                *
 *********************************************************/

/* Änderungen:
 * 
 * noch keine
 */

function Explosion(x0, y0, number_of_particle, explosion_id, particle_color)
  {
  this.x0 = x0;
  this.y0 = y0;
  this.explostionspeed = 200;
  this.timesteps = 0.05;
  this.glowtime = 5;
  this.glowtime_tolerance = 20;
  this.b = 0.9;
  this.g = 9.81;
  this.number_of_particle = number_of_particle;
  this.explosion_id = explosion_id;
  this.particle_color = particle_color;
  this.particle_width = 2;
  this.particles = new Array();
  this.vx0 = 0;
  this.vy0 = 0;
  this.trace = 1;

  this.move = Explosion_Move;
  this.create = Explosion_Create;
  this.random = Explosion_Random;

  eval("Explosion." + explosion_id + " = this;");
  }

Explosion.run = Explosion_run;

function Explosion_run(explosion_id, position)
  {
  var number_of_positions;

  if(!eval("Explosion." + explosion_id)) return;

  with(eval("Explosion." + explosion_id))
    {
    number_of_positions = glowtime / timesteps + trace;
    if(position > number_of_positions) position = 0;
    move(position++);
    window.setTimeout("Explosion.run('" + explosion_id + "', " + position + ")", Math.round(timesteps * 1000));
    }
  }

function Explosion_Random()
  {
  var v_end = 50 * Math.random();
  var x_max = 200 * Math.random() + 50;

  this.timesteps = 0.1;
  this.glowtime = 1.5 * Math.random() + 1;
  this.trace = Math.round(9 * Math.random()) + 1;
  this.number_of_particle = Math.round(this.number_of_particle / this.trace);
  this.b = 1 / this.timesteps / 3 * Math.random() + 0.1;
  this.g = v_end * this.b;
  this.explostionspeed = x_max * this.b;
  this.vx0 = 100 * Math.random() - 50;
  this.vy0 = 25 * Math.random();
  }

function Explosion_Move(position)
  {
  for(var i = 0; i < this.particles.length; i++) this.particles[i].move(Math.round(position));
  }

function Explosion_Create()
  {
  var i;

  document.writeln("&nbsp;"); // behebt Fehler bei Opera, sonst unnütz

  with(this)
    {
    for(i = 0; i < number_of_particle; i++)
      {
      particles[i] = new Particle(x0, y0, explosion_id + "_" + i, particle_color);
      particles[i].explostionspeed = explostionspeed;
      particles[i].timesteps = timesteps;
      particles[i].glowtime = glowtime * (1 - glowtime_tolerance * Math.random() / 100);
      particles[i].b = b;
      particles[i].g = g;
      particles[i].width = particle_width;
      particles[i].vx = vx0;
      particles[i].vy = vy0;
      particles[i].trace = trace;
      particles[i].create();
      }
    }
  }

function Particle(x0, y0, id, color)
  {
  this.x = new Array;
  this.y = new Array;
  this.z = new Array;
  this.id = id;
  this.vx;
  this.vy;
  this.b = 0.9;
  this.g = 9.81;
  this.explostionspeed = 200;
  this.timesteps = 0.05;
  this.glowtime = 5;
  this.color = color;
  this.width = 2;
  this.x0 = x0;
  this.y0 = y0;
  this.object = null;
  this.trace = 1;
  this.move = Particle_Move;
  this.create = Particle_Create;
  }

function Particle_Move(position)
  {
  var i;
  for(i = 0; i < this.trace; i++)
    {
    if(position-i < 0 || position-i >= this.x.length)
      {
      this.object[i].visibility = (document.layer) ? "hide" : "hidden";
      }
    else
      {
      this.object[i].visibility = (document.layer) ? "show" : "visible";
      this.object[i].left = this.x[position-i];
      this.object[i].top = this.y[position-i];
      }
    }
  }

function Particle_Create()
  {
  var temp_v;
  var temp_dv;
  var alpha;
  var i;
  var t;
  var temp_x;
  var temp_y;
  var factor;

  with(this)
    {
    factor = (width - 1) / trace;
    object = new Array(trace);
    for(i = 1; i <= trace; i++)
      {
      object[trace-i] = Graphics_Point(x0, y0, id + "_" + i, color, factor * i + 1, factor * i + 1);
      }
    temp_v = explostionspeed;
    alpha = Math.random() * 2 * Math.PI;
    vy += temp_v * Math.sin(alpha);
    temp_v = temp_v * Math.cos(alpha);
    alpha = Math.random() * 2 * Math.PI;
    vx += temp_v * Math.cos(alpha);

    temp_v = vx;
    temp_x = x0;
    for(t = 0, i = 0; t < glowtime; t = t + timesteps, i++)
      {
      temp_dv = timesteps * b * temp_v;
      temp_v -= temp_dv;
      temp_x -= temp_v * timesteps;
      x[i] = Math.round(temp_x);
      }

    temp_v = vy;
    temp_y = y0;
    for(t = 0, i = 0; t < glowtime; t = t + timesteps, i++)
      {
      temp_dv = timesteps * b * temp_v;
      temp_v = temp_v - g * timesteps - temp_dv;
      temp_y -= temp_v * timesteps;
      y[i] = Math.round(temp_y);
      }
    }
  }

function Graphics_Point(x, y, id, color, width, height)
  {
  x = Math.round(x);
  y = Math.round(y);
  width = Math.round(width);
  height = Math.round(height);

  var p = '';
  if(document.getElementById || document.all)
    {
    p += '<DIV ';
    p += 'ID='			+ id		+ ' ';
    p += 'STYLE="';    p += 'position:'		+ 'absolute'	+ '; ';
    p += 'top:'			+ y		+ 'px; ';
    p += 'left:'		+ x		+ 'px; ';
    p += 'background-color:'	+ color		+ '; ';
    p += 'width:'		+ width		+ 'px; ';
    p += 'height:'		+ height	+ 'px; ';
    p += 'overflow:'		+ 'hidden'	+ '; ';
    p += 'visibility:'		+ 'hidden'	+ ';"';
    p += '></DIV>';
    }
  else if(document.layers)
    {
    p += '<LAYER ';
    p += 'NAME=' 		+ id		+ ' ';
    p += 'TOP=' 		+ y 		+ ' ';
    p += 'LEFT=' 		+ x	 	+ ' ';
    p += 'BGCOLOR=' 		+ color 	+ ' ';
    p += 'WIDTH=' 		+ width 	+ ' ';
    p += 'HEIGHT=' 		+ height	+ ' ';
    p += 'VISIBILETY='		+ 'hide'	+ '';
    p += '></LAYER>';
    }

  document.writeln(p);
  return (document.getElementById) ? document.getElementById(id).style : (document.all) ? eval('document.all.' + id + '.style') : (document.layers) ? eval('document.' + id) : null;
  }