/*
 * HTML-Based WebCam Script
 *
 * Version 1.0 (19.10.2002)
 *
 * written in JavaScript
 *
 * Should work with most browsers.
 *
 * Copyright (C) 2002 NOVASYS des Giacomozzi Udo
 * All rights reserved.
 *
 */

var sec=999;
var loading=false;
var img = new Image;
var webcam_callback=null;


function webcam_tick(id, url, refresh)
{
  sec++;

  if (loading)
  {
    if ((img.complete) || (sec > refresh/2))
    {    
      document[id].src = img.src;
      loading = false;
      if (webcam_callback) webcam_callback(id);
    }
  } else {
    if (sec>=refresh)
    {
      img = new Image;
      img.src = url + "?" + Math.random();
      loading = true;
      sec = 0;
    }
  }


  window.setTimeout("webcam_tick(\"" + id + "\", \"" + url + "\", "+refresh+")",
    1000);
}



// id      = Identified of the image. There must exist an image tag with that
//           name
// url     = URL of the image to load or the helper script
// refresh = Seconds between refreshs
// init_callback = called (if set) after first image has been loaded
//           (the callback function is called with the webcam-id as parameter)

function webcam_init(id, url, refresh, init_callback)
{
  if (!refresh) { refresh=60;}

  webcam_callback = init_callback;

  webcam_tick(id, url, refresh);
}

