/* Preloads images such that there is no time delay when changing images.
 */
function on_load() {
  if ( document.getElementById('illustration') ) {
    var image1 = new Image();
    //    image1.src = 'images/nav_home.jpg';
    //    image1.src = 'images/nav_dart.jpg';
  }
}

/* Diplays the given image in the element with id 'illustration'.
*/
function display(new_illustration,ballnumber) {
  var illustration=document.getElementById('illustration');
  if ( illustration && new_illustration!='' ) {
    illustration.style.backgroundImage="url("+new_illustration+")";
  }
  var ball=document.getElementById('ball'+ballnumber);
  if ( ball ) {
    ball.style.visibility="visible";
    reset_hop_ball();
    hop_ball(ballnumber);
  }
}


/* Displays the standard image of the page.
 */
function display_standard(new_illustration,ballnumber) {
  var illustration=document.getElementById('illustration');
  if ( illustration ) {
    illustration.style.backgroundImage = "url("+new_illustration+")";
  }
  /*
  var ball=document.getElementById('ball'+ballnumber);
  if ( ball ) {
    ball.style.visibility="hidden";
  }
  */
  reset_hop_ball();
}


/* Loads the given page.
 */
function load_page(page,ballnumber) {
  window.location.href=page;
  return true;
}


/* When hovering over a menu item the billiard ball hops.
 */
var ball_pos=0;
var ball_velocity=0;
var last_ballnumber=0;
var timeout_set=0;

function hop_ball(ballnumber) {
  if ( ballnumber==0 ) {
    timeout_set -= 1;
  }
  if ( ballnumber!=0 ) {
    last_ballnumber=ballnumber;
  }
  if ( timeout_set>0 ) {
    return;
  }
  if ( last_ballnumber != 0 ) {
    var ball=document.getElementById('ball'+last_ballnumber);
    if ( ball ) {
      var g=0.7*(-9.8)*4/0.05;
      var dt=0.050;
      ball_pos += dt*(ball_velocity+1*dt*g);
      ball_velocity += dt*g;
      if ( ball_pos<=0 ) {
	ball_pos=0;
	ball_velocity = Math.sqrt(0.8*Math.pow(ball_velocity,2));
      }
      ball.style.bottom=ball_pos+"ex";
      timeout_set += 1;
      window.setTimeout("hop_ball(0)",1000*dt);
      return;
    }
  }
}

function reset_hop_ball() {
  ball_pos=0;
  ball_velocity=1.5*4/0.05;
  var ball=document.getElementById('ball'+last_ballnumber);
  if ( ball ) {
    ball.style.bottom=ball_pos+"ex";
  }
  last_ballnumber=0;
}

