var emerging_moons_grow = true;

this.moon_windup_animation = new animation();
with (moon_windup_animation) {
  add_frame('crescent-moon/moon-eyes-closed.gif', 100);
  add_frame('crescent-moon/moon-eyes-open.gif', 100);
  add_frame('crescent-moon/moon-eyes-closed.gif', 100);
  add_frame('crescent-moon/moon-eyes-open.gif', 100);
  add_frame('crescent-moon/moon-kick-1.gif', 200);
  add_frame('crescent-moon/moon-kick-2.gif', 200);
  add_frame('crescent-moon/moon-kick-3.gif', 200);
  add_frame('crescent-moon/moon-kick-4.gif', 200);
  add_frame('crescent-moon/moon-kick-5.gif', 200);
  add_frame('crescent-moon/moon-kick-6.gif', 1000);
}

moon_kick_animation = new animation();
with (moon_kick_animation) {
  add_frame('crescent-moon/moon-kick-6.gif', 10);
  add_frame('crescent-moon/moon-kick-5.gif', 10);
  add_frame('crescent-moon/moon-kick-4.gif', 10);
  add_frame('crescent-moon/moon-kick-3.gif', 10);
  add_frame('crescent-moon/moon-kick-2.gif', 10);
  add_frame('crescent-moon/moon-kick-1.gif', 10);
  add_frame('crescent-moon/moon-eyes-open.gif', 0);
}

moon_stare_animation = new animation();
with (moon_stare_animation) {
  add_frame('crescent-moon/moon-eyes-open.gif', 500);
};

moon_fall_asleep_animation = new animation();
with (moon_fall_asleep_animation) {
  add_frame('crescent-moon/moon-eyes-open.gif', 1000);
  add_frame('crescent-moon/moon-eyes-closed.gif', 50);
  add_frame('crescent-moon/moon-eyes-open.gif', 1000);
  add_frame('crescent-moon/moon-eyes-closed.gif', 50);
  add_frame('crescent-moon/moon-eyes-open.gif', 500);
  add_frame('crescent-moon/moon-eyes-closed.gif', 500);
  add_frame('crescent-moon/moon-eyes-open.gif', 500);
  add_frame('crescent-moon/moon-eyes-closed.gif', 500);
  add_frame('crescent-moon/moon-eyes-open.gif', 500);
  add_frame('crescent-moon/moon-eyes-closed.gif', 0);
}

var crescent_moon;

function init_crescent_moon (image) {
  if (!image) image = document.getElementById('crescent_moon');
  crescent_moon = make_destination(make_hotspot(image));
  crescent_moon.image = image;
  crescent_moon.burden_entering = crescent_moon_sees_planet_approaching;
  crescent_moon.burden_leaving = crescent_moon_idle;
  crescent_moon.receive_burden = crescent_moon_first_windup;
   with (crescent_moon.hotspot.style) {borderStyle = 'dashed'; color='rgb(16,16,16)'} //// debug
}

function crescent_moon_sees_planet_approaching (planet) {
    crescent_moon.image.src = 'crescent-moon/moon-eyes-open.gif';
    moon_windup_animation.preload_frames();
    preload_moons(planet);
    moon_kick_animation.preload_frames();
   }

function crescent_moon_idle () {
  moon_windup_animation.stop();
    crescent_moon.image.src = 'crescent-moon/moon-eyes-closed.gif';
}

function crescent_moon_first_windup (for_planet) {
   crescent_moon.kicking = for_planet;
   crescent_moon.moons_to_kick_out = moons_of(for_planet);
   crescent_moon.placement_x = crescent_moon.current_x-25-for_planet.image.width;
   crescent_moon.placement_y = (crescent_moon.current_y
                                                                     + crescent_moon.image.height/3
                                                                     - for_planet.image.height/2);
   crescent_moon_windup (for_planet);
}

function crescent_moon_windup (for_planet) {
   set_in_motion(for_planet,
                                  new linear_trajectory(for_planet.current_x,
                                                                                  for_planet.current_y,
                                                                                  crescent_moon.placement_x,
                                                                                  crescent_moon.placement_y,
                                                                                  500));
   moon_windup_animation.run(crescent_moon.image, "crescent_moon_kick()")
}

function crescent_moon_kick () {
  moon_kick_animation.run(crescent_moon.image,
                                                          "crescent_moon_has_kicked(crescent_moon.kicking,crescent_moon.moons_to_kick_out.pop())");
 }

function crescent_moon_has_kicked (planet, moon_emerging) {
  if (moon_emerging == undefined)
     { crescent_moon_has_kicked_away (planet) }
  else
     { crescent_moon_has_kicked_out (moon_emerging) }
}

function crescent_moon_has_kicked_away (planet) {
  set_in_motion(planet,
                                  new linear_trajectory(crescent_moon.placement_x,
                                                                                  crescent_moon.placement_y,
                                                                                  crescent_moon.placement_x-400,
                                                                                  crescent_moon.placement_y-20,
                                                                                  1000));
    moon_fall_asleep_animation.run(crescent_moon.image);
    say (null);
}

function crescent_moon_has_kicked_out (moon) {
     if (emerging_moons_grow) {
        moon.image.width = 5;
        moon.image.height = 6;
        unscale (moon, 5000);
       }
     set_in_motion(moon.primary,
                                      new linear_trajectory(crescent_moon.placement_x,
                                                                                      crescent_moon.placement_y,
                                                                                      crescent_moon.placement_x-10,
                                                                                      crescent_moon.placement_y,
                                                                                      100));
     set_in_motion(moon,
                                      new linear_trajectory(crescent_moon.placement_x,
                                                                                      crescent_moon.placement_y,
                                                                                      crescent_moon.placement_x-400+(unscaled_height*(1+crescent_moon.moons_to_kick_out.length)),
                                                                                      crescent_moon.placement_y-50,
                                                                                      5000));
    moon.image.style.display = 'inherit';
    moon.image.style.visibility = "visible";
    say (moon.name);
    moon_stare_animation.run(crescent_moon.image,
                                                               "crescent_moon_windup(crescent_moon.kicking)");
}

function preload_moons(planet) {
  for (i in worlds)
    if (worlds[i].primary == planet)
      worlds[i].image.style.display = 'inherit';
}

function moons_of(planet) {
  var result = new Array();
  for (i in worlds)
    if (worlds[i].primary == planet)
      result.push(worlds[i]);
  return result;
}
