// Variables
var window_height = 640;
var window_width = 480;

function setWindowHeight( ) {
  if( typeof( window.innerWidth ) == 'number' ) {
    window_width = window.innerWidth;
    window_height = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    window_width = document.documentElement.clientWidth;
    window_height = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    window_width = document.body.clientWidth;
    window_height = document.body.clientHeight;
  }
}

var launchers = 1;
var bullet_timeout = 25;
var bullet_movement_per_timeout = 30;
var number_sparkles = 200;
var explosion_radius;
var max_height;
var starting_x;
var restart_timeout = 3000;

function getX( angle, distance ) {
  return Math.round( Math.cos( 1.0 * angle * ( Math.PI / 180 ) ) * distance );
}

function getY( angle, distance ) {
  return Math.round( Math.sin( 1.0 * angle * ( Math.PI / 180 ) ) * distance );
}

function rand( min_rand, max_rand ) { return Math.round( min_rand + Math.random( ) * max_rand ); }

var sparkle_image = new Image( );
sparkle_image.src = 'images/firework_pink.gif';

function explode( ) {
  image = document.getElementById( 'bullet' );

  var center_x = starting_x;
  var center_y = parseInt( image.style.bottom );

  for( var i = 0; i < number_sparkles; i++ ) {
    var image = document.getElementById( 'sparkle' + i );
    var angle = rand( 0, 360 );
    var radius = rand( 0, explosion_radius )
    
    var x = center_x + getX( angle, radius );
    var y = center_y + getY( angle, radius );
    
    //alert( '<img src="images/firework_pink.gif" style="position: absolute; left: ' + x + 'px; bottom: ' + y + 'px;">' );
    image.src = sparkle_image.src;
    image.style.display = '';
    image.style.left = x;
    image.style.bottom = y;
  }
  
  setTimeout( 'restart( );', restart_timeout );
}

function restart( ) {
  for( var i = 0; i < number_sparkles; i++ ) {
    var image = document.getElementById( 'sparkle' + i );
    image.style.display = 'none';
  }
  
  start( );
}

function fire( height ) {
  image_object = document.getElementById( 'bullet' );
  //alert( image.style.bottom + ' ' + height + ' ' +  max_height );
  
  if( height < max_height ) {
    height += 5;
    
    image_object.style.bottom = 10 + height;
    setTimeout( 'fire( ' + height + ' );', bullet_timeout );
  }
  else {
    image_object.style.display = 'none';
    explode( );
  }
}


function initialize( ) {
  setWindowHeight( );
  start( );
}

function start( ) {
  number_sparkles = rand( 25, 150 );
  explosion_radius = rand( 50, 250 );
  
  max_height = ( window_height - explosion_radius ) - rand( 0, window_height - 2 * explosion_radius );
  starting_x = rand( explosion_radius, window_width - 2 * explosion_radius );
  
  bullet_image = document.getElementById( 'bullet' );
  bullet_image.style.left = starting_x + 'px';
  bullet_image.style.bottom = '10px';
  bullet_image.style.display = '';

  fire( 0 );
}

